-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.py
123 lines (92 loc) · 3.69 KB
/
save.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Save games.
Classes:
Saver
"""
import os
import json
import custom_json as cjson
from tools import empty_folder, copytree
import time
import shutil
class Saver:
"""
"""
def __init__(self, game):
"""
"""
self.game = game
self.title = game.title
self.game_folder = game.loader.game_folder
self.current_folder = game.loader.current_folder
def save_to_current(self):
chapter_key = self.game.game_state["current chapter"]
chapter = self.game.chapters[chapter_key]
for file in chapter.convonodes_files:
f = os.path.join(self.current_folder, "convoNodes", (file + ".json"))
file = file.split('.')
cjson.custom_dump(self.game.convonodes[file[0]][file[1]], f)
for file in chapter.dialogevents_files:
f = os.path.join(self.current_folder, "dialogEvents", (file + ".json"))
cjson.custom_dump(self.game.dialogevents[file], f)
for file in chapter.events_files:
f = os.path.join(self.current_folder, "events", (file + ".json"))
cjson.custom_dump(self.game.events[file], f)
for file in chapter.topics_files:
f = os.path.join(self.current_folder, "topics", (file + ".json"))
cjson.custom_dump(self.game.topics[file], f)
for file in chapter.map_files:
f = os.path.join(self.current_folder, "maps", (file + ".json"))
cjson.custom_dump(self.game.rooms[file], f)
# Save Actors
cjson.custom_dump(self.game.actors, os.path.join(self.current_folder, "actors.json"))
# Save Chapters
cjson.custom_dump(self.game.chapters, os.path.join(self.current_folder, "chapters.json"))
#Save Solar System
try:
solar_system_key = self.game.game_state['current system']
except KeyError:
solar_system_key = None
if solar_system_key:
try:
solar_system = self.game.things[solar_system_key]
except KeyError:
solar_system = {}
else:
solar_system = {}
cjson.custom_dump(solar_system, os.path.join(self.current_folder, "solarsystem.json"))
# Save Game
with open(os.path.join(self.current_folder, "game.json"), "w") as file:
json.dump(self.game.to_json(), file)
file.close()
def save_to_temp(self):
temp_folder = os.path.join(self.game_folder + "_temp", "")
empty_folder(temp_folder)
copytree(self.current_folder, temp_folder)
return
def save_game(self, display=True, quit_=False):
current_time = int(time.time())
save_folder = os.path.join(self.game_folder, f"save_{current_time}", "")
for root, subdirs, files in os.walk(self.game_folder):
for d in subdirs:
if "save_" in d:
shutil.rmtree(os.path.join(root, d))
with open(os.path.join(self.game_folder, "load_game.json"), "r") as load_file:
load_info = json.load(load_file)
load_file.close()
load_info["last_save_key"] = f"save_{current_time}"
with open(os.path.join(self.game_folder, "load_game.json"), "w") as load_file:
json.dump(load_info, load_file)
load_file.close()
self.save_to_current()
os.mkdir(save_folder)
copytree(self.current_folder, save_folder)
if display:
self.game.display.queue("", "Save")
if quit_:
self.empty_current_temp()
return
def empty_current_temp(self):
empty_folder(self.current_folder)
temp_folder = os.path.join(self.game_folder + "_temp", "")
empty_folder(temp_folder)