Skip to content

Commit

Permalink
feat(generalsio): read game updates
Browse files Browse the repository at this point in the history
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?
  • Loading branch information
revolko authored and strakam committed Oct 21, 2024
1 parent fdc1e44 commit 00bb5be
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions generals/remote/generalsio_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
from socketio import SimpleClient # type: ignore

from generals.agents.agent import Agent
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 00bb5be

Please sign in to comment.