Skip to content

Commit af40bba

Browse files
committed
TASK: Implement game handler as singleton
1 parent 86a4d6e commit af40bba

File tree

2 files changed

+66
-51
lines changed

2 files changed

+66
-51
lines changed

gamehandler.py

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,79 @@
1111

1212
# game_handler handles the blackJack-game-objects. When a new game is created, it is saved in the "game_list"
1313
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+
4338
return None
44-
return self.GameList[index]
4539

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)
4842

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]
5648

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
5959

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"""
6262
game_id = ''.join(random.choice(string.digits + string.ascii_letters) for _ in range(8))
6363

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))
6567

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
7069

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
7279

7380
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

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
updater = Updater(token=BOT_TOKEN)
3333
dispatcher = updater.dispatcher
3434

35-
game_handler = GameHandler()
35+
game_handler = GameHandler().get_instance()
3636
tg_bot = updater.bot
3737

3838

0 commit comments

Comments
 (0)