-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.py
182 lines (143 loc) · 6.8 KB
/
inventory.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
####################################
# Driftwood 2D Folkdance Engine #
# inventory.py #
# Copyright 2017 Michael D. Reiley #
# & Paul Merrill #
####################################
# **********
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# **********
# Folkdance Inventory Manager
import ast
import json
class InventoryManager:
"""Inventory Manager
Manages an inventory or bag."""
def __init__(self, size=-1):
# Maximum weight of items that can be held in the inventory. -1 to disable.
self.size = size
# Weight of items currently being held.
self.holding = 0
# A dictionary of item names mapped to item specs.
self.__inventory = {}
def __contains__(self, item):
return self.has(item) is not 0
def __getitem__(self, item):
return self.item(item)
def has(self, item):
"""Return how many of the named item are held currently."""
if item in self.__inventory:
return self.__inventory[item]["quantity"]
return 0
def item(self, item):
"""Return the data for an item."""
if item in self.__inventory:
return self.__inventory[item]
return None
def get(self, item, quantity=1):
"""Increment the quantity of an item and increase the holding variable by the appropriate weight. Call the
item's on_get trigger.
Returns None if no such item, -1 if too heavy, and quantity now held otherwise."""
if item not in self.__inventory:
return None
if self.holding + self.__inventory[item]["weight"] * quantity > self.size >= 0:
return -1
self.__inventory[item]["quantity"] += quantity
self.holding += self.__inventory[item]["weight"] * quantity
if "on_get" in self.__inventory[item]["triggers"]:
Driftwood.script.call(*self.__inventory[item]["triggers"]["on_get"])
return self.__inventory[item]["quantity"]
def drop(self, item, quantity=1):
"""Decrement the quantity of an item and decrease the holding variable by the appropriate weight. Call the
item's on_drop trigger.
Returns None if no such item, -1 if dropping more than held, and quantity now held otherwise."""
if item not in self.__inventory:
return None
if self.__inventory[item]["quantity"] - quantity < 0:
return -1
self.__inventory[item]["quantity"] -= quantity
self.holding -= self.__inventory[item]["weight"] * quantity
if "on_drop" in self.__inventory[item]["triggers"]:
Driftwood.script.call(*self.__inventory[item]["triggers"]["on_drop"])
return self.__inventory[item]["quantity"]
def use(self, item, quantity=1):
"""Use an item.
If the item is not disposable, the value of quantity is meaningless. Otherwise, decrement the quantity
of an item and decrease the holding variable by the appropriate weight. Call the item's on_use trigger.
Returns None if no such item, False if disposable and using more than held, or True if succeeded."""
if item not in self.__inventory:
return None
if self.__inventory[item]["disposable"]:
if self.__inventory[item]["quantity"] - quantity < 0:
return False
self.__inventory[item]["quantity"] -= quantity
self.holding -= self.__inventory[item]["weight"] * quantity
if "on_use" in self.__inventory[item]["triggers"]:
Driftwood.script.call(*self.__inventory[item]["triggers"]["on_use"])
return True
def load(self, filename):
"""Load a dictionary of item specs from a file.
Returns True if succeeded, False if failed."""
data = Driftwood.resource.request_json(filename)
if not data:
Driftwood.log.msg("Error", "Folkdance", "Inventory", "load", "could not load items from", filename)
return False
for item in data.keys():
if item in self.__inventory:
Driftwood.log.msg("WARNING", "Folkdance", "Inventory", "load", "duplicate item", item)
self.__inventory[item] = data[item]
self.__refresh_weight()
return True
def loads(self, data):
"""Load a dictionary of item specs from a string.
Returns True if succeeded, False if failed."""
try:
if type(data) is str:
data = ast.literal_eval(json.loads(data))
except:
Driftwood.log.msg("Error", "Folkdance", "Inventory", "loads", "could not load items")
return False
if not data:
Driftwood.log.msg("Error", "Folkdance", "Inventory", "loads", "could not load items")
return False
for item in data.keys():
if item in self.__inventory:
Driftwood.log.msg("WARNING", "Folkdance", "Inventory", "loads", "duplicate item", item)
self.__inventory[item] = data[item]
self.__refresh_weight()
return True
def save(self, varname="inventory"):
"""Save the item dictionary to the currently open database."""
Driftwood.database.put(varname, self.__inventory)
return True
def restore(self, varname="inventory"):
"""Restore the item dictionary to from currently open database.
Returns True if succeeded, False if failed."""
if varname in Driftwood.database:
self.loads(Driftwood.database[varname])
return True
return False
def dump(self):
"""Return the item dictionary."""
return self.__inventory
def __refresh_weight(self):
"""Recalculate how much the inventory weighs."""
self.holding = 0
for item in self.__inventory.values():
self.holding += item["quantity"] * item["weight"]