2015-05-26 10:16:22 -06:00
|
|
|
#! /usr/bin/env lua
|
2015-04-16 22:29:05 -06:00
|
|
|
|
2015-05-26 10:14:30 -06:00
|
|
|
package.path = "?.lua;cgi-bin/?.lua;www/cgi-bin/?.lua"
|
2015-04-21 07:57:11 -06:00
|
|
|
|
2015-04-16 22:29:05 -06:00
|
|
|
local cgi = require "cgi"
|
2016-02-16 09:25:45 -07:00
|
|
|
local moth = require "moth"
|
2015-04-16 22:29:05 -06:00
|
|
|
|
2015-04-18 18:09:20 -06:00
|
|
|
local team = cgi.fields['t'] or ""
|
|
|
|
local category = cgi.fields['c'] or ""
|
|
|
|
local points = cgi.fields['p'] or ""
|
|
|
|
local answer = cgi.fields['a'] or ""
|
2015-04-18 14:24:00 -06:00
|
|
|
|
2015-04-18 18:09:20 -06:00
|
|
|
-- Defang category name; prevent directory traversal
|
|
|
|
category = category:gsub("[^A-Za-z0-9]", "-")
|
2015-04-18 14:24:00 -06:00
|
|
|
|
2015-04-18 18:09:20 -06:00
|
|
|
-- Check answer
|
|
|
|
local needle = points .. " " .. answer
|
2016-02-16 09:25:45 -07:00
|
|
|
local haystack = moth.path("packages/" .. category .. "/answers.txt")
|
|
|
|
local found, err = moth.anchored_search(haystack, needle)
|
2015-04-18 14:24:00 -06:00
|
|
|
|
2015-04-18 18:09:20 -06:00
|
|
|
if (not found) then
|
2016-02-16 09:25:45 -07:00
|
|
|
moth.page("Wrong answer", err)
|
2015-04-18 18:09:20 -06:00
|
|
|
end
|
2015-04-18 14:24:00 -06:00
|
|
|
|
2016-02-16 09:25:45 -07:00
|
|
|
local ok, err = moth.award_points(team, category, points)
|
2015-04-18 18:09:20 -06:00
|
|
|
if (not ok) then
|
2016-02-16 09:25:45 -07:00
|
|
|
moth.page("Error awarding points",
|
2015-04-20 10:11:38 -06:00
|
|
|
"<p>You got the right answer, but there was a problem trying to give you points:</p>" ..
|
2015-04-18 19:13:07 -06:00
|
|
|
"<p>" .. err .. "</p>")
|
2015-04-18 18:09:20 -06:00
|
|
|
end
|
|
|
|
|
2016-02-16 09:25:45 -07:00
|
|
|
moth.page("Points awarded",
|
2015-04-18 19:13:07 -06:00
|
|
|
"<p>" .. points .. " points for " .. team .. "!</p>" ..
|
2015-04-29 16:41:42 -06:00
|
|
|
"<p><a href=\"../puzzles.html\">Back to puzzles</a></p>")
|