forked from Francesco149/oppai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgentest.py
executable file
·259 lines (195 loc) · 5.95 KB
/
gentest.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
#!/usr/bin/env python
import sys
import os
import time
import json
import traceback
import argparse
import hashlib
''' hack to force utf-8 '''
reload(sys)
sys.setdefaultencoding("utf-8")
try:
import httplib
except ImportError:
import http.client as httplib
try:
import urllib
except ImportError:
import urllib.parse as urllib
''' ----------------------------------------------------------- '''
parser = argparse.ArgumentParser(
description = (
"generates the oppai test suite. outputs c++ code to " +
"stdout and the json dump to a file."
)
)
parser.add_argument(
"-key",
default = None,
help = (
"osu! api key. required if -input-file is not present. " +
"can also be specified through the OSU_API_KEY " +
"environment variable"
)
)
parser.add_argument(
"-output-file",
default = "test_suite.json",
help = "dumps json to this file"
)
parser.add_argument(
"-input-file",
default = None,
help = (
"loads test suite from this json file instead of "
"fetching it from osu api. if set to '-', json will be "
"read from standard input"
)
)
args = parser.parse_args()
if args.key == None and "OSU_API_KEY" in os.environ:
args.key = os.environ["OSU_API_KEY"]
''' ----------------------------------------------------------- '''
osu_treset = time.time() + 60
osu_ncalls = 0
def osu_get(conn, endpoint, paramsdict=None):
'''
GETs /api/endpoint?paramsdict&k=args.key from conn
return json object, exits process on api errors
'''
global osu_treset, osu_ncalls, args
sys.stderr.write("%s %s\n" % (endpoint, str(paramsdict)))
paramsdict["k"] = args.key
path = "/api/%s?%s" % (endpoint, urllib.urlencode(paramsdict))
while True:
while True:
if time.time() >= osu_treset:
osu_ncalls = 0
osu_treset = time.time() + 60
sys.stderr.write("\napi ready\n")
if osu_ncalls < 60:
break
else:
sys.stderr.write("waiting for api cooldown...\r")
time.sleep(1)
try:
conn.request("GET", path)
osu_ncalls += 1
r = conn.getresponse()
raw = ""
while True:
try:
raw += r.read()
break
except httplib.IncompleteRead as e:
raw += e.partial
j = json.loads(raw)
if "error" in j:
sys.stderr.write("%s\n" % j["error"])
sys.exit(1)
return j
except (httplib.HTTPException, ValueError) as e:
sys.stderr.write("%s\n" % (traceback.format_exc()))
try:
'''
prevents exceptions on next request if the
response wasn't previously read due to errors
'''
conn.getresponse().read()
except httplib.HTTPException:
pass
time.sleep(5)
def gen_modstr(bitmask):
''' generates c++ code for a mod combination's bitmask '''
mods = []
allmods = {
(1<< 0, "nf"), (1<< 1, "ez"), (1<< 3, "hd"),
(1<< 4, "hr"), (1<< 6, "dt"), (1<< 8, "ht"),
(1<< 9, "nc"), (1<<10, "fl"), (1<<12, "so")
}
for bit, string in allmods:
if bitmask & bit != 0:
mods.append(string)
if len(mods) == 0:
return "nomod"
return " | ".join(mods)
''' ----------------------------------------------------------- '''
if args.key == None:
sys.stderr.write(
"please set OSU_API_KEY or pass it as a parameter\n"
)
sys.exit(1)
scores = []
if args.input_file == None:
''' fetch a fresh test suite from osu api '''
top_players = [
124493, 4787150, 2558286, 1777162, 2831793, 50265
]
osu = httplib.HTTPSConnection("osu.ppy.sh")
for u in top_players:
params = { "u": u, "limit": 100, "type": "id" }
scores += osu_get(osu, "get_user_best", params)
params = { "m": 0, "since": "2015-11-26" }
maps = osu_get(osu, "get_beatmaps", params)
for m in maps:
params = { "b": m["beatmap_id"] }
map_scores = osu_get(osu, "get_scores", params)
if len(map_scores) == 0:
sys.stderr.write("W: map has no scores???\n")
continue
'''
note: api also returns qualified and loved, so ignore
maps that don't have pp in rankings
'''
if not "pp" in map_scores[0]:
sys.stderr.write("W: ignoring loved/qualified map\n")
continue
for s in map_scores:
s["beatmap_id"] = m["beatmap_id"]
scores += map_scores
with open(args.output_file, "w+") as f:
f.write(json.dumps(scores))
else:
''' load existing test suite from json file '''
with open(args.input_file, "r") as f:
scores = json.loads(f.read())
print("/* this code was automatically generated by gentest.py */")
print("")
''' make code a little nicer by shortening mods '''
allmods = {
"nf", "ez", "hd", "hr", "dt", "ht", "nc", "fl", "so", "nomod"
}
for mod in allmods:
print("#define {0} mods::{0}".format(mod))
print('''
struct score
{
uint32_t id;
uint16_t max_combo;
uint16_t n300, n100, n50, nmiss;
uint32_t mods;
double pp;
};
struct score suite[] =
{''')
seen_hashes = []
for s in scores:
''' why is every value returned by osu api a string? '''
line = (
" { %s, %s, %s, %s, %s, %s, %s, %s }," %
(
s["beatmap_id"], s["maxcombo"], s["count300"],
s["count100"], s["count50"], s["countmiss"],
gen_modstr(int(s["enabled_mods"])), s["pp"]
)
)
''' don't include identical scores by different people '''
s = hashlib.sha1(line).digest()
if s in seen_hashes:
continue
print(line)
seen_hashes.append(s)
print("};\n")
for mod in allmods:
print("#undef %s" % (mod))