Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
improvement(Artifact): enhance getter and setter syntax
Browse files Browse the repository at this point in the history
You can now do `artifact.level = level` instead of `artifact.set_level(level)`
  • Loading branch information
trumully committed Apr 16, 2024
1 parent e456599 commit 27bebbc
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 169 deletions.
32 changes: 13 additions & 19 deletions src/artipy/analysis/analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ def calculate_artifact_roll_value(artifact: Artifact) -> Decimal:
:rtype: Decimal
"""
return Decimal(
sum(
calculate_substat_roll_value(substat) for substat in artifact.get_substats()
)
sum(calculate_substat_roll_value(substat) for substat in artifact.substats)
)


Expand All @@ -142,8 +140,8 @@ def calculate_artifact_maximum_roll_value(artifact: Artifact) -> Decimal:
:return: The maximum roll value of the artifact roll.
:rtype: Decimal
"""
artifact_max_level = artifact.get_rarity() * 4
remaining_rolls = (artifact_max_level - artifact.get_level()) // UPGRADE_STEP
artifact_max_level = artifact.rarity * 4
remaining_rolls = (artifact_max_level - artifact.level) // UPGRADE_STEP
return Decimal(calculate_artifact_roll_value(artifact) + remaining_rolls)


Expand All @@ -157,23 +155,19 @@ def calculate_artifact_crit_value(artifact: Artifact) -> Decimal:
:rtype: Decimal
"""
crit_dmg = (
sum(
[
substat.value
for substat in artifact.get_substats()
if substat.name == StatType.CRIT_DMG
]
)
sum([
substat.value
for substat in artifact.substats
if substat.name == StatType.CRIT_DMG
])
* 100
)
crit_rate = (
sum(
[
substat.value
for substat in artifact.get_substats()
if substat.name == StatType.CRIT_RATE
]
)
sum([
substat.value
for substat in artifact.substats
if substat.name == StatType.CRIT_RATE
])
* 100
)
return Decimal(crit_dmg + crit_rate * 2)
12 changes: 6 additions & 6 deletions src/artipy/analysis/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def plot_artifact_substat_rolls(artifact: Artifact) -> None:
"""
substat_rolls = {
STAT_NAMES[substat.name]: calculate_substat_rolls(substat)
for substat in artifact.get_substats()
for substat in artifact.substats
}
df = pd.DataFrame(substat_rolls.items(), columns=["stat", "rolls"])

Expand All @@ -52,13 +52,13 @@ def plot_artifact_substat_rolls(artifact: Artifact) -> None:

magnitudes_flat = [
tuple(i.value for i in calculate_substat_roll_magnitudes(substat))
for substat in artifact.get_substats()
for substat in artifact.substats
]
magnitudes_to_dict = {
STAT_NAMES[substat.name]: {
i.value: magnitudes_flat[idx].count(i.value) for i in RollMagnitude
}
for idx, substat in enumerate(artifact.get_substats())
for idx, substat in enumerate(artifact.substats)
}
magnitudes_to_long_form = [
{"stat_name": stat_name, "magnitude": magnitude, "count": count}
Expand Down Expand Up @@ -89,7 +89,7 @@ def plot_artifact_substat_rolls(artifact: Artifact) -> None:
f"Substat rolls on Artifact with {sum(substat_rolls.values())} total rolls",
*(
f"{stat} ({substat_rolls[STAT_NAMES[stat.name]]} rolls)"
for stat in artifact.get_substats()
for stat in artifact.substats
),
],
)
Expand Down Expand Up @@ -164,8 +164,8 @@ def plot_expected_against_actual_mainstats(iterations: int = 1000) -> None:
actual_mainstats: dict[str, list[StatType]] = {k: [] for k in expected_mainstats}

for a in artifacts:
if (slot := a.get_artifact_slot()) in expected_mainstats:
actual_mainstats[slot].append(a.get_mainstat().name)
if (slot := a.artifact_slot) in expected_mainstats:
actual_mainstats[slot].append(a.mainstat.name)

