annotate py/rlgall.py @ 68:67bcca6e3cb1

Prevent crashes if no ttyrec files can be associated with a game. If the postprocessing step finds no ttyrec files created between a game's end and the previous game's end, it will no longer index an empty list and crash. The condition of finding no ttyrec files is still a bug that requires investigation.
author John "Elwin" Edwards
date Sat, 16 Jan 2016 02:12:50 +0000
parents 6542a7cf826b
children 2cca66b3e262
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
25843238434a Change the Python module's name back to rlgall.
John "Elwin" Edwards
parents: 31
diff changeset
1 # rlgall.py
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2 # Module for the Roguelike Gallery, using a postgres database
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
3 # Requires Python 3.3
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4
42
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
5 import re
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 import os
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 import psycopg2
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
8 from datetime import datetime
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
9 import pytz
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
10 import html
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12 # Configuration
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 logdir = "/var/dgl/var/games/roguelike/"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14 webdir = "/var/www/lighttpd/scoring/"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15 ppagename = webdir + "players/{0}.html"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
16 hpagename = webdir + "highscores.html"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
17
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
18 # HTML fragments for templating
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 phead = """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 <html><head>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 <title>{0}</title>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22 <link rel="stylesheet" href="/scoring/scores.css" type="text/css">
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
23 </head>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 """
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
25
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
26 ptop = """<body>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
27 <h1>Yendor Guild</h1>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
28 """
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
29
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
30 navtop = '<div class="nav"><a href="/">rlgallery.org</a> -&gt; {0}</div>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
31 navscore = '<div class="nav"><a href="/">rlgallery.org</a> -&gt; \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
32 <a href="/scoring/">Scores</a> -&gt; {0}</div>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
33 navplayer = '<div class="nav"><a href="/">rlgallery.org</a> -&gt; \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
34 <a href="/scoring/">Scores</a> -&gt; <a href="/scoring/players/">Players</a> \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
35 -&gt; {0}</div>'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
36
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
37 pti = '<h2>{0}</h2>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
39 secthead = '<h3>{0}</h3>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 tblhead = '<div class="stable">\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
41 rowstart = '<div class="sentry">\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
42 rowend = '</div>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
43 cell = ' <span class="sdata">{0}</span>\n'
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
44 rcell = ' <span class="sdatar">{0}</span>\n'
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 hcell = ' <span class="shdata">{0}</span>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
46 tblend = '</div>\n'
34
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
47 pend = """<div class="foot">
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
48 <a href="/">rlgallery.org</a>
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
49 <a href="/recent.cgi">Recent Games</a>
36
09ef92dc4439 Remove the static high score page.
John "Elwin" Edwards
parents: 35
diff changeset
50 <a href="/scoring/high.cgi">High Scores</a>
34
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
51 </div>
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
52 </body></html>
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
53 """
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 # This would be more useful if we had to do translation
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
56 headerbook = {"endt":"End time", "score":"Score", "name":"Name", "xl":"XL",
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
57 "fate":"Fate", "rank":"Rank", "game":"Game", "class":"Class",
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
58 "depth":"Depth"}
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
59 # Queries for the games table
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
60 offselstr = "SELECT offbytes FROM games WHERE gname = %s;"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
61 newoffstr = "UPDATE games SET offbytes = %s WHERE gname = %s;"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
62
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
63 def getconn():
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
64 "Returns a database connection, or None if the connection fails."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
65 try:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
66 conn = psycopg2.connect("dbname=rlg")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
67 except psycopg2.OperationalError:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
68 return None
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
69 return conn
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
70
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
71 def recnameToTS(filename):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
72 pattern = "%Y-%m-%d.%H:%M:%S.ttyrec"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
73 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
74 dt = datetime.strptime(filename, pattern).replace(tzinfo=pytz.utc)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
75 return dt
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
76 except ValueError:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
77 return None
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
78
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
79 def playerlink(name):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
80 "Returns a link to a player's page"
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
81 escname = html.escape(name)
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
82 lstr = '<a href="/scoring/players/' + escname + '.html">' + escname + '</a>'
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
83 return lstr
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
84
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
85 def linktoArchive(entry):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
86 "Takes an entry dict and returns a link to the ttyrec archivist."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
87 lstr = '<a href="/archive.cgi?name={0};game={1};time={2}">{3}</a>'
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
88 linktext = entry["endt"].strftime("%Y/%m/%d %H:%M:%S")
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
89 stamp = int(entry["endt"].timestamp())
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
90 escname = html.escape(entry["name"])
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
91 return lstr.format(escname, entry["game"].uname, stamp, linktext)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
92
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
93 def maketablerow(cells, isheader=None):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
94 "Takes a list of strings and returns a HTML table row with each string \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
95 in its own cell. isheader will make them header cells, obviously."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
96 if isheader:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
97 celler = hcell
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
98 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
99 celler = cell
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 rowstr = rowstart
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 for entry in cells:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 rowstr = rowstr + celler.format(entry)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 rowstr = rowstr + rowend
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 return rowstr
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 def printTable(entries, fields, of):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107 "Takes a list of entry dicts and a list of field strings and writes a \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 HTML table to of."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
109 of.write(tblhead)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
110 clist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
111 for field in fields:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112 if field in headerbook:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
113 clist.append(headerbook[field])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 clist.append("{0}".format(field))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
116 of.write(maketablerow(clist, True))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 rnum = 0
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
118 for entry in entries:
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 clist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
120 for field in fields:
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
121 if field in entry:
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 thing = entry[field]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
123 if field == "game":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
124 clist.append((thing.name, cell))
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
125 elif field == "xl" or field == "score" or field == "rank": # Numerics
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
126 clist.append((str(thing), rcell))
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
127 elif field == "depth" or field == "maxdepth":
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
128 clist.append((str(thing), cell))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
129 elif field == "name":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
130 clist.append((playerlink(thing), cell))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
131 elif field == "fate":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
132 clist.append((thing, cell))
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
133 elif field == "endt":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
134 clist.append((linktoArchive(entry), cell))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
135 else:
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
136 clist.append(("{0}".format(thing), cell))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 else:
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
138 clist.append(("N/A", cell))
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
139 rowstr = rowstart + "".join([ t.format(s) for (s, t) in clist ]) + rowend
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
140 of.write(rowstr)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 of.write(tblend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 return
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 class Game:
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
145 def __init__(self, name, uname):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
146 pass
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 def logtoDict(self, entry):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
148 "Processes a log entry string, returning a dict."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
149 ndict = {"game": self}
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 entrylist = entry.strip().split(self.logdelim, len(self.logspec) - 1)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
151 for item, value in zip(self.logspec, entrylist):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152 if self.sqltypes[item] == "int":
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
153 ndict[item] = int(value)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
154 if self.sqltypes[item] == "bool":
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
155 ndict[item] = bool(int(value))
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
156 elif self.sqltypes[item] == "timestamptz":
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
157 ndict[item] = datetime.fromtimestamp(int(value), pytz.utc)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
158 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
159 ndict[item] = value
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
160 return ndict
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
161 def getEntryDicts(self, entfile, entlist):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
162 "Reads logfile entries from entfile, interprets them according to the \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
163 instructions in self.logspec/logdelim, and adds them as dicts to entlist."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
164 while True:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
165 nextentry = entfile.readline()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
166 if not nextentry:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
167 break
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
168 if nextentry[-1] != '\n':
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
169 break
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
170 entlist.append(self.logtoDict(nextentry))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
171 return
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
172 def loadnew(self):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
173 conn = getconn()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
174 if conn == None:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
175 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
176 cur = conn.cursor()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
177 # Get the previous offset
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
178 cur.execute(offselstr, [self.uname])
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
179 offset = cur.fetchone()[0]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
180 newlist = []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
181 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
182 scr = open(self.scores, encoding="utf-8")
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
183 scr.seek(offset)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
184 self.getEntryDicts(scr, newlist)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
185 except IOError:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
186 noffset = offset # Can't read anything, assume no new games
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
187 else:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
188 noffset = scr.tell()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
189 scr.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
190 cur.execute(newoffstr, [noffset, self.uname])
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
191 # The new players must already be added to the players table.
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
192 updatenames = set([ e["name"] for e in newlist ])
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
193 self.postprocess(newlist)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
194 self.putIntoDB(newlist, conn)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
195 cur.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
196 conn.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
197 return updatenames
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
198 def postprocess(self, gamelist):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
199 "Default postprocessing function: adds start time and ttyrec list"
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
200 names = set([ e["name"] for e in gamelist ])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
201 conn = getconn()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
202 if conn == None:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
203 return []
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
204 cur = conn.cursor()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
205 for nameF in names:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
206 # Get all this player's games ordered by time
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
207 itsEntries = [ entry for entry in gamelist if entry["name"] == nameF ]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
208 itsEntries.sort(key=lambda e: e["endt"])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
209 # Find the end time of the latest game already in the db
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
210 tquery = "SELECT endt FROM {0} WHERE name = %s ORDER BY endt DESC LIMIT 1;".format(self.uname)
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
211 cur.execute(tquery, [nameF])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
212 result = cur.fetchone()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
213 if result:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
214 prev = result[0]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
215 else:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
216 prev = datetime.fromtimestamp(0, pytz.utc);
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
217 ttyrecdir = "/var/dgl/dgldir/ttyrec/{0}/{1}/".format(nameF, self.uname)
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
218 allfilekeys = [ (recnameToTS(f), f) for f in os.listdir(ttyrecdir) ]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
219 vfilekeys = [ e for e in allfilekeys if e[0] > prev ]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
220 vfilekeys.sort(key=lambda e: e[0])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
221 # Now determine startt and ttyrecs for each game
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
222 for i in range(0, len(itsEntries)):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
223 if i == 0:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
224 lowlim = prev
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
225 else:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
226 lowlim = itsEntries[i-1]["endt"]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
227 hilim = itsEntries[i]["endt"]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
228 recs = [ k[1] for k in vfilekeys if lowlim <= k[0] < hilim ]
68
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
229 if len(recs) == 0:
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
230 # There inexplicably are no files. TODO log an error.
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
231 itsEntries[i]["startt"] = lowlim
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
232 itsEntries[i]["ttyrecs"] = []
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
233 else:
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
234 itsEntries[i]["startt"] = recnameToTS(recs[0])
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
235 itsEntries[i]["ttyrecs"] = recs
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
236 cur.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
237 conn.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
238 def putIntoDB(self, dictlist, conn):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
239 cur = conn.cursor()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
240 cur.executemany(self.insertq, [ d for d in dictlist if d["game"] == self ])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
241 conn.commit()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
242 cur.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
243 return
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
244 def tablerecent(self, of):
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
245 "Prints the most recent games from the logfile, NOT the database."
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
246 newest = []
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
247 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
248 scr = open(self.scores, encoding="utf-8")
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
249 except FileNotFoundError:
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
250 pass
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
251 else:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
252 # Text streams don't support random seeking.
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
253 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
254 scr.buffer.seek(self.lookback, 2)
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
255 except OSError:
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
256 scr.buffer.seek(0) # The file wasn't that long, start at the beginning
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
257 if scr.buffer.tell() != 0:
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
258 scr.buffer.readline() # Throw away the incomplete line
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
259 self.getEntryDicts(scr, newest)
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
260 newest.reverse()
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
261 scr.close()
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
262 of.write(secthead.format(self.name))
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
263 if not newest:
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
264 of.write("<div>No one has braved this dungeon yet.</div>\n")
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
265 else:
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
266 printTable(newest, self.fields, of)
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
267 return
50
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
268 def getHigh(self, n=10, offset=0, inittime=None, finaltime=None):
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
269 "Gets the n highest scores (starting at offset) from the database, \
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
270 returning a list of dicts."
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
271 qfields = []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
272 for f in self.rankfields:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
273 if f == "rank":
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
274 qfields.append("rank(*) OVER (ORDER BY score DESC)")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
275 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
276 qfields.append(f)
50
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
277 qstr = "SELECT " + ", ".join(qfields) + " FROM {0}".format(self.uname)
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
278 qvals = []
50
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
279 if isinstance(inittime, datetime):
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
280 qstr += " WHERE endt >= %s"
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
281 qvals.append(inittime)
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
282 if isinstance(finaltime, datetime):
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
283 if finaltime < inittime:
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
284 return []
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
285 qstr += " AND endt < %s"
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
286 qvals.append(finaltime)
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
287 elif isinstance(finaltime, datetime):
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
288 qstr += " WHERE endt < %s"
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
289 qvals.append(finaltime)
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
290 try:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
291 n = int(n)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
292 except (ValueError, TypeError):
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
293 return []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
294 if n <= 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
295 return []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
296 qstr += " LIMIT %s"
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
297 qvals.append(n)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
298 try:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
299 offset = int(offset)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
300 except (ValueError, TypeError):
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
301 return []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
302 if offset > 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
303 qstr += " OFFSET %s"
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
304 qvals.append(offset)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
305 qstr += ";"
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
306 conn = psycopg2.connect("dbname=rlg")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
307 cur = conn.cursor()
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
308 cur.execute(qstr, qvals)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
309 cols = [ col.name for col in cur.description ]
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
310 data = [ dict(zip(cols, row)) for row in cur.fetchall() ]
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
311 cur.close()
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
312 conn.close()
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
313 for d in data:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
314 d["game"] = self
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
315 return data
45
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
316 def getXLCounts(self, nmax=15):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
317 "Returns a list of (xlevel, gamecount) pairs."
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
318 lquery = "SELECT count(*) FROM {0} WHERE xl = %s;".format(self.uname)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
319 mquery = "SELECT count(*) FROM {0} WHERE xl >= %s;".format(self.uname)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
320 try:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
321 nmax = int(nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
322 except (ValueError, TypeError):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
323 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
324 if nmax <= 0:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
325 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
326 xlevels = range(1, nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
327 results = []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
328 conn = psycopg2.connect("dbname=rlg")
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
329 cur = conn.cursor()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
330 for xl in xlevels:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
331 cur.execute(lquery, [xl])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
332 results.append((xl, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
333 cur.execute(mquery, [nmax])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
334 results.append((nmax, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
335 cur.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
336 conn.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
337 return results
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
338 def getDepthCounts(self, nmax=30):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
339 "Returns a list of (maxdepth, gamecount) pairs."
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
340 dqry = "SELECT count(*) FROM {0} WHERE maxdepth = %s;".format(self.uname)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
341 mqry = "SELECT count(*) FROM {0} WHERE maxdepth >= %s;".format(self.uname)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
342 try:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
343 nmax = int(nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
344 except (ValueError, TypeError):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
345 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
346 if nmax <= 0:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
347 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
348 depths = range(1, nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
349 results = []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
350 conn = psycopg2.connect("dbname=rlg")
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
351 cur = conn.cursor()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
352 for lev in depths:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
353 cur.execute(dqry, [lev])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
354 results.append((lev, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
355 cur.execute(mqry, [nmax])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
356 results.append((nmax, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
357 cur.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
358 conn.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
359 return results
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
360 def getScoreCounts(self, blocks=10, size=1000):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
361 "Returns a list of (minscore, gamecount) pairs."
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
362 sqry = "SELECT count(*) FROM {0} WHERE score >= %s AND score < %s;".format(self.uname)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
363 mqry = "SELECT count(*) FROM {0} WHERE score >= %s;".format(self.uname)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
364 try:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
365 blocks = int(blocks)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
366 size = int(size)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
367 except (ValueError, TypeError):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
368 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
369 if blocks <= 0 or size <= 0:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
370 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
371 marks = range(blocks - 1)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
372 results = []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
373 conn = psycopg2.connect("dbname=rlg")
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
374 cur = conn.cursor()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
375 for m in marks:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
376 cur.execute(sqry, [m * size, (m + 1) * size])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
377 results.append((m * size, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
378 cur.execute(mqry, [(blocks - 1) * size])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
379 results.append(((blocks - 1) * size, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
380 cur.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
381 conn.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
382 return results
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
383 # End Game class definition
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
384
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
385 class RogueGame(Game):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
386 def __init__(self, name, uname, suffix):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
387 self.name = name
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
388 self.uname = uname
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
389 self.scores = logdir + uname + ".log"
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
390 self.logspec = ["endt", "score", "name", "xl", "fate"]
42
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
391 self.sqltypes = {"endt": "timestamptz", "score": "int", "name": "text",
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
392 "xl": "int", "fate": "text", "ttyrecs": "text ARRAY",
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
393 "startt": "timestamptz", "depth": "int", "maxdepth": "int"}
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
394 self.logdelim = " "
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
395 self.lookback = -1500
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
396 # Construct the insert query
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
397 fields = self.sqltypes.keys()
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
398 colspec = ", ".join(fields)
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
399 valspec = ", ".join([ "%({})s".format(c) for c in fields ])
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
400 self.insertq = "INSERT INTO {0} ({1}) VALUES ({2});".format(self.uname,
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
401 colspec, valspec)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
402 # Class variables, used by some methods
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
403 fields = ["name", "score", "xl", "fate", "endt"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
404 rankfields = ["rank", "score", "name", "xl", "fate", "endt"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
405 pfields = ["score", "xl", "fate", "endt"]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
406 def getRecent(self, n=20):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
407 "Gets the n most recent games out of the database, returning a list \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
408 of dicts."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
409 try:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
410 n = int(n)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
411 except (ValueError, TypeError):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
412 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
413 if n <= 0:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
414 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
415 dictlist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
416 conn = psycopg2.connect("dbname=rlg")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
417 cur = conn.cursor()
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
418 qstr = "SELECT endt, score, name, xl, fate, startt FROM {0} ORDER BY endt DESC \
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
419 LIMIT %s;".format(self.uname)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
420 cur.execute(qstr, [n])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
421 for record in cur:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
422 # This should be less hardcodish
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
423 ndict = {"game": self}
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
424 ndict["endt"] = record[0]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
425 ndict["score"] = record[1]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
426 ndict["name"] = record[2]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
427 ndict["xl"] = record[3]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
428 ndict["fate"] = record[4]
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
429 ndict["startt"] = record[5]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
430 dictlist.append(ndict)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
431 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
432 conn.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
433 return dictlist
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
434 def getPlayer(self, player):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
435 "Gets all player's games from the database."
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
436 qstr = "SELECT endt, score, xl, fate, startt FROM " + self.uname + " WHERE name = %s;"
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
437 conn = getconn()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
438 if conn == None:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
439 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
440 cur = conn.cursor()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
441 entries = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
442 cur.execute(qstr, [player])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
443 for record in cur:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
444 ndict = {"game": self}
66
6542a7cf826b Fix links from player pages to the ttyrec archive.
John "Elwin" Edwards
parents: 64
diff changeset
445 ndict["name"] = player
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
446 ndict["endt"] = record[0]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
447 ndict["score"] = record[1]
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
448 ndict["xl"] = record[2]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
449 ndict["fate"] = record[3]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
450 ndict["startt"] = record[4]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
451 entries.append(ndict)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
452 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
453 conn.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
454 return entries
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
455 def putIntoDB(self, dictlist, conn):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
456 "Add the entries in dictlist to the database through connection conn, \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
457 which needs the INSERT privilege."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
458 cur = conn.cursor()
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
459 cur.executemany(self.insertq, [ d for d in dictlist if d["game"] == self ])
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
460 conn.commit()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
461 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
462 return
42
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
463 def postprocess(self, gamelist):
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
464 lre = re.compile("^(quit|killed by (.*)) on level ([0-9]*)( \\[max ([0-9]*)\\] with the Amulet)?$")
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
465 wre = re.compile("^escaped with the Amulet \\[deepest level: ([0-9]*)\\]$")
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
466 for d in gamelist:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
467 m = lre.match(d["fate"])
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
468 if m:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
469 d["depth"] = int(m.group(3))
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
470 if m.group(4):
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
471 d["maxdepth"] = int(m.group(5))
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
472 else:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
473 d["maxdepth"] = d["depth"]
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
474 else:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
475 m = wre.match(d["fate"])
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
476 if m:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
477 d["depth"] = 0
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
478 d["maxdepth"] = int(m.group(1))
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
479 else:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
480 # Something went wrong
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
481 d["depth"] = -1
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
482 d["maxdepth"] = -1
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
483 Game.postprocess(self, gamelist)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
484 # End RogueGame class definition
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
485
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
486 class ARogueGame(Game):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
487 def __init__(self, name, uname, suffix):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
488 self.name = name
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
489 self.uname = uname
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
490 self.scores = logdir + uname + ".log"
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
491 self.logspec = ["endt", "score", "name", "xl", "class", "depth",
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
492 "maxdepth", "quest", "hadquest", "fate"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
493 self.sqltypes = {"endt": "timestamptz", "score": "int",
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
494 "name": "varchar(20)", "xl": "int", "class": "text", "depth": "int",
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
495 "maxdepth": "int", "quest": "int", "hadquest": "bool",
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
496 "fate": "text", "ttyrecs": "text ARRAY", "startt": "timestamptz"}
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
497 self.logdelim = " "
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
498 self.lookback = -1800
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
499 # Construct the insert query
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
500 fields = self.sqltypes.keys()
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
501 colspec = ", ".join(fields)
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
502 valspec = ", ".join([ "%({})s".format(c) for c in fields ])
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
503 self.insertq = "INSERT INTO {0} ({1}) VALUES ({2});".format(self.uname,
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
504 colspec, valspec)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
505 # Class variables
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
506 fields = ["name", "score", "class", "xl", "fate", "depth", "endt"]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
507 rankfields = ["rank", "score", "name", "class", "xl", "fate", "depth", "endt"]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
508 pfields = ["score", "class", "xl", "fate", "depth", "endt"]
64
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
509 def postprocess(self, gamelist):
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
510 "Enforces consistent capitalization of the class title."
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
511 for d in gamelist:
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
512 d["class"] = d["class"].capitalize()
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
513 Game.postprocess(self, gamelist)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
514 def getRecent(self, n=20):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
515 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
516 def getPlayer(self, player):
20
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
517 "Gets all player's games from the database."
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
518 qstr = "SELECT endt, score, xl, class, fate, depth FROM " + self.uname + " WHERE name = %s;"
20
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
519 conn = getconn()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
520 if conn == None:
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
521 return []
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
522 cur = conn.cursor()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
523 entries = []
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
524 cur.execute(qstr, [player])
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
525 for record in cur:
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
526 ndict = {"game": self}
66
6542a7cf826b Fix links from player pages to the ttyrec archive.
John "Elwin" Edwards
parents: 64
diff changeset
527 ndict["name"] = player
20
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
528 ndict["endt"] = record[0]
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
529 ndict["score"] = record[1]
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
530 ndict["xl"] = record[2]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
531 ndict["class"] = record[3]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
532 ndict["fate"] = record[4]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
533 ndict["depth"] = record[5]
20
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
534 entries.append(ndict)
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
535 cur.close()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
536 conn.close()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
537 return entries
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
538
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
539 rogue3 = RogueGame("Rogue V3", "rogue3", "r3")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
540 rogue4 = RogueGame("Rogue V4", "rogue4", "r4")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
541 rogue5 = RogueGame("Rogue V5", "rogue5", "r5")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
542 srogue = RogueGame("Super-Rogue", "srogue", "sr")
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
543 arogue5 = ARogueGame("Advanced Rogue 5", "arogue5", "ar5")
63
ad103f9f2e3a Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 62
diff changeset
544 arogue7 = ARogueGame("Advanced Rogue 7", "arogue7", "ar7")
ad103f9f2e3a Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 62
diff changeset
545 xrogue = ARogueGame("XRogue", "xrogue", "xr")
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
546
63
ad103f9f2e3a Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 62
diff changeset
547 gamelist = [rogue3, rogue4, rogue5, srogue, arogue5, arogue7, xrogue]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
548
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
549 def playerpage(pname):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
550 "Generate a player's HTML page"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
551 # Write the beginning of the page
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
552 ppagefi = open(ppagename.format(pname), "w", encoding="utf-8")
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
553 cleanpname = html.escape(pname)
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
554 ppagefi.write(phead.format(cleanpname))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
555 ppagefi.write(ptop)
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
556 ppagefi.write(navplayer.format(cleanpname))
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
557 ppagefi.write(pti.format("Results for " + cleanpname))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
558 for game in gamelist:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
559 ppagefi.write(secthead.format(game.name))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
560 entries = game.getPlayer(pname)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
561 if not entries:
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
562 ppagefi.write("<div>" + cleanpname + " has not yet completed an " +
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
563 "expedition in this dungeon.</div>\n")
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
564 else:
19
78580bffc13d Add high score list support for arogue5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
565 entries.sort(key=lambda e: e["endt"])
20
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
566 printTable(entries, game.pfields, ppagefi)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
567 scoresum = 0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
568 for entry in entries:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
569 scoresum += int(entry["score"])
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
570 avgscr = scoresum // len(entries)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
571 ppagefi.write("<p>Average score: {0}</p>\n".format(avgscr))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
572 # That should settle it. Print the end; then stop.
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
573 ppagefi.write(pend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
574 ppagefi.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
575 return
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
576
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
577 def highpage():
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
578 # open the page and print the beginning
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
579 highpfi = open(hpagename, "w", encoding="utf-8")
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
580 highpfi.write(phead.format("High Scores"))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
581 highpfi.write(ptop)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
582 highpfi.write(navscore.format("High Scores"))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
583 highpfi.write(pti.format("List of High Scores"))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
584 for game in gamelist:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
585 highpfi.write(secthead.format(game.name))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
586 entries = game.getHigh(40)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
587 if not entries:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
588 highpfi.write("<div>No one has braved this dungeon yet.</div>\n")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
589 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
590 printTable(entries, game.rankfields, highpfi)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
591 # That should finish it.
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
592 highpfi.write(pend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
593 highpfi.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
594 return