-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.py
289 lines (231 loc) · 10 KB
/
data.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
287
288
289
import argparse
import logging
import os
import re
from yaml import dump, load
try:
from yaml import CDumper as Dumper
from yaml import CLoader as Loader
except ImportError:
from yaml import Dumper, Loader
class PairingException(Exception):
def __init__(self, message):
super().__init__(message)
class ItemData:
def __init__(
self, items_file_name: str | None = None, stats_file_name: str | None = None
):
self.stat_price = {}
self.stats_file_name = stats_file_name or "stats.yml"
self.items_file_name = items_file_name or "items.yml"
self.datas = self.read_yaml(self.items_file_name)
self.bases = self.read_bases()
def get_abs_filename(self, file_name: str) -> str:
script_dir = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(script_dir, f"./_data/{file_name}")
return file_path
def write_yaml(self, file_name: str):
logging.debug(f"write `datas` into {file_name}")
file_path = self.get_abs_filename(file_name)
with open(file_path, "w") as f:
dump(self.datas, f, Dumper=Dumper)
def read_yaml(self, file_name: str):
logging.debug(f"read `datas` from {file_name}")
file_path = self.get_abs_filename(file_name)
with open(file_path, "r") as f:
return load(f, Loader=Loader)
def find_data_with_name(self, datas, name: str):
for data in datas:
if data["name"] == name:
return data
def find_stat_with_type(self, data, _type: str):
_sum = 0
for stat in data["stats"]:
if stat["type"] == _type:
_sum += stat["value"]
return _sum
def read_bases(self):
stats = self.read_yaml(self.stats_file_name)
bases = {"first": {}, "second": {}}
for stat in stats:
if "base_type" in stats[stat]:
bases[stats[stat]["base_type"]][stat] = stats[stat]["base_item"]
return bases
def calculate_base_statistic_prices(self):
logging.debug("calculate base statistic prices")
datas = self.datas
first_base = self.bases["first"]
second_base = self.bases["second"]
logging.debug(" - first base")
for _type in first_base:
_name = first_base[_type]
data = self.find_data_with_name(datas, _name)
target_stat = self.find_stat_with_type(data, _type)
if not (data and target_stat):
raise ValueError(f"data: {data}, target_stat: {target_stat}")
cost = data["cost"]
stat_cost = float("{:.2f}".format(cost / target_stat))
data["first_base"] = {
"type": _type,
"value": stat_cost,
"formula": f"{data['cost']}/{target_stat}",
}
self.stat_price[_type] = stat_cost
logging.debug(" - second base")
for _type in second_base:
_name = second_base[_type]
data = self.find_data_with_name(datas, _name)
target_stat = self.find_stat_with_type(data, _type)
if not (data and target_stat):
raise ValueError(f"data: {data}, target_stat: {target_stat}")
cost = data["cost"]
formula_minus_part = ""
for stat in data["stats"]:
if stat["type"] != _type:
cost -= stat["value"] * self.stat_price[stat["type"]]
formula_minus_part += (
f"-{stat['value']}*{self.stat_price[stat['type']]}"
)
stat_cost = float("{:.2f}".format(cost / target_stat))
data["second_base"] = {
"type": _type,
"value": stat_cost,
"formula": f"({data['cost']}{formula_minus_part})/{target_stat}",
}
self.stat_price[_type] = stat_cost
self.datas = datas
logging.debug("write `datas` into yml")
self.write_yaml(self.items_file_name)
def calculate_gold_efficiency(self):
logging.debug("calculate gold efficiency based on base statistic prices")
datas = self.datas
if not self.stat_price:
for data in datas:
if "first_base" in data:
self.stat_price[data["first_base"]["type"]] = data["first_base"][
"value"
]
if "second_base" in data:
self.stat_price[data["second_base"]["type"]] = data["second_base"][
"value"
]
stat_price = self.stat_price
for data in datas:
if not ("first_base" in data or "second_base" in data):
worth = 0
formula_bonus_part = ""
for stat in data["stats"]:
if "value" in stat:
if "ratio" in stat:
ref_base = 0
if "ref_type" in stat:
for _stat in data["stats"]:
if (
"ratio" not in _stat
and _stat["type"] == stat["ref_type"]
):
ref_base += _stat["value"]
if ref_base and "ref" in stat:
stat["value"] = (stat["ref"] + ref_base) * stat["ratio"]
stat["formula"] = (
f'({stat["ref"]}+{ref_base})*{stat["ratio"]}'
)
elif ref_base:
stat["value"] = ref_base * stat["ratio"]
stat["formula"] = f'{ref_base}*{stat["ratio"]}'
elif "ref" in stat:
stat["value"] = stat["ref"] * stat["ratio"]
stat["formula"] = f'{stat["ref"]}*{stat["ratio"]}'
stat["value"] = float("{:.2f}".format(stat["value"]))
worth += stat["value"] * stat_price.get(stat["type"], 0)
formula_bonus_part += f"{'+' if formula_bonus_part else ''}{stat['value']}*{stat_price.get(stat['type'], 0)}"
amount = "{:.2f}".format(worth / data["cost"] * 100) + "%"
data["amount"] = amount
data["formula"] = (
f"({formula_bonus_part if formula_bonus_part else '0'})/{data['cost']}"
)
else:
data["amount"] = "100%"
data["formula"] = "Base Item"
self.datas = datas
self.write_yaml(self.items_file_name)
def clean_base_GE(self):
logging.debug("Clean gold efficiency and base statistic price from item data")
if self.datas:
datas = self.datas
else:
datas = self.read_yaml(self.items_file_name)
cleared_datas = []
for data in datas:
new_data = dict()
for key in ("category", "cost", "image", "name", "stats"):
new_data[key] = data[key]
cleared_datas.append(new_data)
self.datas = cleared_datas
self.write_yaml(self.items_file_name)
def parse_arguments():
parser = argparse.ArgumentParser(
description="Extract data from WR Meta via web scraping, or calculate the gold efficiency of item data."
)
parser.add_argument("--items", help="The file storage item data")
parser.add_argument("--stats", help="The file storage stat data")
parser.add_argument(
"-c",
"--calculate",
action="store_true",
help="Calculate the gold efficiency of item data",
)
parser.add_argument(
"--clean_only",
action="store_true",
help="Only clean gold efficiency and base statistic price from item data",
)
parser.add_argument("-d", "--debug", action="store_true", help="Enable debug mode")
return parser.parse_args()
def setup_logger(debug_mode):
logging.basicConfig(
level=logging.DEBUG if debug_mode else logging.INFO,
format="%(asctime)s %(filename)s %(levelname)s %(message)s",
datefmt="%a %d %b %Y %H:%M:%S",
)
def process_item_data(items_file, stats_file, clean_only, calculate):
item_data = ItemData(items_file_name=items_file, stats_file_name=stats_file)
if clean_only:
item_data.clean_base_GE()
elif calculate:
item_data.clean_base_GE()
item_data.calculate_base_statistic_prices()
item_data.calculate_gold_efficiency()
def find_and_process_files(clean_only, calculate):
datas = next(os.walk("./_data"), (None, None, []))[2]
pattern = re.compile(r"^(items|stats)_(.+)\.yml$")
parsed_files = {}
for data in datas:
match = pattern.match(data)
if match:
file_type, patch = match.groups()
if patch not in parsed_files:
parsed_files[patch] = {}
parsed_files[patch][file_type] = data
for key, value in sorted(parsed_files.items()):
try:
if "items" in value and "stats" in value:
process_item_data(value["items"], value["stats"], clean_only, calculate)
logging.info(f"finished: ({value['items']}, {value['stats']})")
logging.debug("\n")
else:
raise PairingException(f"No matching pair for patch number {key}")
except PairingException as e:
print(e)
def main():
args = parse_arguments()
setup_logger(args.debug)
if args.items and args.stats:
process_item_data(args.items, args.stats, args.clean_only, args.calculate)
logging.info(f"finished: ({args.items}, {args.stats})")
elif not args.items and not args.stats:
find_and_process_files(args.clean_only, args.calculate)
else:
logging.error("Both items and stats files must be provided together.")
if __name__ == "__main__":
main()