actual_mainstats_pct: dict[str, dict[StatType, float]] = {
k: {
Expand Down
2 changes: 1 addition & 1 deletion src/artipy/analysis/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def upgrade_artifact_to_max(artifact: Artifact) -> Artifact:
:return: The upgraded artifact.
:rtype: Artifact
"""
while artifact.get_level() < artifact.get_rarity() * 4:
while artifact.level < artifact.rarity * 4:
artifact.upgrade()
return artifact

Expand Down
206 changes: 110 additions & 96 deletions src/artipy/artifacts/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,154 +23,168 @@ def __init__(self) -> None:
self._set: str = ""
self._slot: str = ""

def set_mainstat(self, mainstat: MainStat) -> None:
"""Set the mainstat of the artifact.
@property
def mainstat(self) -> MainStat:
"""The mainstat of the artifact. Return a placeholder mainstat if the mainstat
is None.
Returns:
MainStat: The mainstat of the artifact.
"""
if self._mainstat is None:
return PLACEHOLDER_MAINSTAT
return self._mainstat

@mainstat.setter
def mainstat(self, mainstat: MainStat) -> None:
"""Set the mainstat of the artifact. If the rarity of the artifact is greater
than 0, set the rarity of the mainstat.
:param mainstat: The mainstat to set.
:type mainstat: MainStat
Args:
mainstat (MainStat): The mainstat to set.
"""
if (rarity := self.get_rarity()) > 0:
mainstat.rarity = rarity
if self.rarity > 0:
mainstat.rarity = self.rarity
self._mainstat = mainstat

def set_substats(self, substats: list[SubStat]) -> None:
"""Set the substats of the artifact.
@property
def substats(self) -> list[SubStat]:
"""The substats of the artifact.
Returns:
list[SubStat]: The substats of the artifact.
"""
return self._substats

:param substats: The substats to set.
:type substats: list[SubStat]
@substats.setter
def substats(self, substats: list[SubStat]) -> None:
"""Set the substats of the artifact. If the rarity of the artifact is greater
than 0, set the rarity of the substats.
"""
if (rarity := self.get_rarity()) > 0:
if self.rarity > 0:
for substat in substats:
substat.rarity = rarity
substat.rarity = self.rarity
self._substats = substats

def add_substat(self, substat: SubStat) -> None:
"""Add a substat to the artifact.
"""Add a substat to the artifact. If the rarity of the artifact is greater
than 0, set the rarity of the substat.
:param substat: The substat to add.
:type substat: SubStat
Args:
substat (SubStat): The substat to add.
"""
if (rarity := self.get_rarity()) > 0:
substat.rarity = rarity
if self.rarity > 0:
substat.rarity = self.rarity
self._substats.append(substat)

def set_level(self, level: int) -> None:
@property
def level(self) -> int:
"""The level of the artifact.
Returns:
int: The level of the artifact.
"""
return self._level

@level.setter
def level(self, level: int) -> None:
"""Set the level of the artifact.
:param level: The level to set.
:type level: int
Args:
level (int): The level to set.
"""
self._level = level

def set_rarity(self, rarity: int) -> None:
"""Set the rarity of the artifact.
@property
def rarity(self) -> int:
"""The rarity of the artifact.
Returns:
int: The rarity of the artifact.
"""
return self._rarity

@rarity.setter
def rarity(self, rarity: int) -> None:
"""Set the rarity of the artifact. If the rarity of the artifact is greater
than 0, set the rarity of the mainstat and substats.
:param rarity: The rarity to set.
:type rarity: int
Args:
rarity (int): The rarity to set.
"""
self._rarity = rarity
stats: list[MainStat | SubStat] = [self.get_mainstat(), *self.get_substats()]
stats: list[MainStat | SubStat] = [self.mainstat, *self.substats]
for stat in stats:
try:
stat.rarity = rarity
except AttributeError:
continue

def set_artifact_set(self, artifact_set: str) -> None:
"""Set the artifact set of the artifact.
:param set: The set to set.
:type set: str
"""
self._set = artifact_set

def set_artifact_slot(self, slot: str) -> None:
"""Set the artifact slot of the artifact.
:param slot: The slot to set.
:type slot: str
"""
self._slot = slot

def get_mainstat(self) -> MainStat:
"""Get the mainstat of the artifact. If the mainstat is None, return a
placeholder.
:return: The mainstat of the artifact.
:rtype: MainStat
"""
if self._mainstat is None:
return PLACEHOLDER_MAINSTAT
return self._mainstat

def get_substats(self) -> list[SubStat]:
"""Get the substats of the artifact.
:return: The substats of the artifact.
:rtype: list[SubStat]
"""
return self._substats

def get_level(self) -> int:
"""Get the level of the artifact.
@property
def artifact_set(self) -> str:
"""The artifact set of the artifact.
:return: The level of the artifact.
:rtype: int
Returns:
str: The artifact set of the artifact.
"""
return self._level
return self._set

def get_rarity(self) -> int:
"""Get the rarity of the artifact.
@artifact_set.setter
def artifact_set(self, artifact_set: str) -> None:
"""Set the artifact set of the artifact.
:return: The rarity of the artifact.
:rtype: int
Args:
artifact_set (str): The artifact set to set.
"""
return self._rarity
self._set = artifact_set

def get_artifact_set(self) -> str:
"""Get the artifact set of the artifact.
@property
def artifact_slot(self) -> str:
"""The artifact slot of the artifact.
:return: The artifact set of the artifact.
:rtype: str
Returns:
str: The artifact slot of the artifact.
"""
return self._set
return self._slot

def get_artifact_slot(self) -> str:
"""Get the artifact slot of the artifact.
@artifact_slot.setter
def artifact_slot(self, slot: str) -> None:
"""Set the artifact slot of the artifact.
:return: The artifact slot of the artifact.
:rtype: str
Args:
slot (str): The artifact slot to set.
"""
return self._slot

def get_strategy(self) -> UpgradeStrategy:
"""Get the upgrade strategy for the artifact. If the number of substats
is less than the rarity, return an AddStatStrategy, otherwise return an
UpgradeStatStrategy.
self._slot = slot

:return: The upgrade strategy for the artifact.
:rtype: UpgradeStrategy
@property
def strategy(self) -> UpgradeStrategy:
"""The upgrade strategy of the artifact. If the rarity is 1, the strategy
inheriters are skipped in favor of the default strategy. Otherwise, if the
number of substats is less than the rarity - 1, the add stat strategy is
returned. Otherwise, the upgrade stat strategy is returned.
Returns:
UpgradeStrategy: The upgrade strategy of the artifact.
"""
if self.get_rarity() == 1:
if self.rarity == 1:
return UpgradeStrategy()
if len(self.get_substats()) < self.get_rarity() - 1:
if len(self.substats) < self.rarity - 1:
return AddStatStrategy()
return UpgradeStatStrategy()

@property
def max_level(self) -> int:
rarity = self.get_rarity()
return rarity * UPGRADE_STEP if rarity > 2 else UPGRADE_STEP
return self.rarity * UPGRADE_STEP if self.rarity > 2 else UPGRADE_STEP

def upgrade(self) -> None:
"""Upgrade the artifact."""
if self.get_level() < self.max_level:
self.get_strategy().upgrade(self)
if self.level < self.max_level:
self.strategy.upgrade(self)

def __str__(self) -> str:
# Black can't format f-strings with double quotes in them
return (
f"{self.get_artifact_slot()} [+{self.get_level()}]\n"
f"{'★' * self.get_rarity()}\n" # pylint: disable=inconsistent-quotes
f"{self.get_mainstat()}\n{'\n'.join(str(s) for s in self.get_substats())}" # pylint: disable=inconsistent-quotes
f"{self.artifact_slot} [+{self.level}]\n"
f"{'★' * self.rarity}\n" # pylint: disable=inconsistent-quotes
f"{self.mainstat}\n{'\n'.join(str(s) for s in self.substats)}" # pylint: disable=inconsistent-quotes
)
Loading

0 comments on commit 27bebbc

Please sign in to comment.