comparison py/rlgall.py @ 35:6592cdd1fceb

Add a high score CGI script. /scoring/high.cgi shows a score list, with a configurable length, offset, and game list.
author John "Elwin" Edwards
date Fri, 03 Jan 2014 11:11:07 -0500
parents 86b616d88020
children 09ef92dc4439
comparison
equal deleted inserted replaced
34:86b616d88020 35:6592cdd1fceb
113 clist.append(headerbook[field]) 113 clist.append(headerbook[field])
114 else: 114 else:
115 clist.append("{0}".format(field)) 115 clist.append("{0}".format(field))
116 of.write(maketablerow(clist, True)) 116 of.write(maketablerow(clist, True))
117 rnum = 0 117 rnum = 0
118 for i, entry in enumerate(entries): 118 for entry in entries:
119 clist = [] 119 clist = []
120 for field in fields: 120 for field in fields:
121 if field == "rank": 121 if field in entry:
122 clist.append(("{0}".format(i + 1), rcell))
123 elif field in entry:
124 thing = entry[field] 122 thing = entry[field]
125 if field == "game": 123 if field == "game":
126 clist.append((thing.name, cell)) 124 clist.append((thing.name, cell))
127 elif field == "xl" or field == "score": # Numerics 125 elif field == "xl" or field == "score" or field == "rank": # Numerics
128 clist.append((str(thing), rcell)) 126 clist.append((str(thing), rcell))
129 elif field == "name": 127 elif field == "name":
130 clist.append((playerlink(thing), cell)) 128 clist.append((playerlink(thing), cell))
131 elif field == "fate": 129 elif field == "fate":
132 clist.append((thing, cell)) 130 clist.append((thing, cell))
258 if not newest: 256 if not newest:
259 of.write("<div>No one has braved this dungeon yet.</div>\n") 257 of.write("<div>No one has braved this dungeon yet.</div>\n")
260 else: 258 else:
261 printTable(newest, self.fields, of) 259 printTable(newest, self.fields, of)
262 return 260 return
261 def getHigh(self, n=10, offset=0):
262 "Gets the n highest scores (starting at offset) from the database, \
263 returning a list of dicts."
264 qfields = []
265 for f in self.rankfields:
266 if f == "rank":
267 qfields.append("rank(*) OVER (ORDER BY score DESC)")
268 else:
269 qfields.append(f)
270 qstr = "SELECT " + ", ".join(qfields) + " FROM {0} ".format(self.uname)
271 qvals = []
272 try:
273 n = int(n)
274 except (ValueError, TypeError):
275 return []
276 if n <= 0:
277 return []
278 qstr += " LIMIT %s"
279 qvals.append(n)
280 try:
281 offset = int(offset)
282 except (ValueError, TypeError):
283 return []
284 if offset > 0:
285 qstr += " OFFSET %s"
286 qvals.append(offset)
287 qstr += ";"
288 conn = psycopg2.connect("dbname=rlg")
289 cur = conn.cursor()
290 cur.execute(qstr, qvals)
291 cols = [ col.name for col in cur.description ]
292 data = [ dict(zip(cols, row)) for row in cur.fetchall() ]
293 cur.close()
294 conn.close()
295 for d in data:
296 d["game"] = self
297 return data
263 # End Game class definition 298 # End Game class definition
264 299
265 class RogueGame(Game): 300 class RogueGame(Game):
266 def __init__(self, name, uname, suffix): 301 def __init__(self, name, uname, suffix):
267 self.name = name 302 self.name = name
309 ndict["startt"] = record[5] 344 ndict["startt"] = record[5]
310 dictlist.append(ndict) 345 dictlist.append(ndict)
311 cur.close() 346 cur.close()
312 conn.close() 347 conn.close()
313 return dictlist 348 return dictlist
314 def getHigh(self, n=10, offset=0):
315 "Gets the n highest scores (starting at offset) from the database, \
316 returning a list of dicts."
317 qstr = "SELECT endt, score, name, xl, fate, startt FROM {0} ORDER BY score DESC ".format(self.uname)
318 qvals = []
319 try:
320 n = int(n)
321 except (ValueError, TypeError):
322 return []
323 if n <= 0:
324 return []
325 qstr += " LIMIT %s"
326 qvals.append(n)
327 try:
328 offset = int(offset)
329 except (ValueError, TypeError):
330 return []
331 if n > 0:
332 qstr += " OFFSET %s"
333 qvals.append(offset)
334 qstr += ";"
335 conn = psycopg2.connect("dbname=rlg")
336 cur = conn.cursor()
337 cur.execute(qstr, qvals)
338 dictlist = []
339 for record in cur:
340 ndict = {"game": self}
341 ndict["endt"] = record[0]
342 ndict["score"] = record[1]
343 ndict["name"] = record[2]
344 ndict["xl"] = record[3]
345 ndict["fate"] = record[4]
346 ndict["startt"] = record[5]
347 dictlist.append(ndict)
348 cur.close()
349 conn.close()
350 return dictlist
351 def getPlayer(self, player): 349 def getPlayer(self, player):
352 "Gets all player's games from the database." 350 "Gets all player's games from the database."
353 qstr = "SELECT endt, score, name, xl, fate, startt FROM " + self.uname + " WHERE name = %s;" 351 qstr = "SELECT endt, score, name, xl, fate, startt FROM " + self.uname + " WHERE name = %s;"
354 conn = getconn() 352 conn = getconn()
355 if conn == None: 353 if conn == None:
404 colspec, valspec) 402 colspec, valspec)
405 # Class variables 403 # Class variables
406 fields = ["name", "score", "class", "xl", "fate", "endt"] 404 fields = ["name", "score", "class", "xl", "fate", "endt"]
407 rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"] 405 rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"]
408 pfields = ["score", "class", "xl", "fate", "endt"] 406 pfields = ["score", "class", "xl", "fate", "endt"]
409 def getHigh(self, n=10, offset=0):
410 "Gets the n highest scores (starting at offset) from the database, \
411 returning a list of dicts."
412 qstr = "SELECT endt, score, name, xl, class, fate FROM {0} ORDER BY score DESC ".format(self.uname)
413 qvals = []
414 try:
415 n = int(n)
416 except (ValueError, TypeError):
417 return []
418 if n <= 0:
419 return []
420 qstr += " LIMIT %s"
421 qvals.append(n)
422 try:
423 offset = int(offset)
424 except (ValueError, TypeError):
425 return []
426 if n > 0:
427 qstr += " OFFSET %s"
428 qvals.append(offset)
429 qstr += ";"
430 conn = psycopg2.connect("dbname=rlg")
431 cur = conn.cursor()
432 cur.execute(qstr, qvals)
433 dictlist = []
434 for record in cur:
435 ndict = {"game": self}
436 ndict["endt"] = record[0]
437 ndict["score"] = record[1]
438 ndict["name"] = record[2]
439 ndict["xl"] = record[3]
440 ndict["class"] = record[4]
441 ndict["fate"] = record[5]
442 dictlist.append(ndict)
443 cur.close()
444 conn.close()
445 return dictlist
446 def getRecent(self, n=20): 407 def getRecent(self, n=20):
447 return [] 408 return []
448 def getPlayer(self, player): 409 def getPlayer(self, player):
449 "Gets all player's games from the database." 410 "Gets all player's games from the database."
450 qstr = "SELECT endt, score, name, xl, class, fate FROM " + self.uname + " WHERE name = %s;" 411 qstr = "SELECT endt, score, name, xl, class, fate FROM " + self.uname + " WHERE name = %s;"