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:
parent
34585136ed
commit
30bdbebf0b
1 changed files with 67 additions and 41 deletions
108
py/rlgalldb.py
108
py/rlgalldb.py
|
|
@ -198,6 +198,47 @@ class Game:
|
|||
cur.close()
|
||||
conn.close()
|
||||
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
|
||||
|
||||
class RogueGame(Game):
|
||||
|
|
@ -210,6 +251,19 @@ class RogueGame(Game):
|
|||
"name": "varchar(20)", "xl": "int", "fate": "text",
|
||||
"ttyrecs": "text ARRAY", "startt": "timestamptz"}
|
||||
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
|
||||
fields = ["name", "score", "xl", "fate", "endt"]
|
||||
rankfields = ["rank", "score", "name", "xl", "fate", "endt"]
|
||||
|
|
@ -311,40 +365,6 @@ class RogueGame(Game):
|
|||
conn.commit()
|
||||
cur.close()
|
||||
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
|
||||
|
||||
class ARogueGame(Game):
|
||||
|
|
@ -359,17 +379,23 @@ class ARogueGame(Game):
|
|||
"maxdepth": "int", "quest": "int", "hadquest": "bool",
|
||||
"fate": "text", "ttyrecs": "text ARRAY", "startt": "timestamptz"}
|
||||
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
|
||||
fields = ["name", "score", "class", "xl", "fate", "endt"]
|
||||
rankfields = ["rank", "score", "name", "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):
|
||||
return []
|
||||
def getRecent(self, n=20):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue