From 745d83dcfbc9dfbcf14182b1c7f8fee656500337 Mon Sep 17 00:00:00 2001 From: zyr17 Date: Thu, 31 Aug 2023 12:14:43 +0800 Subject: [PATCH] feat: implement toss-up --- server/action.py | 11 +++++ server/card/event/others.py | 81 ++++++++++++++++++++++++------------- server/match.py | 10 ++++- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/server/action.py b/server/action.py index ab167f46..545ea00e 100644 --- a/server/action.py +++ b/server/action.py @@ -44,6 +44,7 @@ class ActionTypes(str, Enum): # generate request actions GENERATE_CHOOSE_CHARACTOR = 'GENERATE_CHOOSE_CHARACTOR' + GENERATE_REROLL_DICE = 'GENERATE_REROLL_DICE' class ActionBase(BaseModel): @@ -325,6 +326,16 @@ class GenerateChooseCharactorRequestAction(ActionBase): player_idx: int +class GenerateRerollDiceRequestAction(ActionBase): + """ + Action for generating reroll dice request. + """ + type: Literal[ActionTypes.GENERATE_REROLL_DICE] = \ + ActionTypes.GENERATE_REROLL_DICE + player_idx: int + reroll_times: int + + Actions = ( ActionBase | DrawCardAction | RestoreCardAction | RemoveCardAction | ChooseCharactorAction | CreateDiceAction | RemoveDiceAction diff --git a/server/card/event/others.py b/server/card/event/others.py index 40cfb5a7..58eded7a 100644 --- a/server/card/event/others.py +++ b/server/card/event/others.py @@ -7,36 +7,13 @@ from ...consts import DieColor, ObjectPositionType from ...object_base import CardBase -from ...action import CreateDiceAction, CreateObjectAction, DrawCardAction +from ...action import ( + CreateDiceAction, CreateObjectAction, DrawCardAction, + GenerateRerollDiceRequestAction +) from ...struct import Cost, ObjectPosition -class Strategize(CardBase): - name: Literal['Strategize'] - desc: str = '''Draw 2 cards.''' - version: Literal['3.3'] = '3.3' - cost: Cost = Cost( - same_dice_number = 1 - ) - - def get_targets(self, match: Any) -> List[ObjectPosition]: - # no targets - return [] - - def get_actions( - self, target: ObjectPosition | None, match: Any - ) -> list[DrawCardAction]: - """ - Act the card. Draw two cards. - """ - assert target is None # no targets - return [DrawCardAction( - player_idx = self.position.player_idx, - number = 2, - draw_if_filtered_not_enough = True - )] - - class TheBestestTravelCompanion(CardBase): name: Literal['The Bestest Travel Companion!'] desc: str = '''Convert the Elemental Dice spent to Omni Element x2.''' @@ -94,4 +71,52 @@ def get_actions( )] -OtherEventCards = Strategize | TheBestestTravelCompanion | ChangingShifts +class TossUp(CardBase): + name: Literal['Toss-Up'] = 'Toss-Up' + desc: str = '''Select any Elemental Dice to reroll. Can reroll 2 times.''' + version: Literal['3.3'] = '3.3' + cost: Cost = Cost() + + def get_targets(self, match: Any) -> List[ObjectPosition]: + # no targets + return [] + + def get_actions( + self, target: ObjectPosition | None, match: Any + ) -> list[GenerateRerollDiceRequestAction]: + assert target is None + return [GenerateRerollDiceRequestAction( + player_idx = self.position.player_idx, + reroll_times = 2, + )] + + +class Strategize(CardBase): + name: Literal['Strategize'] + desc: str = '''Draw 2 cards.''' + version: Literal['3.3'] = '3.3' + cost: Cost = Cost( + same_dice_number = 1 + ) + + def get_targets(self, match: Any) -> List[ObjectPosition]: + # no targets + return [] + + def get_actions( + self, target: ObjectPosition | None, match: Any + ) -> list[DrawCardAction]: + """ + Act the card. Draw two cards. + """ + assert target is None # no targets + return [DrawCardAction( + player_idx = self.position.player_idx, + number = 2, + draw_if_filtered_not_enough = True + )] + + +OtherEventCards = ( + TheBestestTravelCompanion | ChangingShifts | TossUp | Strategize +) diff --git a/server/match.py b/server/match.py index 428730c9..6261a03e 100644 --- a/server/match.py +++ b/server/match.py @@ -28,6 +28,7 @@ MoveObjectAction, CharactorDefeatedAction, GenerateChooseCharactorRequestAction, + GenerateRerollDiceRequestAction, ) from .interaction import ( Requests, Responses, @@ -1137,7 +1138,6 @@ def _respond_reroll_dice(self, response: RerollDiceResponse): if isinstance(req, RerollDiceRequest): if req.player_idx == response.player_idx: if req.reroll_times > 1: - raise NotImplementedError('Not tested part') req.reroll_times -= 1 else: self.requests.pop(num) @@ -1383,6 +1383,8 @@ def _act(self, action: ActionBase) -> List[EventArguments]: return list(self._action_consume_arcane_legend(action)) elif isinstance(action, GenerateChooseCharactorRequestAction): return list(self._action_generate_choose_charactor_request(action)) + elif isinstance(action, GenerateRerollDiceRequestAction): + return list(self._action_generate_reroll_dice_request(action)) else: self._set_match_state(MatchState.ERROR) # pragma no cover raise AssertionError(f'Unknown action {action}.') @@ -2273,3 +2275,9 @@ def _action_generate_choose_charactor_request( ) -> List[EventArgumentsBase]: self._request_choose_charactor(action.player_idx) return [] + + def _action_generate_reroll_dice_request( + self, action: GenerateRerollDiceRequestAction + ) -> List[EventArgumentsBase]: + self._request_reroll_dice(action.player_idx, action.reroll_times) + return []