153 lines
5.2 KiB
Lua
153 lines
5.2 KiB
Lua
-- ===================================================================
|
|
-- CONTEXTUAL STUFF
|
|
|
|
-- First, set up contexts to enable passing across formspecs
|
|
-- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts
|
|
|
|
local _contexts = {}
|
|
local function get_context(name)
|
|
local context = _contexts[name] or {}
|
|
_contexts[name] = context
|
|
return context
|
|
end
|
|
minetest.register_on_leaveplayer(function(player)
|
|
_contexts[player:get_player_name()] = nil
|
|
end)
|
|
|
|
-- table of formspec field responses
|
|
|
|
local formspec_fields = {}
|
|
|
|
-- ===================================================================
|
|
-- MAIN MODPOL DASHBOARD
|
|
-- Function: modpol.modpol(user)
|
|
-- Params: user (string)
|
|
-- Q: Should this return a menu of commands relevant to the specific user?
|
|
-- Output: Displays a menu of commands to the user
|
|
-- TKTK currently a manually curated list---needs major improvement
|
|
modpol.dashboard = function(user)
|
|
-- prepare data
|
|
-- to add: my orgs, nested orgs map
|
|
local commands = "Command list: "
|
|
for key,value in pairs(command_list) do
|
|
commands = commands .. "/" .. value .. " "
|
|
end
|
|
local orgs = "Orgs: " ..
|
|
table.concat(modpol.orgs.list_all(), ", ")
|
|
local users = "Players: "
|
|
.. table.concat(modpol.list_users(), ", ")
|
|
-- set up formspec
|
|
local formspec = {
|
|
"formspec_version[4]",
|
|
"size[10,8]",
|
|
"label[0.5,0.5;", minetest.formspec_escape(commands), "]",
|
|
"label[0.5,1.5;", minetest.formspec_escape(orgs), "]",
|
|
"label[0.5,2.5;", minetest.formspec_escape(users), "]",
|
|
"button[0.5,7;1,0.8;test_poll;Test poll]",
|
|
"button_exit[8.5,7;1,0.8;close;Close]",
|
|
}
|
|
local formspec_string = table.concat(formspec, "")
|
|
-- present to player
|
|
minetest.show_formspec(user, "modpol:dashboard", formspec_string)
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|
if formname == "modpol:dashboard" then
|
|
local pname = player:get_player_name()
|
|
if fields.test_poll then
|
|
modpol.interactions.text_query(pname, "Poll question (yes/no):")
|
|
end
|
|
end
|
|
end)
|
|
|
|
-- BASIC INTERACTION FUNCTIONS --
|
|
|
|
-- ===================================================================
|
|
-- Function: modpol.interactions.message
|
|
-- input: message (string)
|
|
-- output
|
|
modpol.interactions.message = function(user, message)
|
|
minetest.chat_send_player(user, message)
|
|
end
|
|
|
|
|
|
-- ===================================================================
|
|
-- Function: modpol.interactions.text_query
|
|
-- Overrides function at modpol/interactions.lua
|
|
-- input: Query (string), User (string)
|
|
-- output: User response (string)
|
|
-- TODO Need to switch "user" to index not name
|
|
modpol.interactions.text_query = function(user, query)
|
|
-- set up formspec
|
|
local formspec = {
|
|
"formspec_version[4]",
|
|
"size[10,4]",
|
|
"label[0.5,1;", minetest.formspec_escape(query), "]",
|
|
"field[0.5,1.25;9,0.8;input;;]",
|
|
"button[0.5,2.5;1,0.8;yes;OK]",
|
|
}
|
|
local formspec_string = table.concat(formspec, "")
|
|
-- present to players
|
|
minetest.show_formspec(user, "modpol:text_query", formspec_string)
|
|
-- receive input
|
|
-- return output
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|
if formname == "modpol:text_query" then
|
|
local pname = player:get_player_name()
|
|
local users = modpol.list_users()
|
|
for k,v in ipairs(users) do
|
|
modpol.interactions.binary_poll_user(v, fields.input)
|
|
end
|
|
minetest.close_formspec(pname, formname)
|
|
end
|
|
end)
|
|
|
|
|
|
-- ===================================================================
|
|
-- BASIC PARTICIPATION FUNCTIONS
|
|
|
|
|
|
-- ===================================================================
|
|
-- Function: modpol.binary_poll_user(user, question)
|
|
-- Overrides function at modpol/interactions.lua
|
|
-- presents a yes/no poll to a user, returns answer
|
|
--
|
|
function modpol.interactions.binary_poll_user(user, question)
|
|
-- set up formspec
|
|
local text = "Poll: " .. question
|
|
local formspec = {
|
|
"formspec_version[4]",
|
|
"size[5,3]",
|
|
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
|
|
"button[1,1.5;1,0.8;yes;Yes]",
|
|
"button[2,1.5;1,0.8;no;No]",
|
|
--TKTK can we enable text wrapping?
|
|
--TKTK we could use scroll boxes to contain the text
|
|
}
|
|
local formspec_string = table.concat(formspec, "")
|
|
-- present to player
|
|
minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|
local pname = player:get_player_name()
|
|
-- modpol:binary_poll
|
|
if formname == "modpol:binary_poll_user" then
|
|
local vote = nil
|
|
if fields.yes then vote = fields.yes
|
|
elseif fields.no then vote = fields.no
|
|
elseif fields.abstain then vote = fields.abstain
|
|
end
|
|
if vote then
|
|
modpol.interactions.message(pname, "Vote recorded")
|
|
minetest.chat_send_all(pname .. " voted " .. vote)
|
|
--TODO : we should send the message to all in that org, not to all players
|
|
end
|
|
minetest.close_formspec(pname, formname)
|
|
return vote
|
|
end
|
|
end)
|
|
|