-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgamehandler.py
89 lines (67 loc) · 2.87 KB
/
gamehandler.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
import random
import string
from typing import Optional
from game.blackJackGame_old import BlackJackGame
__author__ = 'Rico'
# game_handler handles the blackJack-game-objects. When a new game is created, it is saved in the "game_list"
class GameHandler(object):
class __GameHandler(object):
def __init__(self):
self.game_list = [] # List, where the running Games are stored in
pass
def gl_create(self) -> None:
self.game_list = []
def gl_remove(self, chat_id: int) -> None:
index = self.get_index_by_chatid(chat_id)
if index is None:
return
if not index < 0:
self.game_list.pop(index)
def get_index_by_chatid(self, chat_id: int) -> Optional[int]:
for index, game in enumerate(self.game_list):
if game.chat_id == chat_id:
return index
else:
for player in game.players:
if player.user_id == chat_id:
return index
return None
def add_game(self, blackjackgame: BlackJackGame) -> None:
self.game_list.append(blackjackgame)
def get_game_by_chatid(self, chat_id: int) -> Optional[BlackJackGame]:
index = self.get_index_by_chatid(chat_id)
if index is None:
return None
return self.game_list[index]
def get_game_by_index(self, index: int) -> BlackJackGame:
return self.game_list[index]
def get_game_by_id(self, game_id: int) -> Optional[BlackJackGame]:
if game_id is None:
return None
for game in self.game_list:
if game.game_id == game_id:
return game
return None
def generate_id(self) -> str:
"""Generates a random ID for a game"""
game_id = ''.join(random.choice(string.digits + string.ascii_letters) for _ in range(8))
while self.id_already_existing(game_id):
print("ID already existing: " + str(game_id))
game_id = ''.join(random.choice(string.digits + string.ascii_letters) for _ in range(8))
return game_id
def id_already_existing(self, game_id: str) -> bool:
"""Checks if an ID is already existing in the list of games"""
for game in self.game_list:
if game.game_id == game_id:
return True
return False
instance = None
def __init__(self):
if not GameHandler.instance:
GameHandler.instance = GameHandler.__GameHandler()
@staticmethod
def get_instance() -> __GameHandler:
if not GameHandler.instance:
GameHandler.instance = GameHandler.__GameHandler()
return GameHandler.instance