|
11 | 11 |
|
12 | 12 | # game_handler handles the blackJack-game-objects. When a new game is created, it is saved in the "game_list" |
13 | 13 | class GameHandler(object): |
14 | | - GameList = [] # List, where the running Games are stored in |
15 | | - |
16 | | - def gl_create(self) -> None: |
17 | | - self.GameList = [] |
18 | | - |
19 | | - def gl_remove(self, chat_id: int) -> None: |
20 | | - index = self.get_index_by_chatid(chat_id) |
21 | | - if index is None: |
22 | | - return |
23 | | - if not index < 0: |
24 | | - self.GameList.pop(index) |
25 | | - |
26 | | - def add_game(self, blackjackgame: BlackJackGame) -> None: |
27 | | - self.GameList.append(blackjackgame) |
28 | | - |
29 | | - def get_index_by_chatid(self, chat_id: int) -> Optional[int]: |
30 | | - for index, game in enumerate(self.GameList): |
31 | | - if game.chat_id == chat_id: |
32 | | - return index |
33 | | - else: |
34 | | - for player in game.players: |
35 | | - if player.user_id == chat_id: |
36 | | - return index |
37 | | - |
38 | | - return None |
39 | | - |
40 | | - def get_game_by_chatid(self, chat_id: int) -> Optional[BlackJackGame]: |
41 | | - index = self.get_index_by_chatid(chat_id) |
42 | | - if index is None: |
| 14 | + class __GameHandler(object): |
| 15 | + def __init__(self): |
| 16 | + self.game_list = [] # List, where the running Games are stored in |
| 17 | + pass |
| 18 | + |
| 19 | + def gl_create(self) -> None: |
| 20 | + self.game_list = [] |
| 21 | + |
| 22 | + def gl_remove(self, chat_id: int) -> None: |
| 23 | + index = self.get_index_by_chatid(chat_id) |
| 24 | + if index is None: |
| 25 | + return |
| 26 | + if not index < 0: |
| 27 | + self.game_list.pop(index) |
| 28 | + |
| 29 | + def get_index_by_chatid(self, chat_id: int) -> Optional[int]: |
| 30 | + for index, game in enumerate(self.game_list): |
| 31 | + if game.chat_id == chat_id: |
| 32 | + return index |
| 33 | + else: |
| 34 | + for player in game.players: |
| 35 | + if player.user_id == chat_id: |
| 36 | + return index |
| 37 | + |
43 | 38 | return None |
44 | | - return self.GameList[index] |
45 | 39 |
|
46 | | - def get_game_by_index(self, index: int) -> BlackJackGame: |
47 | | - return self.GameList[index] |
| 40 | + def add_game(self, blackjackgame: BlackJackGame) -> None: |
| 41 | + self.game_list.append(blackjackgame) |
48 | 42 |
|
49 | | - def get_game_by_id(self, game_id: int) -> Optional[BlackJackGame]: |
50 | | - if game_id is None: |
51 | | - return None |
52 | | - for game in self.GameList: |
53 | | - if game.game_id == game_id: |
54 | | - return game |
55 | | - return None |
| 43 | + def get_game_by_chatid(self, chat_id: int) -> Optional[BlackJackGame]: |
| 44 | + index = self.get_index_by_chatid(chat_id) |
| 45 | + if index is None: |
| 46 | + return None |
| 47 | + return self.game_list[index] |
56 | 48 |
|
57 | | - def generate_id(self) -> str: |
58 | | - game_id = ''.join(random.choice(string.digits + string.ascii_letters) for _ in range(8)) |
| 49 | + def get_game_by_index(self, index: int) -> BlackJackGame: |
| 50 | + return self.game_list[index] |
| 51 | + |
| 52 | + def get_game_by_id(self, game_id: int) -> Optional[BlackJackGame]: |
| 53 | + if game_id is None: |
| 54 | + return None |
| 55 | + for game in self.game_list: |
| 56 | + if game.game_id == game_id: |
| 57 | + return game |
| 58 | + return None |
59 | 59 |
|
60 | | - while self.id_already_existing(game_id): |
61 | | - print("ID already existing: " + str(game_id)) |
| 60 | + def generate_id(self) -> str: |
| 61 | + """Generates a random ID for a game""" |
62 | 62 | game_id = ''.join(random.choice(string.digits + string.ascii_letters) for _ in range(8)) |
63 | 63 |
|
64 | | - return game_id |
| 64 | + while self.id_already_existing(game_id): |
| 65 | + print("ID already existing: " + str(game_id)) |
| 66 | + game_id = ''.join(random.choice(string.digits + string.ascii_letters) for _ in range(8)) |
65 | 67 |
|
66 | | - def id_already_existing(self, game_id: str) -> bool: |
67 | | - for game in self.GameList: |
68 | | - if game.game_id == game_id: |
69 | | - return True |
| 68 | + return game_id |
70 | 69 |
|
71 | | - return False |
| 70 | + def id_already_existing(self, game_id: str) -> bool: |
| 71 | + """Checks if an ID is already existing in the list of games""" |
| 72 | + for game in self.game_list: |
| 73 | + if game.game_id == game_id: |
| 74 | + return True |
| 75 | + |
| 76 | + return False |
| 77 | + |
| 78 | + instance = None |
72 | 79 |
|
73 | 80 | def __init__(self): |
74 | | - self.GameList = [] |
| 81 | + if not GameHandler.instance: |
| 82 | + GameHandler.instance = GameHandler.__GameHandler() |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def get_instance() -> __GameHandler: |
| 86 | + if not GameHandler.instance: |
| 87 | + GameHandler.instance = GameHandler.__GameHandler() |
| 88 | + |
| 89 | + return GameHandler.instance |
0 commit comments