Skip to content

Commit

Permalink
refactor: send Match to is_valid function.
Browse files Browse the repository at this point in the history
is_valid is used in card, skill and response to check whether they are
valid to be used.
  • Loading branch information
zyr17 committed Aug 26, 2023
1 parent dbecfc4 commit 4f3e9bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
25 changes: 9 additions & 16 deletions server/interaction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from utils import BaseModel
from typing import Literal, List
from typing import Literal, List, Any
from enum import Enum
from .consts import DieColor
from .struct import DiceCost, CardActionTarget
Expand Down Expand Up @@ -136,7 +136,7 @@ def player_id(self) -> int:
"""
return self.request.player_id

def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Check whether the response is valid.
"""
Expand All @@ -151,8 +151,7 @@ class SwitchCardResponse(ResponseBase):
request: SwitchCardRequest
card_ids: List[int]

@property
def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
if len(self.card_ids) > self.request.maximum_switch_number:
return False
if len(set(self.card_ids)) != len(self.card_ids):
Expand All @@ -175,8 +174,7 @@ class ChooseCharactorResponse(ResponseBase):
request: ChooseCharactorRequest
charactor_id: int

@property
def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
return self.charactor_id in self.request.available_charactor_ids


Expand All @@ -185,8 +183,7 @@ class RerollDiceResponse(ResponseBase):
request: RerollDiceRequest
reroll_dice_ids: List[int]

@property
def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
"""
if have duplicate dice ids, or dice ids out of range, return False.
"""
Expand All @@ -206,8 +203,7 @@ class SwitchCharactorResponse(ResponseBase):
charactor_id: int
cost_ids: List[int]

@property
def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Charactor is in the candidate charactors.
Cost matches the request.
Expand All @@ -224,8 +220,7 @@ class ElementalTuningResponse(ResponseBase):
cost_id: int
card_id: int

@property
def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Check whether the response is valid.
"""
Expand All @@ -246,8 +241,7 @@ class UseSkillResponse(ResponseBase):
cost_ids: List[int]
# TODO: choose target

@property
def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Check whether the response is valid.
"""
Expand All @@ -262,8 +256,7 @@ class UseCardResponse(ResponseBase):
target: CardActionTarget | None
# TODO: choose target

@property
def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Check whether the response is valid.
"""
Expand Down
6 changes: 3 additions & 3 deletions server/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def respond(self, response: Responses) -> bool:
logging.error('Match is not waiting for response.')
return False
# check if the response is valid
if not response.is_valid:
if not response.is_valid(self):
logging.error('Response is not valid.')
return False
# check if the request exist
Expand Down Expand Up @@ -895,7 +895,7 @@ def _request_use_skill(self, player_id: int):
# stunned, cannot use skill.
return
for sid, skill in enumerate(front_charactor.skills):
if skill.is_valid(front_charactor.hp, front_charactor.charge):
if skill.is_valid(self):
cost = skill.cost.copy(deep = True)
cost_value = DiceCostValue(
cost = cost,
Expand Down Expand Up @@ -931,7 +931,7 @@ def _request_use_card(self, player_id: int):
table = self.player_tables[player_id]
cards = table.hands
for cid, card in enumerate(cards):
if card.is_valid():
if card.is_valid(self):
cost = card.cost.copy(deep = True)
cost_value = DiceCostValue(
cost = cost,
Expand Down
10 changes: 6 additions & 4 deletions server/object_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(self, *argv, **kwargs):
if self.cost.original_value is not None:
self.cost.original_value.label = self.cost_label

def is_valid(self, hp: int, charge: int) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Check if the skill can be used.
"""
Expand Down Expand Up @@ -222,11 +222,13 @@ def __init__(self, *argv, **kwargs):
'XXX', self.damage_type.value.lower().capitalize())
self.desc = self.desc.replace('%d', str(self.damage))

def is_valid(self, hp: int, charge: int) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Check if the skill can be used.
"""
return self.charge <= charge
table = match.player_tables[self.position.player_id]
charactor = table.charactors[self.position.charactor_id]
return self.charge <= charactor.charge

def get_actions(self, match: Any) -> List[Actions]:
"""
Expand Down Expand Up @@ -291,7 +293,7 @@ def get_actions(
"""
raise NotImplementedError()

def is_valid(self) -> bool:
def is_valid(self, match: Any) -> bool:
"""
Check if the card can be used. Note that this function will not check
the cost of the card.
Expand Down

0 comments on commit 4f3e9bd

Please sign in to comment.