-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRumbleSelect.py
More file actions
98 lines (79 loc) · 3.51 KB
/
RumbleSelect.py
File metadata and controls
98 lines (79 loc) · 3.51 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
#!/usr/bin/env python
import time
from flask import request
from google.appengine.api import memcache
import structures
from structures import load_blob
def rumble_select():
global_dict = {}
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 ""
timing = bool(requests.get("timing", False))
regen = bool(requests.get("regen", False))
dark = requests.get("theme", "") == "dark"
extraArgs = ""
if timing:
extraArgs += "&timing=1"
if dark:
extraArgs += "&theme=dark"
cacheKey = "home_dark" if dark else "home"
outstr = global_dict.get(cacheKey, None)
if outstr is None and not regen:
outstr = memcache.get(cacheKey)
if outstr is None or regen:
out = []
out.append(structures.header("Home", "LiteRumble - Home", dark))
q = structures.Rumble.all()
rumbles = [[], [], []]
categories = ["1v1", "Melee", "Teams"]
for r in q.run():
memr = memcache.get(r.Name)
if memr is not None:
r = memr
if r.Melee:
rumbles[1].append(r)
elif r.Teams:
rumbles[2].append(r)
else:
rumbles[0].append(r)
for cat, rumbs in zip(categories, rumbles):
for r in rumbs:
scoresdicts = load_blob(r.ParticipantsScores, {})
entries = len(scoresdicts) if hasattr(scoresdicts, "__len__") else len(r.Participants)
r.__dict__["entries"] = entries
rumbs.sort(key=lambda r: -r.__dict__["entries"])
out.append("<table class=\"rumble\">\n<tr>")
out.append("\n<th colspan=\"2\">" + cat + "</th>\n<th>Participants</th>\n</tr>")
for i, r in enumerate(rumbs):
game = r.Name
gameHref = "<a href=\"Rankings?game=" + game + extraArgs + "\" >" + game + "</a>"
topHref = "<a href=\"Rankings?game=" + game + "&limit=50" + extraArgs + "\" >top 50</a>"
out.append("\n<tr>\n<td>" + gameHref + "</td>\n<td>" + topHref + "</td>\n<td>")
out.append(str(r.__dict__["entries"]) + "</td>\n</tr>")
r.__dict__.pop("entries", 1)
memcache.set(r.Name, r)
out.append("</table>")
# extraArgs is prefixed with "&" to chain onto an existing query string;
# these links have no base param, so start a fresh "?" query instead.
baseArgs = ("?" + extraArgs.replace("&", "", 1)) if extraArgs else ""
out.append("<table><tr><td><b><a href=\"RumbleStats" + baseArgs + "\">LiteRumble Statistics</a></b></td></tr>")
out.append("<tr><td><b><a href=\"ScoreExplanation\">Score Explanation</a></b></td></tr></table>")
out.append("<br>Learn more about Robocode at <a href=\"http://robowiki.net\">Robowiki.net</a>")
if dark:
out.append("<br><a href=\"?theme=light\">Light mode</a>")
else:
out.append("<br><a href=\"?theme=dark\">Dark mode</a>")
outstr = "".join(out)
if not timing:
memcache.set(cacheKey, outstr, time=3600)
elapsed = time.time() - starttime
if timing:
outstr += "<br>\n Page served in " + str(int(round(elapsed * 1000))) + "ms."
outstr += "</body></html>"
return outstr