changeset 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
files py/rlgalldb.py
diffstat 1 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/py/rlgalldb.py	Mon Sep 17 09:14:26 2012 -0700
+++ b/py/rlgalldb.py	Mon Sep 17 10:24:38 2012 -0700
@@ -44,7 +44,7 @@
 
 # This would be more useful if we had to do translation
 headerbook = {"endt":"End time", "score":"Score", "name":"Name", "xl":"XL", 
-              "fate":"Fate", "rank":"Rank", "game":"Game"}
+              "fate":"Fate", "rank":"Rank", "game":"Game", "class": "Class"}
 # Queries for the games table
 offselstr = "SELECT offbytes FROM games WHERE gname = %s;"
 newoffstr = "UPDATE games SET offbytes = %s WHERE gname = %s;"
@@ -397,7 +397,42 @@
   rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"]
   pfields = ["score", "class", "xl", "fate", "endt"]
   def getHigh(self, n=10, offset=0):
-    return []
+    "Gets the n highest scores (starting at offset) from the database, \
+     returning a list of dicts."
+    qstr = "SELECT endt, score, name, xl, class, fate FROM {0} ORDER BY score DESC ".format(self.uname)
+    qvals = []
+    try:
+      n = int(n)
+    except (ValueError, TypeError):
+      return []
+    if n <= 0:
+      return []
+    qstr += " LIMIT %s"
+    qvals.append(n)
+    try:
+      offset = int(offset)
+    except (ValueError, TypeError):
+      return []
+    if n > 0:
+      qstr += " OFFSET %s"
+      qvals.append(offset)
+    qstr += ";"
+    conn = psycopg2.connect("dbname=rlg")
+    cur = conn.cursor()
+    cur.execute(qstr, qvals)
+    dictlist = []
+    for record in cur:
+      ndict = {"game": self}
+      ndict["endt"] = record[0]
+      ndict["score"] = record[1]
+      ndict["name"] = record[2]
+      ndict["xl"] = record[3]
+      ndict["class"] = record[4]
+      ndict["fate"] = record[5]
+      dictlist.append(ndict)
+    cur.close()
+    conn.close()
+    return dictlist
   def getRecent(self, n=20):
     return []
   def getPlayer(self, player):
@@ -426,6 +461,7 @@
       ppagefi.write("<div>" + pname + " has not yet completed an expedition\
         in this dungeon.</div>\n")
     else:
+      entries.sort(key=lambda e: e["endt"])
       printTable(entries, RogueGame.pfields, ppagefi)
       scoresum = 0
       for entry in entries: