convert puzzles to lua standalone, json output

This commit is contained in:
Neale Pickett 2016-09-21 10:14:13 -06:00
parent f1a8ef623e
commit b7b21e94bd
4 changed files with 77 additions and 71 deletions

View File

@ -1,6 +1,11 @@
#! /bin/sh #! /bin/sh
if [ -n "$1" ]; then
cd $1
else
cd $(dirname $0)/.. cd $(dirname $0)/..
fi
basedir=$(pwd)
# Do nothing if `disabled` is present # Do nothing if `disabled` is present
if [ -f disabled ]; then if [ -f disabled ]; then
@ -14,6 +19,14 @@ if [ -f reset ]; then
rm -f reset rm -f reset
fi 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 # Collect new points
find state/points.new -type f | while read fn; do find state/points.new -type f | while read fn; do
# Skip files opened by another process # Skip files opened by another process
@ -30,11 +43,11 @@ find state/points.new -type f | while read fn; do
done done
# Generate new puzzles.html # Generate new puzzles.html
if www/cgi-bin/puzzles.cgi > www/puzzles.new; then if bin/puzzles $basedir > www/puzzles.new; then
mv www/puzzles.new www/puzzles.html mv www/puzzles.new www/puzzles.json
fi fi
# Generate new points.json # 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 mv www/points.new www/points.json
fi fi

View File

@ -1,27 +1,24 @@
#! /usr/bin/lua5.3 #! /usr/bin/lua5.3
local basedir = arg[1]
local statedir = basedir .. "/state"
io.write('{\n "points": [\n') io.write('{\n "points": [\n')
local teams = {} local teams = {}
local teamnames = {} local teamnames = {}
local nteams = 0 local nteams = 0
local NR = 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 if not teamno then
teamno = nteams teamno = nteams
teams[hash] = teamno teams[hash] = teamno
nteams = nteams + 1 nteams = nteams + 1
tf = io.open(statedir .. "/teams/" .. hash) teamnames[hash] = io.lines(statedir .. "/teams/" .. hash)()
teamname = tf:read()
tf:close()
teamnames[hash] = teamname
end end
if NR > 0 then if NR > 0 then

53
bin/puzzles Executable file
View File

@ -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')

View File

@ -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 = "<dl>\n"
for cat, biggest in pairs(max_by_cat) do
local points, dirname
body = body .. "<dt>" .. cat .. "</dt>"
body = body .. "<dd>"
for line in io.lines(koth.path("packages/" .. cat .. "/map.txt")) do
points, dirname = line:match("^(%d+) (.*)")
points = tonumber(points)
body = body .. "<a href=\"../" .. cat .. "/" .. dirname .. "/index.html\">" .. points .. "</a> "
if (points > biggest) then
break
end
end
if (points == biggest) then
body = body .. "<span title=\"Category Complete\">⁂</span>"
end
body = body .. "</dd>\n"
end
body = body .. "</dl>\n"
body = body .. "<fieldset><legend>Sandia Token:</legend>"
body = body .. "<p>Example: <samp>sandia:5:xylep-radar-nanox</samp></p>"
body = body .. "<form action='cgi-bin/token.cgi'>"
body = body .. "Team Hash: <input name='t'><br>"
body = body .. "Token: <input name='k'>"
body = body .. "<input type='submit'>"
body = body .. "</form>"
body = body .. "</fieldset>"
body = body .. "<p>Reloading this page periodically may yield updated puzzle lists.</p>"
koth.page("Open Puzzles", body)