comparison py/rlgalldb.py @ 19:78580bffc13d

Add high score list support for arogue5.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 17 Sep 2012 10:24:38 -0700
parents 5731d2ecaec4
children c05050f78d81
comparison
equal deleted inserted replaced
18:5731d2ecaec4 19:78580bffc13d
42 tblend = '</div>\n' 42 tblend = '</div>\n'
43 pend = "</body></html>\n" 43 pend = "</body></html>\n"
44 44
45 # This would be more useful if we had to do translation 45 # This would be more useful if we had to do translation
46 headerbook = {"endt":"End time", "score":"Score", "name":"Name", "xl":"XL", 46 headerbook = {"endt":"End time", "score":"Score", "name":"Name", "xl":"XL",
47 "fate":"Fate", "rank":"Rank", "game":"Game"} 47 "fate":"Fate", "rank":"Rank", "game":"Game", "class": "Class"}
48 # Queries for the games table 48 # Queries for the games table
49 offselstr = "SELECT offbytes FROM games WHERE gname = %s;" 49 offselstr = "SELECT offbytes FROM games WHERE gname = %s;"
50 newoffstr = "UPDATE games SET offbytes = %s WHERE gname = %s;" 50 newoffstr = "UPDATE games SET offbytes = %s WHERE gname = %s;"
51 51
52 # A representation of the UTC time zone. They say Py3k can better handle 52 # A representation of the UTC time zone. They say Py3k can better handle
395 # Class variables 395 # Class variables
396 fields = ["name", "score", "class", "xl", "fate", "endt"] 396 fields = ["name", "score", "class", "xl", "fate", "endt"]
397 rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"] 397 rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"]
398 pfields = ["score", "class", "xl", "fate", "endt"] 398 pfields = ["score", "class", "xl", "fate", "endt"]
399 def getHigh(self, n=10, offset=0): 399 def getHigh(self, n=10, offset=0):
400 return [] 400 "Gets the n highest scores (starting at offset) from the database, \
401 returning a list of dicts."
402 qstr = "SELECT endt, score, name, xl, class, fate FROM {0} ORDER BY score DESC ".format(self.uname)
403 qvals = []
404 try:
405 n = int(n)
406 except (ValueError, TypeError):
407 return []
408 if n <= 0:
409 return []
410 qstr += " LIMIT %s"
411 qvals.append(n)
412 try:
413 offset = int(offset)
414 except (ValueError, TypeError):
415 return []
416 if n > 0:
417 qstr += " OFFSET %s"
418 qvals.append(offset)
419 qstr += ";"
420 conn = psycopg2.connect("dbname=rlg")
421 cur = conn.cursor()
422 cur.execute(qstr, qvals)
423 dictlist = []
424 for record in cur:
425 ndict = {"game": self}
426 ndict["endt"] = record[0]
427 ndict["score"] = record[1]
428 ndict["name"] = record[2]
429 ndict["xl"] = record[3]
430 ndict["class"] = record[4]
431 ndict["fate"] = record[5]
432 dictlist.append(ndict)
433 cur.close()
434 conn.close()
435 return dictlist
401 def getRecent(self, n=20): 436 def getRecent(self, n=20):
402 return [] 437 return []
403 def getPlayer(self, player): 438 def getPlayer(self, player):
404 return [] 439 return []
405 440
424 entries = game.getPlayer(pname) 459 entries = game.getPlayer(pname)
425 if not entries: 460 if not entries:
426 ppagefi.write("<div>" + pname + " has not yet completed an expedition\ 461 ppagefi.write("<div>" + pname + " has not yet completed an expedition\
427 in this dungeon.</div>\n") 462 in this dungeon.</div>\n")
428 else: 463 else:
464 entries.sort(key=lambda e: e["endt"])
429 printTable(entries, RogueGame.pfields, ppagefi) 465 printTable(entries, RogueGame.pfields, ppagefi)
430 scoresum = 0 466 scoresum = 0
431 for entry in entries: 467 for entry in entries:
432 scoresum += int(entry["score"]) 468 scoresum += int(entry["score"])
433 avgscr = scoresum / len(entries) 469 avgscr = scoresum / len(entries)