Skip to content

Commit

Permalink
feat!: Represent HSR character stats with a dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Jun 7, 2024
1 parent a33cf88 commit f46f0b1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions enka/clients/hsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ def _post_process_character(self, character: Character) -> None:
StatType.IMAGINARY_DMG_BOOST: chara_stats["ImaginaryAddedRatio"],
}

character.stats = [
Stat(
character.stats = {
stat_type: Stat(
type=stat_type,
value=value,
name=self._assets.text_map[stat_type.value],
Expand All @@ -200,7 +200,7 @@ def _post_process_character(self, character: Character) -> None:
),
)
for stat_type, value in final_stats.items()
]
}

def _add_up_character_stats(self, character: Character) -> dict[str, float]: # noqa: C901
chara_stats = DEFAULT_STATS.copy()
Expand Down
10 changes: 7 additions & 3 deletions enka/models/hsr/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Character(BaseModel):
rarity: Literal[4, 5] = Field(None)
element: Element = Field(None)
path: Path = Field(None)
stats: list[Stat] = Field(list)
stats: dict[StatType, Stat] = Field(default_factory=dict)

@computed_field
@property
Expand All @@ -158,9 +158,13 @@ def max_level(self) -> Literal[20, 30, 40, 50, 60, 70, 80]:
def highest_dmg_bonus_stat(self) -> Stat:
"""Character's highest damage bonus stat."""
return max(
(stat for stat in self.stats if stat.type in DMG_BONUS_PROPS.values()),
(stat for stat in self.stats.values() if stat.type in DMG_BONUS_PROPS.values()),
key=lambda stat: stat.value,
default=next(
(stat for stat in self.stats if stat.type.value == DMG_BONUS_PROPS[self.element])
(
stat
for stat in self.stats.values()
if stat.type.value == DMG_BONUS_PROPS[self.element]
)
),
)
2 changes: 1 addition & 1 deletion examples/hsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def main() -> None: # noqa: C901, D103
print(stat.name, stat.formatted_value)

print("\nStats:")
for stat in character.stats:
for stat in character.stats.values():
if stat.value == 0 or stat.type.value in enka.hsr.DMG_BONUS_PROPS.values():
continue
print(stat.name, stat.formatted_value)
Expand Down

0 comments on commit f46f0b1

Please sign in to comment.