From 00bb5be0aadaed194ad17a9cac55985058ce023d Mon Sep 17 00:00:00 2001 From: Juraj Paluba Date: Mon, 21 Oct 2024 20:51:45 +0200 Subject: [PATCH] feat(generalsio): read game updates The game state saved in `map` and `cities` is initialized to an empty array. I am thinking that the patch method would at the first iteration push new items to an empty array. Although I am not sure whether it is the best approach as patch method should not have an if checking if it is the first time the patch function is called. Maybe we could create init function that would be called on the first update only? Ideally we would initialize the game states to appropriate size, unfortunatelly, sizes are propagated with the first `game_update`. If we used Python list then we don't have this problem as Python slices would handle that for us. Could numpy slice work as well? --- generals/remote/generalsio_client.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/generals/remote/generalsio_client.py b/generals/remote/generalsio_client.py index 7e195b7..0b18629 100644 --- a/generals/remote/generalsio_client.py +++ b/generals/remote/generalsio_client.py @@ -1,3 +1,4 @@ +import numpy as np from socketio import SimpleClient # type: ignore from generals.agents.agent import Agent @@ -96,12 +97,20 @@ def _play_game(self, agent_index: int) -> None: :param agent_index: The index of agent in the game """ winner = False + map = np.empty([]) # noqa: F841 + cities = np.empty([]) # noqa: F841 + # TODO deserts? while True: - event = self.receive()[0] - if event == "game_lost" or event == "game_won": - # server sends game_lost or game_won before game_over - winner = event == "game_won" - break + event, data, suffix = self.receive() + print('received an event:', event) + match event: + case "game_update": + map_diff = np.array(data["map_diff"]) # noqa: F841 + cities_diff = np.array(data["cities_diff"]) # noqa: F841 + case "game_lost" | "game_won": + # server sends game_lost or game_won before game_over + winner = event == "game_won" + break self._finish_game(winner)