Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasdoehne committed Nov 12, 2023
1 parent cd7a845 commit 862d9d7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
9 changes: 9 additions & 0 deletions stellarisdashboard/dashboard_app/templates/history_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ <h2 class="eventlist_header">{{title[object] | safe}}</h2>
</span>
</div>
</li>
{% elif event["event_type"] == "lost_trait" %}
<li class="eventitem lost_trait">
<div class="eventdescription">
<span class="dateblock">{{event["start_date"]}}:</span>
<span class="eventtext">
{{ event["leader"].leader_class.capitalize() }} {{ links[event["leader"]] | safe }} lost the "{{ event["description"] }}" trait.
</span>
</div>
</li>
{% elif event["event_type"] == "first_contact" %}
<li class="eventitem first_contact">
<div class="eventdescription">
Expand Down
23 changes: 17 additions & 6 deletions stellarisdashboard/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ class Leader(Base):
gender = Column(String(20))
last_level = Column(Integer)

subclass = Column(String()) # optional
subclass = Column(String()) # optional
leader_traits = Column(String()) # comma-separated

date_hired = Column(Integer) # The date when this leader was first encountered
Expand Down Expand Up @@ -1454,10 +1454,21 @@ def rendered_name(self):

@property
def rendered_traits(self) -> List[str]:
# todo: add level to this?
# todo: Imperial heir and ruler traits map to "[triggered_imperial_name]" which is
# then resolved in common/scripted_loc/08_scripted_loc_paragon.txt
return [game_info.lookup_key(t.rstrip("_0123456789")) for t in sorted(self.leader_traits.split(","))]
return [self.render_trait(t) for t in sorted(self.leader_traits.split("|"))]

def render_trait(self, text: str):
key = text.rstrip("_0123456789")
description = game_info.lookup_key(key)

if description == "[triggered_imperial_name]":
# logic from common/scripted_loc/08_scripted_loc_paragon.txt
key = "imperial_ruler" if self == self.country.ruler else "imperial_heir"
return game_info.lookup_key(key)
else:
level = text.removeprefix(key).lstrip("_")
if level:
description = f"{description} ({level})"
return description


class Planet(Base):
Expand Down Expand Up @@ -1825,7 +1836,7 @@ def description(self) -> str:
HistoricalEventType.gained_trait,
HistoricalEventType.lost_trait,
):
return game_info.lookup_key(self.db_description.text)
return self.leader.render_trait(self.db_description.text)
elif self.db_description:
return game_info.lookup_key(self.db_description.text)
else:
Expand Down
23 changes: 17 additions & 6 deletions stellarisdashboard/game_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from typing import Iterable, Optional

logger = logging.getLogger(__name__)
# Regex explanation: https://regex101.com/r/l76XGd/1
loc_re = re.compile(r'(?P<key>\S+?):\d+\s"(?P<value>.*)"')
# Regex explanation: https://regex101.com/r/a1O5Qq/1
loc_re = re.compile(r'(?P<key>\S+?):(\d+)?\s"(?P<value>.*)"')
var_re = re.compile(r"\$(?P<key>\S+)\$")


class NameRenderer:
Expand Down Expand Up @@ -98,7 +99,7 @@ def render_from_dict(self, name_dict: dict) -> str:
render_template = self._substitute_variables(
render_template, substitution_values
)
render_template = self._strip_unresolved_variables(render_template)
render_template = self._handle_unresolved_variables(render_template)
return render_template

def _collect_substitution_values(self, name_dict):
Expand Down Expand Up @@ -136,7 +137,9 @@ def _substitute_variables(self, render_template, substitution_values):
if render_template == "%ACRONYM%":
for key, acronym_base in substitution_values:
if key == "base":
render_template = "".join(s[0].upper() for s in acronym_base.split())
render_template = "".join(
s[0].upper() for s in acronym_base.split()
)
render_template += acronym_base[-1].upper()
# try all combinations of escaping identifiers
parentheses = [
Expand All @@ -161,7 +164,7 @@ def _substitute_variables(self, render_template, substitution_values):
)
return render_template

def _strip_unresolved_variables(self, render_template):
def _handle_unresolved_variables(self, render_template):
# remove any identifiers remaining after substitution:
for pattern in [
r"\[[0-9]*\]",
Expand All @@ -181,7 +184,15 @@ def _strip_unresolved_variables(self, render_template):
match_indirect_reference = re.match(r"\$([a-z0-9_]*)\$", render_template)
if match_indirect_reference:
second_lookup = match_indirect_reference.group(1)
render_template = self.render_from_dict({"key": second_lookup})
render_template = lookup_key(second_lookup)

# Find variables that were not resolved so far:
for match in var_re.findall(render_template):
if match == "ORD":
continue
resolved = lookup_key(match)
render_template = re.sub(f"\${match}\$", resolved, render_template)

return render_template

def _fmt_ord_number(self, num: int):
Expand Down
28 changes: 16 additions & 12 deletions stellarisdashboard/parsing/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,11 +1235,11 @@ def _update_leader_attributes(self, leader: datamodel.Leader, leader_dict):
for event_type, trait in itertools.chain(
zip(
itertools.repeat(datamodel.HistoricalEventType.lost_trait),
gained_traits,
lost_traits,
),
zip(
itertools.repeat(datamodel.HistoricalEventType.gained_trait),
lost_traits,
gained_traits,
),
):
self._session.add(
Expand Down Expand Up @@ -1270,22 +1270,26 @@ def _get_leader_traits(self, leader_dict) -> (str, str):
subclass = trait
break

traits = ",".join(t for t in sorted(leader_traits) if not t.startswith("subclass"))
traits = "|".join(
t for t in sorted(leader_traits) if not t.startswith("subclass")
)
return subclass, traits


def _get_gained_lost_traits(self, old_traits: str, new_traits: str):
old_traits = set(old_traits.split(","))
new_traits = set(new_traits.split(","))
gained_traits = old_traits - new_traits
lost_traits = new_traits - old_traits
old_traits = set(old_traits.split("|"))
new_traits = set(new_traits.split("|"))
lost_traits = old_traits - new_traits
gained_traits = new_traits - old_traits

def strip_level(t: str) -> str:
return t.rstrip("_0123456789")

# Only consider a trait "lost" if it is not replaced by a direct upgrade, e.g.
# trait_ruler_charismatic -> trait_ruler_charismatic_2
# trait_ruler_charismatic -> trait_ruler_charismatic_2 is not considered a lost trait
lost_traits = {
lt
for lt in lost_traits
if not any(gt.startswith(lt.rstrip("_0123456789")) for gt in gained_traits)
t
for t in lost_traits
if all(strip_level(nt) != strip_level(t) for nt in new_traits)
}

return gained_traits, lost_traits
Expand Down
7 changes: 7 additions & 0 deletions test/names_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class NameTestcase:
expected="Commonwealth of Man",
description="empire, built-in",
),
NameTestcase(
{"key": "civic_selective_kinship"},
expected="Selective Kinship",
description="yml line without number",
),
NameTestcase(
dict(
key="EMPIRE_DESIGN_humans2",
Expand Down Expand Up @@ -209,6 +214,8 @@ def test_name_rendering_with_game_files(test_case: NameTestcase):
assert renderer.render_from_dict(test_case.name_dict) == test_case.expected




@pytest.mark.parametrize(
"test_case",
[
Expand Down

0 comments on commit 862d9d7

Please sign in to comment.