From 44d2b92159f3d0efcb24cc16ccf1e8fbde72c4c4 Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Mon, 17 Sep 2012 10:24:38 -0700 Subject: [PATCH] Add high score list support for arogue5. --- py/rlgalldb.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/py/rlgalldb.py b/py/rlgalldb.py index 8505eba..7fb1795 100644 --- a/py/rlgalldb.py +++ b/py/rlgalldb.py @@ -44,7 +44,7 @@ pend = "\n" # 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 @@ class ARogueGame(Game): 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 @@ def playerpage(pname): ppagefi.write("
" + pname + " has not yet completed an expedition\ in this dungeon.
\n") else: + entries.sort(key=lambda e: e["endt"]) printTable(entries, RogueGame.pfields, ppagefi) scoresum = 0 for entry in entries: