summaryrefslogtreecommitdiff
path: root/swiftstory
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2021-12-23 22:49:35 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2021-12-23 23:15:21 +0100
commite9faa90b9e3c8138b5d4763a2d8279f61486238c (patch)
treecf8e4dc745caaf3f2da780a8c5e471ef10a13a73 /swiftstory
parent041f3e63118837acaa2316139dd9ca6c796a79e6 (diff)
Avoid dependency on Client from Game
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'swiftstory')
-rw-r--r--swiftstory/client.py16
-rw-r--r--swiftstory/game.py9
2 files changed, 13 insertions, 12 deletions
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: