|
2 | 2 |
|
3 | 3 | import random |
4 | 4 | import string |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from game.blackJackGame import BlackJackGame |
5 | 8 |
|
6 | 9 | __author__ = 'Rico' |
7 | 10 |
|
8 | 11 |
|
9 | | -# game_handler handles the blackJack-game-objects. When a new object is created, it is saved in "GameList" |
10 | | -# get_index_by_chatid returns the index of a running game in the list |
| 12 | +# game_handler handles the blackJack-game-objects. When a new game is created, it is saved in the "game_list" |
11 | 13 | class GameHandler(object): |
12 | | - GameList = [] # List, where the running Games are stored in |
13 | | - |
14 | | - def gl_create(self): |
15 | | - self.GameList = [] |
16 | | - |
17 | | - def gl_remove(self, chat_id): |
18 | | - index = self.get_index_by_chatid(chat_id) |
19 | | - if index is None: |
20 | | - return |
21 | | - if not index < 0: |
22 | | - self.GameList.pop(index) |
23 | | - |
24 | | - def add_game(self, blackjackgame): |
25 | | - self.GameList.append(blackjackgame) |
26 | | - |
27 | | - def get_index_by_chatid(self, chat_id): |
28 | | - for index, game in enumerate(self.GameList): |
29 | | - if game.chat_id == chat_id: |
30 | | - return index |
31 | | - else: |
32 | | - for player in game.players: |
33 | | - if player.user_id == chat_id: |
34 | | - return index |
35 | | - |
36 | | - return None |
37 | | - |
38 | | - def get_game_by_chatid(self, chat_id): |
39 | | - index = self.get_index_by_chatid(chat_id) |
40 | | - 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 | + |
41 | 38 | return None |
42 | | - return self.GameList[index] |
43 | 39 |
|
44 | | - def get_game_by_index(self, index): |
45 | | - return self.GameList[index] |
| 40 | + def add_game(self, blackjackgame: BlackJackGame) -> None: |
| 41 | + self.game_list.append(blackjackgame) |
46 | 42 |
|
47 | | - def get_game_by_id(self, game_id): |
48 | | - if game_id is None: |
49 | | - return None |
50 | | - for game in self.GameList: |
51 | | - if game.get_game_id() == game_id: |
52 | | - return game |
53 | | - 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] |
54 | 48 |
|
55 | | - def generate_id(self): |
56 | | - 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 |
57 | 59 |
|
58 | | - while self.id_already_existing(game_id): |
59 | | - print("ID already existing: " + str(game_id)) |
| 60 | + def generate_id(self) -> str: |
| 61 | + """Generates a random ID for a game""" |
60 | 62 | game_id = ''.join(random.choice(string.digits + string.ascii_letters) for _ in range(8)) |
61 | 63 |
|
62 | | - 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)) |
63 | 67 |
|
64 | | - def id_already_existing(self, game_id): |
65 | | - for game in self.GameList: |
66 | | - if game.get_game_id() == game_id: |
67 | | - return True |
| 68 | + return game_id |
68 | 69 |
|
69 | | - 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 |
70 | 79 |
|
71 | 80 | def __init__(self): |
72 | | - 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