Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add winner detection for Ark Nova games #262

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions ark_nova_stats/bga_log_parser/game_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


@dataclass
class GameLogEntryData:
class GameLogEventData:
uid: str
type: str
log: str
Expand All @@ -14,20 +14,20 @@ class GameLogEntryData:


@dataclass
class GameLogEntry:
class GameLogEvent:
channel: str
table_id: int
packet_id: str
packet_type: str
move_id: int
time: int
data: list[GameLogEntryData]
data: list[GameLogEventData]

def __post_init__(self):
self.table_id = int(self.table_id)
self.move_id = int(self.move_id)
self.time = int(self.time)
self.data = [GameLogEntryData(**x) for x in self.data] # type: ignore
self.data = [GameLogEventData(**x) for x in self.data] # type: ignore


@dataclass
Expand All @@ -40,12 +40,12 @@ class GameLogPlayer:

@dataclass
class GameLogData:
logs: list[GameLogEntry]
logs: list[GameLogEvent]
players: list[GameLogPlayer]

def __post_init__(self):
self.players = [GameLogPlayer(**x) for x in self.players] # type: ignore
self.logs = [GameLogEntry(**x) for x in self.logs] # type: ignore
self.logs = [GameLogEvent(**x) for x in self.logs] # type: ignore


@dataclass
Expand All @@ -55,3 +55,17 @@ class GameLog:

def __post_init__(self):
self.data = GameLogData(**self.data) # type: ignore

@property
def winner(self) -> Optional[GameLogPlayer]:
if not self.data.logs:
return None

# Look at the last move.
last_move = self.data.logs[-1]
victory_event = next(
e for e in last_move.data if e.type == "simpleNode" and "wins!" in e.log
)
return next(
p for p in self.data.players if p.name == victory_event.args["player_name"]
)
1 change: 1 addition & 0 deletions ark_nova_stats/bga_log_parser/game_log_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_parses_sample_game(self):
assert 537650395 == x.data.logs[0].table_id
assert 1 == x.data.logs[0].move_id
assert 1721046021 == x.data.logs[0].time
assert x.winner is not None and "sorryimlikethis" == x.winner.name


if __name__ == "__main__":
Expand Down