This commit is contained in:
whosit 2025-03-06 11:54:20 +03:00
parent 765fd718e5
commit d3561e3b6c

View File

@ -56,7 +56,11 @@ end
function show_admin_edit()
local listentries = table.concat(mem.entries, ",") -- TODO sanitize :p
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
digiline_send("admin",
{
{command = "clear"},
@ -66,7 +70,7 @@ function show_admin_edit()
real_coordinates = true,
locked = true, -- does not prevent someone from looking at it :(
},
{command = "addtextlist", name = "entries", listelements = listentries, transparent = false, selected_id = mem.admin_entries_idx, X = 1.2, Y = 1.8, W = 10.5, H = 7.9},
{command = "addtextlist", name = "entries", listelements = list_str, transparent = false, selected_id = mem.admin_entries_idx, X = 1.2, Y = 1.8, W = 10.5, H = 7.9},
{command = "addfield", name = "new_entry", label = "New entry", default = "", X = 1.2, Y = 10.4, W = 8.5, H = 0.8},
{command = "addbutton", name = "add_entry", label = "Add entry", X = 9.9, Y = 10.4, W = 1.7, H = 0.8},
{command = "addbutton", name = "delete_entry", label = "Delete selected", X = 12.2, Y = 8, W = 3, H = 0.8},
@ -94,9 +98,10 @@ function show_admin_results()
-- end
local total_scores = {}
for id, e in pairs(mem.entries) do
for i, e in ipairs(mem.entries) do
local id = e.id
local entry_scores = {}
total_scores[id] = entry_scores
total_scores[i] = entry_scores
entry_scores.id = id
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
@ -107,7 +112,7 @@ function show_admin_results()
end
end
local sorted_by_str
local sorted_by_str = "none"
if mem.sort_type == "sum" then
sorted_by_str = "Sorted by sum:"
table.sort(total_scores, function(a,b) return (a[1] + a[2] + a[3] + a[4]) > (b[1] + b[2] + b[3] + b[4]) end)
@ -119,8 +124,9 @@ function show_admin_results()
local entry_list = {}
for _, s in ipairs(total_scores) do
local name = mem.entries[s.id]
table.insert(entry_list, string.format("%03d|%03d|%03d|%03d -- %s", s[1], s[2], s[3], s[4], name))
local name = mem.entries_by_id[s.id].name
local sum = s[1] + s[2] + s[3] + s[4]
table.insert(entry_list, string.format("%03d||%03d|%03d|%03d|%03d -- %s", sum, s[1], s[2], s[3], s[4], name))
end
entry_list = table.concat(entry_list, ",")
@ -137,6 +143,7 @@ function show_admin_results()
locked = true, -- does not prevent someone from looking at it :(
},
{command = "addlabel", label = sorted_by_str, X = 0.9, Y = 0.6},
{command = "addbutton", name = "results_update", label = "Update", X = 8.7, Y = 0.3, W = 3, H = 1},
{command = "addbutton", name = "results_back", label = "Back", X = 12.7, Y = 0.3, W = 3, H = 1},
{command = "addtextlist", name = "", listelements = entry_list, transparent = false, selected_id = 1, X = 0.3, Y = 1.5, W = 12.3, H = 9.2},
{command = "addlabel", label = "Sort by:", X = 13.5, Y = 2.6},
@ -181,14 +188,15 @@ function show_vote_ballot(username)
}
local shift = 1.3
local votes = mem.votes[username] or {}
for i,e in ipairs(mem.entries) do
for i,e in pairs(mem.entries) do
local e_v = votes[i] or {}
local id = e.id
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, X = 0.8, Y = 0.9 + shift*i})
table.insert(c, {command = "adddropdown", name = string.format("v_%d_1", i), 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})
table.insert(c, {command = "adddropdown", name = string.format("v_%d_2", i), index_event = true, selected_id = e_v[2] or 1, choices = {[1] = "1", [2] = "2", [3] = "3"}, X = 12.6, Y = 0.5 + shift*i, W = 0.8, H = 0.8})
table.insert(c, {command = "adddropdown", name = string.format("v_%d_3", i), index_event = true, selected_id = e_v[3] or 1, choices = {[1] = "1", [2] = "2", [3] = "3"}, X = 14.4, Y = 0.5 + shift*i, W = 0.8, H = 0.8})
table.insert(c, {command = "adddropdown", name = string.format("v_%d_4", i), 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})
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})
table.insert(c, {command = "adddropdown", name = string.format("v_%d_2", id), index_event = true, selected_id = e_v[2] or 1, choices = {[1] = "1", [2] = "2", [3] = "3"}, X = 12.6, Y = 0.5 + shift*i, W = 0.8, H = 0.8})
table.insert(c, {command = "adddropdown", name = string.format("v_%d_3", id), index_event = true, selected_id = e_v[3] or 1, choices = {[1] = "1", [2] = "2", [3] = "3"}, X = 14.4, Y = 0.5 + shift*i, W = 0.8, H = 0.8})
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
digiline_send("vote", c)
@ -198,8 +206,10 @@ end
if event.type == "program" then
show_vote_welcome()
show_admin_welcome()
mem.id_count = mem.id_count or 1
mem.username = nil
mem.entries = mem.entries or {"first"}
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"
@ -260,14 +270,22 @@ 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 new ~= "" then
table.insert(mem.entries, new)
if not new:find(",", 1, true) then -- can't have "," in names
local new_id = mem.id_count
mem.id_count = mem.id_count + 1
if new ~= "" then
local entry = {id = new_id, name = new}
table.insert(mem.entries, entry)
mem.entries_by_id[new_id] = entry
end
show_admin_edit()
end
show_admin_edit()
elseif event.msg.delete_entry then
-- removing selected entry
-- 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
-- try to keep stored index up-to-date with what user is seeing
mem.admin_entries_idx = math.min(mem.admin_entries_idx, #mem.entries)
show_admin_edit()
@ -279,6 +297,8 @@ elseif event.type == "digiline" then
if event.msg.results_back then
mem.state_admin = "welcome"
show_admin_welcome()
elseif event.msg.results_update then
show_admin_results()
elseif event.msg.sort_sum then
mem.sort_type = "sum"
show_admin_results()