-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_obj.py
286 lines (221 loc) · 6.89 KB
/
json_obj.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
import json
from utils import *
def parse_dice(string):
def invalid():
raise ValueError("invalid dice format {string!r}")
def check_digit(s):
if not s.isdigit():
invalid()
string = string.strip()
if string.isdigit():
return (0, 0, int(string))
if "d" not in string:
invalid()
p = string.partition("d")
num = p[0]
check_digit(num)
num = int(num)
right = p[2]
if right.isdigit():
return (num, int(right), 0)
if "+" in right:
p = right.partition("+")
elif "-" in right:
p = right.partition("-")
else:
invalid()
sides = p[0]
check_digit(sides)
sides = int(sides)
mod = p[2]
check_digit(mod)
mod = int(mod)
return (num, sides, mod)
def _check_type(d, key, typ):
if typ is not None:
val = d[key]
if isinstance(typ, (list, tuple)):
types = list(typ)
else:
types = [typ]
if float in types:
types.append(int)
if len(types) > 1:
types = tuple(types)
else:
types = types[0]
try:
isinstance(val, types)
except:
raise Exception(str(types))
if not isinstance(val, types):
typ_obtained = type(val).__name__
if len(types) == 1:
typ_expected = typ.__name__
else:
typ_expected = ", ".join(t.__name__ for t in types)
typ_expected = f"({typ_expected})"
raise TypeError(f"JSON field {key!r} expected type {typ_expected}, but got type {typ_obtained}")
class JSONObject:
def __getstate__(self):
return self._attrs.copy()
def __setstate__(self, state):
self._attrs = state
def __init__(self):
self._attrs = {}
def __getattr__(self, key):
if key not in self._attrs:
raise AttributeError(f"{self.__class__.__name__!r} object has no attribute {key!r}")
return self._attrs[key]
def get_optional(self, d, key, default, typ=None, converter=None):
if key in d:
_check_type(d, key, typ)
val = d.get(key, default)
if converter and key in d:
val = converter(val)
return val
def get_required(self, d, key, typ=None, converter=None):
if key not in d:
raise KeyError(f"JSON object missing required key {key!r}")
_check_type(d, key, typ)
val = d[key]
if converter:
val = converter(val)
return val
def set_field(self, key, val):
self._attrs[key] = val
def load_from(self, key, typ):
obj = typ.load(self._attrs[key])
self._attrs[key] = obj
def load_optional(self, d, key, default, typ=None, converter=None):
self.set_field(key, self.get_optional(d, key, default, typ, converter))
def load_required(self, d, key, typ=None, converter=None):
self._attrs[key] = self.get_required(d, key, typ, converter)
class Blindsight(JSONObject):
@classmethod
def load(cls, d):
obj = cls()
obj.load_required(d, "range", int)
obj.load_optional(d, "blind_beyond", False)
return obj
class Poison(JSONObject):
@classmethod
def load(cls, d):
obj = cls()
obj.load_required(d, "max_damage", int)
obj.load_required(d, "potency", int)
obj.load_optional(d, "slowing", False, bool)
return obj
class MeleeAttackType(JSONObject):
@classmethod
def load(cls, d):
obj = cls()
obj.load_required(d, "name", str)
obj.load_optional(d, "attack_cost", 100, int)
obj.load_optional(d, "use_dex", False, bool)
obj.load_optional(d, "reach", 1, int)
obj.load_optional(d, "attack_msg", "<monster> hits <target>", str)
obj.load_optional(d, "acid_strength", 0, int)
dam = obj.get_required(d, "base_damage", str)
obj.set_field("base_damage", Dice(*parse_dice(dam)))
return obj
speed_names = ["tiny", "small", "medium", "large", "huge", "gargantuan"]
class MonsterType(JSONObject):
@classmethod
def load(cls, d):
obj = cls()
obj.load_required(d, "id", str)
obj.load_required(d, "name", str)
obj.load_required(d, "symbol", str)
obj.load_required(d, "STR", int)
obj.load_required(d, "DEX", int)
obj.load_required(d, "CON", int)
obj.load_required(d, "INT", int)
obj.load_required(d, "WIS", int)
obj.load_required(d, "CHA", int)
obj.load_required(d, "HP", int)
obj.load_required(d, "level", int)
obj.load_required(d, "diff", int)
obj.load_optional(d, "armor", 0, int)
obj.load_optional(d, "speed", 100, int)
obj.load_optional(d, "size", "medium", str)
obj.load_optional(d, "use_dex_melee", False, bool)
obj.load_optional(d, "flags", [])
obj.load_optional(d, "immune_status", [])
obj.load_optional(d, "skills", {}, dict)
obj.load_optional(d, "reach", 1, int)
obj.load_optional(d, "blindsight_range", 0, int)
obj.load_optional(d, "poison", False, (bool, dict))
obj.load_optional(d, "weapon", None)
obj.load_optional(d, "shield", False, bool)
obj.load_optional(d, "attacks", [], list)
obj.load_optional(d, "regen_per_turn", 0, int)
for i, typ in enumerate(obj.attacks):
obj.attacks[i] = MeleeAttackType.load(typ)
if obj.poison != False:
if type(obj.poison) != dict:
raise TypeError("poison field must be a dict or False")
obj.load_from("poison", Poison)
return obj
class EffectType(JSONObject):
@classmethod
def load(cls, d):
obj = cls()
obj.load_required(d, "name", str)
obj.load_required(d, "type", str)
obj.load_required(d, "apply_msg", str)
obj.load_required(d, "extend_msg", str)
obj.load_required(d, "remove_msg", str)
obj.load_optional(d, "mon_apply_msg", "", str)
obj.load_optional(d, "mon_extend_msg", "", str)
obj.load_optional(d, "mon_remove_msg", "", str)
return obj
class WeaponType(JSONObject):
@classmethod
def load(cls, d):
obj = cls()
obj.load_required(d, "id", str)
obj.load_required(d, "name", str)
obj.load_required(d, "symbol", str),
obj.load_required(d, "damage_type", str),
ranged = obj.get_optional(d, "ranged", False, (bool, list))
if ranged:
pass
else:
obj.load_optional(d, "finesse", False, bool)
obj.load_optional(d, "heavy", False, bool)
obj.load_optional(d, "thrown", False, (bool, list))
obj.load_optional(d, "two_handed", False, bool)
dam = obj.get_required(d, "base_damage", str)
obj.set_field("base_damage", Dice(*parse_dice(dam)))
return obj
class ArmorType(JSONObject):
@classmethod
def load(cls, d):
obj = cls()
obj.load_required(d, "id", str)
obj.load_required(d, "name", str)
obj.load_required(d, "symbol", str),
obj.load_required(d, "protection", int)
obj.load_optional(d, "encumbrance", 0, int)
obj.load_optional(d, "stealth_pen", 0, int)
return obj
def load_types(filename, field_name, typ_obj):
types = {}
f = open(filename, "r")
data = json.load(f)
for obj in data:
unique = obj[field_name]
if unique in types:
raise ValueError(f"duplicate {field_name} value {unique!r} in {filename}")
typ = typ_obj.load(obj)
types[unique] = typ
return types
def load_monster_types():
return load_types("monsters.json", "id", MonsterType)
def load_weapon_types():
return load_types("weapons.json", "id", WeaponType)
def load_effect_types():
return load_types("effects.json", "name", EffectType)
def load_armor_types():
return load_types("armor.json", "id", ArmorType)