-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.py
190 lines (152 loc) · 6.97 KB
/
load.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Load games.
Classes:
Loader
"""
import os
import json
from tools import empty_folder, copytree
import custom_json as cjson
from errors import CheckCommandError
class Loader:
"""
"""
def __init__(self, game_name, game, from_save=False):
# The game object.
self.game = game
# The directory of all game saves.
self.game_folder = os.path.join("Games", game_name, "")
# The Games/{game_name}/current_folder
self.current_folder = None
# The folder in which the last save of the game exists.
self.save_folder = None
# The arguments going into the game constructor.
self.game_args = {
"actors": None,
"display": None,
"verbs": None,
"chapters": None
}
if not os.path.exists(os.path.join(self.game_folder, "current", "")):
os.makedirs(os.path.join(self.game_folder, "current", ""))
if not os.path.exists(os.path.join(self.game_folder, "_temp", "")):
os.makedirs(os.path.join(self.game_folder, "_temp", ""))
self.__find_save_folder(game_name, from_save)
def load_game(self, display):
self.game_args["display"] = display
empty_folder(self.current_folder)
copytree(self.save_folder, self.current_folder)
game_dict = self.__load_game_file(self.current_folder)
self.__load_game_files(self.current_folder)
return game_dict, self.game_args
def load_prev_commands(self, title):
log_file_path = os.path.join('Games', title, "log_commands.log")
commands = []
times = []
with open(log_file_path, "r") as log_file:
for line in log_file:
try:
time = float(line.rstrip())
times.append(time)
except ValueError:
commands.append(line.rstrip())
return commands, times
def load_prev_seed(self, title):
log_file_path = os.path.join('Games', title, "log_seed.log")
with open(log_file_path, "r") as log_file:
for line in log_file:
seed = int(line.rstrip())
return seed
def load_undo(self):
temp_folder = os.path.join(self.game_folder + "_temp", "")
if not os.listdir(temp_folder):
raise CheckCommandError('UndoError')
empty_folder(self.current_folder)
copytree(temp_folder, self.current_folder)
empty_folder(temp_folder)
game_dct = self.__load_game_file(self.current_folder)
self.game.game_state = game_dct['game_state']
# Load game parameters, chapters and actors
actors_file = os.path.join(self.current_folder, "actors.json")
chapters_file = os.path.join(self.current_folder, "chapters.json")
self.game.actors = cjson.custom_load(actors_file)
self.game.chapters = cjson.custom_load(chapters_file)
# Load Chapter again
self.load_chapter(self.game.game_state['current chapter'], undo=True)
def load_chapter(self, chapter_key, start=False, undo=False):
# Save to current folder before the Chapter Change, so that
# the changes to the rooms are saved.
if not start and not undo:
self.game.saver.save_to_current()
chapter = self.game.chapters[chapter_key]
self.game.events = dict()
self.game.topics = dict()
self.game.convonodes = dict()
self.game.dialogevents = dict()
convonodes_files = [os.path.join(self.current_folder, "convoNodes", (file + ".json")) for file in
chapter.convonodes_files]
dialogevents_files = [os.path.join(self.current_folder, "dialogEvents", (file + ".json")) for file in
chapter.dialogevents_files]
events_files = [os.path.join(self.current_folder, "events", (file + ".json")) for file in chapter.events_files]
map_files = [os.path.join(self.current_folder, "maps", (file + ".json")) for file in chapter.map_files]
topics_files = [os.path.join(self.current_folder, "topics", (file + ".json")) for file in chapter.topics_files]
solar_system_file = os.path.join(self.current_folder, "solarsystem.json")
for i, file in enumerate(convonodes_files):
f = chapter.convonodes_files[i].split('.')
actor = f[0]
node = f[1]
obj = cjson.custom_load(file)
if actor in self.game.convonodes.keys():
self.game.convonodes[actor][node] = obj
else:
self.game.convonodes[actor] = {}
self.game.convonodes[actor][node] = obj
for file in dialogevents_files:
obj = cjson.custom_load(file)
self.game.dialogevents[obj.key] = obj
for file in events_files:
obj = cjson.custom_load(file)
self.game.events[obj.key] = obj
for file in topics_files:
obj = cjson.custom_load(file)
self.game.topics[obj.key] = obj
for file in map_files:
obj = cjson.custom_load(file)
self.game.rooms[obj.key] = obj
sol_system = cjson.custom_load(solar_system_file)
if type(sol_system) != dict:
self.game.game_state['current system'] = sol_system.key
sol_system_dct = dict()
sol_system_dct[sol_system.key] = sol_system
self.game.things = {**self.game.things, **sol_system_dct}
self.game.game_state["current chapter"] = chapter_key
if chapter.first_room and not undo:
self.game.game_state["current room"] = chapter.first_room
for actor in self.game.actors.values():
actor.container = chapter.first_room
if undo:
self.game.refresh_things()
def __find_save_folder(self, game_name, from_save):
self.current_folder = os.path.join(self.game_folder, "current", "")
with open(os.path.join(self.game_folder, "load_game.json"), "r") as load_file:
load_info = json.load(load_file)
load_file.close()
self.game_args = {**self.game_args, **load_info}
if not from_save:
self.game_args["last_save_key"] = None
if self.game_args["last_save_key"]:
self.save_folder = os.path.join(self.game_folder, self.game_args["last_save_key"], "")
else:
self.save_folder = os.path.join(self.game_folder, "_read_only", "")
def __load_game_file(self, path):
with open(os.path.join(path, "game.json"), "r") as file:
dct = json.load(file)
file.close()
return dct
def __load_game_files(self, path):
actors_file = os.path.join(path, "actors.json")
chapters_file = os.path.join(path, "chapters.json")
verb_file = os.path.join("Grammar", "verbs.json")
self.game_args["actors"] = cjson.custom_load(actors_file)
self.game_args["verbs"] = cjson.custom_load(verb_file)
self.game_args["chapters"] = cjson.custom_load(chapters_file)