Browse Source

Strategicka: Závazné časování v API

master
Jiří Kalvoda 2 years ago
parent
commit
419a8b4d5a
  1. 10
      server/bin/control_game
  2. 17
      server/hra/web/api.py

10
server/bin/control_game

@ -31,7 +31,7 @@ if args.autosteps:
while True: while True:
def sleep(timeout=1): def sleep(timeout=1):
ses.commit() ses.commit()
timeout = min(5, max(0.1, timeout)) timeout = max(0.1, timeout)
print(f"Waiting {float(timeout):.2f}") print(f"Waiting {float(timeout):.2f}")
time.sleep(timeout) time.sleep(timeout)
@ -47,13 +47,17 @@ if args.autosteps:
print(f"Step mode {game.step_mode} is not automatic") print(f"Step mode {game.step_mode} is not automatic")
sleep(1) sleep(1)
continue continue
step_every_s = game.step_every_s
t = datetime.now() t = datetime.now()
diff = (t - state.create_time).total_seconds() diff = (t - state.create_time).total_seconds()
if diff >= game.step_every_s: if diff >= step_every_s:
print("Doing step") print("Doing step")
lib.game_step(game_id) lib.game_step(game_id)
print("Done") print("Done")
sleep(0.1) t_new = datetime.now()
diff = (t_new - t).total_seconds()
step_in = game.step_every_s - diff
sleep(step_in)
else: else:
step_in = game.step_every_s - diff step_in = game.step_every_s - diff
print(f"Step in {step_in:.2f} s (every {game.step_every_s})") print(f"Step in {step_in:.2f} s (every {game.step_every_s})")

17
server/hra/web/api.py

@ -1,6 +1,7 @@
from flask import Flask, redirect, flash, render_template, session, g, request, get_flashed_messages from flask import Flask, redirect, flash, render_template, session, g, request, get_flashed_messages
import werkzeug.exceptions import werkzeug.exceptions
import json import json
from datetime import datetime
import hra.config as config import hra.config as config
import hra.db as db import hra.db as db
@ -37,21 +38,27 @@ def api_state():
game = team.game game = team.game
team_id = team.team_id team_id = team.team_id
min_round = args_get("min_round", int, True) min_round = args_get("min_round", int, True)
if min_round is not None and min_round > game.current_round: t = datetime.now()
return json.dumps({
"status": "too_early",
"wait": 5.0,
})
state = game.current_state() state = game.current_state()
if state is None: if state is None:
return json.dumps({ return json.dumps({
"status": "working", "status": "working",
"wait": 1.0, "wait": 1.0,
}) })
if game.step_mode == db.StepMode.automatic:
time_to_end = max(1, game.step_every_s - (t - state.create_time).total_seconds())
else:
time_to_end = None
if min_round is not None and min_round > game.current_round:
return json.dumps({
"status": "too_early",
"wait": time_to_end + 1.0 if time_to_end is not None else 3.0,
})
return json.dumps({ return json.dumps({
"status": "ok", "status": "ok",
"round": state.round, "round": state.round,
"team_id": team_id, "team_id": team_id,
"time_to_response": time_to_end,
"state": game.get_logic().personalize_state(state.get_state(), team_id, state.round), "state": game.get_logic().personalize_state(state.get_state(), team_id, state.round),
}) })

Loading…
Cancel
Save