forked from eVen-gits/warframe_relic_eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelic_eval.py
186 lines (157 loc) · 5.82 KB
/
relic_eval.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
import json
import requests
import sys
import types
import csv
#https://go.microsoft.com/fwlink/?LinkId=691126
#easy_install python-Levenshtein
from fuzzywuzzy import fuzz
from ratelimit import limits, sleep_and_retry
refinement_levels = {
'Intact': 0,
'Exceptional': 25,
'Flawless': 50,
'Radiant': 10
}
relic_tiers = [
'Lith',
'Meso',
'Neo',
'Axi'
]
def median(lst):
n = len(lst)
if n < 1:
return None
if n % 2 == 1:
return sorted(lst)[n//2]
else:
return sum(sorted(lst)[n//2-1:n//2+1])/2.0
@sleep_and_retry
@limits(calls=3, period=2)
def call_api(url):
response = client.get(url)
if response.status_code != 200:
raise Exception('API response: {}'.format(response.status_code))
return response
def json_to_obj_list(json_list):
obj_list = []
for i in range(len(json_list)):
obj_list.append(Json2Obj(json_list[i]))
return obj_list
def item_value(self):
if not hasattr(self, 'url_name'):
return 0
if not self._value:
url = "https://api.warframe.market/v1/items/{0}/orders".format(self.url_name)
orders = [order['platinum'] for order in json.loads(call_api(url).text)['payload']['orders'] if order['order_type'] == 'sell' and order['user']['status'] == 'ingame']
self._value = median(orders)
return float(self._value)
def relic_value(self):
val = 0
for reward in sorted(self.rewards, key=lambda el: el.chance):
val = val + reward.chance/100 * reward.item_value()
return val
def init_data(relic_list, item_list):
keys = [item.item_name for item in item_list]
item_dict = dict(zip(keys, item_list))
relic_dict = dict()
for tier in relic_tiers:
relic_dict[tier] = dict()
n = len(relic_list)
count = 0
for relic in relic_list:
for i, reward in enumerate(relic.rewards):
if reward.itemName == 'Forma Blueprint':
match = reward
else:
item_name = reward.itemName.replace(' Blueprint', '')
if reward.itemName in item_dict.keys():
match = item_dict.get(reward.itemName)
relic.rewards[i].url_name = match.url_name
elif item_name in item_dict.keys():
match = item_dict.get(item_name)
relic.rewards[i].url_name = match.url_name
else:
for j, item in enumerate(item_list):
if fuzz.token_set_ratio(item.item_name, reward.itemName) == 100:
relic.rewards[i].url_name = item.url_name
match = item
break
if not match:
raise KeyError('Failed to find match for', reward.itemName)
#Init value cache field
match._value = None
match.item_value = types.MethodType(item_value, match)
relic.rewards[i].item_value = match.item_value
relic.relic_value = types.MethodType(relic_value, relic)
if relic.relicName not in relic_dict[relic.tier]:
relic_dict[relic.tier][relic.relicName] = dict()
relic_dict[relic.tier][relic.relicName][relic.state] = relic
count = count+1
sys.stdout.write("\rInitializing {:.2f}%".format(100*count/n))
sys.stdout.flush()
print(' ... Done!')
return relic_dict, item_dict
class Json2Obj:
def __init__(self, json):
"""Constructor accepts json string as an argument.
"""
self.__dict__ = json
for i in self.__dict__.keys():
child = self.__dict__[i]
if isinstance(child, dict):
if len(child) > 0:
self.__dict__[i] = Json2Obj(child)
elif isinstance(child, list):
for i in range(len(child)):
child[i] = Json2Obj(child[i])
if __name__ == '__main__':
client = requests.session()
relic_list = json_to_obj_list(
json.loads(
call_api(
'https://drops.warframestat.us/data/relics.json'
).text
)['relics']
)
item_list = json_to_obj_list(
json.loads(
call_api(
'https://api.warframe.market/v1/items'
).text
)['payload']['items']['en']
)
relic_dict, item_dict = init_data(relic_list, item_list)
csv_values = open('export.csv', 'w', newline='', encoding='utf-8')
values_writer = csv.writer(csv_values, delimiter=',')
csv_refinement = open('refinement.csv', 'w', newline='', encoding='utf-8')
refinement_writer = csv.writer(csv_refinement, delimiter=',')
rows = 0
values_writer.writerow(['Era', 'Relic',] + list(refinement_levels.keys()))
refinement_writer.writerow(['Era', 'Relic',] + list(refinement_levels.keys())[1:])
for k_era, v_era in relic_dict.items():
for k_name, v_name in v_era.items():
#if rows > 10:
# break
value_row = [k_era, k_name]
refinement_row = [k_era, k_name]
print(k_era, k_name)
first = prev = None
for r in v_name.values():
relic_value = r.relic_value()
value_row += [relic_value]
if not first:
first = relic_value
if prev:
gain = relic_value - prev
gpp = gain/refinement_levels[r.state]
refinement_row += [gpp]
prev = relic_value
for drop in sorted(v_name['Intact'].rewards, key=lambda el: el.item_value()):
value_row += [drop.itemName, drop.item_value()]
values_writer.writerow(value_row)
refinement_writer.writerow(refinement_row)
#rows = rows+1
csv_values.close()
csv_refinement.close()