forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_engine.py
56 lines (51 loc) · 1.82 KB
/
game_engine.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
"The Game Engine"
import time
from decimal import Decimal
from wallets import Wallets
from reports import Reports
class GameEngine():
"The Game Engine"
_instance = None
_start_time: int = 0
_clock: int = 0
_entries: list[tuple[str, Decimal]] = [] # Python 3.9
# _entries = [] # Python 3.8 or earlier
_game_open = True
def __new__(cls):
if cls._instance is None:
cls._instance = GameEngine
cls._start_time = int(time.time())
cls._clock = 60
return cls._instance
@classmethod
def get_game_state(cls) -> dict:
"Get a snapshot of the current game state"
now = int(time.time())
time_remaining = cls._start_time - now + cls._clock
if time_remaining < 0:
time_remaining = 0
cls._game_open = False
return {
"clock": time_remaining,
"game_open": cls._game_open,
"entries": cls._entries
}
@classmethod
def submit_entry(cls, user_id: str, entry: Decimal) -> bool:
"Submit a new entry for the user in this game"
now = int(time.time())
time_remaining = cls._start_time - now + cls._clock
if time_remaining > 0:
if Wallets.get_balance(user_id) > Decimal('1'):
if Wallets.adjust_balance(user_id, Decimal('-1')):
cls._entries.append((user_id, entry))
Reports.log_event(
f"New entry `{entry}` submitted by `{user_id}`")
return True
Reports.log_event(
f"Problem adjusting balance for `{user_id}`")
return False
Reports.log_event(f"User Balance for `{user_id}` to low")
return False
Reports.log_event("Game Closed")
return False