diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-23 17:09:15 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-23 23:15:21 +0100 |
commit | 041f3e63118837acaa2316139dd9ca6c796a79e6 (patch) | |
tree | 28d26369b838bdc40306319565198ccd0ebdb33e /swiftstory | |
parent | e6b9b68a6539d74bec94c0a361187ed89ec5c124 (diff) |
Refactor GameManager and add control over game creation
GameManager had a method called join_game ; which was not actually doing
any joining. Instead, it was just returning a reference to the game that
would match the specified game name and language. In case the game would
not exist, it would be created before being returned.
The function is now renamed find_by_name. Also a new parameter "create"
has been added. It is a boolean that controls whether a game would be
created if it does not exist.
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'swiftstory')
-rw-r--r-- | swiftstory/client.py | 2 | ||||
-rw-r--r-- | swiftstory/game_manager.py | 17 |
2 files changed, 17 insertions, 2 deletions
diff --git a/swiftstory/client.py b/swiftstory/client.py index c3a2aff..437a3f9 100644 --- a/swiftstory/client.py +++ b/swiftstory/client.py @@ -24,7 +24,7 @@ class Client: lang = 'en' try: - game = self.game_manager.join_game(game_name, lang) + 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() diff --git a/swiftstory/game_manager.py b/swiftstory/game_manager.py index da2e5c7..6c2da1f 100644 --- a/swiftstory/game_manager.py +++ b/swiftstory/game_manager.py @@ -8,6 +8,15 @@ from swiftstory.game import Game from swiftstory.cards import Cards +class NoSuchGameError(Exception): + """ Exception to be raised when no game is found matching the criterias. + """ + def __init__(self, message: str = "", game_name: str = "", lang: str = "") -> None: + self.game_name = game_name + self.lang = lang + super().__init__(message) + + @dataclass class LangContainer: """ Container for game ojects in a given language. """ @@ -32,7 +41,10 @@ class GameManager: """ List available languages based on FS. """ return next(os.walk("usr/share/swiftstory/lang"))[1] - def join_game(self, game_name: str, lang: str) -> Game: + def find_by_name(self, game_name: str, lang: str, create=True) -> Game: + """ Find and return the game that matches the name and language + specified. If the game does not exist but create is set to True, a new + game will be created. """ container = self.lang_containers.get(lang) if container is None: raise UnsupportedLanguage(lang) @@ -43,6 +55,9 @@ class GameManager: game = games.get(game_name) + if game is None and not create: + raise NoSuchGameError(game_name=game_name, lang=lang) + if game is None: logging.info("Starting new game: %s (lang: %s)", game_name, lang) |