From b7b21e94bd3f9ddb4bd321a6fd5fc0f37d452170 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 21 Sep 2016 10:14:13 -0600 Subject: [PATCH] convert puzzles to lua standalone, json output --- bin/once | 21 ++++++++++++--- bin/points | 17 +++++------- bin/puzzles | 53 ++++++++++++++++++++++++++++++++++++++ www/cgi-bin/puzzles.cgi | 57 ----------------------------------------- 4 files changed, 77 insertions(+), 71 deletions(-) create mode 100755 bin/puzzles delete mode 100755 www/cgi-bin/puzzles.cgi diff --git a/bin/once b/bin/once index 1a91905..496aa6f 100755 --- a/bin/once +++ b/bin/once @@ -1,6 +1,11 @@ #! /bin/sh -cd $(dirname $0)/.. +if [ -n "$1" ]; then + cd $1 +else + cd $(dirname $0)/.. +fi +basedir=$(pwd) # Do nothing if `disabled` is present if [ -f disabled ]; then @@ -14,6 +19,14 @@ if [ -f reset ]; then rm -f reset fi +# Create a list of currently-active categories +: > state/categories.txt.new +for dn in packages/*; do + cat=${dn##packages/} + echo "$cat" >> state/categories.txt.new +done +mv state/categories.txt.new state/categories.txt + # Collect new points find state/points.new -type f | while read fn; do # Skip files opened by another process @@ -30,11 +43,11 @@ find state/points.new -type f | while read fn; do done # Generate new puzzles.html -if www/cgi-bin/puzzles.cgi > www/puzzles.new; then - mv www/puzzles.new www/puzzles.html +if bin/puzzles $basedir > www/puzzles.new; then + mv www/puzzles.new www/puzzles.json fi # Generate new points.json -if bin/points state > www/points.new; then +if bin/points $basedir > www/points.new; then mv www/points.new www/points.json fi diff --git a/bin/points b/bin/points index 1ceecd4..79a4e5d 100755 --- a/bin/points +++ b/bin/points @@ -1,27 +1,24 @@ #! /usr/bin/lua5.3 +local basedir = arg[1] +local statedir = basedir .. "/state" + io.write('{\n "points": [\n') local teams = {} local teamnames = {} local nteams = 0 local NR = 0 -local statedir = arg[1] +for line in io.lines(statedir .. "/points.log") do + local ts, hash, cat, points = line:match("(%d+) (%g+) (%g+) (%d+)") + local teamno = teams[hash] -local f = io.open(statedir .. "/points.log") -for line in f:lines() do - ts, hash, cat, points = line:match("(%d+) (%g+) (%g+) (%d+)") - teamno = teams[hash] if not teamno then teamno = nteams teams[hash] = teamno nteams = nteams + 1 - tf = io.open(statedir .. "/teams/" .. hash) - teamname = tf:read() - tf:close() - - teamnames[hash] = teamname + teamnames[hash] = io.lines(statedir .. "/teams/" .. hash)() end if NR > 0 then diff --git a/bin/puzzles b/bin/puzzles new file mode 100755 index 0000000..a390126 --- /dev/null +++ b/bin/puzzles @@ -0,0 +1,53 @@ +#! /usr/bin/lua5.3 + +local basedir = arg[1] +local statedir = basedir .. "/state" + +local max_by_cat = {} +for cat in io.lines(statedir .. "/categories.txt") do + max_by_cat[cat] = 0 +end + +for line in io.lines(statedir .. "/points.log") do + local ts, team, cat, points = line:match("^(%d+) (%g+) (%g+) (%d+)") + points = tonumber(points) or 0 + + -- Skip scores for removed categories + if (max_by_cat[cat] ~= nil) then + max_by_cat[cat] = math.max(max_by_cat[cat], points) + end +end + + +local i = 0 +io.write('{\n') +for cat, biggest in pairs(max_by_cat) do + local points, dirname + local j = 0 + + if i > 0 then + io.write(',\n') + end + i = i + 1 + + io.write(' "' .. cat .. '": [\n') + for line in io.lines(basedir .. "/packages/" .. cat .. "/map.txt") do + points, dirname = line:match("^(%d+) (.*)") + points = tonumber(points) + + if j > 0 then + io.write(',\n') + end + j = j + 1 + io.write(' [' .. points .. ', "' .. dirname .. '"]') + if (points > biggest) then + break + end + end + if (points == biggest) then + io.write(',\n') + io.write(' [0, ""] // Category complete') + end + io.write('\n ]') +end +io.write('\n}\n') diff --git a/www/cgi-bin/puzzles.cgi b/www/cgi-bin/puzzles.cgi deleted file mode 100755 index 052d0bc..0000000 --- a/www/cgi-bin/puzzles.cgi +++ /dev/null @@ -1,57 +0,0 @@ -#! /usr/bin/env lua - -package.path = "?.lua;cgi-bin/?.lua;www/cgi-bin/?.lua" - -local koth = require "koth" - -local max_by_cat = {} - -local f = io.popen("ls " .. koth.path("packages")) -for cat in f:lines() do - max_by_cat[cat] = 0 -end -f:close() - - -for line in io.lines(koth.path("state/points.log")) do - local ts, team, cat, points, comment = line:match("^(%d+) (%w+) ([%w-]+) (%d+) ?(.*)") - points = tonumber(points) or 0 - - -- Skip scores for removed categories - if (max_by_cat[cat] ~= nil) then - max_by_cat[cat] = math.max(max_by_cat[cat], points) - end -end - -local body = "
\n" -for cat, biggest in pairs(max_by_cat) do - local points, dirname - - body = body .. "
" .. cat .. "
" - body = body .. "
" - for line in io.lines(koth.path("packages/" .. cat .. "/map.txt")) do - points, dirname = line:match("^(%d+) (.*)") - points = tonumber(points) - - body = body .. "" .. points .. " " - if (points > biggest) then - break - end - end - if (points == biggest) then - body = body .. "" - end - body = body .. "
\n" -end -body = body .. "
\n" -body = body .. "
Sandia Token:" -body = body .. "

Example: sandia:5:xylep-radar-nanox

" -body = body .. "
" -body = body .. "Team Hash:
" -body = body .. "Token: " -body = body .. "" -body = body .. "
" -body = body .. "
" -body = body .. "

Reloading this page periodically may yield updated puzzle lists.

" - -koth.page("Open Puzzles", body)