From 07d19dbad7d2a11c2862c40d8f558791d5084c1f Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda Date: Wed, 14 Sep 2022 16:16:25 +0200 Subject: [PATCH] =?UTF-8?q?Strategick=C3=A1:=20Autmatick=C3=A9=20spou?= =?UTF-8?q?=C5=A1t=C4=9Bn=C3=AD=20tah=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/bin/control_game | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/server/bin/control_game b/server/bin/control_game index 203086e..5231ff0 100755 --- a/server/bin/control_game +++ b/server/bin/control_game @@ -2,19 +2,68 @@ import hra.db as db import hra.lib as lib from hra.util import hash_passwd +import time +from datetime import datetime import argparse from sqlalchemy import exc, update import sys + parser = argparse.ArgumentParser() parser.add_argument("game_id") parser.add_argument("--step", action="store_true") parser.add_argument("--restore", action="store_true") +parser.add_argument("--set-step-mode", type=str) +parser.add_argument("--set-step-s", type=int) +parser.add_argument("--autosteps", action="store_true", help="Bude periodicky posouvat hru podle konfigurace") args = parser.parse_args() +ses = db.get_session() + +game_id = args.game_id +game = ses.query(db.Game).filter_by(game_id=game_id).one_or_none() + + +if args.set_step_mode: + game.step_mode = db.StepMode(args.set_step_mode) + ses.commit() +if args.set_step_s: + game.step_every_s = db.StepMode(args.set_step_s) + ses.commit() if args.restore: lib.game_restore_broken(args.game_id) if args.step: lib.game_step(args.game_id) +if args.autosteps: + while True: + def sleep(timeout=1): + ses.commit() + timeout = min(5, max(0.1, timeout)) + print(f"Waiting {float(timeout):.2f}") + time.sleep(timeout) + + ses.expire_all() + game = ses.query(db.Game).filter_by(game_id=game_id).one_or_none() + assert game is not None + state = game.current_state() + if not state: + print("There is no state now") + sleep(1) + continue + if game.step_mode != db.StepMode.automatic: + print(f"Step mode {game.step_mode} is not automatic") + sleep(1) + continue + t = datetime.now() + diff = (t - state.create_time).total_seconds() + if diff >= game.step_every_s: + print("Doing step") + lib.game_step(game_id) + print("Done") + sleep(0.1) + else: + step_in = game.step_every_s - diff + print(f"Step in {step_in:.2f} s (every {game.step_every_s})") + sleep(step_in)