annotate py/rlgall.py @ 87:1bddd1839831

Update the dgamelaunch patch to work with the most recent changes. Also set the umask, just in case it defaults to 0077.
author John "Elwin" Edwards
date Sat, 24 Nov 2018 16:47:24 -0500
parents 2cca66b3e262
children d417016bbf73
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>
81
2cca66b3e262 Add some more links to the page footers.
John "Elwin" Edwards
parents: 68
diff changeset
51 <a href="/notes/">Notes</a>
2cca66b3e262 Add some more links to the page footers.
John "Elwin" Edwards
parents: 68
diff changeset
52 <a href="https://rlgallery.org:8080/">Play</a>
34
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
53 </div>
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
54 </body></html>
86b616d88020 Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents: 33
diff changeset
55 """
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
57 # 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
58 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
59 "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
60 "depth":"Depth"}
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
61 # Queries for the games table
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
62 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
63 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
64
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
65 def getconn():
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
66 "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
67 try:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
68 conn = psycopg2.connect("dbname=rlg")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
69 except psycopg2.OperationalError:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
70 return None
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
71 return conn
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
72
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
73 def recnameToTS(filename):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
74 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
75 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
76 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
77 return dt
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
78 except ValueError:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
79 return None
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
80
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
81 def playerlink(name):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
82 "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
83 escname = html.escape(name)
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
84 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
85 return lstr
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
86
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
87 def linktoArchive(entry):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
88 "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
89 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
90 linktext = entry["endt"].strftime("%Y/%m/%d %H:%M:%S")
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
91 stamp = int(entry["endt"].timestamp())
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
92 escname = html.escape(entry["name"])
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
93 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
94
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
95 def maketablerow(cells, isheader=None):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
96 "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
97 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
98 if isheader:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
99 celler = hcell
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 celler = cell
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 rowstr = rowstart
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 for entry in cells:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 rowstr = rowstr + celler.format(entry)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 rowstr = rowstr + rowend
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 return rowstr
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 def printTable(entries, fields, of):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
109 "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
110 HTML table to of."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
111 of.write(tblhead)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112 clist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
113 for field in fields:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 if field in headerbook:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 clist.append(headerbook[field])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
116 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 clist.append("{0}".format(field))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 of.write(maketablerow(clist, True))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 rnum = 0
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
120 for entry in entries:
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
121 clist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 for field in fields:
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
123 if field in entry:
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
124 thing = entry[field]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
125 if field == "game":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
126 clist.append((thing.name, cell))
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
127 elif field == "xl" or field == "score" or field == "rank": # Numerics
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
128 clist.append((str(thing), rcell))
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
129 elif field == "depth" or field == "maxdepth":
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
130 clist.append((str(thing), cell))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
131 elif field == "name":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
132 clist.append((playerlink(thing), cell))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 elif field == "fate":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
134 clist.append((thing, cell))
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
135 elif field == "endt":
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
136 clist.append((linktoArchive(entry), 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(("{0}".format(thing), cell))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 else:
31
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
140 clist.append(("N/A", cell))
7303535b5a5d Right-align the numeric columns.
John "Elwin" Edwards
parents: 30
diff changeset
141 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
142 of.write(rowstr)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143 of.write(tblend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 return
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 class Game:
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
147 def __init__(self, name, uname):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
148 pass
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
149 def logtoDict(self, entry):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 "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
151 ndict = {"game": self}
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152 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
153 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
154 if self.sqltypes[item] == "int":
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
155 ndict[item] = int(value)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
156 if self.sqltypes[item] == "bool":
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
157 ndict[item] = bool(int(value))
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
158 elif self.sqltypes[item] == "timestamptz":
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
159 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
160 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
161 ndict[item] = value
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
162 return ndict
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
163 def getEntryDicts(self, entfile, entlist):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
164 "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
165 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
166 while True:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
167 nextentry = entfile.readline()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
168 if not nextentry:
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 if nextentry[-1] != '\n':
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
171 break
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
172 entlist.append(self.logtoDict(nextentry))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
173 return
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
174 def loadnew(self):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
175 conn = getconn()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
176 if conn == None:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
177 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
178 cur = conn.cursor()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
179 # Get the previous offset
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
180 cur.execute(offselstr, [self.uname])
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
181 offset = cur.fetchone()[0]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
182 newlist = []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
183 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
184 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
185 scr.seek(offset)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
186 self.getEntryDicts(scr, newlist)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
187 except IOError:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
188 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
189 else:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
190 noffset = scr.tell()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
191 scr.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
192 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
193 # 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
194 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
195 self.postprocess(newlist)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
196 self.putIntoDB(newlist, conn)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
197 cur.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
198 conn.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
199 return updatenames
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
200 def postprocess(self, gamelist):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
201 "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
202 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
203 conn = getconn()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
204 if conn == None:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
205 return []
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
206 cur = conn.cursor()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
207 for nameF in names:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
208 # 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
209 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
210 itsEntries.sort(key=lambda e: e["endt"])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
211 # 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
212 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
213 cur.execute(tquery, [nameF])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
214 result = cur.fetchone()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
215 if result:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
216 prev = result[0]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
217 else:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
218 prev = datetime.fromtimestamp(0, pytz.utc);
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
219 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
220 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
221 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
222 vfilekeys.sort(key=lambda e: e[0])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
223 # 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
224 for i in range(0, len(itsEntries)):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
225 if i == 0:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
226 lowlim = prev
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
227 else:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
228 lowlim = itsEntries[i-1]["endt"]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
229 hilim = itsEntries[i]["endt"]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
230 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
231 if len(recs) == 0:
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
232 # 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
233 itsEntries[i]["startt"] = lowlim
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
234 itsEntries[i]["ttyrecs"] = []
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
235 else:
67bcca6e3cb1 Prevent crashes if no ttyrec files can be associated with a game.
John "Elwin" Edwards
parents: 66
diff changeset
236 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
237 itsEntries[i]["ttyrecs"] = recs
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
238 cur.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
239 conn.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
240 def putIntoDB(self, dictlist, conn):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
241 cur = conn.cursor()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
242 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
243 conn.commit()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
244 cur.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
245 return
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
246 def tablerecent(self, of):
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
247 "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
248 newest = []
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
249 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
250 scr = open(self.scores, encoding="utf-8")
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
251 except FileNotFoundError:
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
252 pass
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
253 else:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
254 # 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
255 try:
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
256 scr.buffer.seek(self.lookback, 2)
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
257 except OSError:
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
258 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
259 if scr.buffer.tell() != 0:
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
260 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
261 self.getEntryDicts(scr, newest)
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
262 newest.reverse()
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
263 scr.close()
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
264 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
265 if not newest:
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
266 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
267 else:
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
268 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
269 return
50
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
270 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
271 "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
272 returning a list of dicts."
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
273 qfields = []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
274 for f in self.rankfields:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
275 if f == "rank":
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
276 qfields.append("rank(*) OVER (ORDER BY score DESC)")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
277 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
278 qfields.append(f)
50
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
279 qstr = "SELECT " + ", ".join(qfields) + " FROM {0}".format(self.uname)
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
280 qvals = []
50
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
281 if isinstance(inittime, datetime):
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
282 qstr += " WHERE endt >= %s"
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
283 qvals.append(inittime)
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
284 if isinstance(finaltime, datetime):
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
285 if finaltime < inittime:
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
286 return []
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
287 qstr += " AND endt < %s"
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
288 qvals.append(finaltime)
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
289 elif isinstance(finaltime, datetime):
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
290 qstr += " WHERE endt < %s"
4549b3c0cd28 rlgall.py: add time selection to Game.getHigh().
John "Elwin" Edwards
parents: 49
diff changeset
291 qvals.append(finaltime)
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
292 try:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
293 n = int(n)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
294 except (ValueError, TypeError):
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 if n <= 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
297 return []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
298 qstr += " LIMIT %s"
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
299 qvals.append(n)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
300 try:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
301 offset = int(offset)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
302 except (ValueError, TypeError):
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
303 return []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
304 if offset > 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
305 qstr += " OFFSET %s"
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
306 qvals.append(offset)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
307 qstr += ";"
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
308 conn = psycopg2.connect("dbname=rlg")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
309 cur = conn.cursor()
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
310 cur.execute(qstr, qvals)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
311 cols = [ col.name for col in cur.description ]
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
312 data = [ dict(zip(cols, row)) for row in cur.fetchall() ]
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
313 cur.close()
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
314 conn.close()
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
315 for d in data:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
316 d["game"] = self
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents: 34
diff changeset
317 return data
45
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
318 def getXLCounts(self, nmax=15):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
319 "Returns a list of (xlevel, gamecount) pairs."
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
320 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
321 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
322 try:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
323 nmax = int(nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
324 except (ValueError, TypeError):
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 if nmax <= 0:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
327 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
328 xlevels = range(1, nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
329 results = []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
330 conn = psycopg2.connect("dbname=rlg")
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
331 cur = conn.cursor()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
332 for xl in xlevels:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
333 cur.execute(lquery, [xl])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
334 results.append((xl, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
335 cur.execute(mquery, [nmax])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
336 results.append((nmax, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
337 cur.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
338 conn.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
339 return results
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
340 def getDepthCounts(self, nmax=30):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
341 "Returns a list of (maxdepth, gamecount) pairs."
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
342 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
343 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
344 try:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
345 nmax = int(nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
346 except (ValueError, TypeError):
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 if nmax <= 0:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
349 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
350 depths = range(1, nmax)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
351 results = []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
352 conn = psycopg2.connect("dbname=rlg")
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
353 cur = conn.cursor()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
354 for lev in depths:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
355 cur.execute(dqry, [lev])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
356 results.append((lev, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
357 cur.execute(mqry, [nmax])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
358 results.append((nmax, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
359 cur.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
360 conn.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
361 return results
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
362 def getScoreCounts(self, blocks=10, size=1000):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
363 "Returns a list of (minscore, gamecount) pairs."
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
364 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
365 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
366 try:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
367 blocks = int(blocks)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
368 size = int(size)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
369 except (ValueError, TypeError):
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 if blocks <= 0 or size <= 0:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
372 return []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
373 marks = range(blocks - 1)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
374 results = []
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
375 conn = psycopg2.connect("dbname=rlg")
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
376 cur = conn.cursor()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
377 for m in marks:
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
378 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
379 results.append((m * size, cur.fetchone()[0]))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
380 cur.execute(mqry, [(blocks - 1) * size])
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
381 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
382 cur.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
383 conn.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 42
diff changeset
384 return results
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
385 # End Game class definition
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
386
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
387 class RogueGame(Game):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
388 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
389 self.name = name
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
390 self.uname = uname
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
391 self.scores = logdir + uname + ".log"
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
392 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
393 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
394 "xl": "int", "fate": "text", "ttyrecs": "text ARRAY",
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
395 "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
396 self.logdelim = " "
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
397 self.lookback = -1500
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
398 # Construct the insert query
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
399 fields = self.sqltypes.keys()
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
400 colspec = ", ".join(fields)
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
401 valspec = ", ".join([ "%({})s".format(c) for c in fields ])
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
402 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
403 colspec, valspec)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
404 # 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
405 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
406 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
407 pfields = ["score", "xl", "fate", "endt"]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
408 def getRecent(self, n=20):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
409 "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
410 of dicts."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
411 try:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
412 n = int(n)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
413 except (ValueError, TypeError):
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 if n <= 0:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
416 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
417 dictlist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
418 conn = psycopg2.connect("dbname=rlg")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
419 cur = conn.cursor()
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
420 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
421 LIMIT %s;".format(self.uname)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
422 cur.execute(qstr, [n])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
423 for record in cur:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
424 # This should be less hardcodish
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
425 ndict = {"game": self}
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
426 ndict["endt"] = record[0]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
427 ndict["score"] = record[1]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
428 ndict["name"] = record[2]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
429 ndict["xl"] = record[3]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
430 ndict["fate"] = record[4]
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
431 ndict["startt"] = record[5]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
432 dictlist.append(ndict)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
433 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
434 conn.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
435 return dictlist
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
436 def getPlayer(self, player):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
437 "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
438 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
439 conn = getconn()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
440 if conn == None:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
441 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
442 cur = conn.cursor()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
443 entries = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
444 cur.execute(qstr, [player])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
445 for record in cur:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
446 ndict = {"game": self}
66
6542a7cf826b Fix links from player pages to the ttyrec archive.
John "Elwin" Edwards
parents: 64
diff changeset
447 ndict["name"] = player
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
448 ndict["endt"] = record[0]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
449 ndict["score"] = record[1]
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
450 ndict["xl"] = record[2]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
451 ndict["fate"] = record[3]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
452 ndict["startt"] = record[4]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
453 entries.append(ndict)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
454 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
455 conn.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
456 return entries
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
457 def putIntoDB(self, dictlist, conn):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
458 "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
459 which needs the INSERT privilege."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
460 cur = conn.cursor()
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
461 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
462 conn.commit()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
463 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
464 return
42
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
465 def postprocess(self, gamelist):
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
466 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
467 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
468 for d in gamelist:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
469 m = lre.match(d["fate"])
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
470 if m:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
471 d["depth"] = int(m.group(3))
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
472 if m.group(4):
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
473 d["maxdepth"] = int(m.group(5))
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 d["maxdepth"] = d["depth"]
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
476 else:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
477 m = wre.match(d["fate"])
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
478 if m:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
479 d["depth"] = 0
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
480 d["maxdepth"] = int(m.group(1))
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
481 else:
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
482 # Something went wrong
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
483 d["depth"] = -1
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
484 d["maxdepth"] = -1
e1de8aeb5ed4 Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents: 41
diff changeset
485 Game.postprocess(self, gamelist)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
486 # End RogueGame class definition
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
487
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
488 class ARogueGame(Game):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
489 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
490 self.name = name
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
491 self.uname = uname
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
492 self.scores = logdir + uname + ".log"
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
493 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
494 "maxdepth", "quest", "hadquest", "fate"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
495 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
496 "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
497 "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
498 "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
499 self.logdelim = " "
21
453278a81371 Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
500 self.lookback = -1800
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
501 # Construct the insert query
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
502 fields = self.sqltypes.keys()
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
503 colspec = ", ".join(fields)
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
504 valspec = ", ".join([ "%({})s".format(c) for c in fields ])
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
505 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
506 colspec, valspec)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
507 # Class variables
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
508 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
509 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
510 pfields = ["score", "class", "xl", "fate", "depth", "endt"]
64
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
511 def postprocess(self, gamelist):
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
512 "Enforces consistent capitalization of the class title."
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
513 for d in gamelist:
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
514 d["class"] = d["class"].capitalize()
86ae333bb4c0 Capitalize the class field in database entries.
John "Elwin" Edwards
parents: 63
diff changeset
515 Game.postprocess(self, gamelist)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
516 def getRecent(self, n=20):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
517 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
518 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
519 "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
520 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
521 conn = getconn()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
522 if conn == None:
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
523 return []
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
524 cur = conn.cursor()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
525 entries = []
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
526 cur.execute(qstr, [player])
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
527 for record in cur:
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
528 ndict = {"game": self}
66
6542a7cf826b Fix links from player pages to the ttyrec archive.
John "Elwin" Edwards
parents: 64
diff changeset
529 ndict["name"] = player
20
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
530 ndict["endt"] = record[0]
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
531 ndict["score"] = record[1]
62
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
532 ndict["xl"] = record[2]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
533 ndict["class"] = record[3]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
534 ndict["fate"] = record[4]
df7acc5653b3 Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents: 50
diff changeset
535 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
536 entries.append(ndict)
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
537 cur.close()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
538 conn.close()
c05050f78d81 Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
539 return entries
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
540
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
541 rogue3 = RogueGame("Rogue V3", "rogue3", "r3")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
542 rogue4 = RogueGame("Rogue V4", "rogue4", "r4")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
543 rogue5 = RogueGame("Rogue V5", "rogue5", "r5")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
544 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
545 arogue5 = ARogueGame("Advanced Rogue 5", "arogue5", "ar5")
63
ad103f9f2e3a Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 62
diff changeset
546 arogue7 = ARogueGame("Advanced Rogue 7", "arogue7", "ar7")
ad103f9f2e3a Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 62
diff changeset
547 xrogue = ARogueGame("XRogue", "xrogue", "xr")
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
548
63
ad103f9f2e3a Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 62
diff changeset
549 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
550
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
551 def playerpage(pname):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
552 "Generate a player's HTML page"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
553 # Write the beginning of the page
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
554 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
555 cleanpname = html.escape(pname)
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
556 ppagefi.write(phead.format(cleanpname))
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
557 ppagefi.write(ptop)
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
558 ppagefi.write(navplayer.format(cleanpname))
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
559 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
560 for game in gamelist:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
561 ppagefi.write(secthead.format(game.name))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
562 entries = game.getPlayer(pname)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
563 if not entries:
49
6138c27d1950 Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents: 45
diff changeset
564 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
565 "expedition in this dungeon.</div>\n")
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
566 else:
19
78580bffc13d Add high score list support for arogue5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
567 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
568 printTable(entries, game.pfields, ppagefi)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
569 scoresum = 0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
570 for entry in entries:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
571 scoresum += int(entry["score"])
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
572 avgscr = scoresum // len(entries)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
573 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
574 # 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
575 ppagefi.write(pend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
576 ppagefi.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
577 return
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
578
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
579 def highpage():
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
580 # open the page and print the beginning
30
e8f3b7994d88 Port to Python 3.
John "Elwin" Edwards
parents: 21
diff changeset
581 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
582 highpfi.write(phead.format("High Scores"))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
583 highpfi.write(ptop)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
584 highpfi.write(navscore.format("High Scores"))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
585 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
586 for game in gamelist:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
587 highpfi.write(secthead.format(game.name))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
588 entries = game.getHigh(40)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
589 if not entries:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
590 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
591 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
592 printTable(entries, game.rankfields, highpfi)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
593 # That should finish it.
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
594 highpfi.write(pend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
595 highpfi.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
596 return