From 2d6ac8464151454f33058a70bfd1aa06758ea966 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sat, 18 Apr 2015 19:13:07 -0600 Subject: [PATCH] Puzzler that awards points. Maybe. --- TODO | 0 bin/once | 3 +++ cgi/koth.lua | 27 +++++++++++++++++++++++++++ cgi/puzzler.cgi | 8 +++++--- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 0000000..e69de29 diff --git a/bin/once b/bin/once index ac41fab..2f199b0 100755 --- a/bin/once +++ b/bin/once @@ -15,6 +15,9 @@ fi # Collect new points find state/points.new -type f | while read fn; do + # Skip partially-written files + [ $(wc -l $fn) -eq 1 ] || continue + cat $fn >> state/points.log rm $fn done diff --git a/cgi/koth.lua b/cgi/koth.lua index ac6aa39..529b610 100644 --- a/cgi/koth.lua +++ b/cgi/koth.lua @@ -41,5 +41,32 @@ function koth.page(title, body) os.exit(0) end +-- +-- We're going to rely on `bin/once` only processing files with the right number of lines. +-- +function koth.award_points(team, category, points, comment) + local filename = team .. "." .. category .. "." points + local entry = team .. " " .. category .. " " .. points + + if (comment) then + entry = entry .. " " .. comment + end + + local ok = anchored_search("../state/points.log", entry, " ") + if (not ok) then + return false, "Points already awarded" + end + + local f = io.open("../state/points.new/" .. filename, "a") + if (not f) then + return false, "Unable to write to points file" + end + + f:write(os.time(), " ", entry, "\n") + f:close() + + return true +end + return koth diff --git a/cgi/puzzler.cgi b/cgi/puzzler.cgi index 04206d9..ee4d24a 100755 --- a/cgi/puzzler.cgi +++ b/cgi/puzzler.cgi @@ -20,11 +20,13 @@ if (not found) then koth.page("Wrong answer") end -local ok = koth.award_points(team, category, points, "P"); +local ok, err = koth.award_points(team, category, points) if (not ok) then - koth.page("Error awarding points", "You got the right answer, but something blew up trying to give you points. Try again in a few seconds.") + koth.page("Error awarding points", + "

You got the right answer, but something blew up trying to give you points.

" .. + "

" .. err .. "

") end koth.page("Points awarded", - "

" .. points .. " points for " .. team .. ".

" .. + "

" .. points .. " points for " .. team .. "!

" .. "

Back to puzzles

")