Browse Source

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

master
Jiří Kalvoda 2 years ago
parent
commit
616f554549
  1. 6
      obsazovani.md
  2. 17
      server/hra/lib.py
  3. 21
      server/hra/web/api.py
  4. 9
      server/hra/web/pages.py

6
obsazovani.md

@ -114,10 +114,8 @@ Odpověď:
nebo nebo
{ {
status: "error" status: "too_early"
description: "Je zak\u00e1z\u00e1no krokovat tuto hru" wait: 5
http-code: 403
http-name: "Forbidden"
} }
``` ```

17
server/hra/lib.py

@ -7,8 +7,10 @@ from sqlalchemy import exc, update
class DuplicitMakeStep(Exception): class DuplicitMakeStep(Exception):
pass 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 = db.get_session()
ses.expire_all() ses.expire_all()
@ -18,12 +20,17 @@ def game_step(game_id: int):
if game.working_on_next_state: if game.working_on_next_state:
ses.commit() ses.commit()
raise DuplicitMakeStep() raise DuplicitMakeStep()
game.working_on_next_state = True
ses.commit()
old_round_id = game.current_round old_round_id = game.current_round
new_round_id = old_round_id + 1 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)] moves = [None for _ in range(game.teams_count)]
points = [0 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() 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)] points = [a+b for a,b in zip(points, add_points)]

21
server/hra/web/api.py

@ -174,10 +174,25 @@ def api_step():
if game.step_mode != db.StepMode.user: if game.step_mode != db.StepMode.user:
raise werkzeug.exceptions.Forbidden("Je zakázáno krokovat tuto hru") raise werkzeug.exceptions.Forbidden("Je zakázáno krokovat tuto hru")
lib.game_step(game.game_id) 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") log(team, db.Endpoint.step, "ok")
db.get_session.commit() db.get_session().commit()
return { return {
"status": "ok", "status": "ok",
} }

9
server/hra/web/pages.py

@ -309,7 +309,14 @@ def web_game_step(game_id):
if not right_for_step(game): if not right_for_step(game):
raise werkzeug.exceptions.Forbidden() 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)) return redirect(app.url_for(web_game.__name__, game_id=game_id))

Loading…
Cancel
Save