Skip to content

Commit

Permalink
refactor: change structure of DamageValue
Browse files Browse the repository at this point in the history
remove DamageSourceType, combine target_player_id and
target_charactor_id as target_position.
  • Loading branch information
zyr17 committed Aug 27, 2023
1 parent 02c1ebb commit 90698a4
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 78 deletions.
5 changes: 1 addition & 4 deletions server/charactor/electro/fischl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
29 changes: 0 additions & 29 deletions server/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 12 additions & 8 deletions server/elemental_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions server/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions server/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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', (
Expand Down
31 changes: 16 additions & 15 deletions server/modifiable_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand All @@ -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.'
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions server/object_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions server/status/charactor_status/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
6 changes: 3 additions & 3 deletions server/status/team_status/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions server/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from utils import BaseModel
from .consts import (
ObjectPositionType, DamageType, DamageElementalType, DamageSourceType,
ObjectPositionType, DamageType, DamageElementalType,
DieColor
)

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions server/summon/base.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 [
Expand All @@ -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,
Expand Down

0 comments on commit 90698a4

Please sign in to comment.