summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2021-12-23 16:31:42 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2021-12-23 23:15:21 +0100
commitf1ebe18b9bcf646e8cfd0db4cabb0bb8cadae5e3 (patch)
tree2f93c81187fecf56570a4bcaca5047e6628e47ee
parent7d39216a3bbea50223ac4a6d7a9e6343b8247886 (diff)
Use a language container rather than a dictionary
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rw-r--r--swiftstory/game_manager.py37
1 files changed, 23 insertions, 14 deletions
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)