diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-11-02 00:03:20 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-11-02 00:03:20 +0100 |
commit | 1f18b5053c44d418e3059a789cb9bdf40f3f9b10 (patch) | |
tree | b3719b088cd22f5585a157ce97d9deaf754bcc43 /swiftstory | |
parent | bb062fcd972fb0a281fcdd743647f22d0852e737 (diff) |
also add the JoinError exception type
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'swiftstory')
-rw-r--r-- | swiftstory/Client.py | 7 | ||||
-rw-r--r-- | swiftstory/Game.py | 6 | ||||
-rw-r--r-- | swiftstory/SwiftStory.py | 5 | ||||
-rw-r--r-- | swiftstory/exception.py | 3 |
4 files changed, 15 insertions, 6 deletions
diff --git a/swiftstory/Client.py b/swiftstory/Client.py index 93ea232..a84783d 100644 --- a/swiftstory/Client.py +++ b/swiftstory/Client.py @@ -1,7 +1,7 @@ import asyncio import logging -from swiftstory.exception import WrongAction +from swiftstory.exception import WrongAction, UnsupportedLanguage, JoinError class Client: @@ -19,7 +19,10 @@ class Client: if lang is None: lang = 'en' - game = self.game_manager.join_game(game_name, lang) + try: + game = self.game_manager.join_game(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) diff --git a/swiftstory/Game.py b/swiftstory/Game.py index 0df2e16..0057bc7 100644 --- a/swiftstory/Game.py +++ b/swiftstory/Game.py @@ -1,6 +1,6 @@ import random -from swiftstory.exception import WrongAction +from swiftstory.exception import WrongAction, JoinError from swiftstory.Player import Player from swiftstory.Board import Board from swiftstory.Status import error, success @@ -30,7 +30,7 @@ class Game: def try_join(self, client): if len(self.players) >= 10: - return error('too many players in this game') + raise JoinError('too many players in this game') cards = [] @@ -38,7 +38,7 @@ class Game: for _ in range(10): cards.append(self.board.pick_white_card()) except IndexError: - return error('no enough white cards for player') + raise JoinError('not enough white cards for player') player = Player(client) diff --git a/swiftstory/SwiftStory.py b/swiftstory/SwiftStory.py index b81b0c3..17d88fe 100644 --- a/swiftstory/SwiftStory.py +++ b/swiftstory/SwiftStory.py @@ -8,7 +8,7 @@ import logging import websockets import swiftstory.GameManager -from swiftstory.exception import WrongAction +from swiftstory.exception import WrongAction, JoinError from swiftstory.Client import Client from swiftstory.Status import error @@ -65,6 +65,9 @@ def message_received_handler(client, message): return error('invalid command') except WrongAction as e: return error(str(e)) + except JoinError as e: + logging.warning("player could not join game: %s", e.__repr__()) + return error(str(e)) async def connection_handler(websocket, path): diff --git a/swiftstory/exception.py b/swiftstory/exception.py index ac2a074..764289f 100644 --- a/swiftstory/exception.py +++ b/swiftstory/exception.py @@ -4,3 +4,6 @@ class WrongAction(Exception): class UnsupportedLanguage(Exception): pass + +class JoinError(Exception): + pass |