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