comparison 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
comparison
equal deleted inserted replaced
44:7c789e87ee5d 45:0f4163dbbafc
294 cur.close() 294 cur.close()
295 conn.close() 295 conn.close()
296 for d in data: 296 for d in data:
297 d["game"] = self 297 d["game"] = self
298 return data 298 return data
299 def getXLCounts(self, nmax=15):
300 "Returns a list of (xlevel, gamecount) pairs."
301 lquery = "SELECT count(*) FROM {0} WHERE xl = %s;".format(self.uname)
302 mquery = "SELECT count(*) FROM {0} WHERE xl >= %s;".format(self.uname)
303 try:
304 nmax = int(nmax)
305 except (ValueError, TypeError):
306 return []
307 if nmax <= 0:
308 return []
309 xlevels = range(1, nmax)
310 results = []
311 conn = psycopg2.connect("dbname=rlg")
312 cur = conn.cursor()
313 for xl in xlevels:
314 cur.execute(lquery, [xl])
315 results.append((xl, cur.fetchone()[0]))
316 cur.execute(mquery, [nmax])
317 results.append((nmax, cur.fetchone()[0]))
318 cur.close()
319 conn.close()
320 return results
321 def getDepthCounts(self, nmax=30):
322 "Returns a list of (maxdepth, gamecount) pairs."
323 dqry = "SELECT count(*) FROM {0} WHERE maxdepth = %s;".format(self.uname)
324 mqry = "SELECT count(*) FROM {0} WHERE maxdepth >= %s;".format(self.uname)
325 try:
326 nmax = int(nmax)
327 except (ValueError, TypeError):
328 return []
329 if nmax <= 0:
330 return []
331 depths = range(1, nmax)
332 results = []
333 conn = psycopg2.connect("dbname=rlg")
334 cur = conn.cursor()
335 for lev in depths:
336 cur.execute(dqry, [lev])
337 results.append((lev, cur.fetchone()[0]))
338 cur.execute(mqry, [nmax])
339 results.append((nmax, cur.fetchone()[0]))
340 cur.close()
341 conn.close()
342 return results
343 def getScoreCounts(self, blocks=10, size=1000):
344 "Returns a list of (minscore, gamecount) pairs."
345 sqry = "SELECT count(*) FROM {0} WHERE score >= %s AND score < %s;".format(self.uname)
346 mqry = "SELECT count(*) FROM {0} WHERE score >= %s;".format(self.uname)
347 try:
348 blocks = int(blocks)
349 size = int(size)
350 except (ValueError, TypeError):
351 return []
352 if blocks <= 0 or size <= 0:
353 return []
354 marks = range(blocks - 1)
355 results = []
356 conn = psycopg2.connect("dbname=rlg")
357 cur = conn.cursor()
358 for m in marks:
359 cur.execute(sqry, [m * size, (m + 1) * size])
360 results.append((m * size, cur.fetchone()[0]))
361 cur.execute(mqry, [(blocks - 1) * size])
362 results.append(((blocks - 1) * size, cur.fetchone()[0]))
363 cur.close()
364 conn.close()
365 return results
299 # End Game class definition 366 # End Game class definition
300 367
301 class RogueGame(Game): 368 class RogueGame(Game):
302 def __init__(self, name, uname, suffix): 369 def __init__(self, name, uname, suffix):
303 self.name = name 370 self.name = name