Strategická: Krok od uživatelů pouze jednou za 5 sekund

This commit is contained in:
Jiří Kalvoda 2022-09-22 19:37:04 +02:00
parent c0c3bfc4a2
commit 616f554549
4 changed files with 44 additions and 17 deletions

View file

@ -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
}
```

View file

@ -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)]

View file

@ -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",
}

View file

@ -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))