Browse Source

Strategická: Oprava chybek v šabloně

master
David Klement 2 years ago
parent
commit
582e0e7385
  1. 8
      klient/client.py
  2. 10
      klient/play.py

8
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

10
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

Loading…
Cancel
Save