Browse Source

Strategická: Autmatické spouštění tahů

master
Jiří Kalvoda 2 years ago
parent
commit
07d19dbad7
  1. 49
      server/bin/control_game

49
server/bin/control_game

@ -2,19 +2,68 @@
import hra.db as db import hra.db as db
import hra.lib as lib import hra.lib as lib
from hra.util import hash_passwd from hra.util import hash_passwd
import time
from datetime import datetime
import argparse import argparse
from sqlalchemy import exc, update from sqlalchemy import exc, update
import sys import sys
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("game_id") parser.add_argument("game_id")
parser.add_argument("--step", action="store_true") parser.add_argument("--step", action="store_true")
parser.add_argument("--restore", 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() 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: if args.restore:
lib.game_restore_broken(args.game_id) lib.game_restore_broken(args.game_id)
if args.step: if args.step:
lib.game_step(args.game_id) 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)

Loading…
Cancel
Save