Strategicka: Závazné časování v API
This commit is contained in:
parent
748a640b83
commit
419a8b4d5a
2 changed files with 19 additions and 8 deletions
|
@ -31,7 +31,7 @@ if args.autosteps:
|
|||
while True:
|
||||
def sleep(timeout=1):
|
||||
ses.commit()
|
||||
timeout = min(5, max(0.1, timeout))
|
||||
timeout = max(0.1, timeout)
|
||||
print(f"Waiting {float(timeout):.2f}")
|
||||
time.sleep(timeout)
|
||||
|
||||
|
@ -47,13 +47,17 @@ if args.autosteps:
|
|||
print(f"Step mode {game.step_mode} is not automatic")
|
||||
sleep(1)
|
||||
continue
|
||||
step_every_s = game.step_every_s
|
||||
t = datetime.now()
|
||||
diff = (t - state.create_time).total_seconds()
|
||||
if diff >= game.step_every_s:
|
||||
if diff >= step_every_s:
|
||||
print("Doing step")
|
||||
lib.game_step(game_id)
|
||||
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:
|
||||
step_in = game.step_every_s - diff
|
||||
print(f"Step in {step_in:.2f} s (every {game.step_every_s})")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from flask import Flask, redirect, flash, render_template, session, g, request, get_flashed_messages
|
||||
import werkzeug.exceptions
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
import hra.config as config
|
||||
import hra.db as db
|
||||
|
@ -37,21 +38,27 @@ def api_state():
|
|||
game = team.game
|
||||
team_id = team.team_id
|
||||
min_round = args_get("min_round", int, True)
|
||||
if min_round is not None and min_round > game.current_round:
|
||||
return json.dumps({
|
||||
"status": "too_early",
|
||||
"wait": 5.0,
|
||||
})
|
||||
t = datetime.now()
|
||||
state = game.current_state()
|
||||
if state is None:
|
||||
return json.dumps({
|
||||
"status": "working",
|
||||
"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({
|
||||
"status": "ok",
|
||||
"round": state.round,
|
||||
"team_id": team_id,
|
||||
"time_to_response": time_to_end,
|
||||
"state": game.get_logic().personalize_state(state.get_state(), team_id, state.round),
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue