From 90698a40c114e97a96d09e54727390aa67e0b507 Mon Sep 17 00:00:00 2001 From: zyr17 Date: Sun, 27 Aug 2023 19:32:32 +0800 Subject: [PATCH] refactor: change structure of DamageValue remove DamageSourceType, combine target_player_id and target_charactor_id as target_position. --- server/charactor/electro/fischl.py | 5 +--- server/consts.py | 29 ---------------------- server/elemental_reaction.py | 20 +++++++++------ server/event_handler.py | 4 +-- server/match.py | 15 ++++++++---- server/modifiable_values.py | 31 ++++++++++++------------ server/object_base.py | 4 +-- server/status/charactor_status/system.py | 6 +++-- server/status/team_status/system.py | 6 ++--- server/struct.py | 7 +++--- server/summon/base.py | 4 +-- 11 files changed, 53 insertions(+), 78 deletions(-) diff --git a/server/charactor/electro/fischl.py b/server/charactor/electro/fischl.py index 0fb017fc..ef0b5256 100644 --- a/server/charactor/electro/fischl.py +++ b/server/charactor/electro/fischl.py @@ -9,7 +9,7 @@ ) from ...consts import ( ElementType, FactionType, SkillType, WeaponType, DamageElementalType, - ObjectPositionType, DamageSourceType, DamageType, DieColor + ObjectPositionType, DamageType, DieColor ) from ..charactor_base import CharactorBase, SkillTalent from ...struct import DamageValue, DiceCost @@ -62,7 +62,6 @@ def get_actions(self, match: Any) -> List[Actions]: position = self.position.copy(deep = True), id = self.id, damage_type = DamageType.DAMAGE, - damage_source_type = DamageSourceType.CURRENT_PLAYER_CHARACTOR, damage = 2, damage_elemental_type = DamageElementalType.PIERCING, charge_cost = self.charge, @@ -128,7 +127,6 @@ def event_handler_SKILL_END( # add RemoveObjectAction here. assert self.usage > 0 self.usage -= 1 - source_type = DamageSourceType.CURRENT_PLAYER_SUMMON return [ MakeDamageAction( player_id = self.position.player_id, @@ -138,7 +136,6 @@ def event_handler_SKILL_END( position = self.position, id = self.id, damage_type = DamageType.DAMAGE, - damage_source_type = source_type, damage = 2, damage_elemental_type = self.damage_elemental_type, charge_cost = 0, diff --git a/server/consts.py b/server/consts.py index ccaa6831..2a4ced12 100644 --- a/server/consts.py +++ b/server/consts.py @@ -266,35 +266,6 @@ def __repr__(self): return self.value -class DamageSourceType(str, Enum): - """ - Types of damage sources. Current player means the player doing action. - """ - - CURRENT_PLAYER_CHARACTOR = 'CURRENT_PLAYER_CHARACTOR' - CURRENT_PLAYER_SUMMON = 'CURRENT_PLAYER_SUMMON' - CURRENT_PLAYER_SUPPORT = 'CURRENT_PLAYER_SUPPORT' - CURRENT_PLAYER_CHARACTOR_STATUS = 'CURRENT_PLAYER_CHARACTOR_STATUS' - CURRENT_PLAYER_TEAM_STATUS = 'CURRENT_PLAYER_TEAM_STATUS' - CURRENT_PLAYER_WEAPON = 'CURRENT_PLAYER_WEAPON' - CURRENT_PLAYER_ARTIFACT = 'CURRENT_PLAYER_ARTIFACT' - CURRENT_PLAYER_TALENT = 'CURRENT_PLAYER_TALENT' - ENEMY_PLAYER_CHARACTOR = 'ENEMY_PLAYER_CHARACTOR' - ENEMY_PLAYER_SUMMON = 'ENEMY_PLAYER_SUMMON' - ENEMY_PLAYER_SUPPORT = 'ENEMY_PLAYER_SUPPORT' - ENEMY_PLAYER_CHARACTOR_STATUS = 'ENEMY_PLAYER_CHARACTOR_STATUS' - ENEMY_PLAYER_TEAM_STATUS = 'ENEMY_PLAYER_TEAM_STATUS' - ENEMY_PLAYER_WEAPON = 'ENEMY_PLAYER_WEAPON' - ENEMY_PLAYER_ARTIFACT = 'ENEMY_PLAYER_ARTIFACT' - ENEMY_PLAYER_TALENT = 'ENEMY_PLAYER_TALENT' - - def __str__(self): - return self.value - - def __repr__(self): - return self.value - - class ElementalReactionType(str, Enum): """ Types of elemental reactions. diff --git a/server/elemental_reaction.py b/server/elemental_reaction.py index 599624e0..88f4d770 100644 --- a/server/elemental_reaction.py +++ b/server/elemental_reaction.py @@ -279,7 +279,7 @@ def apply_elemental_reaction( element_type = reacted_elements[0] if element_type == ElementType.ANEMO: element_type = reacted_elements[1] - attack_target = damage.target_charactor_id + attack_target = damage.target_position.charactor_id for cnum in range(1, len(target_charactors)): cnum = (cnum + attack_target) % len(target_charactors) c = target_charactors[cnum] @@ -288,14 +288,16 @@ def apply_elemental_reaction( match = damage.match, position = damage.position, id = damage.id, - target_player_id = damage.target_player_id, - target_charactor_id = cnum, + target_position = ObjectPosition( + player_id = damage.target_position.player_id, + charactor_id = cnum, + area = damage.target_position.area + ), damage = 1, damage_type = DamageType.DAMAGE, damage_elemental_type = ELEMENT_TO_DAMAGE_TYPE[ element_type], charge_cost = 0, - damage_source_type = damage.damage_source_type )) else: # all others are +1 damage types @@ -304,7 +306,7 @@ def apply_elemental_reaction( ElementalReactionType.SUPERCONDUCT]: # 1 piercing dmg for other charactors damage_type = DamageElementalType.PIERCING - attack_target = damage.target_charactor_id + attack_target = damage.target_position.charactor_id for cnum, c in enumerate(target_charactors): if cnum == attack_target: continue @@ -313,13 +315,15 @@ def apply_elemental_reaction( match = damage.match, position = damage.position, id = damage.id, - target_player_id = damage.target_player_id, - target_charactor_id = cnum, + target_position = ObjectPosition( + player_id = damage.target_position.player_id, + charactor_id = cnum, + area = damage.target_position.area + ), damage = 1, damage_type = DamageType.DAMAGE, damage_elemental_type = damage_type, charge_cost = 0, - damage_source_type = damage.damage_source_type )) return res diff --git a/server/event_handler.py b/server/event_handler.py index 70547551..d0206bd2 100644 --- a/server/event_handler.py +++ b/server/event_handler.py @@ -66,8 +66,8 @@ def event_handler_RECEIVE_DAMAGE( After receive damage, generate side effects of elemental reaction. """ reaction = event.elemental_reaction - player_id = event.final_damage.target_player_id - charactor_id = event.final_damage.target_charactor_id + player_id = event.final_damage.target_position.player_id + charactor_id = event.final_damage.target_position.charactor_id act = elemental_reaction_side_effect( reaction, player_id, charactor_id, version = self.version diff --git a/server/match.py b/server/match.py index 599b31e2..c2673994 100644 --- a/server/match.py +++ b/server/match.py @@ -1628,8 +1628,13 @@ def _action_make_damage(self, action: MakeDamageAction) \ while len(damage_increase_value_lists) > 0: damage = damage_increase_value_lists.pop(0) damage_original = damage.copy(deep = True) - table = self.player_tables[damage.target_player_id] - charactor = table.charactors[damage.target_charactor_id] + assert ( + damage.target_position.area == ObjectPositionType.CHARACTOR + ), ( + 'Damage target position should be charactor.' + ) + table = self.player_tables[damage.target_position.player_id] + charactor = table.charactors[damage.target_position.charactor_id] damage_element_type = DAMAGE_TYPE_TO_ELEMENT[ damage.damage_elemental_type] target_element_application = charactor.element_application @@ -1639,14 +1644,14 @@ def _action_make_damage(self, action: MakeDamageAction) \ target_element_application ) if (elemental_reaction is ElementalReactionType.OVERLOADED - and damage.target_charactor_id + and damage.target_position.charactor_id == table.active_charactor_id): # overloaded to next charactor - if damage.target_player_id != target_id: + if damage.target_position.player_id != target_id: self._set_match_state(MatchState.ERROR) raise ValueError( 'Overloaded damage target player ' - f'{damage.target_player_id} ' + f'{damage.target_position.player_id} ' f'does not match action target player {target_id}.' ) assert action.charactor_change_rule == 'NONE', ( diff --git a/server/modifiable_values.py b/server/modifiable_values.py index eb2064ed..7b25cf80 100644 --- a/server/modifiable_values.py +++ b/server/modifiable_values.py @@ -2,8 +2,8 @@ from typing import List, Any, Literal from utils import BaseModel from .consts import ( - DieColor, DamageType, DamageElementalType, DamageSourceType, - ElementalReactionType, ElementType + DieColor, DamageType, DamageElementalType, + ElementalReactionType, ElementType, ObjectPositionType ) from .struct import DamageValue, ObjectPosition, DiceCost @@ -91,9 +91,7 @@ class DamageIncreaseValue(ModifiableValueBase): type: ModifiableValueTypes = ModifiableValueTypes.DAMAGE_INCREASE damage_type: DamageType - damage_source_type: DamageSourceType - target_player_id: int - target_charactor_id: int + target_position: ObjectPosition damage: int damage_elemental_type: DamageElementalType charge_cost: int @@ -123,7 +121,7 @@ def from_damage_value( if damage_value.target_charactor == 'ACTIVE': assert not charactors[active_id], 'Active charactor is defeated.' target_charactor = [active_id] - if damage_value.target_charactor == 'BACK': + elif damage_value.target_charactor == 'BACK': target_charactor = [x for x in range(len(charactors)) if not charactors[x] and x != active_id] elif damage_value.target_charactor == 'NEXT': @@ -137,6 +135,11 @@ def from_damage_value( if not charactors[idx]: target_charactor.append(idx) break + else: + raise NotImplementedError( + f'Unknown target charactor type: ' + f'{damage_value.target_charactor}' + ) result: List[DamageIncreaseValue] = [] for target in target_charactor: assert not charactors[target], 'Target charactor is defeated.' @@ -145,9 +148,11 @@ def from_damage_value( position = damage_value.position, id = damage_value.id, damage_type = damage_value.damage_type, - damage_source_type = damage_value.damage_source_type, - target_player_id = target_player, - target_charactor_id = target, + target_position = ObjectPosition( + player_id = target_player, + charactor_id = target, + area = ObjectPositionType.CHARACTOR + ), damage = damage_value.damage, damage_elemental_type = damage_value.damage_elemental_type, charge_cost = damage_value.charge_cost, @@ -168,9 +173,7 @@ def from_increase_value( position = increase_value.position, id = increase_value.id, damage_type = increase_value.damage_type, - damage_source_type = increase_value.damage_source_type, - target_player_id = increase_value.target_player_id, - target_charactor_id = increase_value.target_charactor_id, + target_position = increase_value.target_position, damage = increase_value.damage, damage_elemental_type = increase_value.damage_elemental_type, charge_cost = increase_value.charge_cost, @@ -191,9 +194,7 @@ def from_multiply_value( position = multiply_value.position, id = multiply_value.id, damage_type = multiply_value.damage_type, - damage_source_type = multiply_value.damage_source_type, - target_player_id = multiply_value.target_player_id, - target_charactor_id = multiply_value.target_charactor_id, + target_position = multiply_value.target_position, damage = multiply_value.damage, damage_elemental_type = multiply_value.damage_elemental_type, charge_cost = multiply_value.charge_cost, diff --git a/server/object_base.py b/server/object_base.py index 12629a98..36f58a55 100644 --- a/server/object_base.py +++ b/server/object_base.py @@ -14,7 +14,7 @@ ) from .consts import ( ObjectType, WeaponType, ElementType, DamageElementalType, SkillType, - DamageType, DamageSourceType, ELEMENT_TO_DIE_COLOR, ObjectPositionType, + DamageType, ELEMENT_TO_DIE_COLOR, ObjectPositionType, DiceCostLabels, ) from .modifiable_values import ModifiableValueTypes, DamageValue @@ -118,8 +118,6 @@ def get_actions(self, match: Any) -> List[Actions]: position = self.position, id = self.id, damage_type = DamageType.DAMAGE, - damage_source_type - = DamageSourceType.CURRENT_PLAYER_CHARACTOR, damage = self.damage, damage_elemental_type = self.damage_type, charge_cost = 0, diff --git a/server/status/charactor_status/system.py b/server/status/charactor_status/system.py index 292c0de9..8908a026 100644 --- a/server/status/charactor_status/system.py +++ b/server/status/charactor_status/system.py @@ -31,8 +31,10 @@ def value_modifier_DAMAGE_INCREASE( Increase damage for pyro and physical damages to self by 2, and decrease usage. """ - if (value.target_player_id != self.position.player_id - or value.target_charactor_id != self.position.charactor_id): + if ( + value.target_position.player_id != self.position.player_id + or value.target_position.charactor_id != self.position.charactor_id + ): # not attack self, not activate return value if value.damage_elemental_type in [ diff --git a/server/status/team_status/system.py b/server/status/team_status/system.py index c3681937..887a0fc1 100644 --- a/server/status/team_status/system.py +++ b/server/status/team_status/system.py @@ -30,7 +30,7 @@ def value_modifier_DAMAGE_INCREASE( Increase damage for dendro or electro damages, and decrease usage. TODO only active charactor will count! """ - if value.target_player_id == self.position.player_id: + if value.target_position.player_id == self.position.player_id: # attack self, not activate return value if value.damage_elemental_type in [ @@ -72,7 +72,7 @@ def value_modifier_DAMAGE_INCREASE( """ Increase damage for electro or pyro damages by 2, and decrease usage. """ - if value.target_player_id == self.position.player_id: + if value.target_position.player_id == self.position.player_id: # attack self, not activate return value if value.damage_elemental_type in [ @@ -111,7 +111,7 @@ def value_modifier_DAMAGE_DECREASE( """ Decrease damage by its usage, and decrease usage. """ - if value.target_player_id != self.position.player_id: + if value.target_position.player_id != self.position.player_id: # attack enemy, not activate return value if self.usage > 0: diff --git a/server/struct.py b/server/struct.py index 21e7bd8f..82148194 100644 --- a/server/struct.py +++ b/server/struct.py @@ -2,7 +2,7 @@ from utils import BaseModel from .consts import ( - ObjectPositionType, DamageType, DamageElementalType, DamageSourceType, + ObjectPositionType, DamageType, DamageElementalType, DieColor ) @@ -34,8 +34,8 @@ class DamageValue(BaseModel): damage. id (int): The id of the object who makes the damage, e.g. Skill, Summon, Status. - damage_type (DamageType): The type of the damage. - damage_source_type (DamageSourceType): The source type of the damage. + damage_type (DamageType): The type of the damage, damage, heal, or + element application (zero damage). damage (int): The damage value. damage_elemental_type (DamageElementalType): The elemental type of the damage. @@ -52,7 +52,6 @@ class DamageValue(BaseModel): position: ObjectPosition id: int damage_type: DamageType - damage_source_type: DamageSourceType damage: int damage_elemental_type: DamageElementalType charge_cost: int diff --git a/server/summon/base.py b/server/summon/base.py index 0136b748..fc0a10c2 100644 --- a/server/summon/base.py +++ b/server/summon/base.py @@ -1,7 +1,7 @@ from typing import Literal, Any from ..object_base import CardBase from ..consts import ( - ObjectType, DamageElementalType, DamageSourceType, DamageType + ObjectType, DamageElementalType, DamageType ) from ..event import ( RoundEndEventArguments, @@ -53,7 +53,6 @@ def event_handler_ROUND_END(self, event: RoundEndEventArguments) \ When round end, make damage to the opponent. """ player_id = self.position.player_id - source_type = DamageSourceType.CURRENT_PLAYER_SUMMON assert self.usage > 0 self.usage -= 1 return [ @@ -65,7 +64,6 @@ def event_handler_ROUND_END(self, event: RoundEndEventArguments) \ position = self.position, id = self.id, damage_type = DamageType.DAMAGE, - damage_source_type = source_type, damage = self.damage, damage_elemental_type = self.damage_elemental_type, charge_cost = 0,