Skip to content

Commit

Permalink
feat(generalsio): Add patch parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
strakam committed Oct 25, 2024
1 parent f745832 commit 39b30d0
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions generals/remote/generalsio_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import numpy as np
from socketio import SimpleClient # type: ignore

from generals.agents.agent import Agent
from generals.core.channels import Channels


class GeneralsBotError(Exception):
Expand Down Expand Up @@ -30,6 +28,20 @@ def __str__(self) -> str:
return f"Failed to register the agent. Error: {self.msg}"


def apply_diff(old: list[int], diff: list[int]) -> list[int]:
i = 0
new = []
while i < len(diff):
if diff[i] > 0: # matching
new.extend(old[len(new) : len(new) + diff[i]])
i += 1
if i < len(diff) and diff[i] > 0: # applying diffs
new.extend(diff[i + 1 : i + 1 + diff[i]])
i += diff[i]
i += 1
return new


class GeneralsIOState:
def __init__(self, data: dict):
self.replay_id = data["replay_id"]
Expand All @@ -40,17 +52,13 @@ def __init__(self, data: dict):
self.generals = []
self.scores = []
self.stars = []

self.turn = 0

def update(self, data: dict) -> None:
self.turn = data["turn"]
self.map = self._apply_diff(self.map, data["map_diff"])
self.cities = self._apply_diff(self.cities, data["cities_diff"])

def _apply_diff(self, old: list[int], diff: list[int]) -> list[int]:
print(diff)
return old
self.map = apply_diff(self.map, data["map_diff"])
self.cities = apply_diff(self.cities, data["cities_diff"])


class GeneralsIOClient(SimpleClient):
Expand Down Expand Up @@ -131,7 +139,7 @@ def _play_game(self) -> None:
# TODO deserts?
while True:
event, data, suffix = self.receive()
print('received an event:', event)
print("received an event:", event)
match event:
case "game_update":
self.game_state.update(data)
Expand Down

0 comments on commit 39b30d0

Please sign in to comment.