Mercurial > hg > rlgallery-misc
annotate py/rlgall.py @ 97:fb292a0184d9
Add a page about UltraRogue.
author | John "Elwin" Edwards |
---|---|
date | Mon, 05 Apr 2021 21:57:20 -0400 |
parents | d417016bbf73 |
children | 44e8aaa20d02 |
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 | |
49
6138c27d1950
Escape the player's name when printing it into HTML.
John "Elwin" Edwards
parents:
45
diff
changeset
|
10 import html |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
11 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
12 # Configuration |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
13 logdir = "/var/dgl/var/games/roguelike/" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
14 webdir = "/var/www/lighttpd/scoring/" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
15 ppagename = webdir + "players/{0}.html" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
16 hpagename = webdir + "highscores.html" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
17 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
18 # HTML fragments for templating |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
19 phead = """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
20 <html><head> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
21 <title>{0}</title> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
22 <link rel="stylesheet" href="/scoring/scores.css" type="text/css"> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
23 </head> |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
24 """ |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
25 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
26 ptop = """<body> |
91 | 27 <h1>Roguelike Gallery Records</h1> |
0
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 |
91 | 30 navtop = '<div class="nav"><a href="/">RLGallery</a> -> {0}</div>\n' |
31 navscore = '<div class="nav"><a href="/">RLGallery</a> -> \ | |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
32 <a href="/scoring/">Scores</a> -> {0}</div>\n' |
91 | 33 navplayer = '<div class="nav"><a href="/">RLGallery</a> -> \ |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
34 <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
|
35 -> {0}</div>' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
36 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
37 pti = '<h2>{0}</h2>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
38 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
39 secthead = '<h3>{0}</h3>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
40 tblhead = '<div class="stable">\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
41 rowstart = '<div class="sentry">\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
42 rowend = '</div>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
43 cell = ' <span class="sdata">{0}</span>\n' |
31 | 44 rcell = ' <span class="sdatar">{0}</span>\n' |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
45 hcell = ' <span class="shdata">{0}</span>\n' |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
46 tblend = '</div>\n' |
34
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
47 pend = """<div class="foot"> |
91 | 48 <a href="/">RLGallery</a> |
34
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
49 <a href="/recent.cgi">Recent Games</a> |
36 | 50 <a href="/scoring/high.cgi">High Scores</a> |
81
2cca66b3e262
Add some more links to the page footers.
John "Elwin" Edwards
parents:
68
diff
changeset
|
51 <a href="/notes/">Notes</a> |
2cca66b3e262
Add some more links to the page footers.
John "Elwin" Edwards
parents:
68
diff
changeset
|
52 <a href="https://rlgallery.org:8080/">Play</a> |
34
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
53 </div> |
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
54 </body></html> |
86b616d88020
Add a footer with some links to the Web pages.
John "Elwin" Edwards
parents:
33
diff
changeset
|
55 """ |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
56 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
57 # This would be more useful if we had to do translation |
1
def7fecbd437
Switch to SQL timestamps.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
58 headerbook = {"endt":"End time", "score":"Score", "name":"Name", "xl":"XL", |
62
df7acc5653b3
Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents:
50
diff
changeset
|
59 "fate":"Fate", "rank":"Rank", "game":"Game", "class":"Class", |
df7acc5653b3
Make the final depth appear in Advanced Rogue 5 tables.
John "Elwin" Edwards
parents:
50
diff
changeset
|
60 "depth":"Depth"} |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
61 # Queries for the games table |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
62 offselstr = "SELECT offbytes FROM games WHERE gname = %s;" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
63 newoffstr = "UPDATE games SET offbytes = %s WHERE gname = %s;" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
64 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
65 def getconn(): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
66 "Returns a database connection, or None if the connection fails." |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
67 try: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
68 conn = psycopg2.connect("dbname=rlg") |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
69 except psycopg2.OperationalError: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
70 return None |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
71 return conn |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
72 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
73 def recnameToTS(filename): |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
74 pattern = "%Y-%m-%d.%H:%M:%S.ttyrec" |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
75 try: |
30 | 76 dt = datetime.strptime(filename, pattern).replace(tzinfo=pytz.utc) |
0
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
77 return dt |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
78 except ValueError: |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
79 return None |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
80 |
5ba2123d2c20
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
81 |