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.
This commit is contained in:
John "Elwin" Edwards 2014-01-16 16:25:09 -08:00
parent bc4dda7e9e
commit 479c173caf
2 changed files with 109 additions and 63 deletions

View file

@ -296,6 +296,73 @@ class Game:
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):