annotate py/rlgalldb.py @ 18:5731d2ecaec4

Store arogue5 results in the database. The ARogueGame class is functional enough to put game results into the database, though it still can't get them back out.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 17 Sep 2012 09:14:26 -0700
parents 7f7b3da664d6
children 78580bffc13d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 # rlgalldb.py
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
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 import os
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 import calendar
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 import psycopg2
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 from datetime import datetime, tzinfo, timedelta
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 # Configuration
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
10 logdir = "/var/dgl/var/games/roguelike/"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11 webdir = "/var/www/lighttpd/scoring/"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12 ppagename = webdir + "players/{0}.html"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 hpagename = webdir + "highscores.html"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15 # HTML fragments for templating
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
16 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
17 <html><head>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
18 <title>{0}</title>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 <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
20 </head>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 """
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
23 ptop = """<body>
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 <h1>Yendor Guild</h1>
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
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
27 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
28 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
29 <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
30 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
31 <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
32 -&gt; {0}</div>'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
33
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
34 pti = '<h2>{0}</h2>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
35
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
36 secthead = '<h3>{0}</h3>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
37 tblhead = '<div class="stable">\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38 rowstart = '<div class="sentry">\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
39 rowend = '</div>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 cell = ' <span class="sdata">{0}</span>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
41 hcell = ' <span class="shdata">{0}</span>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
42 tblend = '</div>\n'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
43 pend = "</body></html>\n"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
44
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 # 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
46 headerbook = {"endt":"End time", "score":"Score", "name":"Name", "xl":"XL",
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
47 "fate":"Fate", "rank":"Rank", "game":"Game"}
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
48 # Queries for the games table
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
49 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
50 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
51
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
52 # A representation of the UTC time zone. They say Py3k can better handle
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
53 # this madness.
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54 class UTC(tzinfo):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 def utcoffset(self, dt):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56 return timedelta(0)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
57 def dst(self, dt):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
58 return timedelta(0)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
59 def tzname(self, dt):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
60 return "UTC"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
61 utc = UTC()
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:
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
74 dt = datetime.strptime(filename, pattern).replace(tzinfo=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 ttyreclink(text, name, game, gtime):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
80 "Returns a link to the ttyrec archivist"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
81 lstr = '<a href="/archive.cgi?name={0};game={1};time={2}">{3}</a>'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
82 return lstr.format(name, game, gtime, text)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
83
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
84 def playerlink(name):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
85 "Returns a link to a player's page"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
86 lstr = '<a href="/scoring/players/' + name + '.html">' + name + '</a>'
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
87 return lstr
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
88
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
89 def linktoArchive(entry):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
90 "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
91 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
92 linktext = entry["endt"].strftime("%Y/%m/%d %H:%M:%S")
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
93 stamp = calendar.timegm(entry["endt"].utctimetuple())
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
94 return lstr.format(entry["name"], entry["game"].uname, stamp, linktext)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
95
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
96 def maketablerow(cells, isheader=None):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
97 "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
98 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
99 if isheader:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 celler = hcell
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 celler = cell
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 rowstr = rowstart
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 for entry in cells:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 rowstr = rowstr + celler.format(entry)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 rowstr = rowstr + rowend
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107 return rowstr
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
109 def printTable(entries, fields, of):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
110 "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
111 HTML table to of."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112 of.write(tblhead)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
113 clist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 for field in fields:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 if field in headerbook:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
116 clist.append(headerbook[field])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 clist.append("{0}".format(field))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 of.write(maketablerow(clist, True))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
120 rnum = 0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
121 for i, entry in enumerate(entries):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 clist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
123 for field in fields:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
124 if field == "rank":
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
125 clist.append("{0}".format(i + 1))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
126 elif field in entry:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
127 thing = entry[field]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
128 if field == "game":
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
129 clist.append(thing.name)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
130 elif field == "xl" or field == "score": # Numerics
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
131 clist.append(str(thing))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
132 elif field == "name":
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 clist.append(playerlink(thing))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
134 elif field == "fate":
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
135 clist.append(thing)
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
136 elif field == "endt":
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 clist.append(linktoArchive(entry))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
138 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 clist.append("{0}".format(thing))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
140 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 clist.append("N/A")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 of.write(maketablerow(clist))
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":
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
159 ndict[item] = datetime.fromtimestamp(int(value), 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 # TODO how does this work with postprocess?
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
175 def loadnew(self):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
176 conn = getconn()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
177 if conn == None:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
178 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
179 cur = conn.cursor()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
180 # Get the previous offset
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
181 cur.execute(offselstr, [self.uname])
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
182 offset = cur.fetchone()[0]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
183 newlist = []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
184 try:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
185 scr = open(self.scores)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
186 scr.seek(offset)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
187 self.getEntryDicts(scr, newlist)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
188 except IOError:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
189 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
190 else:
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
191 noffset = scr.tell()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
192 scr.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
193 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
194 # 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
195 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
196 self.postprocess(newlist)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
197 self.putIntoDB(newlist, conn)
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
198 cur.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
199 conn.close()
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
200 return updatenames
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
201 def postprocess(self, gamelist):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
202 "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
203 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
204 conn = getconn()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
205 if conn == None:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
206 return []
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
207 cur = conn.cursor()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
208 for nameF in names:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
209 # 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
210 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
211 itsEntries.sort(key=lambda e: e["endt"])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
212 # 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
213 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
214 cur.execute(tquery, [nameF])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
215 result = cur.fetchone()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
216 if result:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
217 prev = result[0]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
218 else:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
219 prev = datetime.fromtimestamp(0, utc);
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
220 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
221 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
222 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
223 vfilekeys.sort(key=lambda e: e[0])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
224 # 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
225 for i in range(0, len(itsEntries)):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
226 if i == 0:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
227 lowlim = prev
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
228 else:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
229 lowlim = itsEntries[i-1]["endt"]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
230 hilim = itsEntries[i]["endt"]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
231 recs = [ k[1] for k in vfilekeys if lowlim <= k[0] < hilim ]
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
232 itsEntries[i]["startt"] = recnameToTS(recs[0])
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
233 itsEntries[i]["ttyrecs"] = recs
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
234 cur.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
235 conn.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
236 def putIntoDB(self, dictlist, conn):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
237 cur = conn.cursor()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
238 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
239 conn.commit()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
240 cur.close()
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
241 return
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
242 # End Game class definition
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
243
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
244 class RogueGame(Game):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
245 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
246 self.name = name
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
247 self.uname = uname
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
248 self.scores = logdir + uname + ".log"
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
249 self.logspec = ["endt", "score", "name", "xl", "fate"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
250 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
251 "name": "varchar(20)", "xl": "int", "fate": "text",
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
252 "ttyrecs": "text ARRAY", "startt": "timestamptz"}
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
253 self.logdelim = " "
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
254 colspec = "("
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
255 valspec = "("
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
256 for i, col in enumerate(self.sqltypes.keys()):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
257 colspec += col
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
258 valspec += "%({0})s".format(col)
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
259 if i == len(self.sqltypes) - 1:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
260 colspec += ")"
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
261 valspec += ")"
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
262 else:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
263 colspec += ", "
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
264 valspec += ", "
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
265 self.insertq = "INSERT INTO {0} {1} VALUES {2};".format(self.uname,
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
266 colspec, valspec)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
267 # 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
268 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
269 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
270 pfields = ["score", "xl", "fate", "endt"]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
271 def getRecent(self, n=20):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
272 "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
273 of dicts."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
274 try:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
275 n = int(n)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
276 except (ValueError, TypeError):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
277 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
278 if n <= 0:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
279 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
280 dictlist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
281 conn = psycopg2.connect("dbname=rlg")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
282 cur = conn.cursor()
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
283 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
284 LIMIT %s;".format(self.uname)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
285 cur.execute(qstr, [n])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
286 for record in cur:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
287 # This should be less hardcodish
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
288 ndict = {"game": self}
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
289 ndict["endt"] = record[0]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
290 ndict["score"] = record[1]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
291 ndict["name"] = record[2]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
292 ndict["xl"] = record[3]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
293 ndict["fate"] = record[4]
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
294 ndict["startt"] = record[5]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
295 dictlist.append(ndict)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
296 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
297 conn.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
298 return dictlist
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
299 def getHigh(self, n=10, offset=0):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
300 "Gets the n highest scores (starting at offset) from the database, \
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
301 returning a list of dicts."
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
302 qstr = "SELECT endt, score, name, xl, fate, startt FROM {0} ORDER BY score DESC ".format(self.uname)
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
303 qvals = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
304 try:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
305 n = int(n)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
306 except (ValueError, TypeError):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
307 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
308 if n <= 0:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
309 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
310 qstr += " LIMIT %s"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
311 qvals.append(n)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
312 try:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
313 offset = int(offset)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
314 except (ValueError, TypeError):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
315 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
316 if n > 0:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
317 qstr += " OFFSET %s"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
318 qvals.append(offset)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
319 qstr += ";"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
320 conn = psycopg2.connect("dbname=rlg")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
321 cur = conn.cursor()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
322 cur.execute(qstr, qvals)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
323 dictlist = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
324 for record in cur:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
325 ndict = {"game": self}
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
326 ndict["endt"] = record[0]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
327 ndict["score"] = record[1]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
328 ndict["name"] = record[2]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
329 ndict["xl"] = record[3]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
330 ndict["fate"] = record[4]
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
331 ndict["startt"] = record[5]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
332 dictlist.append(ndict)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
333 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
334 conn.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
335 return dictlist
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
336 def getPlayer(self, player):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
337 "Gets all player's games from the database."
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
338 qstr = "SELECT endt, score, name, 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
339 conn = getconn()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
340 if conn == None:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
341 return []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
342 cur = conn.cursor()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
343 entries = []
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
344 cur.execute(qstr, [player])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
345 for record in cur:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
346 ndict = {"game": self}
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
347 ndict["endt"] = record[0]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
348 ndict["score"] = record[1]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
349 ndict["name"] = record[2]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
350 ndict["xl"] = record[3]
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
351 ndict["fate"] = record[4]
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
352 ndict["startt"] = record[5]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
353 entries.append(ndict)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
354 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
355 conn.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
356 return entries
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
357 def putIntoDB(self, dictlist, conn):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
358 "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
359 which needs the INSERT privilege."
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
360 # FIXME this monster needs to be procedurally generated
1
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
361 qstr = "INSERT INTO " + self.uname + " (endt, score, name, xl, fate, ttyrecs, startt) \
def7fecbd437 Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
362 VALUES (%(endt)s, %(score)s, %(name)s, %(xl)s, %(fate)s, %(ttyrecs)s, %(startt)s);"
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
363 cur = conn.cursor()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
364 cur.executemany(qstr, [ d for d in dictlist if d["game"] == self ])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
365 conn.commit()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
366 cur.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
367 return
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
368 # End RogueGame class definition
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
369
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
370 class ARogueGame(Game):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
371 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
372 self.name = name
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
373 self.uname = uname
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
374 self.scores = logdir + uname + ".log"
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
375 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
376 "maxdepth", "quest", "hadquest", "fate"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
377 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
378 "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
379 "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
380 "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
381 self.logdelim = " "
18
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
382 colspec = "("
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
383 valspec = "("
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
384 for i, col in enumerate(self.sqltypes.keys()):
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
385 colspec += col
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
386 valspec += "%({0})s".format(col)
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
387 if i == len(self.sqltypes) - 1:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
388 colspec += ")"
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
389 valspec += ")"
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
390 else:
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
391 colspec += ", "
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
392 valspec += ", "
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
393 self.insertq = "INSERT INTO {0} {1} VALUES {2};".format(self.uname,
5731d2ecaec4 Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
394 colspec, valspec)
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
395 # Class variables
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
396 fields = ["name", "score", "class", "xl", "fate", "endt"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
397 rankfields = ["rank", "score", "name", "class", "xl", "fate", "endt"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
398 pfields = ["score", "class", "xl", "fate", "endt"]
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
399 def getHigh(self, n=10, offset=0):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
400 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
401 def getRecent(self, n=20):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
402 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
403 def getPlayer(self, player):
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
404 return []
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
405
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
406 rogue3 = RogueGame("Rogue V3", "rogue3", "r3")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
407 rogue4 = RogueGame("Rogue V4", "rogue4", "r4")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
408 rogue5 = RogueGame("Rogue V5", "rogue5", "r5")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
409 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
410 arogue5 = ARogueGame("Advanced Rogue 5", "arogue5", "ar5")
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
411
17
7f7b3da664d6 Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents: 4
diff changeset
412 gamelist = [rogue3, rogue4, rogue5, srogue, arogue5]
0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
413
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
414 def playerpage(pname):
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
415 "Generate a player's HTML page"
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
416 # Write the beginning of the page
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
417 ppagefi = open(ppagename.format(pname), "w")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
418 ppagefi.write(phead.format(pname))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
419 ppagefi.write(ptop)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
420 ppagefi.write(navplayer.format(pname))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
421 ppagefi.write(pti.format("Results for " + pname))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
422 for game in gamelist:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
423 ppagefi.write(secthead.format(game.name))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
424 entries = game.getPlayer(pname)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
425 if not entries:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
426 ppagefi.write("<div>" + pname + " has not yet completed an expedition\
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
427 in this dungeon.</div>\n")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
428 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
429 printTable(entries, RogueGame.pfields, ppagefi)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
430 scoresum = 0
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
431 for entry in entries:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
432 scoresum += int(entry["score"])
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
433 avgscr = scoresum / len(entries)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
434 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
435 # 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
436 ppagefi.write(pend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
437 ppagefi.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
438 return
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
439
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
440 def highpage():
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
441 # open the page and print the beginning
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
442 highpfi = open(hpagename, "w")
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
443 highpfi.write(phead.format("High Scores"))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
444 highpfi.write(ptop)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
445 highpfi.write(navscore.format("High Scores"))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
446 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
447 for game in gamelist:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
448 highpfi.write(secthead.format(game.name))
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
449 entries = game.getHigh(40)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
450 if not entries:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
451 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
452 else:
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
453 printTable(entries, game.rankfields, highpfi)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
454 # That should finish it.
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
455 highpfi.write(pend)
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
456 highpfi.close()
5ba2123d2c20 Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
457 return