From 0226fb3a60039497e5de3fb201c599cb4a20f8aa Mon Sep 17 00:00:00 2001 From: legonzaur Date: Sat, 20 Apr 2024 15:04:27 +0200 Subject: [PATCH] Some game actions work --- src/controller/client.py | 13 +++++-- src/controller/game.py | 2 ++ src/model/card_board.py | 2 ++ src/model/game.py | 5 +++ src/model/player.py | 74 +++++++++++++++++++++++++++------------- 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/src/controller/client.py b/src/controller/client.py index 1f9e78e..7f6d852 100644 --- a/src/controller/client.py +++ b/src/controller/client.py @@ -81,11 +81,19 @@ class ClientController(ClientControllerInterface): if self.player.team != self.game_controller.game.turn: return await self.send_error("it is not your turn to play!") if message.type == "action": + print("message.data_type") if message.data_type == "buy": - for c in message.data: - await self.player.buy_card(c) + ## TODO : pydantic type check for message.data + await self.player.buy_cards(message.data) elif message.data_type == "play_cards": await self.player.play_cards(message.data) + elif message.data_type == "discard": + await self.player.discard_cards(message.data) + elif message.data_type == "draw": + await self.player.draw(message.data) + elif message.data_type == "end_turn": + + await self.player.end_turn() @vibe_check(LoginMessage) async def login_context(self, message: LoginMessage): @@ -149,4 +157,3 @@ class ClientController(ClientControllerInterface): self.player.ready = bool(message.data) if all(p.ready for p in self.game_controller.game.players): await self.game_controller.start_game() - self.on_message = self.game_context diff --git a/src/controller/game.py b/src/controller/game.py index 43c50ed..f7b2f22 100644 --- a/src/controller/game.py +++ b/src/controller/game.py @@ -106,6 +106,8 @@ class GameController(GameControllerInterface): async def start_game(self): await self.game.start_game() + for c in self.clients: + c.on_message = c.game_context await self.broadcast( Message(type="context", data_type="game_context_id", data="game") ) diff --git a/src/model/card_board.py b/src/model/card_board.py index 24dc009..24f3637 100644 --- a/src/model/card_board.py +++ b/src/model/card_board.py @@ -5,6 +5,8 @@ from model import Card, Serializable class BoardCard(Serializable): """Represents a card on a player's board during a game turn""" + card: Card + def __init__(self, card: Card): super().__init__() self.card = card diff --git a/src/model/game.py b/src/model/game.py index bfc6b94..a9a541e 100644 --- a/src/model/game.py +++ b/src/model/game.py @@ -189,3 +189,8 @@ class Game(Serializable): + " players : " + ", ".join([p.username for p in self.players]) ) + + async def end_turn(self): + team_index = self.teams.index(self.turn) + self.turn = self.teams[(team_index + 1) % len(self.teams)] + await self.notify_controller("update", self.serialize_guest()) diff --git a/src/model/player.py b/src/model/player.py index 0fe39a8..0d509a8 100644 --- a/src/model/player.py +++ b/src/model/player.py @@ -115,10 +115,30 @@ class Player(Serializable): self.hand.remove(card) await self.notify_update() - async def discard_card(self, cards_ids: List[str]): - pass + async def discard_cards(self, cards_ids: List[str]): + for s in cards_ids: + card = next( + (m for m in self.hand if str(m.uuid) == s), + None, + ) + if card is not None: + self.discard_pile.append(card) + self.hand.remove(card) + else: + board_card = next( + (m for m in self.board if str(m.card.uuid) == s), + None, + ) + if board_card is not None: + self.discard_pile.append(board_card.card) + self.board.remove(board_card) + else: + print("Card not found in board or hand") + continue + await self.notify_update() async def end_turn(self): + temp_board = [*self.board] for c in self.board: if c.card.card_type in [ CardType.HERO, @@ -127,44 +147,50 @@ class Player(Serializable): ]: c.reset() else: - self.board.remove(c) + temp_board.remove(c) self.discard_pile.append(c.card) + self.board = temp_board + for c in self.hand: - self.hand.remove(c) self.discard_pile.append(c) + self.hand = [] await self.draw(5) + await self.game.end_turn() - async def buy_card(self, card_id: str): - card = next( - ( - m - for m in [f for f in self.game.market if f is not None] - if str(m.uuid) == card_id - ), - None, - ) - if card is not None: - index = self.game.market.index(card) - self.game.market[index] = None - self.discard_pile.append(card) - await self.game.distribute_market_cards() - else: + async def buy_cards(self, card_ids: List[str]): + for card_id in card_ids: card = next( ( m - for m in [f for f in self.game.gem_stack if f is not None] + for m in [f for f in self.game.market if f is not None] if str(m.uuid) == card_id ), None, ) if card is not None: - self.game.gem_stack.remove(card) + index = self.game.market.index(card) + self.game.market[index] = None self.discard_pile.append(card) - await self.game.notify_controller("update", self.game.serialize_guest()) + await self.game.distribute_market_cards() else: - print("Card not found in market or gem_stack") - return + card = next( + ( + m + for m in [f for f in self.game.gem_stack if f is not None] + if str(m.uuid) == card_id + ), + None, + ) + if card is not None: + self.game.gem_stack.remove(card) + self.discard_pile.append(card) + await self.game.notify_controller( + "update", self.game.serialize_guest() + ) + else: + print("Card not found in market or gem_stack") + continue await self.notify_update()