From 616f5545497928852a8f4e8a8d6d2fc5fd54f6e0 Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda Date: Thu, 22 Sep 2022 19:37:04 +0200 Subject: [PATCH] =?UTF-8?q?Strategick=C3=A1:=20Krok=20od=20u=C5=BEivatel?= =?UTF-8?q?=C5=AF=20pouze=20jednou=20za=205=20sekund?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- obsazovani.md | 6 ++---- server/hra/lib.py | 17 ++++++++++++----- server/hra/web/api.py | 29 ++++++++++++++++++++++------- server/hra/web/pages.py | 9 ++++++++- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/obsazovani.md b/obsazovani.md index ac0e373..b4092f3 100644 --- a/obsazovani.md +++ b/obsazovani.md @@ -114,10 +114,8 @@ Odpověď: nebo { - status: "error" - description: "Je zak\u00e1z\u00e1no krokovat tuto hru" - http-code: 403 - http-name: "Forbidden" + status: "too_early" + wait: 5 } ``` diff --git a/server/hra/lib.py b/server/hra/lib.py index 942e0a9..34a527e 100644 --- a/server/hra/lib.py +++ b/server/hra/lib.py @@ -7,8 +7,10 @@ from sqlalchemy import exc, update class DuplicitMakeStep(Exception): pass +class TooEarlyStep(Exception): + pass -def game_step(game_id: int): +def game_step(game_id: int, by_user: bool = False): ses = db.get_session() ses.expire_all() @@ -18,12 +20,17 @@ def game_step(game_id: int): if game.working_on_next_state: ses.commit() raise DuplicitMakeStep() - game.working_on_next_state = True - ses.commit() old_round_id = game.current_round new_round_id = old_round_id + 1 - old_state = ses.query(db.State).filter_by(game_id=game.game_id, round=old_round_id).one_or_none().get_state() + old_state = ses.query(db.State).filter_by(game_id=game.game_id, round=old_round_id).one_or_none() + + if by_user: + if (datetime.now() - old_state.create_time).total_seconds() < 5: + raise TooEarlyStep() + game.working_on_next_state = True + ses.commit() + moves = [None for _ in range(game.teams_count)] points = [0 for _ in range(game.teams_count)] @@ -33,7 +40,7 @@ def game_step(game_id: int): ses.commit() - x, add_points = game.get_logic().step(old_state, moves, old_round_id) + x, add_points = game.get_logic().step(old_state.get_state(), moves, old_round_id) points = [a+b for a,b in zip(points, add_points)] diff --git a/server/hra/web/api.py b/server/hra/web/api.py index ebecd8f..c81adf6 100644 --- a/server/hra/web/api.py +++ b/server/hra/web/api.py @@ -174,10 +174,25 @@ def api_step(): if game.step_mode != db.StepMode.user: raise werkzeug.exceptions.Forbidden("Je zakázáno krokovat tuto hru") - lib.game_step(game.game_id) - log(team, db.Endpoint.step, "ok") - db.get_session.commit() - - return { - "status": "ok", - } + try: + lib.game_step(game.game_id, by_user=True) + except lib.DuplicitMakeStep: + log(team, db.Endpoint.step, "too_early", text="Duplicit") + db.get_session().commit() + return { + "status": "too_early", + "wait": 5, + } + except lib.TooEarlyStep: + log(team, db.Endpoint.step, "too_early") + db.get_session().commit() + return { + "status": "too_early", + "wait": 5, + } + else: + log(team, db.Endpoint.step, "ok") + db.get_session().commit() + return { + "status": "ok", + } diff --git a/server/hra/web/pages.py b/server/hra/web/pages.py index a200ad2..ed32f1b 100644 --- a/server/hra/web/pages.py +++ b/server/hra/web/pages.py @@ -309,7 +309,14 @@ def web_game_step(game_id): if not right_for_step(game): raise werkzeug.exceptions.Forbidden() - lib.game_step(game_id) + try: + lib.game_step(game_id, by_user=True) + except lib.DuplicitMakeStep: + flash("Duplicitní požadavek na krok", 'danger') + except lib.TooEarlyStep: + flash("Moc brzy na další požadavek na krok", 'danger') + else: + flash("Krok proveden", "success") return redirect(app.url_for(web_game.__name__, game_id=game_id))