From 4f3e9bd2f216c8070c1d80b6ed1bf1d974dbdb2b Mon Sep 17 00:00:00 2001 From: zyr17 Date: Sat, 26 Aug 2023 22:28:01 +0800 Subject: [PATCH] refactor: send Match to is_valid function. is_valid is used in card, skill and response to check whether they are valid to be used. --- server/interaction.py | 25 +++++++++---------------- server/match.py | 6 +++--- server/object_base.py | 10 ++++++---- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/server/interaction.py b/server/interaction.py index cbd8d2ba..157e4c25 100644 --- a/server/interaction.py +++ b/server/interaction.py @@ -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 @@ -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. """ @@ -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): @@ -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 @@ -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. """ @@ -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. @@ -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. """ @@ -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. """ @@ -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. """ diff --git a/server/match.py b/server/match.py index bed289f1..9c62357f 100644 --- a/server/match.py +++ b/server/match.py @@ -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 @@ -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, @@ -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, diff --git a/server/object_base.py b/server/object_base.py index 3af9b4bb..a539c4ab 100644 --- a/server/object_base.py +++ b/server/object_base.py @@ -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. """ @@ -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]: """ @@ -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.