-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
76 lines (62 loc) · 2.03 KB
/
Copy pathdata.py
File metadata and controls
76 lines (62 loc) · 2.03 KB
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
import json
class ItemList():
def __init__(self):
self._items = {}
self.load()
def load(self):
with open("data/data.json") as infile:
json_data = infile.read()
game_data = json.loads(json_data)
# Items are defined using the Item Class
for item_data in game_data["items"]:
new_item = Item.fromJSON(item_data)
self._items[new_item.name] = new_item
def save(self):
data = {
"items": [item.toJSON() for item in self._items.values],
}
with open("data/data.json", "w") as outfile:
outfile.write(json.dumps(data,indent=4))
def addItem(self, newItem):
self._items[newItem.name] = newItem
self.save()
def getItem(self, itemName):
if itemName in self._items.keys():
return self._items[itemName]
else:
return None
class Item():
def __init__(self):
self.name = ""
self.base = False
self.components = {}
#Arbitrary default value
self.quantity = 1
def fromJSON(data):
new_item = Item()
new_item.name = data["name"]
if "base" in data:
new_item.base= data["base"]
if "components" in data:
for component in data["components"]:
new_item.components[component["name"]] = component["quantity"]
if "quantity" in data:
new_item.quantity = data["quantity"]
return new_item
def toJSON(self):
data = {
"name": self.name,
"base": self.base,
"components": list(),
"quantity": self.quantity,
}
if not self.base:
for key, value in self.components.items():
comp_data = {
"name": key,
"quantity": value,
}
data["components"].add(comp_data)
else:
data["base"] = True
return data