-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
247 lines (178 loc) · 6.46 KB
/
Game.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
import json
from datetime import time
from LootCrate import LootCrate
from LootItem import LootItem
class Game:
"""
Attributes
- ``kredsratio`` How many kreds you get per dollar.
- ``rarities`` List of rarities.
- ``itempool`` List of all available items.
- ``itemgroups`` List of groups of items, to be used in crates for convenience.
- ``itemcrates`` List of all item crates.
- ``kreds`` User's kreds.
- ``inventory`` User's owned items.
- ``crates`` User's owned, unopened crates.
A class that facilitates/organizes the following:
- Currency
- Purchasing/using currency
- Keeping track of the player's currency
- LootCrate objects
- Registering LootCrate objects
- Deciding what items a LootCrate object will contain
- Having a master list of all items
- Indexing an item by an attribute (name, rarity, etc)
- Keeping a transaction log and changelog
"""
def __init__(self, kredsratio=10, kreds=None):
"""
:param kredsratio: How many kreds per dollar. 10 by default.
:param kreds How many kreds the user starts with. 10$ worth by default.
"""
if kreds is None:
kreds = 10 * kredsratio # 10$ worth
self.kredsratio = kredsratio
self.rarities = []
self.transactionLog = []
self.itempool = []
self.itemgroups = {}
self.itemcrates = {}
self.kreds = kreds
self.inventory = []
self.crates = {}
def kredsToMoney(self, kreds):
"""
Given ``kreds``, converts them into dollars.
"""
return kreds / self.kredsratio
def moneyToKreds(self, dollars):
"""
Given ``dollars``, converts them into Kreds.
"""
return self.kredsratio * dollars
def itemByName(self, name):
"""
Given a ``name``, return an item with that ``name``.
"""
for item in self.itempool:
if item.name == name:
return item
print("No item by '"+name+"' found!")
return None
def logPurchaseKreds(self, kreds, dollars):
"""
Log that we purchased ``kreds`` for some amount of ``dollars``.
"""
self.transactionLog.append({self.purchaseKreds.__name__: {
"time": time.time(),
"kreds": kreds,
"dollars": dollars,
"balance": self.kreds,
}})
def purchaseKreds(self, k):
"""Add ``k`` kreds to the Game's balance.
This costs money."""
dollars = k / self.kredsratio
self.logPurchaseKreds(k, dollars)
self.kreds += k
return
def import_rarities(self, path):
"""
Parse a JSON file representing rarities of items.
"""
d = {}
with open(path) as json_data:
d = json.load(json_data)
json_data.close()
self.rarities += d
return d
def import_items(self, path):
"""
Parse a JSON file representing a list of items.
"""
d = {}
items = []
with open(path) as json_data:
d = json.load(json_data)
json_data.close()
for name in d:
json_item = {name: d[name]}
item = LootItem.from_dict(json_item)
items.append(item)
self.itempool += items
# for item in self.itempool:
# print(item)
return items
def import_groups(self, path):
"""
Parse a JSON file representing a list of item groups.
"""
d = {}
groups = {}
with open(path) as json_data:
d = json.load(json_data)
json_data.close()
for jgname in d: # for group X
jgroup = d[jgname]
items = [] # make a new list for a group name
for jiname in jgroup: # for all items in group X
item = self.itemByName(jiname)
items.append(item) # add the item object
groups[jgname] = items # add our single group w/ items
self.itemgroups[jgname] = items
return groups
def import_crates(self, path):
"""
Parse a JSON file representing a list of item crates.
"""
d = {}
crates = {}
with open(path) as json_data:
d = json.load(json_data)
json_data.close()
# print("All jcrates")
# print(d)
for crateName in d: # go through all crates
jcrate = d[crateName]
capacity = jcrate['capacity']
cost = jcrate['cost']
# print(f"'{crateName}'-named crate:")
# print(jcrate)
items = [] # items for one crate
for groupName in jcrate['groups']: # go through all groups
items += self.itemgroups[groupName] # add that group's items
for itemName in jcrate['items']: # go through just items
items.append(self.itemByName(itemName)) # add that item
crate = LootCrate(crateName, cost, items, capacity) # make a new Crate object
# print('crate object:')
# print(str(crate))
crates[crateName] = crate
self.itemcrates[crateName] = crate
return crates
def ncrateidx(self):
"""Normalize crate index."""
if hasattr(self, '_crate_idx'):
if self._crate_idx < 0:
self._crate_idx += len(self.itemcrates.values()) # normalize up
elif self._crate_idx >= len(self.itemcrates.values()) - 1:
self._crate_idx -= len(self.itemcrates.values()) # normalize down
else:
self._crate_idx = 0
def cycleLeft(self):
"""Cycles crates left and returns the crate."""
self.ncrateidx()
self._crate_idx -= 1
print("Current idx is " + str(self._crate_idx))
allcrates = list(self.itemcrates.values())
acrate = allcrates[self._crate_idx]
print("total crates:")
print(len(allcrates))
return acrate
def cycleRight(self):
"""Cycles crates right and returns the crate."""
self.ncrateidx()
self._crate_idx += 1
print("Current idx is " + str(self._crate_idx))
allcrates = list(self.itemcrates.values())
acrate = allcrates[self._crate_idx]
return (acrate)