diff options
Diffstat (limited to 'swiftstory')
-rw-r--r-- | swiftstory/client.py | 37 | ||||
-rw-r--r-- | swiftstory/game.py | 26 |
2 files changed, 39 insertions, 24 deletions
diff --git a/swiftstory/client.py b/swiftstory/client.py index 9ac08ed..98ba530 100644 --- a/swiftstory/client.py +++ b/swiftstory/client.py @@ -5,6 +5,7 @@ from typing import Optional import websockets.exceptions from swiftstory.game import Game +from swiftstory.game_manager import GameManager from swiftstory.exception import WrongAction, UnsupportedLanguage, JoinError from swiftstory.player import Player @@ -13,14 +14,14 @@ 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): + def __init__(self, socket, game_manager: GameManager) -> None: self.game: Optional[Game] = None - self.game_manager = game_manager + self.game_manager: GameManager = game_manager self.socket = socket self.player: Optional[Player] = None - def join_game(self, game_name, lang) -> str: + def join_game(self, game_name: str, lang: Optional[str]) -> str: if self.game is not None: raise WrongAction('You are already in a game') @@ -39,39 +40,53 @@ class Client: return status - def play_white_card(self, card_id): + def play_white_card(self, card_id: int) -> str: if self.game is None: raise WrongAction('You have to join a game first') + if self.player is None: + raise ValueError("Player is None") return self.game.try_play_card(self.player, card_id) - def pick_black_card(self): + def pick_black_card(self) -> str: if self.game is None: raise WrongAction('You have to join a game first') + if self.player is None: + raise ValueError("Player is None") return self.game.try_become_judge(self.player) - def collect_cards(self): + def collect_cards(self) -> str: if self.game is None: raise WrongAction('You have to join a game first') + if self.player is None: + raise ValueError("Player is None") return self.game.try_collect_cards(self.player) - def designate_card(self, card_id): + def designate_card(self, card_id: int) -> str: if self.game is None: raise WrongAction('You have to join a game first') + if self.player is None: + raise ValueError("Player is None") return self.game.try_designate_card(self.player, card_id) - def view_player_cards(self): + def view_player_cards(self) -> str: if self.game is None: raise WrongAction('You have to join a game first') + if self.player is None: + raise ValueError("Player is None") return self.game.try_view_player_cards(self.player) - def view_played_cards(self): + def view_played_cards(self) -> str: if self.game is None: raise WrongAction('You have to join a game first') + if self.player is None: + raise ValueError("Player is None") return self.game.try_view_played_cards(self.player) - def view_black_card(self): + def view_black_card(self) -> str: if self.game is None: raise WrongAction('You have to join a game first') + if self.player is None: + raise ValueError("Player is None") return self.game.try_view_black_card(self.player) def monitor_player(self) -> None: @@ -87,7 +102,7 @@ class Client: asyncio.create_task(f()) - def disconnect(self): + def disconnect(self) -> None: if self.player is not None: if self.game is None: raise ValueError("Disconnect from inexistent game.") diff --git a/swiftstory/game.py b/swiftstory/game.py index 1655d73..4569a5c 100644 --- a/swiftstory/game.py +++ b/swiftstory/game.py @@ -1,6 +1,6 @@ from enum import Enum, auto import random -from typing import Optional +from typing import List, Optional, Tuple from swiftstory.exception import WrongAction, JoinError from swiftstory.player import Player @@ -15,11 +15,11 @@ class GameState(Enum): class Game: - def __init__(self, white_desc, black_desc): + def __init__(self, white_desc: List[str], black_desc: List[str]) -> None: self.state = GameState.WAITING_NEW_JUDGE - self.players = [] + self.players: List[Player] = [] self.judge: Optional[Player] = None @@ -30,11 +30,11 @@ class Game: random.shuffle(self.board.white_pick) random.shuffle(self.board.black_pick) - def try_join(self, player): + def try_join(self, player: Player) -> str: if len(self.players) >= 10: raise JoinError('too many players in this game') - cards = [] + cards: List[Tuple[int, str]] = [] try: for _ in range(10): @@ -63,7 +63,7 @@ class Game: return success({'cards': cards, 'game_state': state}) - def try_become_judge(self, player): + def try_become_judge(self, player: Player) -> str: if self.state is not GameState.WAITING_NEW_JUDGE: # TODO what if the judge has quit ? raise WrongAction('Someone is judge already') @@ -80,7 +80,7 @@ class Game: return self.try_view_black_card(player) - def try_play_card(self, player, card_id): + def try_play_card(self, player: Player, card_id: int) -> str: if self.state is not GameState.WAITING_COLLECTION: raise WrongAction('Who asked you to play now ?!') @@ -105,7 +105,7 @@ class Game: return success({'card_id': card_id}) - def try_collect_cards(self, player): + def try_collect_cards(self, player: Player) -> str: if self.state is not GameState.WAITING_COLLECTION: raise WrongAction("Do you think it's the moment for collection !?") @@ -124,7 +124,7 @@ class Game: return self.try_view_played_cards(player) - def try_designate_card(self, player, card_id): + def try_designate_card(self, player: Player, card_id: int) -> str: if self.state is not GameState.WAITING_DESIGNATION: raise WrongAction('Not now, moron !') @@ -173,16 +173,16 @@ class Game: return success(None) - def try_view_player_cards(self, player): + def try_view_player_cards(self, player: Player) -> str: return success([(idx, desc) for idx, (_, desc) in player.cards.items()]) - def try_view_played_cards(self, player): + def try_view_played_cards(self, player: Player): if self.state is not GameState.WAITING_DESIGNATION: raise WrongAction('Not now, moron !') return success([desc for (_, desc), _ in self.board.played_cards]) - def try_view_black_card(self, player): + def try_view_black_card(self, player: Player) -> str: card = self.board.current_black_card if card is not None: @@ -190,7 +190,7 @@ class Game: raise WrongAction('The black card has not been revealed yet') - def disconnect(self, player): + def disconnect(self, player: Player) -> None: if self.judge is player: self.judge = None |