Mercurial > hg > rlgallery-misc
annotate py/rlgall.py @ 42:e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
author | John "Elwin" Edwards |
---|---|
date | Sat, 11 Jan 2014 10:45:33 -0500 |
parents | d3ccdc195796 |
children | 0f4163dbbafc |
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 | 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 | 8 from datetime import datetime |
9 import pytz | |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
10 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
11 # Configuration |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
12 logdir = "/var/dgl/var/games/roguelike/" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
13 webdir = "/var/www/lighttpd/scoring/" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
14 ppagename = webdir + "players/{0}.html" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
15 hpagename = webdir + "highscores.html" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
16 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
17 # HTML fragments for templating |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
18 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
|
19 <html><head> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
20 <title>{0}</title> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
21 <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
|
22 </head> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
23 """ |
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 ptop = """<body> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
26 <h1>Yendor Guild</h1> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
27 """ |
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 navtop = '<div class="nav"><a href="/">rlgallery.org</a> -> {0}</div>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
30 navscore = '<div class="nav"><a href="/">rlgallery.org</a> -> \ |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
31 <a href="/scoring/">Scores</a> -> {0}</div>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
32 navplayer = '<div class="nav"><a href="/">rlgallery.org</a> -> \ |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
33 <a href="/scoring/">Scores</a> -> <a href="/scoring/players/">Players</a> \ |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
34 -> {0}</div>' |
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 pti = '<h2>{0}</h2>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
37 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
38 secthead = '<h3>{0}</h3>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
39 tblhead = '<div class="stable">\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
40 rowstart = '<div class="sentry">\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
41 rowend = '</div>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
42 cell = ' <span class="sdata">{0}</span>\n' |
31 | 43 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
|
44 hcell = ' <span class="shdata">{0}</span>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
45 tblend = '</div>\n' |
34
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
46 pend = """<div class="foot"> |
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
47 <a href="/">rlgallery.org</a> |
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
48 <a href="/recent.cgi">Recent Games</a> |
36 | 49 <a href="/scoring/high.cgi">High Scores</a> |
34
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
50 </div> |
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
51 </body></html> |
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
52 """ |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
53 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
54 # 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
|
55 headerbook = {"endt":"End time", "score":"Score", "name":"Name", "xl":"XL", |
19
78580bffc13d
Add high score list support for arogue5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
56 "fate":"Fate", "rank":"Rank", "game":"Game", "class": "Class"} |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
57 # Queries for the games table |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
58 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
|
59 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
|
60 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
61 def getconn(): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
62 "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
|
63 try: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
64 conn = psycopg2.connect("dbname=rlg") |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
65 except psycopg2.OperationalError: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
66 return None |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
67 return conn |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
68 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
69 def recnameToTS(filename): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
70 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
|
71 try: |
30 | 72 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
|
73 return dt |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
74 except ValueError: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
75 return None |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
76 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
77 def ttyreclink(text, name, game, gtime): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
78 "Returns a link to the ttyrec archivist" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
79 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
|
80 return lstr.format(name, game, gtime, text) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
81 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
82 def playerlink(name): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
83 "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
|
84 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
|
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 | 91 stamp = int(entry["endt"].timestamp()) |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
92 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
|
93 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
94 def maketablerow(cells, isheader=None): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
95 "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
|
96 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
|
97 if isheader: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
98 celler = hcell |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
99 else: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
100 celler = cell |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
101 rowstr = rowstart |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
102 for entry in cells: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
103 rowstr = rowstr + celler.format(entry) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
104 rowstr = rowstr + rowend |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
105 return rowstr |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
106 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
107 def printTable(entries, fields, of): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
108 "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
|
109 HTML table to of." |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
110 of.write(tblhead) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
111 clist = [] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
112 for field in fields: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
113 if field in headerbook: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
114 clist.append(headerbook[field]) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
115 else: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
116 clist.append("{0}".format(field)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
117 of.write(maketablerow(clist, True)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
118 rnum = 0 |
35 | 119 for entry in entries: |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
120 clist = [] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
121 for field in fields: |
35 | 122 if field in entry: |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
123 thing = entry[field] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
124 if field == "game": |
31 | 125 clist.append((thing.name, cell)) |
35 | 126 elif field == "xl" or field == "score" or field == "rank": # Numerics |
31 | 127 clist.append((str(thing), rcell)) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
128 elif field == "name": |
31 | 129 clist.append((playerlink(thing), cell)) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
130 elif field == "fate": |
31 | 131 clist.append((thing, cell)) |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
132 elif field == "endt": |
31 | 133 clist.append((linktoArchive(entry), cell)) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
134 else: |
31 | 135 clist.append(("{0}".format(thing), cell)) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
136 else: |
31 | 137 clist.append(("N/A", cell)) |
138 rowstr = rowstart + "".join([ t.format(s) for (s, t) in clist ]) + rowend | |
139 of.write(rowstr) | |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
140 of.write(tblend) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
141 return |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
142 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
143 class Game: |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
144 def __init__(self, name, uname): |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
145 pass |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
146 def logtoDict(self, entry): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
147 "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
|
148 ndict = {"game": self} |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
149 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
|
150 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
|
151 if self.sqltypes[item] == "int": |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
152 ndict[item] = int(value) |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
153 if self.sqltypes[item] == "bool": |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
154 ndict[item] = bool(int(value)) |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
155 elif self.sqltypes[item] == "timestamptz": |
30 | 156 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
|
157 else: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
158 ndict[item] = value |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
159 return ndict |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
160 def getEntryDicts(self, entfile, entlist): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
161 "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
|
162 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
|
163 while True: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
164 nextentry = entfile.readline() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
165 if not nextentry: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
166 break |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
167 if nextentry[-1] != '\n': |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
168 break |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
169 entlist.append(self.logtoDict(nextentry)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
170 return |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
171 def loadnew(self): |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
172 conn = getconn() |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
173 if conn == None: |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
174 return [] |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
175 cur = conn.cursor() |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
176 # Get the previous offset |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
177 cur.execute(offselstr, [self.uname]) |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
178 offset = cur.fetchone()[0] |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
179 newlist = [] |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
180 try: |
30 | 181 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
|
182 scr.seek(offset) |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
183 self.getEntryDicts(scr, newlist) |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
184 except IOError: |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
185 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
|
186 else: |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
187 noffset = scr.tell() |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
188 scr.close() |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
189 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
|
190 # 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
|
191 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
|
192 self.postprocess(newlist) |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
193 self.putIntoDB(newlist, conn) |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
194 cur.close() |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
195 conn.close() |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
196 return updatenames |
18
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
197 def postprocess(self, gamelist): |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
198 "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
|
199 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
|
200 conn = getconn() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
201 if conn == None: |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
202 return [] |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
203 cur = conn.cursor() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
204 for nameF in names: |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
205 # 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
|
206 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
|
207 itsEntries.sort(key=lambda e: e["endt"]) |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
208 # 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
|
209 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
|
210 cur.execute(tquery, [nameF]) |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
211 result = cur.fetchone() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
212 if result: |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
213 prev = result[0] |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
214 else: |
30 | 215 prev = datetime.fromtimestamp(0, pytz.utc); |
18
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 vfilekeys.sort(key=lambda e: e[0]) |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
220 # 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
|
221 for i in range(0, len(itsEntries)): |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
222 if i == 0: |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
223 lowlim = prev |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
224 else: |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
225 lowlim = itsEntries[i-1]["endt"] |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
226 hilim = itsEntries[i]["endt"] |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
227 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
|
228 itsEntries[i]["startt"] = recnameToTS(recs[0]) |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
229 itsEntries[i]["ttyrecs"] = recs |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
230 cur.close() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
231 conn.close() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
232 def putIntoDB(self, dictlist, conn): |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
233 cur = conn.cursor() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
234 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
|
235 conn.commit() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
236 cur.close() |
5731d2ecaec4
Store arogue5 results in the database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
237 return |
21
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
238 def tablerecent(self, of): |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
239 "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
|
240 newest = [] |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
241 try: |
30 | 242 scr = open(self.scores, encoding="utf-8") |
243 except FileNotFoundError: | |
21
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
244 pass |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
245 else: |
30 | 246 # 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
|
247 try: |
30 | 248 scr.buffer.seek(self.lookback, 2) |
249 except OSError: | |
250 scr.buffer.seek(0) # The file wasn't that long, start at the beginning | |
251 if scr.buffer.tell() != 0: | |
252 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
|
253 self.getEntryDicts(scr, newest) |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
254 newest.reverse() |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
255 scr.close() |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
256 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
|
257 if not newest: |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
258 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
|
259 else: |
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
260 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
|
261 return |
35 | 262 def getHigh(self, n=10, offset=0): |
263 "Gets the n highest scores (starting at offset) from the database, \ | |
264 returning a list of dicts." | |
265 qfields = [] | |
266 for f in self.rankfields: | |
267 if f == "rank": | |
268 qfields.append("rank(*) OVER (ORDER BY score DESC)") | |
269 else: | |
270 qfields.append(f) | |
271 qstr = "SELECT " + ", ".join(qfields) + " FROM {0} ".format(self.uname) | |
272 qvals = [] | |
273 try: | |
274 n = int(n) | |
275 except (ValueError, TypeError): | |
276 return [] | |
277 if n <= 0: | |
278 return [] | |
279 qstr += " LIMIT %s" | |
280 qvals.append(n) | |
281 try: | |
282 offset = int(offset) | |
283 except (ValueError, TypeError): | |
284 return [] | |
285 if offset > 0: | |
286 qstr += " OFFSET %s" | |
287 qvals.append(offset) | |
288 qstr += ";" | |
289 conn = psycopg2.connect("dbname=rlg") | |
290 cur = conn.cursor() | |
291 cur.execute(qstr, qvals) | |
292 cols = [ col.name for col in cur.description ] | |
293 data = [ dict(zip(cols, row)) for row in cur.fetchall() ] | |
294 cur.close() | |
295 conn.close() | |
296 for d in data: | |
297 d["game"] = self | |
298 return data | |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
299 # End Game class definition |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
300 |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
301 class RogueGame(Game): |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
302 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
|
303 self.name = name |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
304 self.uname = uname |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
305 self.scores = logdir + uname + ".log" |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
306 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
|
307 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
|
308 "xl": "int", "fate": "text", "ttyrecs": "text ARRAY", |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
309 "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
|
310 self.logdelim = " " |
21
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
311 self.lookback = -1500 |
30 | 312 # Construct the insert query |
313 fields = self.sqltypes.keys() | |
314 colspec = ", ".join(fields) | |
315 valspec = ", ".join([ "%({})s".format(c) for c in fields ]) | |
316 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
|
317 colspec, valspec) |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
318 # 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
|
319 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
|
320 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
|
321 pfields = ["score", "xl", "fate", "endt"] |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
322 def getRecent(self, n=20): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
323 "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
|
324 of dicts." |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
325 try: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
326 n = int(n) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
327 except (ValueError, TypeError): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
328 return [] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
329 if n <= 0: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
330 return [] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
331 dictlist = [] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
332 conn = psycopg2.connect("dbname=rlg") |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
333 cur = conn.cursor() |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
334 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
|
335 LIMIT %s;".format(self.uname) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
336 cur.execute(qstr, [n]) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
337 for record in cur: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
338 # This should be less hardcodish |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
339 ndict = {"game": self} |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
340 ndict["endt"] = record[0] |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
341 ndict["score"] = record[1] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
342 ndict["name"] = record[2] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
343 ndict["xl"] = record[3] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
344 ndict["fate"] = record[4] |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
345 ndict["startt"] = record[5] |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
346 dictlist.append(ndict) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
347 cur.close() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
348 conn.close() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
349 return dictlist |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
350 def getPlayer(self, player): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
351 "Gets all player's games from the database." |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
352 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
|
353 conn = getconn() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
354 if conn == None: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
355 return [] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
356 cur = conn.cursor() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
357 entries = [] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
358 cur.execute(qstr, [player]) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
359 for record in cur: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
360 ndict = {"game": self} |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
361 ndict["endt"] = record[0] |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
362 ndict["score"] = record[1] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
363 ndict["name"] = record[2] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
364 ndict["xl"] = record[3] |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
365 ndict["fate"] = record[4] |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
366 ndict["startt"] = record[5] |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
367 entries.append(ndict) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
368 cur.close() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
369 conn.close() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
370 return entries |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
371 def putIntoDB(self, dictlist, conn): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
372 "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
|
373 which needs the INSERT privilege." |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
374 cur = conn.cursor() |
30 | 375 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
|
376 conn.commit() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
377 cur.close() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
378 return |
42
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
379 def postprocess(self, gamelist): |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
380 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
|
381 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
|
382 for d in gamelist: |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
383 m = lre.match(d["fate"]) |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
384 if m: |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
385 d["depth"] = int(m.group(3)) |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
386 if m.group(4): |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
387 d["maxdepth"] = int(m.group(5)) |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
388 else: |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
389 d["maxdepth"] = d["depth"] |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
390 else: |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
391 m = wre.match(d["fate"]) |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
392 if m: |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
393 d["depth"] = 0 |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
394 d["maxdepth"] = int(m.group(1)) |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
395 else: |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
396 # Something went wrong |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
397 d["depth"] = -1 |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
398 d["maxdepth"] = -1 |
e1de8aeb5ed4
Add depth and maxdepth columns to the RogueGame tables.
John "Elwin" Edwards
parents:
41
diff
changeset
|
399 Game.postprocess(self, gamelist) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
400 # End RogueGame class definition |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
401 |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
402 class ARogueGame(Game): |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
403 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
|
404 self.name = name |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
405 self.uname = uname |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
406 self.scores = logdir + uname + ".log" |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
407 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
|
408 "maxdepth", "quest", "hadquest", "fate"] |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
409 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
|
410 "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
|
411 "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
|
412 "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
|
413 self.logdelim = " " |
21
453278a81371
Add tablerecent() so recent.cgi will work as before.
John "Elwin" Edwards <elwin@sdf.org>
parents:
20
diff
changeset
|
414 self.lookback = -1800 |
30 | 415 # Construct the insert query |
416 fields = self.sqltypes.keys() | |
417 colspec = ", ".join(fields) | |
418 valspec = ", ".join([ "%({})s".format(c) for c in fields ]) | |
419 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
|
420 colspec, valspec) |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
421 # Class variables |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
422 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
|
423 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
|
424 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
|
425 def getRecent(self, n=20): |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
426 return [] |
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
427 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
|
428 "Gets all player's games from the database." |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
429 qstr = "SELECT endt, score, name, xl, class, fate FROM " + self.uname + " WHERE name = %s;" |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
430 conn = getconn() |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
431 if conn == None: |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
432 return [] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
433 cur = conn.cursor() |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
434 entries = [] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
435 cur.execute(qstr, [player]) |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
436 for record in cur: |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
437 ndict = {"game": self} |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
438 ndict["endt"] = record[0] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
439 ndict["score"] = record[1] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
440 ndict["name"] = record[2] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
441 ndict["xl"] = record[3] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
442 ndict["class"] = record[4] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
443 ndict["fate"] = record[5] |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
444 entries.append(ndict) |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
445 cur.close() |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
446 conn.close() |
c05050f78d81
Make arogue5 scores appear on the player pages too.
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
447 return entries |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
448 |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
449 rogue3 = RogueGame("Rogue V3", "rogue3", "r3") |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
450 rogue4 = RogueGame("Rogue V4", "rogue4", "r4") |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
451 rogue5 = RogueGame("Rogue V5", "rogue5", "r5") |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
452 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
|
453 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
|
454 |
17
7f7b3da664d6
Begin adding arogue5 support to the recorder script.
John "Elwin" Edwards <elwin@sdf.org>
parents:
4
diff
changeset
|
455 gamelist = [rogue3, rogue4, rogue5, srogue, arogue5] |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
456 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
457 def playerpage(pname): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
458 "Generate a player's HTML page" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
459 # Write the beginning of the page |
30 | 460 ppagefi = open(ppagename.format(pname), "w", encoding="utf-8") |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
461 ppagefi.write(phead.format(pname)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
462 ppagefi.write(ptop) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
463 ppagefi.write(navplayer.format(pname)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
464 ppagefi.write(pti.format("Results for " + pname)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
465 for game in gamelist: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
466 ppagefi.write(secthead.format(game.name)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
467 entries = game.getPlayer(pname) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
468 if not entries: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
469 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
|
470 in this dungeon.</div>\n") |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
471 else: |
19
78580bffc13d
Add high score list support for arogue5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
472 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
|
473 printTable(entries, game.pfields, ppagefi) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
474 scoresum = 0 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
475 for entry in entries: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
476 scoresum += int(entry["score"]) |
30 | 477 avgscr = scoresum // len(entries) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
478 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
|
479 # 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
|
480 ppagefi.write(pend) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
481 ppagefi.close() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
482 return |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
483 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
484 def highpage(): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
485 # open the page and print the beginning |
30 | 486 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
|
487 highpfi.write(phead.format("High Scores")) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
488 highpfi.write(ptop) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
489 highpfi.write(navscore.format("High Scores")) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
490 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
|
491 for game in gamelist: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
492 highpfi.write(secthead.format(game.name)) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
493 entries = game.getHigh(40) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
494 if not entries: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
495 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
|
496 else: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
497 printTable(entries, game.rankfields, highpfi) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
498 # That should finish it. |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
499 highpfi.write(pend) |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
500 highpfi.close() |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
501 return |