Store arogue5 results in the database.

The ARogueGame class is functional enough to put game results into the
database, though it still can't get them back out.
This commit is contained in:
John "Elwin" Edwards 2012-09-17 09:14:26 -07:00
parent 34585136ed
commit 30bdbebf0b

View file

@ -198,6 +198,47 @@ class Game:
cur.close() cur.close()
conn.close() conn.close()
return updatenames return updatenames
def postprocess(self, gamelist):
"Default postprocessing function: adds start time and ttyrec list"
names = set([ e["name"] for e in gamelist ])
conn = getconn()
if conn == None:
return []
cur = conn.cursor()
for nameF in names:
# Get all this player's games ordered by time
itsEntries = [ entry for entry in gamelist if entry["name"] == nameF ]
itsEntries.sort(key=lambda e: e["endt"])
# Find the end time of the latest game already in the db
tquery = "SELECT endt FROM {0} WHERE name = %s ORDER BY endt DESC LIMIT 1;".format(self.uname)
cur.execute(tquery, [nameF])
result = cur.fetchone()
if result:
prev = result[0]
else:
prev = datetime.fromtimestamp(0, utc);
ttyrecdir = "/var/dgl/dgldir/ttyrec/{0}/{1}/".format(nameF, self.uname)
allfilekeys = [ (recnameToTS(f), f) for f in os.listdir(ttyrecdir) ]
vfilekeys = [ e for e in allfilekeys if e[0] > prev ]
vfilekeys.sort(key=lambda e: e[0])
# Now determine startt and ttyrecs for each game
for i in range(0, len(itsEntries)):
if i == 0:
lowlim = prev
else:
lowlim = itsEntries[i-1]["endt"]
hilim = itsEntries[i]["endt"]
recs = [ k[1] for k in vfilekeys if lowlim <= k[0] < hilim ]
itsEntries[i]["startt"] = recnameToTS(recs[0])
itsEntries[i]["ttyrecs"] = recs
cur.close()
conn.close()
def putIntoDB(self, dictlist, conn):
cur = conn.cursor()
cur.executemany(self.insertq, [ d for d in dictlist if d["game"] == self ])
conn.commit()
cur.close()
return
# End Game class definition # End Game class definition
class RogueGame(Game): class RogueGame(Game):
@ -210,6 +251,19 @@ class RogueGame(Game):
"name": "varchar(20)", "xl": "int", "fate": "text", "name": "varchar(20)", "xl": "int", "fate": "text",
"ttyrecs": "text ARRAY", "startt": "timestamptz"} "ttyrecs": "text ARRAY", "startt": "timestamptz"}
self.logdelim = " " self.logdelim = " "
colspec = "("
valspec = "("
for i, col in enumerate(self.sqltypes.keys()):
colspec += col
valspec += "%({0})s".format(col)
if i == len(self.sqltypes) - 1:
colspec += ")"
valspec += ")"
else:
colspec += ", "
valspec += ", "
self.insertq = "INSERT INTO {0} {1} VALUES {2};".format(self.uname,
colspec, valspec)
# Class variables, used by some methods # Class variables, used by some methods
fields = ["name", "score", "xl", "fate", "endt"] fields = ["name", "score", "xl", "fate", "endt"]
rankfields = ["rank", "score", "name", "xl", "fate", "endt"] rankfields = ["rank", "score", "name", "xl", "fate", "endt"]
@ -311,40 +365,6 @@ class RogueGame(Game):
conn.commit() conn.commit()
cur.close() cur.close()
return return
def postprocess(self, gamelist):
names = set([ e["name"] for e in gamelist ])
conn = getconn()
if conn == None:
return []
cur = conn.cursor()
for nameF in names:
# Get all this player's games ordered by time
itsEntries = [ entry for entry in gamelist if entry["name"] == nameF ]
itsEntries.sort(key=lambda e: e["endt"])
# Find the end time of the latest game already in the db
tquery = "SELECT endt FROM {0} WHERE name = %s ORDER BY endt DESC LIMIT 1;".format(self.uname)
cur.execute(tquery, [nameF])
result = cur.fetchone()
if result:
prev = result[0]
else:
prev = datetime.fromtimestamp(0, utc);
ttyrecdir = "/var/dgl/dgldir/ttyrec/{0}/{1}/".format(nameF, self.uname)
allfilekeys = [ (recnameToTS(f), f) for f in os.listdir(ttyrecdir) ]
vfilekeys = [ e for e in allfilekeys if e[0] > prev ]
vfilekeys.sort(key=lambda e: e[0])
# Now determine startt and ttyrecs for each game
for i in range(0, len(itsEntries)):
if i == 0:
lowlim = prev
else:
lowlim = itsEntries[i-1]["endt"]
hilim = itsEntries[i]["endt"]
recs = [ k[1] for k in vfilekeys if lowlim <= k[0] < hilim ]
itsEntries[i]["startt"] = recnameToTS(recs[0])
itsEntries[i]["ttyrecs"] = recs
cur.close()
conn.close()
# End RogueGame class definition # End RogueGame class definition
class ARogueGame(Game): class ARogueGame(Game):
@ -359,17 +379,23 @@ class ARogueGame(Game):
"maxdepth": "int", "quest": "int", "hadquest": "bool", "maxdepth": "int", "quest": "int", "hadquest": "bool",
"fate": "text", "ttyrecs": "text ARRAY", "startt": "timestamptz"} "fate": "text", "ttyrecs": "text ARRAY", "startt": "timestamptz"}
self.logdelim = " " self.logdelim = " "
colspec = "("
valspec = "("
for i, col in enumerate(self.sqltypes.keys()):
colspec += col
valspec += "%({0})s".format(col)
if i == len(self.sqltypes) - 1:
colspec += ")"
valspec += ")"
else:
colspec += ", "
valspec += ", "
self.insertq = "INSERT INTO {0} {1} VALUES {2};".format(self.uname,
colspec, valspec)
# Class variables # Class variables
fields = ["name", "score", "class", "xl", "fate", "endt"] fields = ["name", "score", "class", "xl", "fate", "endt"]
rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"] rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"]
pfields = ["score", "class", "xl", "fate", "endt"] pfields = ["score", "class", "xl", "fate", "endt"]
def postprocess(self, gamelist):
"Not Implemented"
return
def putIntoDB(self, dictlist, conn):
"Not Implemented"
conn.commit()
return
def getHigh(self, n=10, offset=0): def getHigh(self, n=10, offset=0):
return [] return []
def getRecent(self, n=20): def getRecent(self, n=20):