# HG changeset patch
# User John "Elwin" Edwards <elwin@sdf.org>
# Date 1347898466 25200
# Node ID 5731d2ecaec4aacacc91001ee2a72f8ac9562ace
# Parent  7f7b3da664d6474edfc7ca864d9a2d4414961734
Store arogue5 results in the database.

The ARogueGame class is functional enough to put game results into the
database, though it still can't get them back out.

diff -r 7f7b3da664d6 -r 5731d2ecaec4 py/rlgalldb.py
--- a/py/rlgalldb.py	Sun Sep 16 20:44:22 2012 -0700
+++ b/py/rlgalldb.py	Mon Sep 17 09:14:26 2012 -0700
@@ -198,6 +198,47 @@
     cur.close()
     conn.close()
     return updatenames
+  def postprocess(self, gamelist):
+    "Default postprocessing function: adds start time and ttyrec list"
+    names = set([ e["name"] for e in gamelist ])
+    conn = getconn()
+    if conn == None:
+      return []
+    cur = conn.cursor()
+    for nameF in names:
+      # Get all this player's games ordered by time
+      itsEntries = [ entry for entry in gamelist if entry["name"] == nameF ]
+      itsEntries.sort(key=lambda e: e["endt"])
+      # Find the end time of the latest game already in the db
+      tquery = "SELECT endt FROM {0} WHERE name = %s ORDER BY endt DESC LIMIT 1;".format(self.uname)
+      cur.execute(tquery, [nameF])
+      result = cur.fetchone()
+      if result:
+        prev = result[0]
+      else:
+        prev = datetime.fromtimestamp(0, utc);
+      ttyrecdir = "/var/dgl/dgldir/ttyrec/{0}/{1}/".format(nameF, self.uname)
+      allfilekeys = [ (recnameToTS(f), f) for f in os.listdir(ttyrecdir) ]
+      vfilekeys = [ e for e in allfilekeys if e[0] > prev ]
+      vfilekeys.sort(key=lambda e: e[0])
+      # Now determine startt and ttyrecs for each game
+      for i in range(0, len(itsEntries)):
+        if i == 0:
+          lowlim = prev
+        else:
+          lowlim = itsEntries[i-1]["endt"]
+        hilim = itsEntries[i]["endt"]
+        recs = [ k[1] for k in vfilekeys if lowlim <= k[0] < hilim ]
+        itsEntries[i]["startt"] = recnameToTS(recs[0])
+        itsEntries[i]["ttyrecs"] = recs
+    cur.close()
+    conn.close()
+  def putIntoDB(self, dictlist, conn):
+    cur = conn.cursor()
+    cur.executemany(self.insertq, [ d for d in dictlist if d["game"] == self ])
+    conn.commit()
+    cur.close()
+    return
 # End Game class definition
 
 class RogueGame(Game):
@@ -210,6 +251,19 @@
             "name": "varchar(20)", "xl": "int", "fate": "text",
             "ttyrecs": "text ARRAY", "startt": "timestamptz"}
     self.logdelim = " "
+    colspec = "("
+    valspec = "("
+    for i, col in enumerate(self.sqltypes.keys()):
+      colspec += col
+      valspec += "%({0})s".format(col)
+      if i == len(self.sqltypes) - 1:
+        colspec += ")"
+        valspec += ")"
+      else:
+        colspec += ", "
+        valspec += ", "
+    self.insertq = "INSERT INTO {0} {1} VALUES {2};".format(self.uname, 
+                    colspec, valspec)
   # Class variables, used by some methods
   fields = ["name", "score", "xl", "fate", "endt"]
   rankfields = ["rank", "score", "name", "xl", "fate", "endt"]
@@ -311,40 +365,6 @@
     conn.commit()
     cur.close()
     return
-  def postprocess(self, gamelist):
-    names = set([ e["name"] for e in gamelist ])
-    conn = getconn()
-    if conn == None:
-      return []
-    cur = conn.cursor()
-    for nameF in names:
-      # Get all this player's games ordered by time
-      itsEntries = [ entry for entry in gamelist if entry["name"] == nameF ]
-      itsEntries.sort(key=lambda e: e["endt"])
-      # Find the end time of the latest game already in the db
-      tquery = "SELECT endt FROM {0} WHERE name = %s ORDER BY endt DESC LIMIT 1;".format(self.uname)
-      cur.execute(tquery, [nameF])
-      result = cur.fetchone()
-      if result:
-        prev = result[0]
-      else:
-        prev = datetime.fromtimestamp(0, utc);
-      ttyrecdir = "/var/dgl/dgldir/ttyrec/{0}/{1}/".format(nameF, self.uname)
-      allfilekeys = [ (recnameToTS(f), f) for f in os.listdir(ttyrecdir) ]
-      vfilekeys = [ e for e in allfilekeys if e[0] > prev ]
-      vfilekeys.sort(key=lambda e: e[0])
-      # Now determine startt and ttyrecs for each game
-      for i in range(0, len(itsEntries)):
-        if i == 0:
-          lowlim = prev
-        else:
-          lowlim = itsEntries[i-1]["endt"]
-        hilim = itsEntries[i]["endt"]
-        recs = [ k[1] for k in vfilekeys if lowlim <= k[0] < hilim ]
-        itsEntries[i]["startt"] = recnameToTS(recs[0])
-        itsEntries[i]["ttyrecs"] = recs
-    cur.close()
-    conn.close()
 # End RogueGame class definition
 
 class ARogueGame(Game):
@@ -359,17 +379,23 @@
             "maxdepth": "int", "quest": "int", "hadquest": "bool",
             "fate": "text", "ttyrecs": "text ARRAY", "startt": "timestamptz"}
     self.logdelim = " "
+    colspec = "("
+    valspec = "("
+    for i, col in enumerate(self.sqltypes.keys()):
+      colspec += col
+      valspec += "%({0})s".format(col)
+      if i == len(self.sqltypes) - 1:
+        colspec += ")"
+        valspec += ")"
+      else:
+        colspec += ", "
+        valspec += ", "
+    self.insertq = "INSERT INTO {0} {1} VALUES {2};".format(self.uname, 
+                    colspec, valspec)
   # Class variables
   fields = ["name", "score", "class", "xl", "fate", "endt"]
   rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"]
   pfields = ["score", "class", "xl", "fate", "endt"]
-  def postprocess(self, gamelist):
-    "Not Implemented"
-    return
-  def putIntoDB(self, dictlist, conn):
-    "Not Implemented"
-    conn.commit()
-    return
   def getHigh(self, n=10, offset=0):
     return []
   def getRecent(self, n=20):