comparison py/rlgalldb.py @ 21:453278a81371

Add tablerecent() so recent.cgi will work as before. Port the tablerecent() function, which reads the most recent games directly from the logfile instead of the database. This allows recent.cgi to show games immediately without waiting for the recorder to put them in the database.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 17 Sep 2012 13:41:10 -0700
parents c05050f78d81
children e8f3b7994d88
comparison
equal deleted inserted replaced
20:c05050f78d81 21:453278a81371
169 break 169 break
170 if nextentry[-1] != '\n': 170 if nextentry[-1] != '\n':
171 break 171 break
172 entlist.append(self.logtoDict(nextentry)) 172 entlist.append(self.logtoDict(nextentry))
173 return 173 return
174 # TODO how does this work with postprocess?
175 def loadnew(self): 174 def loadnew(self):
176 conn = getconn() 175 conn = getconn()
177 if conn == None: 176 if conn == None:
178 return [] 177 return []
179 cur = conn.cursor() 178 cur = conn.cursor()
237 cur = conn.cursor() 236 cur = conn.cursor()
238 cur.executemany(self.insertq, [ d for d in dictlist if d["game"] == self ]) 237 cur.executemany(self.insertq, [ d for d in dictlist if d["game"] == self ])
239 conn.commit() 238 conn.commit()
240 cur.close() 239 cur.close()
241 return 240 return
241 def tablerecent(self, of):
242 "Prints the most recent games from the logfile, NOT the database."
243 newest = []
244 try:
245 scr = open(self.scores)
246 except IOError:
247 pass
248 else:
249 try:
250 scr.seek(self.lookback, 2)
251 except IOError:
252 scr.seek(0) # The file wasn't that long, start at the beginning
253 if scr.tell() != 0:
254 scr.readline() # Throw away the incomplete line
255 self.getEntryDicts(scr, newest)
256 newest.reverse()
257 scr.close()
258 of.write(secthead.format(self.name))
259 if not newest:
260 of.write("<div>No one has braved this dungeon yet.</div>\n")
261 else:
262 printTable(newest, self.fields, of)
263 return
242 # End Game class definition 264 # End Game class definition
243 265
244 class RogueGame(Game): 266 class RogueGame(Game):
245 def __init__(self, name, uname, suffix): 267 def __init__(self, name, uname, suffix):
246 self.name = name 268 self.name = name
249 self.logspec = ["endt", "score", "name", "xl", "fate"] 271 self.logspec = ["endt", "score", "name", "xl", "fate"]
250 self.sqltypes = {"endt": "timestamptz", "score": "int", 272 self.sqltypes = {"endt": "timestamptz", "score": "int",
251 "name": "varchar(20)", "xl": "int", "fate": "text", 273 "name": "varchar(20)", "xl": "int", "fate": "text",
252 "ttyrecs": "text ARRAY", "startt": "timestamptz"} 274 "ttyrecs": "text ARRAY", "startt": "timestamptz"}
253 self.logdelim = " " 275 self.logdelim = " "
276 self.lookback = -1500
254 colspec = "(" 277 colspec = "("
255 valspec = "(" 278 valspec = "("
256 for i, col in enumerate(self.sqltypes.keys()): 279 for i, col in enumerate(self.sqltypes.keys()):
257 colspec += col 280 colspec += col
258 valspec += "%({0})s".format(col) 281 valspec += "%({0})s".format(col)
377 self.sqltypes = {"endt": "timestamptz", "score": "int", 400 self.sqltypes = {"endt": "timestamptz", "score": "int",
378 "name": "varchar(20)", "xl": "int", "class": "text", "depth": "int", 401 "name": "varchar(20)", "xl": "int", "class": "text", "depth": "int",
379 "maxdepth": "int", "quest": "int", "hadquest": "bool", 402 "maxdepth": "int", "quest": "int", "hadquest": "bool",
380 "fate": "text", "ttyrecs": "text ARRAY", "startt": "timestamptz"} 403 "fate": "text", "ttyrecs": "text ARRAY", "startt": "timestamptz"}
381 self.logdelim = " " 404 self.logdelim = " "
405 self.lookback = -1800
382 colspec = "(" 406 colspec = "("
383 valspec = "(" 407 valspec = "("
384 for i, col in enumerate(self.sqltypes.keys()): 408 for i, col in enumerate(self.sqltypes.keys()):
385 colspec += col 409 colspec += col
386 valspec += "%({0})s".format(col) 410 valspec += "%({0})s".format(col)