diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/scoring/high.cgi	Fri Jan 03 11:11:07 2014 -0500
@@ -0,0 +1,84 @@
+#!/usr/bin/python3
+
+import sys
+import cgi
+import rlgall
+
+checkstr = '{0}:<input type="checkbox" name="g" value="{1}" checked="checked"> '
+uncheckstr = '{0}:<input type="checkbox" name="g" value="{1}"> '
+
+cgidata = cgi.FieldStorage()
+
+formgames = cgidata.getlist("g")
+if "all" in formgames:
+  games = rlgall.gamelist
+else:
+  games = []
+  for g in rlgall.gamelist:
+    if g.uname in formgames:
+      games.append(g)
+  if not games:
+    games = rlgall.gamelist
+
+formstart = cgidata.getfirst("s")
+if not formstart:
+  start = 0
+else:
+  try:
+    start = int(formstart)
+  except ValueError:
+    start = 0
+if start < 0:
+  start = 0
+
+deflimit = (12 // len(games)) * 5
+formlimit = cgidata.getfirst("l")
+if not formlimit:
+  limit = deflimit
+else:
+  try:
+    limit = int(formlimit)
+  except ValueError:
+    limit = deflimit
+if limit <= 0:
+  limit = deflimit
+
+
+sys.stdout.write("Content-Type: text/html; charset=utf-8\r\n\r\n")
+sys.stdout.write(rlgall.phead.format("High Scores"))
+sys.stdout.write(rlgall.ptop)
+sys.stdout.write(rlgall.navscore.format("High Scores"))
+if len(games) == 1:
+  if start == 0:
+    intitle = "Top {0} scores for {1}".format(limit, games[0].name)
+  else:
+    intitle = "Scores {0} - {1} for {2}".format(start + 1, start + limit, 
+                games[0].name)
+  sys.stdout.write(rlgall.pti.format(intitle))
+  highlist = games[0].getHigh(limit, start)
+  rlgall.printTable(highlist, games[0].rankfields, sys.stdout)
+else:
+  if start == 0:
+    intitle = "Top {0} scores".format(limit)
+  else:
+    intitle = "Scores {0} - {1}".format(start + 1, start + limit)
+  sys.stdout.write(rlgall.pti.format(intitle))
+  for g in games:
+    sys.stdout.write(rlgall.secthead.format(g.name))
+    highlist = g.getHigh(limit, start)
+    rlgall.printTable(highlist, g.rankfields, sys.stdout)
+sys.stdout.write('<form action="/scoring/high.cgi" method="get">\n')
+sys.stdout.write('<div>Number of scores: ')
+sys.stdout.write('<input type="text" name="l" value="{0}">'.format(limit))
+sys.stdout.write(' Skip the first: ')
+sys.stdout.write('<input type="text" name="s" value="{0}">'.format(start))
+sys.stdout.write('</div><div>')
+for game in rlgall.gamelist:
+  if game in games:
+    sys.stdout.write(checkstr.format(game.name, game.uname))
+  else:
+    sys.stdout.write(uncheckstr.format(game.name, game.uname))
+sys.stdout.write('</div><div><input type="submit" value="Get Scores">')
+sys.stdout.write('</div></form>')
+sys.stdout.write(rlgall.pend)
+exit()