-
Notifications
You must be signed in to change notification settings - Fork 3
/
instance.py
356 lines (292 loc) · 12 KB
/
instance.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# PythonWars copyright © 2020, 2021 by Paul Penner. All rights reserved.
# In order to use this codebase you must comply with all licenses.
#
# Original Diku Mud copyright © 1990, 1991 by Sebastian Hammer,
# Michael Seifert, Hans Henrik Stærfeldt, Tom Madsen, and Katja Nyboe.
#
# Merc Diku Mud improvements copyright © 1992, 1993 by Michael
# Chastain, Michael Quan, and Mitchell Tse.
#
# ROM 2.4 is copyright 1993-1998 Russ Taylor. ROM has been brought to
# you by the ROM consortium: Russ Taylor (rtaylor@hypercube.org),
# Gabrielle Taylor (gtaylor@hypercube.org), and Brian Moore (zump@rom.org).
#
# Ported to Python by Davion of MudBytes.net using Miniboa
# (https://code.google.com/p/miniboa/).
#
# In order to use any part of this Merc Diku Mud, you must comply with
# both the original Diku license in 'license.doc' as well the Merc
# license in 'license.txt'. In particular, you may not remove either of
# these copyright notices.
#
# Much time and thought has gone into this software, and you are
# benefiting. We hope that you share your changes too. What goes
# around, comes around.
from collections import OrderedDict, namedtuple
import json
import os
import re
import comm
import merc
import settings
import time
'''For the instance dicts, we are not going to make another pointer, or copy, of
the original entity. We are going to alias, or bind, the specific entity
_instances[dict key] [entity] back to the original global_instances[dict key]
[entity] pointer. This will give us a shared alias, not a copy. Lets us do a clean
and simple subset dict of whatever we want, and not over-reference memory locations
directly.
We do this as the following:
given mob entity
mob.instance_id = 1000 (autogenerated)
instance.global_instances[mob.instance_id] = mob
merc.mob_instances[mob.instance_id] = instance.global_instances[mob.instance_id]
Any changes to mob will reflect in both dicts simultaneously
Likewise you can effect a change TO mob from accessing either dict:
>>>mob.name = 'bob'
>>>instance.global_instances[mob.instance_id].name
'bob'
>>>merc.mob_instances[mob.instance_id].name
'bob'
'''
max_instance_id = 0
previous_max_instance_id = 0
area_templates = {}
item_templates = {}
room_templates = {}
npc_templates = {}
shop_templates = {}
global_instances = {} # This is the global instance list, the heart of the game data set
areas = {}
characters = {} # This is PC and NPC combined
helps = {}
items = {}
npcs = {}
players = {}
resets = {}
rooms = {}
shops = {}
socials = {}
# Old Style Lists
ban_list = []
descriptor_list = []
greeting_list = []
help_list = []
note_list = []
shop_list = []
'''
Contains lists of instances,
Key: string VNUM
Value: list Instance_ID of object associated with Key:VNUM
'''
instances_by_area = {}
instances_by_item = {}
instances_by_room = {}
instances_by_npc = {}
instances_by_shop = {}
instances_by_player = {}
# Things to omit from instances that are in templates.
not_to_instance = []
def isnamedtuple(obj):
"""
Named Tuples look, to python, like a normal tuple, so we have to poke around
their innards a bit to see if they're actually the fancy version.
-Updated by Syn,
we need to account for namedtuples that are created without using anamedtuple._make()
The check can now handle both scenarios, and will NOT hit positive on tuples or other types.
:param obj: potential namedtuple container
:type obj:
:return: True if obj is a namedtuple
:rtype: bool
"""
b = type(obj).__bases__
if len(b) != 1:
return False
f = getattr(obj, "_fields", None)
if not isinstance(f, tuple):
return False
elif not hasattr(obj, "_asdict"):
return False
elif not callable(obj._asdict):
return False
else:
return True
def to_json(data):
"""
This function takes an arbitrary data object and attempts to return a JSON
compatible dict-based structure, which from_json() can use to recreate the
original object.
:param data: data object to be serialized
:type data:
:return: JSON compatible data element
:rtype:
"""
# Order matters here. It's important to immediately return a base type.
if data is None or isinstance(data, (bool, int, float, str)):
return data
if isinstance(data, OrderedDict):
return {
"__type__/OrderedDict": [[to_json(k), to_json(v)] for k, v in data.items()]
}
# We MUST check for namedtuple() before ordinary tuples.
# Python's normal checks can't tell the difference.
if isnamedtuple(data):
return {
"__type__/namedtuple": {
"type": type(data).__name__,
"fields": list(data._fields),
"values": [to_json(getattr(data, f)) for f in data._fields]
}
}
if isinstance(data, set):
return {
"__type__/set": [to_json(val) for val in data]
}
if isinstance(data, tuple):
return {
"__type__/tuple": [to_json(val) for val in data]
}
if isinstance(data, list):
return [to_json(val) for val in data]
# Here, we return a plain dict if, and ONLY if, every key is a string.
# JSON dicts require string keys... so otherwise, we have to manipulate.
if isinstance(data, dict):
if all(isinstance(k, str) for k in data):
return {k: to_json(v) for k, v in data.items()}
return {
"__type__/dict": [[to_json(k), to_json(v)] for k, v in data.items()]
}
# Finally, the magic part.... if it wasn't a "normal" thing, check to see
# if it has a to_json method. If so, use it!
if hasattr(data, "to_json"):
return data.to_json(to_json)
# And if we still get nothing useful, PUNT!
raise TypeError("Type %r not data-serializable" % type(data))
def from_json(data):
"""
This function takes a JSON-encoded string and returns the original object
it represents.
:param data: JSON data chunks, passed in by json.loads()
:type data:
:return: An object
:rtype:
"""
# Order matters here. It's important to immediately return a base type.
if data is None or isinstance(data, (bool, int, float, str)):
return data
# Basic types we've labeled are easy to reconstruct.
if "__type__/tuple" in data:
return tuple(data["__type__/tuple"])
if "__type__/set" in data:
return set(data["__type__/set"])
if "__type__/dict" in data:
return dict(data["__type__/dict"])
# In the case of an OrderedDict(), we just pass the data to the class.
if "__type__/OrderedDict" in data:
return OrderedDict(data["__type__/OrderedDict"])
# For a namedtuple, we have to rebuild it as a class and then make an instance.
if "__type__/namedtuple" in data:
tmp = data["__type__/namedtuple"]
return namedtuple(tmp["type"], tmp["fields"])(*tmp["values"])
# If we're a dict, we can check to see if we're a custom class.
# If we are, we need to find out class definition and make sure
# there's a from_json() method to call. If so, let it handle things.
if hasattr(data, "keys"):
for k in data.keys():
found = re.findall(r"__class__/((?:\w+)\.)*(\w+)", k)
if found:
import importlib
module_name = found[0][0].rstrip(".")
class_name = found[0][1]
if module_name != "" and class_name != "":
if data[k].get("import_module", None):
module_ref = importlib.import_module(data[k]["import_module"])
else:
module_ref = importlib.import_module(module_name)
class_ref = getattr(module_ref, class_name)
if hasattr(class_ref, "from_json"):
return class_ref.from_json(data, from_json)
# If we have no idea, return whatever we are and hope someone else
# will handle it up (or down) stream.
return data
def save():
os.makedirs(settings.INSTANCE_DIR, 0o755, True)
filename = os.path.join(settings.INSTANCE_DIR, "list.json")
tmp_dict = {}
for i in global_instances:
if i in players:
pass
else:
tmp_dict[i] = [global_instances[i].__module__, global_instances[i].__class__.__name__]
with open(filename, "w") as fp:
json.dump({"max_instance_id": max_instance_id, "data": tmp_dict}, fp, default=to_json, indent=2, sort_keys=True)
data_save_time = time.time()
comm.notify(f"Saving area data... {len(areas):,} areas", merc.CONSOLE_BOOT)
for i in areas:
areas[i].save(force=True)
comm.notify(f"Areas saved in {time.time() - data_save_time:.3f} seconds", merc.CONSOLE_BOOT)
data_save_time = time.time()
comm.notify(f"Saving room data... {len(rooms):,} rooms", merc.CONSOLE_BOOT)
for i in rooms:
rooms[i].save(force=True)
comm.notify(f"Rooms saved in {time.time() - data_save_time:.3f} seconds", merc.CONSOLE_BOOT)
data_save_time = time.time()
comm.notify(f"Saving NPC data... {len(npcs):,} npcs", merc.CONSOLE_BOOT)
for i in npcs:
npcs[i].save(force=True)
comm.notify(f"NPCs saved in {time.time() - data_save_time:.3f} seconds", merc.CONSOLE_BOOT)
data_save_time = time.time()
comm.notify(f"Saving player data... {len(players):,} players", merc.CONSOLE_BOOT)
for i in players:
players[i].save(force=True)
comm.notify(f"Players saved in {time.time() - data_save_time:.3f} seconds", merc.CONSOLE_BOOT)
data_save_time = time.time()
comm.notify(f"Saving item data... {len(items):,} items", merc.CONSOLE_BOOT)
for i in items:
it = items[i]
if it.in_living is not None:
continue
if it.in_room is not None:
continue
if it.in_item is not None:
continue
it.save(force=True)
comm.notify(f"Items saved in {time.time() - data_save_time:.3f} seconds", merc.CONSOLE_BOOT)
def load():
filename = os.path.join(settings.INSTANCE_DIR, "list.json")
if os.path.isfile(filename):
with open(filename, "r") as fp:
tmp_dict = json.load(fp, object_hook=from_json)
global max_instance_id
global global_instances
max_instance_id = tmp_dict["max_instance_id"]
import importlib
for k, v in tmp_dict["data"]:
module_ref = importlib.import_module(v[0])
class_ref = getattr(module_ref, v[1])
if hasattr(class_ref, "load"):
obj = class_ref.load(instance_id=k)
if isinstance(obj, class_ref):
comm.notify("Restored instance {} ({!r})".format(k, repr(obj)), merc.CONSOLE_BOOT)
class Instancer:
def __init__(self):
"""Here is the backbone of our instancing. This function takes the global instance
number and increments it. After dealing with the dicts for our objects, we will save
the global instance number to a file, which will be important later when persistence
is, or if someone wants to, be implemented.
It is passed the object instance, for which we will make an identification.
First we match the type we need to make, then add that to each dict that it needs to be in.
As we are using just a single pointer between all of these dicts, we populate global_instances
first, with a pointer to the object. The following dicts 'alias' their value to the value
represented in global_instances[instance_id].
This lets us maintain a single pointer, with windows to that single pointer from our sub dicts,
allowing for a saner environment.
This means that the destructor should destruct in reverse order, just in case."""
super().__init__()
self.instance_id = None
def instancer(self):
global max_instance_id, global_instances
max_instance_id += 1
if global_instances.get(max_instance_id, None):
raise ValueError("houston we have a problem - instance number already in global instances")
self.instance_id = max_instance_id