annotate web/scoring/high.cgi @ 35:6592cdd1fceb

Add a high score CGI script. /scoring/high.cgi shows a score list, with a configurable length, offset, and game list.
author John "Elwin" Edwards
date Fri, 03 Jan 2014 11:11:07 -0500
parents
children 1d8dc3ed22cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
1 #!/usr/bin/python3
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
2
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
3 import sys
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
4 import cgi
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
5 import rlgall
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
6
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
7 checkstr = '{0}:<input type="checkbox" name="g" value="{1}" checked="checked"> '
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
8 uncheckstr = '{0}:<input type="checkbox" name="g" value="{1}"> '
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
9
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
10 cgidata = cgi.FieldStorage()
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
11
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
12 formgames = cgidata.getlist("g")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
13 if "all" in formgames:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
14 games = rlgall.gamelist
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
15 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
16 games = []
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
17 for g in rlgall.gamelist:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
18 if g.uname in formgames:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
19 games.append(g)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
20 if not games:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
21 games = rlgall.gamelist
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
22
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
23 formstart = cgidata.getfirst("s")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
24 if not formstart:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
25 start = 0
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
26 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
27 try:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
28 start = int(formstart)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
29 except ValueError:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
30 start = 0
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
31 if start < 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
32 start = 0
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
33
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
34 deflimit = (12 // len(games)) * 5
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
35 formlimit = cgidata.getfirst("l")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
36 if not formlimit:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
37 limit = deflimit
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
38 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
39 try:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
40 limit = int(formlimit)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
41 except ValueError:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
42 limit = deflimit
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
43 if limit <= 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
44 limit = deflimit
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
45
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
46
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
47 sys.stdout.write("Content-Type: text/html; charset=utf-8\r\n\r\n")
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
48 sys.stdout.write(rlgall.phead.format("High Scores"))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
49 sys.stdout.write(rlgall.ptop)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
50 sys.stdout.write(rlgall.navscore.format("High Scores"))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
51 if len(games) == 1:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
52 if start == 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
53 intitle = "Top {0} scores for {1}".format(limit, games[0].name)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
54 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
55 intitle = "Scores {0} - {1} for {2}".format(start + 1, start + limit,
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
56 games[0].name)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
57 sys.stdout.write(rlgall.pti.format(intitle))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
58 highlist = games[0].getHigh(limit, start)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
59 rlgall.printTable(highlist, games[0].rankfields, sys.stdout)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
60 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
61 if start == 0:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
62 intitle = "Top {0} scores".format(limit)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
63 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
64 intitle = "Scores {0} - {1}".format(start + 1, start + limit)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
65 sys.stdout.write(rlgall.pti.format(intitle))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
66 for g in games:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
67 sys.stdout.write(rlgall.secthead.format(g.name))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
68 highlist = g.getHigh(limit, start)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
69 rlgall.printTable(highlist, g.rankfields, sys.stdout)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
70 sys.stdout.write('<form action="/scoring/high.cgi" method="get">\n')
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
71 sys.stdout.write('<div>Number of scores: ')
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
72 sys.stdout.write('<input type="text" name="l" value="{0}">'.format(limit))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
73 sys.stdout.write(' Skip the first: ')
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
74 sys.stdout.write('<input type="text" name="s" value="{0}">'.format(start))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
75 sys.stdout.write('</div><div>')
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
76 for game in rlgall.gamelist:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
77 if game in games:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
78 sys.stdout.write(checkstr.format(game.name, game.uname))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
79 else:
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
80 sys.stdout.write(uncheckstr.format(game.name, game.uname))
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
81 sys.stdout.write('</div><div><input type="submit" value="Get Scores">')
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
82 sys.stdout.write('</div></form>')
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
83 sys.stdout.write(rlgall.pend)
6592cdd1fceb Add a high score CGI script.
John "Elwin" Edwards
parents:
diff changeset
84 exit()