diff --git a/server/card/equipment/artifact/element_artifacts.py b/server/card/equipment/artifact/element_artifacts.py index dafcbf76..9b0a1230 100644 --- a/server/card/equipment/artifact/element_artifacts.py +++ b/server/card/equipment/artifact/element_artifacts.py @@ -3,7 +3,9 @@ from .base import ArtifactBase from ....struct import Cost from ....modifiable_values import CostValue -from ....consts import ElementType, ObjectPositionType, CostLabels +from ....consts import ( + ELEMENT_TO_DIE_COLOR, ElementType, ObjectPositionType, CostLabels +) from ....event import RoundPrepareEventArguments from ....action import Actions @@ -117,16 +119,10 @@ def value_modifier_COST( # talent card not for this charactor return value # can decrease cost - used = 0 - if (value.cost.elemental_dice_color == self.element - and value.cost.elemental_dice_number > 0): - value.cost.elemental_dice_number -= 1 - used += 1 - elif value.cost.any_dice_number > 0: - value.cost.any_dice_number -= 1 - used += 1 - if mode == 'REAL': - self.usage -= used + if value.cost.decrease_cost(ELEMENT_TO_DIE_COLOR[self.element]): + # decrease cost success + if mode == 'REAL': + self.usage -= 1 return value diff --git a/server/status/team_status/event_cards.py b/server/status/team_status/event_cards.py index f0cb16ce..9cc308e3 100644 --- a/server/status/team_status/event_cards.py +++ b/server/status/team_status/event_cards.py @@ -37,13 +37,11 @@ def value_modifier_COST( if not (value.cost.label & CostLabels.SWITCH_CHARACTOR.value): # not switch charactor, do nothing return value - if value.cost.any_dice_number <= 0: - # no need dice, do nothing - return value - # self switch charactor, reduce cost by 1 - value.cost.any_dice_number -= 1 - if mode == 'REAL': - self.usage -= 1 + # decrease 1 any cost + if value.cost.decrease_cost(None): + # decrease success + if mode == 'REAL': + self.usage -= 1 return value def event_handler_SWITCH_CHARACTOR( diff --git a/server/struct.py b/server/struct.py index 9d1c4c87..e2e1b5a0 100644 --- a/server/struct.py +++ b/server/struct.py @@ -146,6 +146,39 @@ def total_dice_cost(self) -> int: + self.any_dice_number + self.omni_dice_number ) + def decrease_cost(self, dice_color: DieColor | None) -> bool: + """ + Decrease the cost by 1 dice of the given color in-place. + + Return True if decrease successfully, False if not. + + Args: + dice_color (DieColor | None): The dice color to be decreased. + If None, decrease same or any dice; Otherwise, decrease + elemental or any dice. + """ + assert dice_color != DieColor.OMNI, 'Omni dice is not supported yet.' + if dice_color is None: + # decrease same or any dice + if self.same_dice_number > 0: + self.same_dice_number -= 1 + return True + if self.any_dice_number > 0: + self.any_dice_number -= 1 + return True + return False + # decrease elemental or any dice + if ( + self.elemental_dice_color == dice_color + and self.elemental_dice_number > 0 + ): + self.elemental_dice_number -= 1 + return True + if self.any_dice_number > 0: + self.any_dice_number -= 1 + return True + return False + def is_valid(self, dice_colors: List[DieColor], charge: int, arcane_legend: bool, strict = True) -> bool: """