diff 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
line wrap: on
line diff
--- a/py/rlgall.py	Thu Jan 02 13:58:05 2014 -0500
+++ b/py/rlgall.py	Fri Jan 03 11:11:07 2014 -0500
@@ -115,16 +115,14 @@
       clist.append("{0}".format(field))
   of.write(maketablerow(clist, True))
   rnum = 0
-  for i, entry in enumerate(entries):
+  for entry in entries:
     clist = []
     for field in fields:
-      if field == "rank":
-        clist.append(("{0}".format(i + 1), rcell))
-      elif field in entry:
+      if field in entry:
         thing = entry[field]
         if field == "game":
           clist.append((thing.name, cell))
-        elif field == "xl" or field == "score": # Numerics
+        elif field == "xl" or field == "score" or field == "rank": # Numerics
           clist.append((str(thing), rcell))
         elif field == "name":
           clist.append((playerlink(thing), cell))
@@ -260,6 +258,43 @@
     else:
       printTable(newest, self.fields, of)
     return
+  def getHigh(self, n=10, offset=0):
+    "Gets the n highest scores (starting at offset) from the database, \
+     returning a list of dicts."
+    qfields = []
+    for f in self.rankfields:
+      if f == "rank":
+        qfields.append("rank(*) OVER (ORDER BY score DESC)")
+      else:
+        qfields.append(f)
+    qstr = "SELECT " + ", ".join(qfields) + " FROM {0} ".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 offset > 0:
+      qstr += " OFFSET %s"
+      qvals.append(offset)
+    qstr += ";"
+    conn = psycopg2.connect("dbname=rlg")
+    cur = conn.cursor()
+    cur.execute(qstr, qvals)
+    cols = [ col.name for col in cur.description ]
+    data = [ dict(zip(cols, row)) for row in cur.fetchall() ]
+    cur.close()
+    conn.close()
+    for d in data:
+      d["game"] = self
+    return data
 # End Game class definition
 
 class RogueGame(Game):
@@ -311,43 +346,6 @@
     cur.close()
     conn.close()
     return dictlist
-  def getHigh(self, n=10, offset=0):
-    "Gets the n highest scores (starting at offset) from the database, \
-     returning a list of dicts."
-    qstr = "SELECT endt, score, name, xl, fate, startt 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["fate"] = record[4]
-      ndict["startt"] = record[5]
-      dictlist.append(ndict)
-    cur.close()
-    conn.close()
-    return dictlist
   def getPlayer(self, player):
     "Gets all player's games from the database."
     qstr = "SELECT endt, score, name, xl, fate, startt FROM " + self.uname + " WHERE name = %s;"
@@ -406,43 +404,6 @@
   fields = ["name", "score", "class", "xl", "fate", "endt"]
   rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"]
   pfields = ["score", "class", "xl", "fate", "endt"]
-  def getHigh(self, n=10, offset=0):
-    "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):