moth/www/koth.lua

91 lines
1.9 KiB
Lua
Raw Normal View History

2015-04-18 18:09:20 -06:00
#! /usr/bin/lua
local koth = {}
-- cut -d$ANCHOR -f2- | grep -Fx "$NEEDLE"
function koth.anchored_search(haystack, needle, anchor)
local f, err = io.open(haystack)
if (not f) then
return false, err
end
for line in f:lines() do
2015-04-18 18:09:20 -06:00
if (anchor) then
pos = line:find(anchor)
if (pos) then
line = line:sub(pos+1)
end
end
if (line == needle) then
f:close()
2015-04-18 18:09:20 -06:00
return true
end
end
f:close()
2015-04-18 18:09:20 -06:00
return false
end
function koth.page(title, body)
2015-04-19 23:14:31 -06:00
if (os.getenv("REQUEST_METHOD")) then
print("Content-type: text/html")
print()
end
2015-04-18 18:09:20 -06:00
print("<!DOCTYPE html>")
2015-04-20 12:08:21 -06:00
print("<html><head><title>" .. title .. "</title><link rel=\"stylesheet\" href=\"style.css\"><meta name=\"viewport\" content=\"width=device-width\"></head>")
2015-04-18 18:09:20 -06:00
print("<body><h1>" .. title .. "</h1>")
if (body) then
print("<section>")
print(body)
print("</section>")
end
2015-04-19 23:14:31 -06:00
print([[<section id="sponsors">
<img src="images/lanl.png" alt="Los Alamos National Laboratory">
<img src="images/doe.png" alt="US Department Of Energy">
<img src="images/sandia.png" alt="Sandia National Laboratories">
</section>]])
2015-04-18 18:09:20 -06:00
print("</body></html>")
os.exit(0)
end
2015-04-18 19:13:07 -06:00
--
-- 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)
team = team:gsub("[^0-9a-f]", "-")
2015-04-19 22:57:35 -06:00
local filename = team .. "." .. category .. "." .. points
2015-04-18 19:13:07 -06:00
local entry = team .. " " .. category .. " " .. points
if (comment) then
entry = entry .. " " .. comment
end
local f = io.open("../state/teams/" .. team)
if (f) then
f:close()
else
return false, "No such team"
end
local ok = koth.anchored_search("../state/points.log", entry, " ")
if (ok) then
2015-04-18 19:13:07 -06:00
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
2015-04-18 18:09:20 -06:00
return koth