print moves only if debug is on and better error handeling if running game into found in dict

This commit is contained in:
2024-04-18 08:23:10 +02:00
parent 9950fa1952
commit 5743f5c111
+19 -11
View File
@@ -107,11 +107,16 @@ class Server:
next_move = self.running_snake[game_state["game"]["id"]].choose_move(game_state)
if self.store_game_state:
self.running_games[game_state["game"]["id"]].add_moves(game_state["turn"], game_state["board"], next_move)
if self.debug:
print(self.running_games[game_state["game"]["id"]])
try:
self.running_games[game_state["game"]["id"]].add_moves(game_state["turn"], game_state["board"], next_move)
if self.debug:
print(self.running_games[game_state["game"]["id"]])
except KeyError:
pass
if self.debug:
print("TURN:", f'{game_state["turn"]:3},', "MOVE:", f"{next_move:5}")
print("TURN:", f'{game_state["turn"]:3},', "MOVE:", f"{next_move:5}")
return {"move": next_move}
# end is called when your Battlesnake finishes a game
@@ -119,12 +124,15 @@ class Server:
if self.store_game_state:
snake = self.running_snake[game_state["game"]["id"]]
self.running_games[game_state["game"]["id"]].add_end_state(game_state["board"], snake.get_history(), game_state["turn"])
self.running_games[game_state["game"]["id"]].save(
f"{snake.__class__.__name__}_{datetime.now().strftime('%H-%M-%S')}_{game_state['game']['id']}.json",
callback=json.dump, indent=2, ensure_ascii=False
)
del self.running_games[game_state["game"]["id"]]
try:
self.running_games[game_state["game"]["id"]].add_end_state(game_state["board"], snake.get_history(), game_state["turn"])
self.running_games[game_state["game"]["id"]].save(
f"{snake.__class__.__name__}_{datetime.now().strftime('%H-%M-%S')}_{game_state['game']['id']}.json",
callback=json.dump, indent=2, ensure_ascii=False
)
del self.running_games[game_state["game"]["id"]]
except KeyError:
print(f"ERROR: Can't find Game {game_state['game']['id']} in Storage")
print("GAME OVER:\n- Winner is", [ x["name"] for x in game_state["board"]['snakes']])
print("GAME ENDED: Winner is", [ x["name"] for x in game_state["board"]['snakes']])
del self.running_snake[game_state["game"]["id"]]