Skip to content

Commit

Permalink
In Ark game events, add method to return cards played in event
Browse files Browse the repository at this point in the history
  • Loading branch information
shaldengeki committed Jul 22, 2024
1 parent 73d4627 commit e07500f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"uid": "669b91f5aa2de",
"type": "moveProjects",
"log": "${player_name} plays a new conservation project: ${card_names}",
"args": {
"cards": [
{
"id": "P114_ReleaseYosemite",
"location": "projects_0",
"state": 22,
"pId": 0,
"extraDatas": null
}
],
"projects": [
{
"id": "P114_ReleaseYosemite",
"location": "projects_0",
"state": 22,
"pId": 0,
"extraDatas": null
}
],
"fromDisplay": false,
"player_name": "sorryimlikethis",
"player_id": 86346298,
"card_names": {
"log": "${card_name_0}",
"args": {
"i18n": [
"card_name_0"
],
"card_name_0": {
"log": "${card_name}",
"args": {
"i18n": [
"card_name"
],
"card_name": "Yosemite national park",
"card_id": "P114_ReleaseYosemite",
"preserve": [
"card_id"
]
}
}
}
},
"i18n": [
"card_names"
]
}
}
19 changes: 17 additions & 2 deletions ark_nova_stats/bga_log_parser/game_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,33 @@ class GameLogEventData:

PLAY_LOGS = [
"plays",
"supports a conservation project",
"plays a new conservation project",
"and places it in",
"buys",
]

@property
def is_play_action(self) -> bool:
if "card_name" not in self.args:
if "card_name" not in self.args and "cards" not in self.args:
return False

return any(play_log in self.log for play_log in self.PLAY_LOGS)

@property
def played_card_names(self) -> Optional[set[str]]:
card_names = set()
if "card_name" in self.args:
card_names.add(self.args["card_name"])
elif "card_names" in self.args:
# potentially multiple cards are played in this action.
for arg_key, arg_val in self.args["card_names"]["args"].items():
if "args" in arg_val and "card_name" in arg_val["args"]:
card_names.add(arg_val["args"]["card_name"])
else:
return None

return card_names

@property
def player(self) -> Optional[dict[str, int | str]]:
player_data = {
Expand Down
15 changes: 15 additions & 0 deletions ark_nova_stats/bga_log_parser/game_log_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,26 @@ def test_is_play_event_returns_true_for_play_action(self):
x = GameLogEventData(**play_log)
assert x.is_play_action

def test_is_play_event_returns_true_for_new_conservation_project(self):
play_log = load_data_from_fixture_file("play_new_conservation_project.log.json")
x = GameLogEventData(**play_log)
assert x.is_play_action

def test_is_play_event_returns_false_for_other_actions(self):
non_play_log = load_data_from_fixture_file("non_play_event.log.json")
x = GameLogEventData(**non_play_log)
assert not x.is_play_action

def test_played_card_names_for_play_action(self):
play_log = load_data_from_fixture_file("play_event.log.json")
x = GameLogEventData(**play_log)
assert set(["Crested Porcupine"]) == x.played_card_names

def test_played_card_names_for_new_conservation_project(self):
play_log = load_data_from_fixture_file("play_new_conservation_project.log.json")
x = GameLogEventData(**play_log)
assert set(["Yosemite national park"]) == x.played_card_names


if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))
18 changes: 10 additions & 8 deletions ark_nova_stats/emu_cup/analyze_games.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,28 @@ def main() -> int:

winner = log.winner

game_cards = set()
game_winner_cards = set()
game_loser_cards = set()
game_cards: set[str] = set()
game_winner_cards: set[str] = set()
game_loser_cards: set[str] = set()
for event in log.data.logs:
for event_data in event.data:
if not event_data.is_play_action:
continue

card_name = event_data.args["card_name"]
game_cards.add(card_name)
card_names = event_data.played_card_names
if card_names is None:
continue

game_cards = game_cards.union(card_names)

if log.is_tie:
continue

if event_data.player is not None and winner is not None:
if event_data.player["id"] == winner.id:
game_winner_cards.add(card_name)
game_winner_cards = game_winner_cards.union(card_names)
else:
game_loser_cards.add(card_name)

game_loser_cards = game_loser_cards.union(card_names)
all_cards.update(game_cards)
winner_cards.update(game_winner_cards)
loser_cards.update(game_loser_cards)
Expand Down

0 comments on commit e07500f

Please sign in to comment.