rlgall.py: add time selection to Game.getHigh().

Optional inittime and finaltime parameters set the range of time over
which to calculate high scores.
This commit is contained in:
John "Elwin" Edwards 2014-03-28 09:33:49 -07:00
parent d67561aa42
commit 2eb8cef1c9

View file

@ -257,7 +257,7 @@ class Game:
else:
printTable(newest, self.fields, of)
return
def getHigh(self, n=10, offset=0):
def getHigh(self, n=10, offset=0, inittime=None, finaltime=None):
"Gets the n highest scores (starting at offset) from the database, \
returning a list of dicts."
qfields = []
@ -266,8 +266,19 @@ class Game:
qfields.append("rank(*) OVER (ORDER BY score DESC)")
else:
qfields.append(f)
qstr = "SELECT " + ", ".join(qfields) + " FROM {0} ".format(self.uname)
qstr = "SELECT " + ", ".join(qfields) + " FROM {0}".format(self.uname)
qvals = []
if isinstance(inittime, datetime):
qstr += " WHERE endt >= %s"
qvals.append(inittime)
if isinstance(finaltime, datetime):
if finaltime < inittime:
return []
qstr += " AND endt < %s"
qvals.append(finaltime)
elif isinstance(finaltime, datetime):
qstr += " WHERE endt < %s"
qvals.append(finaltime)
try:
n = int(n)
except (ValueError, TypeError):