Skip to content

Commit

Permalink
Update for upstream changes to Game.entities handling
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
jleclanche committed Jun 8, 2018
1 parent 450f17f commit 93f6ff1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
18 changes: 8 additions & 10 deletions hslog/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,11 @@ def handle_full_entity(self, packet):
# Check if the entity already exists in the game first.
# This prevents creating it twice.
# This can legitimately happen in case of GAME_RESET
if entity_id <= len(self.game.entities):
# That first if check is an optimization to prevent always looping over all of
# the game's entities every single FULL_ENTITY packet...
# FIXME: Switching to a dict for game.entities would simplify this.
existing_entity = self.game.find_entity_by_id(entity_id)
if existing_entity is not None:
existing_entity.card_id = packet.card_id
existing_entity.tags = dict(packet.tags)
return existing_entity
existing_entity = self.game.find_entity_by_id(entity_id)
if existing_entity is not None:
existing_entity.card_id = packet.card_id
existing_entity.tags = dict(packet.tags)
return existing_entity

entity = self.card_class(entity_id, packet.card_id)
entity.tags = dict(packet.tags)
Expand All @@ -167,7 +163,9 @@ def handle_change_entity(self, packet):
# This can only happen if the entity's initial reveal was missed.
# Not raising this can cause entities to have a card id, but no initial_card_id.
# If you want that behaviour, subclass EntityTreeExporter and override this.
raise ExporterError("Attempted CHANGE_ENTITY on an entity with no known card ID.")
raise ExporterError(
f"CHANGE_ENTITY {packet.entity} to {packet.card_id} with no previous known CardID."
)
entity.change(packet.card_id, dict(packet.tags))
return entity

Expand Down
11 changes: 11 additions & 0 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ def test_bad_ids(parser):
exporter = EntityTreeExporter(packet_tree)
with pytest.raises(ExporterError):
exporter.export()


@regression_suite
def test_game_reset(parser):
with open(logfile("toki.power.log")) as f:
parser.read(f)

packet_tree = parser.games[0]
exporter = EntityTreeExporter(packet_tree)
exporter.export()
assert True
12 changes: 6 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def test_create_empty_game():

packet_tree = parser.games[0]
game = packet_tree.export().game
assert len(game.entities) == 3
assert len(game._entities) == 3
assert len(game.players) == 2
assert game.entities[0] is game
assert game.entities[0].id == 1
assert game.entities[1] is game.players[0]
assert game.entities[2] is game.players[1]
assert game._entities[0] is game
assert game._entities[0].id == 1
assert game._entities[1] is game.players[0]
assert game._entities[2] is game.players[1]
assert game.initial_state == State.INVALID
assert game.initial_step == Step.INVALID

Expand Down Expand Up @@ -101,7 +101,7 @@ def test_game_initialization():
assert len(parser.games) == 1
packet_tree = parser.games[0]
game = packet_tree.export().game
assert len(game.entities) == 3
assert len(game._entities) == 3
assert len(game.players) == 2

assert game.tags == {
Expand Down

0 comments on commit 93f6ff1

Please sign in to comment.