Skip to content

Commit

Permalink
Handle new 3.6 templates: %ADJECTIVE%, %ADJ%, and %LEADER_2%. Fixes #90
Browse files Browse the repository at this point in the history
… (#91)

* Handle %ADJECTIVE% and %ADJ%

* Add template for leader names and handle new leader name save format

* Add LEADER_2 template test

* Also handle LEADER_1
  • Loading branch information
chennin authored Jan 5, 2023
1 parent 67bb2d1 commit 9ac6b01
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
16 changes: 16 additions & 0 deletions stellarisdashboard/game_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def load_name_mapping(self):
self.name_mapping[key.strip().rstrip(":0")] = val.strip()
except Exception:
pass
# Add missing format that is similar to but not the same as adj_format in practice
if "%ADJECTIVE%" not in self.name_mapping:
self.name_mapping["%ADJECTIVE%"] = "$adjective$ $1$"
# Alternate format with no template (meant to be concatenated?). Incomplete solution.
# if "%ADJ%" not in self.name_mapping:
# self.name_mapping["%ADJ%"] = "$1$"
if "%LEADER_1%" not in self.name_mapping:
self.name_mapping["%LEADER_1%"] = "$1$ $2$"
if "%LEADER_2%" not in self.name_mapping:
self.name_mapping["%LEADER_2%"] = "$1$ $2$"

def render_from_json(self, name_json: str):
try:
Expand All @@ -52,6 +62,11 @@ def render_from_dict(self, name_dict: dict) -> str:
return key

render_template = self.name_mapping.get(key, key)
# The %ADJ% template is odd. See GitHub #90
if render_template == "%ADJ%":
render_template = "$1$"
if "variables" in name_dict and "value" in name_dict["variables"][0] and "key" in name_dict["variables"][0]["value"]:
name_dict["variables"][0]["value"]["key"] += " $1$"

substitution_values = []
if "value" in name_dict:
Expand All @@ -69,6 +84,7 @@ def render_from_dict(self, name_dict: dict) -> str:
("<", ">"),
("[", "]"),
("$", "$"),
("%", "%"),
]:
render_template = render_template.replace(
f"{lparen}{subst_key}{rparen}", subst_value
Expand Down
8 changes: 6 additions & 2 deletions stellarisdashboard/parsing/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,14 @@ def _add_new_leader(
return leader

def get_leader_name(self, leader_dict):
name_dict = leader_dict.get("name", {})
# Look for "first_name" then "full_names" (3.6+)
first_name = dump_name(
leader_dict.get("name", {}).get("first_name", "Unknown Leader")
name_dict.get("first_name",
name_dict.get("full_names", "Unknown Leader")
)
)
last_name = dump_name(leader_dict.get("name", {}).get("second_name", ""))
last_name = dump_name(name_dict.get("second_name", ""))
return first_name, last_name

def _update_leader_attributes(self, leader: datamodel.Leader, leader_dict):
Expand Down
53 changes: 53 additions & 0 deletions test/names_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,59 @@ def test_name_rendering_with_game_files(test_case: NameTestcase):
expected="Combined SPEC_DOESNOTEXIST Suns",
description="empire, template with variables, nested does not exist",
),
NameTestcase(
dict(
key="%LEADER_2%",
variables=[
{"key": "1", "value": {"key": "Golveso"}},
{"key": "2", "value": {"key": "Selvayn"}},
],
),
expected="Golveso Selvayn",
description="%LEADER_2% template",
),
NameTestcase(
dict(
key="%ADJECTIVE%",
variables=[
{"key": "adjective", "value": {"key": "Nexitron"}},
{"key": "1", "value": {"key": "Awareness"}},
],
),
expected="Nexitron Awareness",
description="%ADJECTIVE% template",
),
NameTestcase(
dict(
key="%ADJ%",
variables=[
{"key": "1", "value": {"key": "Allied", "variables": [
{"key": "1", "value": {"key": "%ADJECTIVE%", "variables": [
{"key": "adjective", "value": {"key": "Jing"}},
{"key": "1", "value": {"key": "Systems"}}
]}}
]}}
]
),
expected="Allied Jing Systems",
description="%ADJ% test",
),
NameTestcase(
dict(
key="%ADJECTIVE%",
variables=[
{"key": "adjective", "value": {"key": "Quetzan"}},
{"key": "1", "value": {"key": "%ADJ%", "variables": [
{"key": "1", "value": {"key": "Consolidated", "variables": [
{"key": "1", "value": {"key": "Worlds"}}
]}}
]}}
]
),
expected="Quetzan Consolidated Worlds",
description="Nested %ADJECTIVE% and %ADJ% test",
),
],
ids=lambda tc: tc.description,
)
Expand Down

0 comments on commit 9ac6b01

Please sign in to comment.