Skip to content

Commit

Permalink
fix: unify game_meta handling
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Sep 8, 2023
1 parent f84f515 commit 2c22762
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
3 changes: 2 additions & 1 deletion hsreplay/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hslog.player import coerce_to_entity_id, PlayerManager

from . import elements
from .utils import set_game_meta_on_game


def serialize_entity(entity):
Expand Down Expand Up @@ -179,7 +180,7 @@ def game_to_xml(
players = game_element.players

if game_meta is not None:
game_element._attributes = game_meta
set_game_meta_on_game(game_meta, game_element)

if player_meta is not None:
for player, meta in zip(players, player_meta):
Expand Down
13 changes: 2 additions & 11 deletions hsreplay/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from . import elements
from .dumper import serialize_entity
from .utils import set_game_meta_on_game


@contextmanager
Expand Down Expand Up @@ -330,17 +331,7 @@ def game_to_xml_stream(
game_element = elements.GameNode(tree.ts)

if game_meta is not None:
if game_meta.get("id") is not None:
game_element._attributes["id"] = str(game_meta["id"])
if game_meta.get("format") is not None:
game_element._attributes["format"] = str(game_meta["format"])
if game_meta.get("hs_game_type") is not None:
game_element._attributes["type"] = str(game_meta["hs_game_type"])
if game_meta.get("scenario_id") is not None:
game_element._attributes["scenarioID"] = str(game_meta["scenario_id"])

if "reconnecting" in game_meta:
game_element._attributes["reconnecting"] = str(game_meta["reconnecting"])
set_game_meta_on_game(game_meta, game_element)

with element_context(xf, game_element, indent=indent):
write_packets_recursive(
Expand Down
17 changes: 17 additions & 0 deletions hsreplay/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,20 @@ def annotate_replay(infile, outfile):
target.set("EntityName", _get_card_name(db, entities[target.attrib["entity"]]))

tree.write(outfile, pretty_print=True)


def set_game_meta_on_game(game_meta, game_element):
if game_meta is None:
return

if game_meta.get("id") is not None:
game_element._attributes["id"] = str(game_meta["id"])
if game_meta.get("format") is not None:
game_element._attributes["format"] = str(game_meta["format"])
if game_meta.get("hs_game_type") is not None:
game_element._attributes["type"] = str(game_meta["hs_game_type"])
if game_meta.get("scenario_id") is not None:
game_element._attributes["scenarioID"] = str(game_meta["scenario_id"])

if "reconnecting" in game_meta:
game_element._attributes["reconnecting"] = str(game_meta["reconnecting"])

0 comments on commit 2c22762

Please sign in to comment.