Repository navigation
Expand file tree
/
Copy pathBotDetails.py
More file actions
412 lines (368 loc) · 16.4 KB
/
Copy pathBotDetails.py
File metadata and controls
412 lines (368 loc) · 16.4 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python
import datetime
import time
from operator import attrgetter
from flask import Response, request
import store
import structures
from structures import global_dict, load_blob
def _retired_score(game, name, pairings):
"""A LiteBot for a bot whose RumbleScore row is gone, or None if it never had
one here. Every field is the same aggregate over live pairings that
BatchRankings would have written; the batch-only figures (ANPP, Vote, APS_CI)
have no pairing to come from and stay at their unset -1."""
if not pairings:
return None
apss = [float(p.APS) for p in pairings]
return structures.LiteBot(loadDict={
"Name": name,
"Rumble": game,
"Active": False,
"Pairings": len(pairings),
"Battles": sum(int(p.Battles) for p in pairings),
"APS": sum(apss) / len(apss),
"Survival": sum(float(p.Survival) for p in pairings) / len(pairings),
"PL": sum((a > 50.0) - (a < 50.0) for a in apss),
"LastUpload": max(str(p.LastUpload) for p in pairings),
"ANPP": -1.0,
"VoteScore": -1.0,
"APS_CI": -1.0,
"Uploaders": [],
})
def bot_details():
starttime = time.time()
query = request.query_string.decode('utf-8').replace("%20", " ")
parts = query.split("&")
requests = {}
if parts[0] != "":
for pair in parts:
ab = pair.split('=', 1)
requests[ab[0]] = ab[1] if len(ab) > 1 else ""
game = requests.get("game")
if game is None:
return "ERROR: RUMBLE NOT SPECIFIED IN FORMAT game=____"
name = requests.get("name", None)
if name is None:
return "ERROR: BOT NOT SPECIFIED IN FORMAT name=____"
lim = int(requests.get("limit", "10000000"))
order = requests.get("order", None)
timing = bool(requests.get("timing", False))
api = bool(requests.get("api", False))
dark = requests.get("theme", "") == "dark"
extraArgs = ""
if timing:
extraArgs += "&timing=1"
if dark:
extraArgs += "&theme=dark"
reverseSort = True
if order is None or order.replace(" ", "") == "":
order = "Name"
reverseSort = False
elif order[0] == "-":
order = order[1:]
reverseSort = False
if order == "Latest Battle":
order = "LastUpload"
parsetime = time.time() - starttime
cached = False
bot = store.load_score(game, name)
# A retired bot keeps its Pairing rows but loses its RumbleScore row, so its
# own columns have to come back off the pairings that outlived it. Loaded
# here rather than below so the retired path doesn't read them twice.
retiredPairings = None
if bot is None:
retiredPairings = store.load_pairings_alive(game, name)
bot = _retired_score(game, name, retiredPairings)
rumble = None
if not api:
rumble = global_dict.get(game, None)
if rumble is None:
rumble = structures.Rumble.get_by_key_name(game)
if rumble is None:
return "RUMBLE NOT FOUND"
global_dict[game] = rumble
if bot is None:
return "ERROR. name/game combination does not exist: " + name + "/" + game
flagmap = global_dict.get(structures.default_flag_map)
if flagmap is None:
flagmapholder = structures.FlagMap.get_by_key_name(structures.default_flag_map)
if flagmapholder is not None:
flagmap = flagmapholder.InternalMap
global_dict[structures.default_flag_map] = flagmap
flagmap = load_blob(flagmap, {})
if not isinstance(flagmap, dict):
flagmap = {}
bots = None
if lim > 0 or not api:
# join to RumbleScore: only opponents still in the rumble (dead pairings are
# kept for history but not shown), each carrying the opponent's OppAPS/OppSurvival.
bots = (retiredPairings if retiredPairings is not None
else store.load_pairings_alive(game, name))
removes = []
for b in bots:
try:
_ = b.LastUpload
except AttributeError:
b.LastUpload = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
package = b.Name.split(".")[0]
if package in flagmap:
b.Flag = flagmap[package]
else:
b.Flag = "NONE"
try:
b.APS = float(b.APS)
b.KNNPBI = float(b.KNNPBI)
b.NPP = float(b.NPP)
b.Battles = int(b.Battles)
except (TypeError, ValueError, AttributeError):
removes.append(b)
for b in removes:
bots.pop(bots.index(b))
package = bot.Name.split(".")[0]
if package in flagmap:
bot.Flag = flagmap[package]
else:
bot.Flag = "NONE"
retrievetime = time.time() - parsetime - starttime
if lim > 0:
# load_pairings_alive already returns only live opponents
if order == "APS CI":
ci = structures.pairing_ci
bots = sorted(bots, key=lambda b: ci(b) if ci(b) is not None else -1.0, reverse=reverseSort)
else:
if bots and order not in bots[0].__dict__:
order = "Name"
bots = sorted(bots, key=attrgetter(order), reverse=reverseSort)
if api:
outs = ["{"]
outs.append("\n\"name\":\"")
outs.append(name)
outs.append("\",\n\"flag\":\"")
outs.append(bot.Flag)
outs.append("\",\n\"APS\":")
outs.append(structures.fmt(bot.APS))
outs.append(",\n\"APS_CI\":")
outs.append(structures.fmt(getattr(bot, "APS_CI", -1.0)))
outs.append(",\n\"PWIN\":")
outs.append(structures.fmt(50.0 * float(bot.PL) / bot.Pairings + 50.0))
outs.append(",\n\"ANPP\":")
outs.append(structures.fmt(bot.ANPP))
outs.append(",\n\"vote\":")
outs.append(structures.fmt(bot.VoteScore))
outs.append(",\n\"survival\":")
outs.append(structures.fmt(bot.Survival))
outs.append(",\n\"pairings\":")
outs.append(str(bot.Pairings))
outs.append(",\n\"battles\":")
outs.append(str(bot.Battles))
outs.append(",\n\"latest\":\"")
outs.append(str(bot.LastUpload))
outs.append("\"")
if lim > 0:
outs.append(",\n\"pairingsList\":[\n")
headings = [
"\"name\"",
"\"flag\"",
"\"rank\"",
"\"APS\"",
"\"APS_CI\"",
"\"NPP\"",
"\"survival\"",
"\"KNNPBI\"",
"\"battles\"",
"\"latest\""]
escapes = ["\"", "\"", "", "", "", "", "", "", "", "\""]
count = 0
for b in bots:
count += 1
if count > lim:
break
bci = structures.pairing_ci(b)
cells = [b.Name,
b.Flag,
count,
b.APS,
-1.0 if bci is None else bci,
b.NPP,
b.Survival,
b.KNNPBI,
b.Battles, b.LastUpload]
outs.append(" {")
for i in range(len(cells)):
outs.append(headings[i])
outs.append(":")
outs.append(escapes[i])
outs.append(structures.fmt(cells[i]))
outs.append(escapes[i])
outs.append(",")
outs[-1] = "},\n"
outs[-1] = "}\n]"
outs.append("\n}")
return Response("".join(outs), mimetype="application/json")
sorttime = time.time() - retrievetime - parsetime - starttime
if order == "LastUpload":
order = "Latest Battle"
out = []
gameHref = "<a href=\"Rankings?game=" + game + extraArgs + "\">" + game + "</a>"
gameTitle = "Bot details of <b>" + name + "</b> in " + gameHref + " vs. " + str(len(bots)) + " bots."
flagtag = "<img id='flag' src=\"/flags/" + bot.Flag + ".gif\"> " + structures.country_lookup[bot.Flag]
endName = name.split(" ")[0].split(".")[-1]
out.append(structures.header(endName + " in " + game, gameTitle, dark))
out.append("<table>\n")
wikiurl = "<a href=\"https://robowiki.net/wiki/{identifier}\">{name}</a>" \
.format(identifier=name.split(" ")[0].split(".")[-1], name=name)
out.append("<tr>\n<th>Name</th>\n<td>\n" + wikiurl + "</td>\n<th>APS</th><th>Survival</th><th>KNNPBI</th></tr>")
out.append("<tr>\n<th>Flag</th>\n<td>\n" + flagtag + "</td>")
out.append("<td rowspan=\"11\"><canvas id=\"distAPS\" width=\"231\" height=\"231\" style=\"border: black 1px solid;\"></canvas></td>")
out.append("<td rowspan=\"11\"><canvas id=\"distSurvival\" width=\"231\" height=\"231\" style=\"border: black 1px solid;\"></canvas></td>")
out.append("<td rowspan=\"11\"><canvas id=\"distKNNPBI\" width=\"231\" height=\"231\" style=\"border: black 1px solid;\"></canvas></td>")
out.append("</td></tr>")
out.append("<tr>\n<th>APS</th>\n<td>\n" + structures.fmt(bot.APS) + "</td></tr>")
botCI = getattr(bot, "APS_CI", -1.0)
botCIStr = "n/a" if (botCI is None or botCI < 0) else "±" + structures.fmt(botCI)
out.append("<tr>\n<th>APS CI</th>\n<td>\n" + botCIStr + "</td></tr>")
out.append("<tr>\n<th>PWIN</th>\n<td>\n" + structures.fmt(50.0 * float(bot.PL) / bot.Pairings + 50.0) + "</td></tr>")
out.append("<tr>\n<th>ANPP</th>\n<td>\n" + structures.fmt(bot.ANPP) + "</td></tr>")
out.append("<tr>\n<th>Vote</th>\n<td>\n" + structures.fmt(bot.VoteScore) + "</td></tr>")
out.append("<tr>\n<th>Survival</th>\n<td>\n" + structures.fmt(bot.Survival) + "</td></tr>")
out.append("<tr>\n<th>Pairings</th>\n<td>\n" + str(bot.Pairings) + "</td></tr>")
out.append("<tr>\n<th>Battles</th>\n<td>\n" + str(bot.Battles) + "</td></tr>")
out.append("<tr>\n<th>Latest Battle</th>\n<td>\n" + str(bot.LastUpload) + " UTC</td></tr>")
out.append("<tr>\n<td colspan=\"2\">")
out.append("<form name=\"input\" action=\"BotCompare\" method=\"get\">")
out.append("<input type=\"hidden\" name=\"game\" value=\"" + game + "\" />")
out.append("<input type=\"hidden\" name=\"bota\" value=\"" + name + "\" />")
out.append("<input type=\"text\" name=\"botb\" value=\"" + name + "\" />")
if dark:
out.append("<input type=\"hidden\" name=\"theme\" value=\"dark\" />")
out.append("<input type=\"submit\" value=\"Compare\" /></form>")
out.append("</td></tr></table>")
if lim > 0:
out.append("\n<table>\n<tr>\n")
headings = [" ",
"Flag",
"Name",
"",
"APS",
"APS CI",
"NPP",
"Survival",
"KNNPBI",
"Battles",
"Latest Battle",
"Opponent APS",
"Opponent Survival"]
for heading in headings:
sortedBy = (order == heading)
headinglink = heading
if sortedBy and reverseSort:
heading = "-" + heading
headinglink = heading
elif not sortedBy:
headinglink = "-" + headinglink
orderHref = "<a href=\"BotDetails?game=" + game + "&name=" + name.replace(" ", "%20") + "&order=" + headinglink.replace(" ", "%20") + extraArgs + "\">" + heading + "</a>"
if sortedBy:
out.append("\n<th class=\"sortedby\">" + orderHref + "</th>")
else:
out.append("\n<th>" + orderHref + "</th>")
out.append("\n</tr>")
rank = 0
highlightKey = [False, False, False, False, True, False, True, True, True, False, False, True, True]
mins = [0, 0, 0, 0, 40, 0, 40, 40, -0.1, 0, 0, 40, 40]
maxs = [0, 0, 0, 0, 60, 0, 70, 60, 0.1, 0, 0, 60, 60]
for b in bots:
rank += 1
if rank > lim:
break
botName = b.Name
botNameHref = "<a href=\"BotDetails?game=" + game + "&name=" + botName.replace(" ", "%20") + extraArgs + "\">" + botName + " </a>"
compareHref = "<a rel=\"nofollow\" href=\"BotCompare?game=" + game + "&bota=" + name.replace(" ", "%20") + "&botb=" + botName.replace(" ", "%20") + extraArgs + "\">compare</a>"
ft = []
ft.append("<img id='flag' src=\"/flags/")
ft.append(b.Flag)
ft.append(".gif\">")
flagtag2 = "".join(ft)
bci = structures.pairing_ci(b)
bciStr = "n/a" if bci is None else "±" + structures.fmt(bci)
cells = [str(rank),
flagtag2,
botNameHref,
compareHref,
b.APS,
bciStr,
b.NPP,
b.Survival,
b.KNNPBI,
b.Battles,
b.LastUpload,
b.OppAPS,
b.OppSurvival]
out.append("\n<tr>")
for i, cell in enumerate(cells):
if highlightKey[i]:
if cell < mins[i]:
out.append("\n<td class=\"red\">" + structures.fmt(cell) + "</td>")
elif cell > maxs[i]:
out.append("\n<td class=\"green\">" + structures.fmt(cell) + "</td>")
else:
out.append("\n<td>" + structures.fmt(cell) + "</td>")
else:
out.append("\n<td>" + structures.fmt(cell) + "</td>")
out.append("\n</tr>")
out.append("</table>")
htmltime = time.time() - sorttime - retrievetime - parsetime - starttime
elapsed = time.time() - starttime
if timing:
out.append("<br />\n Page served in " + str(int(round(elapsed * 1000))) + "ms. Bot cached: " + str(cached))
out.append("\n<br /> parsing: " + str(int(round(parsetime * 1000))))
out.append("\n<br /> retrieve: " + str(int(round(retrievetime * 1000))))
out.append("\n<br /> sort: " + str(int(round(sorttime * 1000))))
out.append("\n<br /> html generation: " + str(int(round(htmltime * 1000))))
out.append("""<div id="tip" style="position:fixed;padding:1px 4px;font-size:12px;pointer-events:none;display:none"></div>
<script>
(function() {
var S = 230;
var dark = document.body.classList.contains('dark');
var tip = document.getElementById('tip');
function draw(id, col, xcol, color, offset) {
var canvas = document.getElementById(id);
var ctx = canvas.getContext('2d');
ctx.fillStyle = dark ? '#1e1e1e' : '#fff';
ctx.fillRect(0, 0, S + 1, S + 1);
ctx.fillStyle = dark ? '#555' : '#7f7f7f';
ctx.fillRect(0, 115, S + 1, 1);
var rows = document.querySelectorAll('table')[1].querySelectorAll('tr');
var names = {};
ctx.fillStyle = color;
for (var i = 1; i < rows.length; i++) {
var c = rows[i].querySelectorAll('td');
if (c.length < 13) continue;
var v = parseFloat(c[col].textContent);
var ox = parseFloat(c[xcol].textContent);
if (isNaN(v) || isNaN(ox)) continue;
var px = Math.round(ox * 0.01 * S);
var py = Math.max(0, Math.min(S, S - Math.round((v + offset) * 0.01 * S)));
ctx.beginPath(); ctx.arc(px, py, 1.5, 0, 6.283); ctx.fill();
names[py * (S + 1) + px] = c[2].textContent;
}
canvas.onmousemove = function(e) {
var r = canvas.getBoundingClientRect();
var mx = Math.floor(e.clientX - r.left);
var my = Math.floor(e.clientY - r.top);
var best = '', bestD = 26;
for (var dy = -5; dy <= 5; dy++) for (var dx = -5; dx <= 5; dx++) {
var d = dx*dx + dy*dy;
if (d < bestD) { var n = names[(my+dy)*(S+1)+(mx+dx)]; if (n) { best = n; bestD = d; } }
}
if (best) { tip.textContent = best; tip.style.left = e.clientX + 12 + 'px'; tip.style.top = e.clientY + 2 + 'px'; tip.style.display = ''; }
else tip.style.display = 'none';
};
canvas.onmouseleave = function() { tip.style.display = 'none'; };
}
draw('distAPS', 4, 11, dark ? 'rgba(255,100,100,0.5)' : 'rgba(204,37,41,0.4)', 0);
draw('distSurvival', 7, 12, dark ? 'rgba(100,220,130,0.5)' : 'rgba(62,150,81,0.4)', 0);
draw('distKNNPBI', 8, 11, dark ? 'rgba(120,160,255,0.5)' : 'rgba(57,106,177,0.4)', 50);
})();
</script>""")
out.append("</body></html>")
return "".join(out)