comparison py/rlgall.py @ 50:4549b3c0cd28

rlgall.py: add time selection to Game.getHigh(). Optional inittime and finaltime parameters set the range of time over which to calculate high scores.
author John "Elwin" Edwards
date Fri, 28 Mar 2014 09:33:49 -0700
parents 6138c27d1950
children df7acc5653b3
comparison
equal deleted inserted replaced
49:6138c27d1950 50:4549b3c0cd28
255 if not newest: 255 if not newest:
256 of.write("<div>No one has braved this dungeon yet.</div>\n") 256 of.write("<div>No one has braved this dungeon yet.</div>\n")
257 else: 257 else:
258 printTable(newest, self.fields, of) 258 printTable(newest, self.fields, of)
259 return 259 return
260 def getHigh(self, n=10, offset=0): 260 def getHigh(self, n=10, offset=0, inittime=None, finaltime=None):
261 "Gets the n highest scores (starting at offset) from the database, \ 261 "Gets the n highest scores (starting at offset) from the database, \
262 returning a list of dicts." 262 returning a list of dicts."
263 qfields = [] 263 qfields = []
264 for f in self.rankfields: 264 for f in self.rankfields:
265 if f == "rank": 265 if f == "rank":
266 qfields.append("rank(*) OVER (ORDER BY score DESC)") 266 qfields.append("rank(*) OVER (ORDER BY score DESC)")
267 else: 267 else:
268 qfields.append(f) 268 qfields.append(f)
269 qstr = "SELECT " + ", ".join(qfields) + " FROM {0} ".format(self.uname) 269 qstr = "SELECT " + ", ".join(qfields) + " FROM {0}".format(self.uname)
270 qvals = [] 270 qvals = []
271 if isinstance(inittime, datetime):
272 qstr += " WHERE endt >= %s"
273 qvals.append(inittime)
274 if isinstance(finaltime, datetime):
275 if finaltime < inittime:
276 return []
277 qstr += " AND endt < %s"
278 qvals.append(finaltime)
279 elif isinstance(finaltime, datetime):
280 qstr += " WHERE endt < %s"
281 qvals.append(finaltime)
271 try: 282 try:
272 n = int(n) 283 n = int(n)
273 except (ValueError, TypeError): 284 except (ValueError, TypeError):
274 return [] 285 return []
275 if n <= 0: 286 if n <= 0: