add comments

This commit is contained in:
whosit 2025-03-06 12:49:17 +03:00
parent 73a3a88eb5
commit 37820ba3d3

View File

@ -1,9 +1,9 @@
local categories = {"Creativity", "Appeal", "Execution", "Use of space"}
local ENTRIES_PER_PAGE = 8
-- TODO make a "login" button for the admin screen
-- TODO clear admin screen on "quit" event
function show_vote_welcome()
-- window shown before player can see the ballot
-- by making them click the button, we can know their name
digiline_send("vote",
{
{command = "clear"},
@ -20,6 +20,7 @@ end
function show_vote_error(voter, offender)
-- show this when owner of the ballot and clicker do not match
local msg = string.format("%s has tried to cast a vote instead of %s!\nThey also saw %s's votes, they're naughty!", offender, voter, voter)
digiline_send("vote",
{
@ -39,6 +40,7 @@ end
function show_admin_welcome()
-- just a menu to select from 2 windows
digiline_send("admin",
{
{command = "clear"},
@ -57,11 +59,12 @@ end
function show_admin_edit()
-- screen for adding/removing entries
local list_entries = {}
for i,e in ipairs(mem.entries) do
table.insert(list_entries, string.format("%s|%s", e.id, e.name))
end
local list_str = table.concat(list_entries, ",") -- TODO sanitize :p
local list_str = table.concat(list_entries, ",") -- TODO sanitize?
digiline_send("admin",
{
{command = "clear"},
@ -84,6 +87,7 @@ end
function show_admin_results()
-- count and show results
-- count the votes that we actually cast
-- local total_scores = {}
@ -98,6 +102,8 @@ function show_admin_results()
-- end
-- end
-- for all entries, check all known votes and add them up, using defaults if none
-- TODO can be made more efficient probably
local total_scores = {}
for i, e in ipairs(mem.entries) do
local id = e.id
@ -113,6 +119,7 @@ function show_admin_results()
end
end
-- sort by different fields
local sorted_by_str = "none"
if mem.sort_type == "sum" then
sorted_by_str = "Sorted by sum:"
@ -123,6 +130,7 @@ function show_admin_results()
table.sort(total_scores, function(a,b) return a[idx] > b[idx] end)
end
-- actually generate strings for the results table
local entry_list = {}
for _, s in ipairs(total_scores) do
local name = mem.entries_by_id[s.id].name
@ -131,9 +139,6 @@ function show_admin_results()
end
entry_list = table.concat(entry_list, ",")
--digiline_send("debug", total_scores)
digiline_send("admin",
{
{command = "clear"},
@ -175,17 +180,17 @@ function show_vote_ballot(username)
{command = "addlabel", label = "Use of", X = 16.1, Y = 0.6},
{command = "addlabel", label = "space", X = 16.2, Y = 1},
}
local shift = 1.3
local shift = 1.3 -- vertical spacing between entries
local votes = mem.votes[username] or {}
local page = mem.ballot_page
for i=1,ENTRIES_PER_PAGE do
local e = mem.entries[i + ENTRIES_PER_PAGE*page]
if not e then
break
break -- not enough entries to fill the page
end
local id = e.id
local e_v = votes[id] or {}
local e_v = votes[id] or {} -- previous votes of this player for this entry (if any)
table.insert(c, {command = "addimage", texture_name = "halo.png^[colorize:#222233", X = 0.5, Y = 0.4 + shift*i, W = 16.8, H = 1})
table.insert(c, {command = "addlabel", label = e.name, X = 0.8, Y = 0.9 + shift*i})
table.insert(c, {command = "adddropdown", name = string.format("v_%d_1", id), index_event = true, selected_id = e_v[1] or 1, choices = {[1] = "1", [2] = "2", [3] = "3"}, X = 10.8, Y = 0.5 + shift*i, W = 0.8, H = 0.8})
@ -194,19 +199,14 @@ function show_vote_ballot(username)
table.insert(c, {command = "adddropdown", name = string.format("v_%d_4", id), index_event = true, selected_id = e_v[4] or 1, choices = {[1] = "1", [2] = "2", [3] = "3"}, X = 16.2, Y = 0.5 + shift*i, W = 0.8, H = 0.8})
end
-- snow number of pages and page flipping buttons
local total_pages = math.ceil(#mem.entries / ENTRIES_PER_PAGE)
table.insert(c, {command = "addlabel", label = string.format("Page %s/%s", mem.ballot_page+1, total_pages), X = 0.8, Y = 12.5})
if mem.ballot_page > 0 then
table.insert(c,
{command = "addbutton", name = "ballot_page_prev", label = "Prev page", X = 2.5, Y = 12.5, W = 3, H = 1}
)
table.insert(c, {command = "addbutton", name = "ballot_page_prev", label = "Prev page", X = 2.5, Y = 12.5, W = 3, H = 1})
end
if (mem.ballot_page + 1) < total_pages then
table.insert(c,
{command = "addbutton", name = "ballot_page_next", label = "Next page", X = 5.5, Y = 12.5, W = 3, H = 1}
)
table.insert(c, {command = "addbutton", name = "ballot_page_next", label = "Next page", X = 5.5, Y = 12.5, W = 3, H = 1})
end
digiline_send("vote", c)
@ -214,17 +214,21 @@ function show_vote_ballot(username)
end
if event.type == "program" then
-- initialize defaults or use stored values
-- reset screens
show_vote_welcome()
show_admin_welcome()
mem.id_count = mem.id_count or 1
mem.username = nil
mem.entries_by_id = mem.entries_by_id or {}
mem.entries = mem.entries or {}
mem.admin_entries_idx = nil
mem.votes = mem.votes or {}
mem.state_admin = "welcome"
mem.sort_type = "sum"
mem.ballot_page = 0
mem.id_count = mem.id_count or 1 -- unique entry ID generator
mem.username = nil -- current voter's name
mem.entries_by_id = mem.entries_by_id or {} -- mapping from id to entry
mem.entries = mem.entries or {} -- ordered list of entries
mem.admin_entries_idx = nil -- stores list selection for "edit" screen
mem.votes = mem.votes or {} -- user votes
mem.state_admin = "welcome" -- which admin window is currently displayed
mem.sort_type = "sum" -- what sort to use ("sum" or 1,2,3,4)
mem.ballot_page = 0 -- current ballot page
elseif event.type == "digiline" then
if event.channel == "vote" then
if event.msg.start then
@ -236,9 +240,11 @@ elseif event.type == "digiline" then
-- clear screen after user closed the window
show_vote_welcome()
elseif event.msg.ballot_page_next then
-- flip page
mem.ballot_page = mem.ballot_page + 1
show_vote_ballot(mem.username)
elseif event.msg.ballot_page_prev then
-- flip page
mem.ballot_page = mem.ballot_page - 1
show_vote_ballot(mem.username)
else
@ -259,11 +265,12 @@ elseif event.type == "digiline" then
if k:sub(1,2) == "v_" then
local vote = k:sub(3)
local off = vote:find("_", 1, true)
local e_id = tonumber(vote:sub(1,off-1)) or 0
local c_id = tonumber(vote:sub(off+1)) or 0
local e_id = tonumber(vote:sub(1,off-1)) or 0 -- entry ID
local c_id = tonumber(vote:sub(off+1)) or 0 -- category ID
local entry = votes[e_id] or {}
-- store vote
votes[e_id] = entry
entry[c_id] = tonumber(v) or 0
entry[c_id] = tonumber(v) or 0 -- vote value for this category
end
end
--digiline_send("debug", mem.votes) -- TODO remove this
@ -271,9 +278,11 @@ elseif event.type == "digiline" then
elseif event.channel == "admin" then
if mem.state_admin == "welcome" then
if event.msg.edit_entries then
-- show "edit" window
mem.state_admin = "edit"
show_admin_edit()
elseif event.msg.view_results then
-- show "results" window
mem.state_admin = "results"
show_admin_results()
end
@ -288,19 +297,19 @@ elseif event.type == "digiline" then
elseif event.msg.add_entry or (event.msg.key_enter_field == "new_entry") then
-- adding new entry
local new = event.msg.new_entry
if not new:find(",", 1, true) then -- can't have "," in names
if not new:find(",", 1, true) then -- can't have "," in names, do nothing if found
local new_id = mem.id_count
mem.id_count = mem.id_count + 1
if new ~= "" then
mem.id_count = mem.id_count + 1 -- update unique ID generator
if new ~= "" then -- add when not empty
local entry = {id = new_id, name = new}
table.insert(mem.entries, entry)
mem.entries_by_id[new_id] = entry
table.insert(mem.entries, entry) -- store in ordered list
mem.entries_by_id[new_id] = entry -- store in ID -> entry map
end
show_admin_edit()
end
elseif event.msg.delete_entry then
-- removing selected entry
-- TODO either re-index existing votes, or even wipe all of them
-- TODO either re-index existing votes, or even wipe all of them?!
local entry = mem.entries[mem.admin_entries_idx]
table.remove(mem.entries, mem.admin_entries_idx)
mem.entries_by_id[entry.id] = entry
@ -316,8 +325,10 @@ elseif event.type == "digiline" then
mem.state_admin = "welcome"
show_admin_welcome()
elseif event.msg.results_update then
-- just re-generate window (will recalculate results)
show_admin_results()
elseif event.msg.sort_sum then
-- change sorting type and show updated window
mem.sort_type = "sum"
show_admin_results()
elseif event.msg.sort_1 then
@ -335,6 +346,4 @@ elseif event.type == "digiline" then
end
end
end
elseif false then
end