From f1ebe18b9bcf646e8cfd0db4cabb0bb8cadae5e3 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 23 Dec 2021 16:31:42 +0100 Subject: Use a language container rather than a dictionary Signed-off-by: Olivier Gayot --- swiftstory/game_manager.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'swiftstory') diff --git a/swiftstory/game_manager.py b/swiftstory/game_manager.py index e7b3305..7551ac4 100644 --- a/swiftstory/game_manager.py +++ b/swiftstory/game_manager.py @@ -1,3 +1,4 @@ +from dataclasses import dataclass import logging import os from typing import Dict, List @@ -7,26 +8,34 @@ from swiftstory.game import Game from swiftstory.cards import Cards -class GameManager: - def __init__(self): - self.langs: Dict[str, dict] = {} +@dataclass +class LangContainer: + """ Container for game ojects in a given language. """ + black_cards: List[str] + white_cards: List[str] + games: Dict[str, Game] + - for filename in next(os.walk('usr/share/swiftstory/lang'))[1]: - self.langs[filename] = {} +class GameManager: - for lang in self.langs: - self.langs[lang]['black_cards'] = Cards.get_black_cards(lang) - self.langs[lang]['white_cards'] = Cards.get_white_cards(lang) + def __init__(self) -> None: + self.lang_containers: Dict[str, LangContainer] = {} - self.langs[lang]['games'] = {} + for language in next(os.walk('usr/share/swiftstory/lang'))[1]: + self.lang_containers[language] = LangContainer( + black_cards=Cards.get_black_cards(language), + white_cards=Cards.get_white_cards(language), + games={} + ) - def join_game(self, game_name, lang): - if self.langs.get(lang) is None: + def join_game(self, game_name: str, lang: str) -> Game: + container = self.lang_containers.get(lang) + if container is None: raise UnsupportedLanguage(lang) - games = self.langs[lang]['games'] - black_cards = self.langs[lang]['black_cards'] - white_cards = self.langs[lang]['white_cards'] + games = container.games + black_cards = container.black_cards + white_cards = container.white_cards game = games.get(game_name) -- cgit v1.2.3