Skip to content

Commit

Permalink
refactor: move Match out of Values
Browse files Browse the repository at this point in the history
  • Loading branch information
zyr17 committed Aug 30, 2023
1 parent 8f22325 commit 07dec10
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 46 deletions.
7 changes: 4 additions & 3 deletions server/card/equipment/artifact/element_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def event_handler_ROUND_PREPARE(
def value_modifier_COST(
self,
value: CostValue,
match: Any,
mode: Literal['TEST', 'REAL'],
) -> CostValue:
"""
Expand All @@ -82,7 +83,7 @@ def value_modifier_COST(
if self.usage > 0:
# has usage
if not self.position.check_position_valid(
value.position, value.match,
value.position, match,
player_idx_same = True,
source_area = ObjectPositionType.CHARACTOR,
):
Expand All @@ -106,10 +107,10 @@ def value_modifier_COST(
else:
assert position.area == ObjectPositionType.HAND
# cost from hand card, is a talent card
equipped_charactor = value.match.player_tables[
equipped_charactor = match.player_tables[
self.position.player_idx
].charactors[self.position.charactor_idx]
for card in value.match.player_tables[
for card in match.player_tables[
self.position.player_idx].hands:
if card.id == value.position.id:
if card.charactor_name != equipped_charactor.name:
Expand Down
12 changes: 8 additions & 4 deletions server/charactor/hydro/mona.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def event_handler_ROUND_PREPARE(
return []

def value_modifier_COMBAT_ACTION(
self, value: CombatActionValue, mode: Literal['TEST', 'REAL'],
self, value: CombatActionValue,
match: Any,
mode: Literal['TEST', 'REAL'],
) -> CombatActionValue:
"""
When combat action is switch, player index is self, switch from mona to
Expand All @@ -113,7 +115,7 @@ def value_modifier_COMBAT_ACTION(
if value.action_type != 'SWITCH':
return value
if not self.position.check_position_valid(
value.position, value.match, player_idx_same = True,
value.position, match, player_idx_same = True,
charactor_idx_same = True,
):
return value
Expand Down Expand Up @@ -145,14 +147,16 @@ class ProphecyOfSubmersion(SkillTalent):
skill: StellarisPhantasm = StellarisPhantasm()

def value_modifier_DAMAGE_INCREASE(
self, value: DamageIncreaseValue, mode: Literal['TEST', 'REAL'],
self, value: DamageIncreaseValue,
match: Any,
mode: Literal['TEST', 'REAL'],
) -> DamageIncreaseValue:
"""
If mona is active charactor, and damage triggered hydro reaction,
which is made by self, increase damage by 2.
"""
if not self.position.check_position_valid(
value.position, value.match,
value.position, match,
source_area = ObjectPositionType.CHARACTOR, # quipped
source_is_active_charactor = True, # active charactor
player_idx_same = True, # self damage
Expand Down
2 changes: 0 additions & 2 deletions server/elemental_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ def apply_elemental_reaction(
c = target_charactors[cnum]
if c.is_alive:
res.append(DamageIncreaseValue(
match = damage.match,
position = damage.position,
target_position = ObjectPosition(
player_idx = damage.target_position.player_idx,
Expand All @@ -316,7 +315,6 @@ def apply_elemental_reaction(
continue
if c.is_alive:
res.append(DamageIncreaseValue(
match = damage.match,
position = damage.position,
target_position = ObjectPosition(
player_idx = damage.target_position.player_idx,
Expand Down
2 changes: 2 additions & 0 deletions server/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class OmnipotentGuideEventHandler(SystemEventHandlerBase):

def value_modifier_INITIAL_DICE_COLOR(
self, value: InitialDiceColorValue,
match: Any,
mode: Literal['REAL', 'TEST']) -> InitialDiceColorValue:
"""
remove current dice color and add 100 omni dice color
Expand All @@ -144,6 +145,7 @@ def value_modifier_INITIAL_DICE_COLOR(

def value_modifier_REROLL(
self, value: RerollValue,
match: Any,
mode: Literal['REAL', 'TEST']) -> RerollValue:
"""
reroll set to 0
Expand Down
11 changes: 1 addition & 10 deletions server/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,6 @@ def _round_start(self):
for pnum, player_table in enumerate(self.player_tables):
initial_color_value = InitialDiceColorValue(
player_idx = pnum,
match = self,
position = ObjectPosition(
player_idx = pnum,
area = ObjectPositionType.SYSTEM,
Expand Down Expand Up @@ -689,7 +688,6 @@ def _round_start(self):
for pnum, player_table in enumerate(self.player_tables):
reroll_times = self.config.initial_dice_reroll_times
reroll_value = RerollValue(
match = self,
position = ObjectPosition(
player_idx = pnum,
area = ObjectPositionType.SYSTEM,
Expand Down Expand Up @@ -895,7 +893,7 @@ def _modify_value(self, value: ModifiableValueBase,
func = getattr(obj, modifier_name, None)
if func is not None:
logging.debug(f'Modify value {value.type.name} for {name}.')
value = func(value, mode)
value = func(value, self, mode)

"""
Request functions. To generate specific requests.
Expand Down Expand Up @@ -943,7 +941,6 @@ def _request_switch_charactor(self, player_idx: int):
dice_cost = Cost(any_dice_number = 1)
dice_cost_value = CostValue(
cost = dice_cost,
match = self,
position = ObjectPosition(
player_idx = player_idx,
area = ObjectPositionType.SYSTEM,
Expand Down Expand Up @@ -1012,7 +1009,6 @@ def _request_use_skill(self, player_idx: int):
cost = skill.cost.copy(deep = True)
cost_value = CostValue(
cost = cost,
match = self,
position = skill.position,
)
self._modify_value(cost_value, mode = 'TEST')
Expand All @@ -1039,7 +1035,6 @@ def _request_use_card(self, player_idx: int):
cost = card.cost.copy(deep = True)
cost_value = CostValue(
cost = cost,
match = self,
position = card.position,
)
self._modify_value(cost_value, mode = 'TEST')
Expand Down Expand Up @@ -1163,7 +1158,6 @@ def _respond_switch_charactor(self, response: SwitchCharactorResponse):
raise NotImplementedError('Not tested part')
cost = response.request.cost.original_value
cost_value = CostValue(
match = self,
position = ObjectPosition(
player_idx = response.player_idx,
area = ObjectPositionType.SYSTEM,
Expand Down Expand Up @@ -1259,7 +1253,6 @@ def _respond_use_skill(self, response: UseSkillResponse):
request.charactor_idx].skills[request.skill_idx]
cost = response.request.cost.original_value
cost_value = CostValue(
match = self,
position = skill.position,
cost = cost,
)
Expand Down Expand Up @@ -1299,7 +1292,6 @@ def _respond_use_card(self, response: UseCardResponse):
card = table.hands[request.card_idx]
cost = response.request.cost.original_value
cost_value = CostValue(
match = self,
position = card.position,
cost = cost,
)
Expand Down Expand Up @@ -1671,7 +1663,6 @@ def _action_combat_action(self, action: CombatActionAction) \
-> List[CombatActionEventArguments]:
player_idx = self.current_player
combat_action_value = CombatActionValue(
match = self,
position = action.position,
action_type = action.action_type
)
Expand Down
8 changes: 0 additions & 8 deletions server/modifiable_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,12 @@ class ModifiableValueBase(BaseModel):
"""
type: ModifiableValueTypes
original_value: Any = None
match: Any
position: ObjectPosition

def __init__(self, *argv, **kwargs):
super().__init__(*argv, **kwargs)
self.original_value = None
# set match to None to avoid deepcopy
match = self.match
self.match = None
self.original_value = self.copy(deep = True)
self.original_value.match = self.match = match


class InitialDiceColorValue(ModifiableValueBase):
Expand Down Expand Up @@ -144,7 +139,6 @@ def from_damage_value(
for target in target_charactor:
assert alive[target], 'Target charactor is defeated.'
value = DamageIncreaseValue(
match = match,
position = damage_value.position,
damage_type = damage_value.damage_type,
target_position = ObjectPosition(
Expand All @@ -169,7 +163,6 @@ def from_increase_value(
increase_value: DamageIncreaseValue
) -> 'DamageMultiplyValue':
return DamageMultiplyValue(
match = increase_value.match,
position = increase_value.position,
damage_type = increase_value.damage_type,
target_position = increase_value.target_position,
Expand All @@ -189,7 +182,6 @@ def from_multiply_value(
multiply_value: DamageMultiplyValue
) -> 'DamageDecreaseValue':
return DamageDecreaseValue(
match = multiply_value.match,
position = multiply_value.position,
damage_type = multiply_value.damage_type,
target_position = multiply_value.target_position,
Expand Down
4 changes: 2 additions & 2 deletions server/status/charactor_status/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class Frozen(RoundCharactorStatus):
max_usage: int = 1

def value_modifier_DAMAGE_INCREASE(
self, value: DamageIncreaseValue,
self, value: DamageIncreaseValue, match: Any,
mode: Literal['TEST', 'REAL']) -> DamageIncreaseValue:
"""
Increase damage for pyro and physical damages to self by 2, and
decrease usage.
"""
if not self.position.check_position_valid(
value.target_position, value.match,
value.target_position, match,
player_idx_same = True, charactor_idx_same = True,
):
# not attack self, not activate
Expand Down
2 changes: 1 addition & 1 deletion server/status/team_status/dendro_charactors.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def event_handler_CREATE_OBJECT(
return []

def value_modifier_DAMAGE_INCREASE(
self, value: DamageIncreaseValue,
self, value: DamageIncreaseValue, match: Any,
mode: Literal['TEST', 'REAL']) -> DamageIncreaseValue:
"""
+1 on self elemental reaction damage.
Expand Down
7 changes: 4 additions & 3 deletions server/status/team_status/hydro_charactors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal
from typing import Any, Literal

from ...consts import ObjectPositionType

Expand All @@ -20,13 +20,14 @@ class IllusoryBubble(UsageTeamStatus):
max_usage: int = 1

def value_modifier_DAMAGE_MULTIPLY(
self, value: DamageMultiplyValue, mode: Literal['TEST', 'REAL']
self, value: DamageMultiplyValue, match: Any,
mode: Literal['TEST', 'REAL']
) -> DamageMultiplyValue:
"""
Double damage when skill damage made.
"""
if not self.position.check_position_valid(
value.position, value.match,
value.position, match,
player_idx_same = True, target_area = ObjectPositionType.CHARACTOR,
):
# not from self position or not charactor skill
Expand Down
6 changes: 3 additions & 3 deletions server/status/team_status/old_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ class CatalyzingField(UsageTeamStatus):
max_usage: int = 3

def value_modifier_DAMAGE_INCREASE(
self, value: DamageIncreaseValue,
self, value: DamageIncreaseValue, match: Any,
mode: Literal['TEST', 'REAL']) -> DamageIncreaseValue:
"""
Increase damage for dendro or electro damages, and decrease usage.
"""
if not self.position.check_position_valid(
value.position, value.match, player_idx_same = True,
value.position, match, player_idx_same = True,
):
# source not self, not activate
return value
if not self.position.check_position_valid(
value.target_position, value.match,
value.target_position, match,
player_idx_same = False, target_is_active_charactor = True,
):
# target not enemy, or target not active charactor, not activate
Expand Down
16 changes: 8 additions & 8 deletions server/status/team_status/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Team status generated by system.
"""

from typing import Literal
from typing import Any, Literal
from .base import UsageTeamStatus
from ...modifiable_values import DamageIncreaseValue, DamageDecreaseValue
from ...consts import DamageElementalType
Expand All @@ -22,18 +22,18 @@ class CatalyzingField(UsageTeamStatus):
max_usage: int = 2

def value_modifier_DAMAGE_INCREASE(
self, value: DamageIncreaseValue,
self, value: DamageIncreaseValue, match: Any,
mode: Literal['TEST', 'REAL']) -> DamageIncreaseValue:
"""
Increase damage for dendro or electro damages, and decrease usage.
"""
if not self.position.check_position_valid(
value.position, value.match, player_idx_same = True,
value.position, match, player_idx_same = True,
):
# source not self, not activate
return value
if not self.position.check_position_valid(
value.target_position, value.match,
value.target_position, match,
player_idx_same = False, target_is_active_charactor = True,
):
# target not enemy, or target not active charactor, not activate
Expand Down Expand Up @@ -62,18 +62,18 @@ class DendroCore(UsageTeamStatus):
max_usage: int = 1

def value_modifier_DAMAGE_INCREASE(
self, value: DamageIncreaseValue,
self, value: DamageIncreaseValue, match: Any,
mode: Literal['TEST', 'REAL']) -> DamageIncreaseValue:
"""
Increase damage for electro or pyro damages by 2, and decrease usage.
"""
if not self.position.check_position_valid(
value.position, value.match, player_idx_same = True,
value.position, match, player_idx_same = True,
):
# source not self, not activate
return value
if not self.position.check_position_valid(
value.target_position, value.match,
value.target_position, match,
player_idx_same = False, target_is_active_charactor = True,
):
# target not enemy, or target not active charactor, not activate
Expand Down Expand Up @@ -102,7 +102,7 @@ class Crystallize(UsageTeamStatus):
max_usage: int = 2

def value_modifier_DAMAGE_DECREASE(
self, value: DamageDecreaseValue,
self, value: DamageDecreaseValue, match: Any,
mode: Literal['TEST', 'REAL']) -> DamageDecreaseValue:
"""
Decrease damage by its usage, and decrease usage.
Expand Down
2 changes: 1 addition & 1 deletion server/summon/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _remove(self) -> list[RemoveObjectAction]:
]

def value_modifier_DAMAGE_DECREASE(
self, value: DamageDecreaseValue,
self, value: DamageDecreaseValue, match: Any,
mode: Literal['TEST', 'REAL']) -> DamageDecreaseValue:
"""
Decrease damage.
Expand Down
2 changes: 1 addition & 1 deletion tests/server/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def test_support_over_maximum_and_error_tests():

if __name__ == '__main__':
# test_match_pipeline()
# test_save_load()
test_save_load()
test_random_same_after_load()
# test_use_card()
# test_support_over_maximum_and_error_tests()

0 comments on commit 07dec10

Please sign in to comment.