From 419a8b4d5ac4a677f1a7e81abcd8f98bdd20b352 Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda Date: Fri, 16 Sep 2022 22:20:18 +0200 Subject: [PATCH] =?UTF-8?q?Strategicka:=20Z=C3=A1vazn=C3=A9=20=C4=8Dasov?= =?UTF-8?q?=C3=A1n=C3=AD=20v=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/bin/control_game | 10 +++++++--- server/hra/web/api.py | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/server/bin/control_game b/server/bin/control_game index fd117f1..20b8330 100755 --- a/server/bin/control_game +++ b/server/bin/control_game @@ -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})") diff --git a/server/hra/web/api.py b/server/hra/web/api.py index 8e94a0b..6411f3c 100644 --- a/server/hra/web/api.py +++ b/server/hra/web/api.py @@ -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), })