summaryrefslogtreecommitdiff
path: root/swiftstory/game_manager.py
blob: 7551ac48d61e543e9cfcbeb2c54e0180d5fb9bea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from dataclasses import dataclass
import logging
import os
from typing import Dict, List

from swiftstory.exception import UnsupportedLanguage
from swiftstory.game import Game
from swiftstory.cards import Cards


@dataclass
class LangContainer:
    """ Container for game ojects in a given language. """
    black_cards: List[str]
    white_cards: List[str]
    games: Dict[str, Game]


class GameManager:

    def __init__(self) -> None:
        self.lang_containers: Dict[str, LangContainer] = {}

        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: str, lang: str) -> Game:
        container = self.lang_containers.get(lang)
        if container is None:
            raise UnsupportedLanguage(lang)

        games = container.games
        black_cards = container.black_cards
        white_cards = container.white_cards

        game = games.get(game_name)

        if game is None:
            logging.info("Starting new game: %s (lang: %s)", game_name, lang)

            game = games[game_name] = Game(white_cards, black_cards)

        return game