some changes and futile attempts to make it not timeout

This commit is contained in:
whosit 2025-03-23 16:23:16 +03:00
parent 070b445757
commit 695351b797

View File

@ -1,14 +1,13 @@
-- luacheck: globals event mem digiline_send, no max line length
-- The machine requires _three_ touch screens connected: one set to channel "vote", other set to "admin", and third to "submit"
-- FIXME: "results" view not opening when there are no votes?
local categories = {"Creativity", "Aesthetic Appeal", "Detail & Execution", "Use of space"}
local categories = {"Creativity", "Aesthetic\n Appeal", "Detail & \nExecution", "Use of\nspace"}
local num_categories = #categories
local min_cat_score = 1
local max_cat_score = 3
local ENTRIES_PER_PAGE = 8 -- for ballots with large amount of entries
function show_vote_welcome()
local function show_vote_welcome()
-- window shown before player can see the ballot
-- by making them click the button, we can know their name
@ -43,7 +42,7 @@ function show_vote_welcome()
end
function show_vote_error(voter, offender)
local 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(
@ -64,7 +63,7 @@ function show_vote_error(voter, offender)
end
function show_admin_welcome()
local function show_admin_welcome()
-- just a menu to select from 2 windows
local toggle_submit_label = mem.submit_active and "Stop submissions" or "Allow submissions"
@ -102,7 +101,8 @@ function show_admin_welcome()
end
function show_admin_edit()
local function show_admin_edit()
-- screen for adding/removing entries
local list_entries = {}
for _,e in ipairs(mem.entries) do
@ -136,8 +136,7 @@ function show_admin_edit()
)
end
function show_admin_results()
local function show_admin_results()
-- count and show results
-- count the votes that we actually cast
@ -168,7 +167,7 @@ function show_admin_results()
for _,player_votes in pairs(mem.votes) do
local entry_votes = player_votes[id] or {} -- nil if didn't cast any votes for this entry
for c_i = 1, num_categories do
local vote = (entry_votes[c_i] or min_cat_score)
local vote = (entry_votes[c_i] or 1)
entry_scores[c_i] = (entry_scores[c_i] or 0) + vote
end
end
@ -218,7 +217,7 @@ function show_admin_results()
end
function show_admin_danger()
local function show_admin_danger()
digiline_send(
"admin",
{
@ -237,7 +236,8 @@ function show_admin_danger()
end
function show_admin_messages()
local function show_admin_messages()
digiline_send(
"admin",
{
@ -260,8 +260,9 @@ end
function show_vote_ballot(username)
local c =
local function show_vote_ballot(username)
local total_pages = math.ceil(#mem.ballot_entries / ENTRIES_PER_PAGE)
local c1 =
{
{command = "clear"},
{command = "set",
@ -271,22 +272,22 @@ function show_vote_ballot(username)
},
{command = "addtextarea", name='', default = username .. " - " .. mem.message_welcome_vote, X = 0.5, Y = 0.4, W = 9.0, H = 0.8},
{command = "addtextarea", name='', default = categories[1], X = 10.5, Y = 0.4, W = 1.6, H = 1.0},
{command = "addtextarea", name='', default = categories[2], X = 12.2, Y = 0.4, W = 1.6, H = 1.0},
{command = "addtextarea", name='', default = categories[3], X = 13.9, Y = 0.4, W = 1.6, H = 1.0},
{command = "addtextarea", name='', default = categories[4], X = 15.7, Y = 0.4, W = 1.6, H = 1.0},
{command = "addlabel", label = categories[1], X = 10.5, Y = 0.4, W = 1.6, H = 1.0},
{command = "addlabel", label = categories[2], X = 12.2, Y = 0.4, W = 1.6, H = 1.0},
{command = "addlabel", label = categories[3], X = 13.9, Y = 0.4, W = 1.6, H = 1.0},
{command = "addlabel", label = categories[4], X = 15.7, Y = 0.4, W = 1.6, H = 1.0},
{command = "addlabel", label = "Your choices are saved automatically!", X = 11.5, Y = 12.1},
{command = "addbutton", name = "vote_done", label = "Done", X = 14.0, Y = 12.5, W = 3, H = 1},
{command = "addlabel", label = ("Page %s/%s"):format(mem.ballot_page+1, total_pages), X = 0.8, Y = 12.5}
}
digiline_send("vote", c1)
local shift = 1.3 -- vertical spacing between entries
local votes = mem.votes[username] or {}
local page = mem.ballot_page
local entries = mem.ballot_entries
local choices = {}
for idx = 0, (max_cat_score - min_cat_score) do
choices[idx+1] = tostring(idx + min_cat_score)
end
local choices = "1,2,3,4,5"
for i=1,ENTRIES_PER_PAGE do
local e = entries[i + ENTRIES_PER_PAGE*page]
@ -294,7 +295,7 @@ function show_vote_ballot(username)
break -- not enough entries to fill the page
end
local id = e.id
local default_id = min_cat_score == 0 and 0 or 1
local default_id = 1
local e_v = votes[id] or {} -- previous votes of this player for this entry (if any)
local y_offset = shift*i
@ -302,33 +303,36 @@ function show_vote_ballot(username)
local v_2 = e_v[2]
local v_3 = e_v[3]
local v_4 = e_v[4]
local id_str = "v_" .. tostring(id)
table.insert(c, {command = "addimage", texture_name = "halo.png^[colorize:#222233",
X = 0.5, Y = 0.4 + y_offset, W = 16.8, H = 1})
table.insert(c, {command = "addlabel",
label = string.sub(string.format("[%s] - %s", e.user, e.title), 1, 75),
X = 0.8, Y = 0.9 + y_offset})
local c = {
{command = "addimage", texture_name = "halo.png^[colorize:#222233",
X = 0.5, Y = 0.4 + y_offset, W = 16.8, H = 1},
{command = "addlabel",
label = ("[%s] - %s"):format(e.user, e.title),
X = 0.8, Y = 0.9 + y_offset},
table.insert(c, {command = "adddropdown", name = string.format("v_%d_1", id), index_event = true,
selected_id = v_1 and (v_1 - min_cat_score + 1) or default_id,
choices = choices, X = 10.8, Y = 0.5 + y_offset, W = 0.8, H = 0.8})
{command = "adddropdown", name = id_str.."_1", index_event = true,
selected_id = v_1 or default_id,
choices = choices, X = 10.8, Y = 0.5 + y_offset, W = 0.8, H = 0.8},
table.insert(c, {command = "adddropdown", name = string.format("v_%d_2", id), index_event = true,
selected_id = v_2 and (v_2 - min_cat_score + 1) or default_id,
choices = choices, X = 12.6, Y = 0.5 + y_offset, W = 0.8, H = 0.8})
{command = "adddropdown", name = id_str.."_2", index_event = true,
selected_id = v_2 or default_id,
choices = choices, X = 12.6, Y = 0.5 + y_offset, W = 0.8, H = 0.8},
table.insert(c, {command = "adddropdown", name = string.format("v_%d_3", id), index_event = true,
selected_id = v_3 and (v_3 - min_cat_score + 1) or default_id,
choices = choices, X = 14.4, Y = 0.5 + y_offset, W = 0.8, H = 0.8})
{command = "adddropdown", name = id_str.."_3", index_event = true,
selected_id = v_3 or default_id,
choices = choices, X = 14.4, Y = 0.5 + y_offset, W = 0.8, H = 0.8},
table.insert(c, {command = "adddropdown", name = string.format("v_%d_4", id), index_event = true,
selected_id = v_4 and (v_4 - min_cat_score + 1) or default_id,
choices = choices, X = 16.2, Y = 0.5 + y_offset, W = 0.8, H = 0.8})
{command = "adddropdown", name = id_str.."_4", index_event = true,
selected_id = v_4 or default_id,
choices = choices, X = 16.2, Y = 0.5 + y_offset, W = 0.8, H = 0.8},
}
digiline_send("vote", c)
end
local c = {}
-- snow number of pages and page flipping buttons
local total_pages = math.ceil(#mem.ballot_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})
end
@ -337,11 +341,12 @@ function show_vote_ballot(username)
end
digiline_send("vote", c)
end
function show_submit_welcome()
local function show_submit_welcome()
if mem.submit_active then
digiline_send(
"submit",
@ -373,7 +378,7 @@ function show_submit_welcome()
end
function show_submit(username)
local function show_submit(username)
local submission = mem.entries_by_user[username] or {}
digiline_send(
@ -398,6 +403,7 @@ function show_submit(username)
)
end
if event.type == "program" then
-- initialize defaults or use stored values
@ -455,6 +461,7 @@ elseif event.type == "digiline" then
-- TODO show the offender's name on this screen
local name = mem.username
mem.username = nil
show_vote_error(name, event.msg.clicker)
end
@ -470,7 +477,7 @@ elseif event.type == "digiline" then
local entry = votes[e_id] or {}
-- store vote
votes[e_id] = entry
entry[c_id] = tonumber(v) + min_cat_score - 1 or min_cat_score -- vote value for this category
entry[c_id] = tonumber(v) or 1 -- vote value for this category
end
end
--digiline_send("debug", mem.votes) -- TODO remove this
@ -493,7 +500,7 @@ elseif event.type == "digiline" then
}
)
end
if not (event.msg.clicker == mem.submitter_name) then
if event.msg.clicker ~= mem.submitter_name then
show_error("#FF0000", string.format("%s pressed submit!\nYou can't submit for another user! Press 'back' and enter again!", user))
return
end
@ -564,9 +571,11 @@ elseif event.type == "digiline" then
show_submit_welcome()
elseif event.msg.edit_messages then
mem.state_admin = "messages"
show_admin_messages()
elseif event.msg.danger_zone then
mem.state_admin = "danger_zone"
show_admin_danger()
end
elseif mem.state_admin == "edit" then
@ -576,6 +585,8 @@ elseif event.type == "digiline" then
if e:sub(1,4) == "CHG:" then
mem.admin_entries_idx = tonumber(e:sub(5))
end
show_admin_edit()
elseif event.msg.add_entry or (event.msg.key_enter_field == "new_entry_title") then
-- adding new entry
@ -583,7 +594,7 @@ elseif event.type == "digiline" then
if not new_title: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 -- update unique ID generator
if new ~= "" then -- add when not empty
if new_title ~= "" then -- add when not empty
local user = event.msg.new_entry_submitter
local entry = { id = new_id, title = new_title, user = user }
table.insert(mem.entries, entry) -- store in ordered list
@ -602,7 +613,7 @@ elseif event.type == "digiline" then
if entry.user then
mem.entries_by_user[entry.user] = nil
end
for pname, votes in pairs(mem.votes) do
for _pname, votes in pairs(mem.votes) do
votes[entry.id] = nil
end
-- try to keep stored index up-to-date with what user is seeing