-
Notifications
You must be signed in to change notification settings - Fork 5
/
storage_loader.py
47 lines (37 loc) · 1.34 KB
/
storage_loader.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
from json import JSONDecodeError, dumps, load
from pygame.event import Event
from pacman.data_core import EvenType, IEventful, event_append
from . import LevelStorage, SkinStorage
from .main_storage import MainStorage
class StorageLoader(IEventful):
def __init__(self, path: str):
self.__path = path
self.__cheat_on = False
def to_file(self) -> None:
if self.__cheat_on:
return
string = dumps(MainStorage().serialize(), indent=2)
with open(self.__path, "w") as f:
f.write(string)
def from_file(self) -> None:
if self.__cheat_on:
return
try:
with open(self.__path, "r") as f:
MainStorage().deserialize(load(f))
except (FileNotFoundError, JSONDecodeError):
self.to_file()
def event_handler(self, event: Event):
if self.__cheat_on:
return
if event.type == EvenType.UNLOCK_SAVES:
self.__handle_unlock_saves_event(event)
elif event.type == EvenType.SET_SETTINGS:
self.to_file()
elif event.type == EvenType.GET_SETTINGS:
self.from_file()
def __handle_unlock_saves_event(self, event: Event):
self.to_file()
SkinStorage().event_handler(event)
LevelStorage().event_handler(event)
self.__cheat_on = True