From e9faa90b9e3c8138b5d4763a2d8279f61486238c Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 23 Dec 2021 22:49:35 +0100 Subject: Avoid dependency on Client from Game Signed-off-by: Olivier Gayot --- swiftstory/client.py | 16 ++++++++++++---- swiftstory/game.py | 9 +-------- 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'swiftstory') diff --git a/swiftstory/client.py b/swiftstory/client.py index 437a3f9..9ac08ed 100644 --- a/swiftstory/client.py +++ b/swiftstory/client.py @@ -6,17 +6,21 @@ import websockets.exceptions from swiftstory.game import Game from swiftstory.exception import WrongAction, UnsupportedLanguage, JoinError +from swiftstory.player import Player class Client: + """ Represent a client. + A client manages a (web)socket to communicate with the outside world. + It also manages the associated player when in a game. """ def __init__(self, socket, game_manager): self.game: Optional[Game] = None self.game_manager = game_manager self.socket = socket - self.player = None + self.player: Optional[Player] = None - def join_game(self, game_name, lang): + def join_game(self, game_name, lang) -> str: if self.game is not None: raise WrongAction('You are already in a game') @@ -27,9 +31,13 @@ class Client: game = self.game_manager.find_by_name(game_name, lang) except UnsupportedLanguage as e: raise JoinError(f"unsupported language: {str(e)}") from e - # XXX self.game will be assigned by game.try_join() - return game.try_join(self) + self.player = Player() + status = game.try_join(self.player) + self.game = game + self.monitor_player() + + return status def play_white_card(self, card_id): if self.game is None: diff --git a/swiftstory/game.py b/swiftstory/game.py index f6dca14..1655d73 100644 --- a/swiftstory/game.py +++ b/swiftstory/game.py @@ -30,7 +30,7 @@ class Game: random.shuffle(self.board.white_pick) random.shuffle(self.board.black_pick) - def try_join(self, client): + def try_join(self, player): if len(self.players) >= 10: raise JoinError('too many players in this game') @@ -42,16 +42,9 @@ class Game: except IndexError: raise JoinError('not enough white cards for player') - player = Player() - for card in cards: player.receive_card(card) - client.player = player - client.game = self - - client.monitor_player() - self.players.append(player) for p in self.players: -- cgit v1.2.3