comparison 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
comparison
equal deleted inserted replaced
34:86b616d88020 35:6592cdd1fceb
1 #!/usr/bin/python3
2
3 import sys
4 import cgi
5 import rlgall
6
7 checkstr = '{0}:<input type="checkbox" name="g" value="{1}" checked="checked"> '
8 uncheckstr = '{0}:<input type="checkbox" name="g" value="{1}"> '
9
10 cgidata = cgi.FieldStorage()
11
12 formgames = cgidata.getlist("g")
13 if "all" in formgames:
14 games = rlgall.gamelist
15 else:
16 games = []
17 for g in rlgall.gamelist:
18 if g.uname in formgames:
19 games.append(g)
20 if not games:
21 games = rlgall.gamelist
22
23 formstart = cgidata.getfirst("s")
24 if not formstart:
25 start = 0
26 else:
27 try:
28 start = int(formstart)
29 except ValueError:
30 start = 0
31 if start < 0:
32 start = 0
33
34 deflimit = (12 // len(games)) * 5
35 formlimit = cgidata.getfirst("l")
36 if not formlimit:
37 limit = deflimit
38 else:
39 try:
40 limit = int(formlimit)
41 except ValueError:
42 limit = deflimit
43 if limit <= 0:
44 limit = deflimit
45
46
47 sys.stdout.write("Content-Type: text/html; charset=utf-8\r\n\r\n")
48 sys.stdout.write(rlgall.phead.format("High Scores"))
49 sys.stdout.write(rlgall.ptop)
50 sys.stdout.write(rlgall.navscore.format("High Scores"))
51 if len(games) == 1:
52 if start == 0:
53 intitle = "Top {0} scores for {1}".format(limit, games[0].name)
54 else:
55 intitle = "Scores {0} - {1} for {2}".format(start + 1, start + limit,
56 games[0].name)
57 sys.stdout.write(rlgall.pti.format(intitle))
58 highlist = games[0].getHigh(limit, start)
59 rlgall.printTable(highlist, games[0].rankfields, sys.stdout)
60 else:
61 if start == 0:
62 intitle = "Top {0} scores".format(limit)
63 else:
64 intitle = "Scores {0} - {1}".format(start + 1, start + limit)
65 sys.stdout.write(rlgall.pti.format(intitle))
66 for g in games:
67 sys.stdout.write(rlgall.secthead.format(g.name))
68 highlist = g.getHigh(limit, start)
69 rlgall.printTable(highlist, g.rankfields, sys.stdout)
70 sys.stdout.write('<form action="/scoring/high.cgi" method="get">\n')
71 sys.stdout.write('<div>Number of scores: ')
72 sys.stdout.write('<input type="text" name="l" value="{0}">'.format(limit))
73 sys.stdout.write(' Skip the first: ')
74 sys.stdout.write('<input type="text" name="s" value="{0}">'.format(start))
75 sys.stdout.write('</div><div>')
76 for game in rlgall.gamelist:
77 if game in games:
78 sys.stdout.write(checkstr.format(game.name, game.uname))
79 else:
80 sys.stdout.write(uncheckstr.format(game.name, game.uname))
81 sys.stdout.write('</div><div><input type="submit" value="Get Scores">')
82 sys.stdout.write('</div></form>')
83 sys.stdout.write(rlgall.pend)
84 exit()