-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseCSGOItems.py
216 lines (161 loc) · 5.86 KB
/
ParseCSGOItems.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
import json
from pprint import *
import guiLib
from guiLib import *
class CSGOParser:
"""
A class that parses CS:GO JSON item/loot dumps.
"""
dictionary = None
rarities = None
jitems = None
items = []
verbose = False
def __init__(self, filepath: str, verbose=False):
"""
Initialize ``self`` with JSON data located at ``filepath``.
:param filepath: The location of the JSON datafile.
:param verbose: Should we print everything?
"""
self.verbose = verbose
dictionary = self.parse(filepath)
self.dictionary = dictionary
self.rarities = dictionary["rarities"]
self.qualities = dictionary["qualities"]
self.jitems = dictionary["items"]
printif("Items:", self.verbose)
if self.verbose: pprint(self.jitems)
# go through all JSON objects
for item_id in self.jitems:
jitem = self.jitems[item_id]
item = self.jitem_to_item(jitem)
# if item exists
if item:
self.items.append(item)
print(item, end="\n\n")
def item_weight(self, jitem: dict) -> int:
"""
Get the weight for a JSON item.
:param jitem: A JSON item.
:return: The weight associated with this item.
Large weights are more common, small weights are less common.
"""
if "item_quality" in jitem:
return self.quality_to_weight(jitem)
elif "item_rarity" in jitem:
return self.rarity_to_weight(jitem)
return 0
def item_percent(self, jitem: dict) -> float:
"""
Get the percent for a JSON item.
:param jitem: A JSON item.
:return: The percentage chance to get with this item.
"""
weight = total = None
percent = 0
# higher weight means LOWER chance
if "item_quality" in jitem:
weight = self.quality_to_weight(jitem["item_quality"])
total = self.total_qualities()
percent = 1 / weight
printif(f"Quality: higher weight means LOWER chance.", self.verbose)
printif(f"{weight} weight, {total} total.", self.verbose)
printif("1 / weight = percent", self.verbose)
printif(f"{1} / {weight} = {percent}", self.verbose)
# higher weight means larger chance
elif "item_rarity" in jitem:
weight = self.rarity_to_weight(jitem["item_rarity"])
total = self.total_rarities()
percent = weight / total
# print(f"Rarity: higher weight means LARGER chance")
# print(f"{weight} weight, {total} total.")
# print("weight / total = percent")
# print(f"{weight} / {total} = {percent}")
return percent
def total_rarities(self) -> int:
"""
Get all rarity weights summed up.
:return: The sum of all weight values for all rarities.
"""
total = 0
for rarity in self.rarities:
if "weight" in self.rarities[rarity]:
printif(f"Rarity {rarity} is {self.rarities[rarity]['weight']}", self.verbose)
total += int(self.rarities[rarity]["weight"])
# print(f"Total rarities: {total}")
return total
def total_qualities(self) -> int:
"""
Get all quality weights summed up.
:return: The sum of all weight values for all qualities.
"""
total = 0
for quality in self.qualities:
if "weight" in self.qualities[quality]:
printif(f"Quality {quality} is {self.qualities[quality]['weight']}", self.verbose)
total += int(self.qualities[quality]["weight"])
# print(f"Total qualities: {total}")
return total
def rarity_to_weight(self, rarity: str) -> int:
"""
Turn an ``item_rarity`` into a weight value.
:param rarity: The string representing a rarity.
:return: The weight associated with the string.
Example:\n
``rarity_to_weight("common") -> 10000000``\n
``rarity_to_weight("mythical") -> 80000``\n
"""
try:
return int(self.rarities[rarity]["weight"])
except Exception:
pass
return 0
def quality_to_weight(self, quality: str) -> int:
"""
Turn an ``item_quality`` into a weight value.
:param rarity: The string representing a quality.
:return: The weight associated with the string.
Example:\n
``quality_to_weight("normal") -> 0``\n
``quality_to_weight("genuine") -> 30``\n
"""
try:
return int(self.qualities[quality]["weight"])
except Exception:
pass
return 0
def jitem_to_item(self, jitem: dict) -> LootItem:
"""
Parses a single CSGO JSON item into a ``LootItem.``\n
Returns ``None`` upon failure.
:param jitem: A CSGO JSON item.
:return: A ``LootItem.``
"""
print(jitem)
if "name" in jitem:
name = jitem["name"]
# return None if JSON item doesn't have these vars
try:
name
except NameError:
return None
percent = self.item_percent(jitem)
item = LootItem(name, percent)
return item
def parse(self, filepath: str) -> {}:
"""
Parses a CSGO JSON Dump file into a list of ``LootItem`` objects.
:param filepath: The JSON file's location.
:return: a list of ``LootItem`` objects.
"""
file = open(filepath)
json_data = json.load(file)
if self.verbose: pprint(json_data)
return json_data
def test():
"""
Test out the functionality of our ``CSGOParser`` class.
"""
csgo = CSGOParser("_data/items_game_CSGO.json", verbose=True)
if __name__ == '__main__':
test()