-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rankings.py
291 lines (246 loc) · 10.2 KB
/
Rankings.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
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
#!/usr/bin/env python
#import cgi
#import datetime
import wsgiref.handlers
import time
#try:
# import json
#except:
# import simplejson as json
import string
import cPickle as pickle
import marshal
#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
import zlib
#from structures import global_dict
class Rankings(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]
game = requests.get("game","meleerumble")
lim = int(requests.get("limit","10000000"))
#ofst = int(requests.get("offset","0"))
order = requests.get("order","APS")
timing = bool(requests.get("timing",False))
api = bool(requests.get("api",False))
extraArgs = ""
if timing:
extraArgs += "&timing=1"
if lim < 100000:
extraArgs += "&limit=" + str(lim)
reverseSort = True
if len(order) == 0:
order = "APS"
if order[0] == "-":
order = order[1:]
reverseSort = False
if order == "Latest Battle":
order = "LastUpload"
elif order == "Competitor":
order = "Name"
elif order == "Vote":
order = "VoteScore"
parsing = time.time() - starttime
#rumble = global_dict.get(game,None)
#if rumble is None:
rumble = memcache.get(game)
if rumble is None:
rumble = structures.Rumble.get_by_key_name(game)
if rumble is None:
self.response.out.write("RUMBLE NOT FOUND")
return
else:
#global_dict[game]=rumble
memcache.set(game,rumble)
#else:
#global_dict[game] = rumble
#flagmap = global_dict.get(structures.default_flag_map,None)
#if flagmap is None:
flagmap = memcache.get(structures.default_flag_map)
if flagmap is None:
flagmapholder = structures.FlagMap.get_by_key_name(structures.default_flag_map)
if flagmapholder is None:
flagmap = zlib.compress(marshal.dumps({}))
else:
flagmap = flagmapholder.InternalMap
memcache.set(structures.default_flag_map,flagmap)
#global_dict[structures.default_flag_map] = flagmap
# else:
# global_dict[structures.default_flag_map] = flagmap
try:
flagmap = pickle.loads(zlib.decompress(flagmap))
except:
flagmap = marshal.loads(zlib.decompress(flagmap))
try:
#print "try json"
botsdict = pickle.loads(zlib.decompress(rumble.ParticipantsScores))
bots = botsdict.values()
# try:
# self.response.out.write( " json worked")
except:
# self.response.out.write( "\ntry pickle")
scoresdicts = marshal.loads(zlib.decompress(rumble.ParticipantsScores))
scoreslist = [structures.LiteBot() for _ in scoresdicts]
for s,d in zip(scoreslist,scoresdicts):
s.__dict__.update(d)
#r = {s.Name:s for s in scoreslist}
bots = scoreslist
# print " pickle worked"
retrievetime = time.time() - starttime - parsing
#newbots = []
for b in bots:
b.PWIN = 50.0*float(b.PL)/b.Pairings + 50.0
if b.VoteScore is None:
b.VoteScore = 0
if b.ANPP is None:
b.ANPP = 0
package = string.split(b.Name,".")[0]
if package in flagmap:
b.Flag = flagmap[package]
else:
b.Flag = "NONE"
get_key = attrgetter(order)
bots.sort( key=lambda b: get_key(b), reverse=reverseSort)
if api:
headings = ["\"name\"",
"\"flag\"",
"\"rank\"",
"\"APS\"",
"\"PWIN\"",
"\"ANPP\"",
"\"vote\"",
"\"survival\"",
"\"pairings\"",
"\"battles\"",
"\"latest\""]
escapes = ["\"","\"","","","","","","","","","\""]
outs = ["[\n"]
count = 0
for bot in bots:
count += 1
if count > lim:
break
cells = [
bot.Name,bot.Flag,count,
round(100.0*bot.APS)*0.01,
round(100.0*bot.PWIN)*0.01,
round(100.0*bot.ANPP)*0.01,
round(100.0*bot.VoteScore)*0.01,
round(100.0*bot.Survival)*0.01,
bot.Pairings,bot.Battles,bot.LastUpload]
outs.append("{")
for i in range(len(cells)):
outs.append(headings[i])
outs.append(":")
outs.append(escapes[i])
outs.append(str(cells[i]))
outs.append(escapes[i])
outs.append(",")
outs[-1] = "},\n"
outs[-1] = ("}\n]")
self.response.out.write(''.join(outs))
else:
sorttime = time.time() - parsing - retrievetime - starttime
if order == "LastUpload":
order = "Latest Battle"
elif order == "Name":
order = "Competitor"
elif order == "VoteScore":
order = "Vote"
out = []
gameTitle = "RANKINGS - " + string.upper(game) + " WITH " + str(len(bots)) + " BOTS"
out.append(structures.html_header % (game,gameTitle))
pairVals = [b.Pairings for b in bots]
if max(pairVals) == min(pairVals) == (len(bots)-1):
out.append("<big>Rankings Stable</big>")
else:
out.append("<big>Rankings Not Stable</big>")
out.append("\n<table>\n<tr>");
headings = ["","Flag","Competitor","APS","PWIN","ANPP","Vote","Survival","Pairings","Battles","Latest Battle"]
for heading in headings:
sortedBy = order == heading
if order == heading and reverseSort:
heading = "-" + heading
orderl = []
orderl.append("<a href=\"Rankings?game=")
orderl.append(game)
orderl.append("&order=")
orderl.append(heading.replace(" ","%20"))
orderl.append(extraArgs)
orderl.append("\">")
orderl.append(heading)
orderl.append("</a>")
orderHref = ''.join(orderl)
if sortedBy:
out.append( "\n<th class=\"sortedby\">" + orderHref + "</th>")
else:
out.append( "\n<th>" + orderHref + "</th>")
out.append("\n</tr>")
rank = 1
for bot in bots:
if rank > lim:
break
botName=bot.Name
bnh = []
bnh.append("<a href=\"BotDetails?game=")
bnh.append(game)
bnh.append("&name=")
bnh.append(botName.replace(" ","%20"))
bnh.append(extraArgs)
bnh.append("\" >")
bnh.append(botName)
bnh.append("</a>")
botNameHref = ''.join(bnh) #"<a href=BotDetails?game="+game+"&name=" + botName.replace(" ","%20")+extraArgs+">"+botName+"</a>"
ft = []
ft.append("<img id='flag' src=\"/flags/")
ft.append(bot.Flag)
ft.append(".gif\">")
flagtag = ''.join(ft)
cells = [rank,flagtag,botNameHref,
round(100.0*bot.APS)*0.01,
round(100.0*bot.PWIN)*0.01,
round(100.0*bot.ANPP)*0.01,
round(100.0*bot.VoteScore)*0.01,
round(100.0*bot.Survival)*0.01,
bot.Pairings,bot.Battles,bot.LastUpload]
out.append("\n<tr>")
for cell in cells:
out.append( "\n<td>")
out.append(str(cell))
out.append("</td>")
out.append("\n</tr>")
del bot.PWIN
rank += 1
out.append( "</table>")
htmltime = time.time() - parsing - retrievetime - sorttime - starttime
elapsed = time.time() - starttime
if timing:
out.append("\n<br> Page served in " + str(int(round(elapsed*1000))) + "ms. ")# + str(len(missingHashes)) + " bots retrieved from datastore.")
out.append("\n<br> parsing: " + str(int(round(parsing*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( "</body></html>")
outstr = ''.join(out)
self.response.out.write(outstr)
application = webapp.WSGIApplication([
('/Rankings', Rankings)
], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()