From 582e0e738584ee5ba7f63a52e18f94ab2b86f45a Mon Sep 17 00:00:00 2001 From: kulisak12 Date: Wed, 14 Sep 2022 14:50:47 +0200 Subject: [PATCH] =?UTF-8?q?Strategick=C3=A1:=20Oprava=20chybek=20v=20?= =?UTF-8?q?=C5=A1ablon=C4=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- klient/client.py | 8 ++++---- klient/play.py | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/klient/client.py b/klient/client.py index ccfeb8a..9b2b875 100644 --- a/klient/client.py +++ b/klient/client.py @@ -25,6 +25,7 @@ def get_state(min_round: int, args) -> Tuple[Optional[dict], float]: logger.warning(f"Server error: {r.status_code} {r.reason}") return None, TIME_BEFORE_RETRY + state = r.json() # also retry if the server is not willing to give us the state yet if state["status"] == "waiting": logger.info("Server is busy.") @@ -33,7 +34,6 @@ def get_state(min_round: int, args) -> Tuple[Optional[dict], float]: logger.info("Round didn't start yet.") return None, state["wait"] - state = json.loads(r.json()) logger.info("Received new state.") return state, 0 @@ -43,7 +43,7 @@ def send_turn(turn: dict, round: int, args) -> bool: try: r = requests.post( - f"{args.server}/api/state", + f"{args.server}/api/action", params={ "game": args.game, "token": args.token, @@ -61,7 +61,7 @@ def send_turn(turn: dict, round: int, args) -> bool: # print errors # because such errors are caused by the submitted turn, # retrying will not help, so return True - response = json.loads(r.json()) + response = r.json() if response["status"] == "ok": logger.info("Turn accepted.") elif response["status"] == "too_late": @@ -73,6 +73,6 @@ def send_turn(turn: dict, round: int, args) -> bool: f" {member}: {error}" for member, error in response["errors"].items() ] - logger.error(f"Member errors:\n{'\n'.join(member_errors)}") + logger.error("Member errors:\n" + "\n".join(member_errors)) return True diff --git a/klient/play.py b/klient/play.py index 780d180..af30dc6 100755 --- a/klient/play.py +++ b/klient/play.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import argparse import time -from typing import Callable, List, Optional, Tuple +from typing import Callable, Iterable, List, Optional, Tuple from client import get_state, send_turn, logger, TIME_BEFORE_RETRY @@ -76,7 +76,7 @@ def strategy(team_id: int, state: dict, args: argparse.Namespace) -> dict: # TODO: implement your strategy here for member, coords in members: member.action = "up" - return build_turn(members) + return build_turn(map(lambda x: x[0], members)) def find_fields( @@ -108,7 +108,7 @@ def find_members( return result -def build_turn(members: List[Member]) -> dict: +def build_turn(members: Iterable[Member]) -> dict: return { "members": [ {"id": member.id, "action": member.action} @@ -144,7 +144,7 @@ def main(args: argparse.Namespace): logger.setLevel(args.log_level) while True: - state, wait_time = get_state(min_round) + state, wait_time = get_state(min_round, args) if state is None: # retry later time.sleep(wait_time) @@ -153,7 +153,7 @@ def main(args: argparse.Namespace): turn = strategy(state["team_id"], state["state"], args) round = state["round"] - while not send_turn(turn, round): + while not send_turn(turn, round, args): # if there was a connection error, retry later time.sleep(TIME_BEFORE_RETRY) min_round = round + 1