diff py/rlgall.py @ 45:0f4163dbbafc

SVG charts: separate database-querying and chart-printing code. Create Game methods in rlgall.py to get histogram data for score, xl, maxdepth. In stats2.py, make the chart-printing functions use this data and avoid running hard-coded queries.
author John "Elwin" Edwards
date Thu, 16 Jan 2014 16:25:09 -0800
parents e1de8aeb5ed4
children 6138c27d1950
line wrap: on
line diff
--- a/py/rlgall.py	Thu Jan 16 11:04:45 2014 -0800
+++ b/py/rlgall.py	Thu Jan 16 16:25:09 2014 -0800
@@ -296,6 +296,73 @@
     for d in data:
       d["game"] = self
     return data
+  def getXLCounts(self, nmax=15):
+    "Returns a list of (xlevel, gamecount) pairs."
+    lquery = "SELECT count(*) FROM {0} WHERE xl = %s;".format(self.uname)
+    mquery = "SELECT count(*) FROM {0} WHERE xl >= %s;".format(self.uname)
+    try:
+      nmax = int(nmax)
+    except (ValueError, TypeError):
+      return []
+    if nmax <= 0:
+      return []
+    xlevels = range(1, nmax)
+    results = []
+    conn = psycopg2.connect("dbname=rlg")
+    cur = conn.cursor()
+    for xl in xlevels:
+      cur.execute(lquery, [xl])
+      results.append((xl, cur.fetchone()[0]))
+    cur.execute(mquery, [nmax])
+    results.append((nmax, cur.fetchone()[0]))
+    cur.close()
+    conn.close()
+    return results
+  def getDepthCounts(self, nmax=30):
+    "Returns a list of (maxdepth, gamecount) pairs."
+    dqry = "SELECT count(*) FROM {0} WHERE maxdepth = %s;".format(self.uname)
+    mqry = "SELECT count(*) FROM {0} WHERE maxdepth >= %s;".format(self.uname)
+    try:
+      nmax = int(nmax)
+    except (ValueError, TypeError):
+      return []
+    if nmax <= 0:
+      return []
+    depths = range(1, nmax)
+    results = []
+    conn = psycopg2.connect("dbname=rlg")
+    cur = conn.cursor()
+    for lev in depths:
+      cur.execute(dqry, [lev])
+      results.append((lev, cur.fetchone()[0]))
+    cur.execute(mqry, [nmax])
+    results.append((nmax, cur.fetchone()[0]))
+    cur.close()
+    conn.close()
+    return results
+  def getScoreCounts(self, blocks=10, size=1000):
+    "Returns a list of (minscore, gamecount) pairs."
+    sqry = "SELECT count(*) FROM {0} WHERE score >= %s AND score < %s;".format(self.uname)
+    mqry = "SELECT count(*) FROM {0} WHERE score >= %s;".format(self.uname)
+    try:
+      blocks = int(blocks)
+      size = int(size)
+    except (ValueError, TypeError):
+      return []
+    if blocks <= 0 or size <= 0:
+      return []
+    marks = range(blocks - 1)
+    results = []
+    conn = psycopg2.connect("dbname=rlg")
+    cur = conn.cursor()
+    for m in marks:
+      cur.execute(sqry, [m * size, (m + 1) * size])
+      results.append((m * size, cur.fetchone()[0]))
+    cur.execute(mqry, [(blocks - 1) * size])
+    results.append(((blocks - 1) * size, cur.fetchone()[0]))
+    cur.close()
+    conn.close()
+    return results
 # End Game class definition
 
 class RogueGame(Game):