-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRumbleSelect.py
137 lines (105 loc) · 4.67 KB
/
RumbleSelect.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
#!/usr/bin/env python
# This Python file uses the following encoding: utf-8
#import cgi
#import datetime
import wsgiref.handlers
import time
#try:
# import json
#except:
# import simplejson as json
import string
import marshal
import zlib
import pickle
#from google.appengine.ext import db
#from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.api import memcache
#from operator import attrgetter
import structures
#from structures import global_dict
class RumbleSelect(webapp.RequestHandler):
def get(self):
#global global_dict
global_dict = {}
starttime = time.time()
query = self.request.query_string
query = query.replace("%20"," ")
parts = query.split("&")
requests = {}
if parts[0] != "":
for pair in parts:
ab = pair.split('=')
requests[ab[0]] = ab[1]
timing = bool(requests.get("timing",False))
regen = bool(requests.get("regen",False))
extraArgs = ""
if timing:
extraArgs += "&timing=1"
outstr = global_dict.get("home",None)
if outstr is None and not regen:
outstr = memcache.get("home")
if outstr is None or regen:
#gameHref = "<a href=Rankings?game=" + game + extraArgs + ">" + game + "</a>"
out = []
out.append(structures.html_header % ("Home","LiteRumble - Home"))
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:
try:
scoresdicts = pickle.loads(zlib.decompress(r.ParticipantsScores))
entries = len(scoresdicts)
# print entries
# try:
# print "fun!"
except:
try:
scores = marshal.loads(zlib.decompress(r.ParticipantsScores))
entries = len(scores)
except:
entries = 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>")
out.append("<table><tr><td><b><a href=\"RumbleStats\">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>")
outstr = string.join(out,"")
if not timing:
memcache.set("home",outstr)
elapsed = time.time() - starttime
if timing:
outstr += "<br>\n Page served in " + str(int(round(elapsed*1000))) + "ms."
outstr += "</body></html>"
self.response.out.write(outstr)
application = webapp.WSGIApplication([
('/RumbleSelect', RumbleSelect),
('/',RumbleSelect)
], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()