2021-04-28 23:13:36 -06:00

356 lines
12 KiB
Lua

-- INTERACTIONS.LUA (for Minetest)
-- 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 = {}
-- UTILITIES
-- =========
-- Function: formspec_list
-- for use generating option lists in formspecs from tables
-- input: table of strings
-- output: a formspec-ready list of the strings
local function formspec_list(array)
local escaped = {}
if not array then
return ""
end
for i = 1, #array do
escaped[i] = minetest.formspec_escape(array[i])
end
return table.concat(escaped,",")
end
-- DASHBOARDS
-- ==========
-- Function: modpol.interactions.dashboard(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
-- TODO currently a manually curated list---needs major improvement
function modpol.interactions.dashboard(user)
-- prepare data
-- to add: nested orgs map
local all_orgs = modpol.orgs.list_all()
local user_orgs = modpol.orgs.user_orgs(user)
local all_users = modpol.list_users()
-- set up formspec
local formspec = {
"formspec_version[4]",
"size[10,8]",
"label[0.5,0.5;M O D U L A R P O L I T I C S]",
"label[0.5,2;All orgs:]",
"dropdown[2,1.5;5,0.8;all_orgs;"..formspec_list(all_orgs)..";;]",
"label[0.5,3;Your orgs:]",
"dropdown[2,2.5;5,0.8;user_orgs;"..formspec_list(user_orgs)..";;]",
"label[0.5,4;All users:]",
"dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]",
"label[0.5,5;Processes:]",
"dropdown[2,4.5;5,0.8;processes;TBA;;]",
"button[0.5,7;1,0.8;test_poll;Test poll]",
"button[2,7;1,0.8;add_org;Add org]",
"button[3.5,7;1.5,0.8;remove_org;Remove org]",
"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
-- receive input
minetest.register_on_player_receive_fields(function (player, formname, fields)
if formname == "modpol:dashboard" then
local pname = player:get_player_name()
if nil then
-- buttons first
elseif fields.test_poll then
modpol.interactions.binary_poll_org(pname, 1, "Poll question (yes/no):")
elseif fields.add_org then
modpol.interactions.add_org(pname, 1)
elseif fields.remove_org then
modpol.interactions.remove_org(pname)
-- dropdowns need to go last
elseif fields.close then
minetest.close_formspec(pname, formname)
elseif fields.all_orgs or fields.user_orgs then
local org_name = fields.all_orgs or fields.user_orgs
modpol.interactions.org_dashboard(pname, org_name)
end
end
end)
-- Function: modpol.interactions.org_dashboard
-- Params: user (string), org_name (string)
-- Output: Displays a menu of org-specific commands to the user
function modpol.interactions.org_dashboard(user, org_name)
-- prepare data
local org = modpol.orgs.get_org(org_name)
if not org then return nil end
local is_member = org:has_member(user)
local membership_toggle = function()
local toggle_code = ""
if is_member then
toggle_code = toggle_code
..minetest.formspec_escape("leave")..";"
..minetest.formspec_escape("Leave").."]"
else
toggle_code = toggle_code
..minetest.formspec_escape("join")..";"
..minetest.formspec_escape("Join").."]"
end
return toggle_code
end
local children = {}
for k,v in ipairs(org.children) do
local this_child = modpol.orgs.get_org(v)
table.insert(children, this_child.name)
end
-- set player context
local user_context = {}
user_context["current_org"] = org_name
_contexts[user] = user_context
-- set up formspec
local formspec = {
"formspec_version[4]",
"size[10,8]",
"label[0.5,0.5;Org: "..
minetest.formspec_escape(org_name).."]",
"label[0.5,1;Parent: TODO]",
"button[8.5,0.5;1,0.8;"..membership_toggle(),
"label[0.5,2;Members:]",
"dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]",
"label[0.5,3;Children:]",
"dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]",
"label[0.5,4;Policies:]",
"dropdown[2,3.5;5,0.8;policies;"..formspec_list(org.policies)..";;]",
"label[0.5,5;Processes:]",
"dropdown[2,4.5;5,0.8;processes;"..formspec_list(org.processes)..";;]",
"button[0.5,7;1,0.8;test_poll;Test poll]",
"button[2,7;1,0.8;add_child;Add child]",
"button[3.5,7;1.5,0.8;remove_org;Remove org]",
"button[8.5,7;1,0.8;back;Back]",
}
local formspec_string = table.concat(formspec, "")
-- present to player
minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
end
-- receive input
minetest.register_on_player_receive_fields(function (player, formname, fields)
if formname == "modpol:org_dashboard" then
local pname = player:get_player_name()
local org = modpol.orgs.get_org(_contexts[pname].current_org)
if nil then
elseif fields.join then
org:add_member(pname)
modpol.interactions.org_dashboard(pname,org.name)
elseif fields.leave then
org:remove_member(pname)
modpol.interactions.dashboard(pname)
elseif fields.test_poll then
modpol.interactions.binary_poll_org(pname, _contexts.pname.current_org.id, "Poll question (yes/no):")
elseif fields.add_child then
modpol.interactions.add_org(pname, org.id)
elseif fields.remove_org then
modpol.interactions.remove_org(pname)
elseif fields.back then
modpol.interactions.dashboard(pname)
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
function modpol.interactions.text_query(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)
end
-- receive fields
minetest.register_on_player_receive_fields(function (player, formname, fields)
if formname == "modpol:text_query" then
local pname = player:get_player_name()
if _contexts[pname] then
_contexts[pname](fields.input)
end
minetest.close_formspec(pname, formname)
end
end)
-- Function: dropdown_query
-- input: user (string), label (string), options (table of strings)
function modpol.interactions.dropdown_query(user, label, options)
-- set up formspec
local formspec = {
"formspec_version[4]",
"size[10,4]",
"label[0.5,1;"..minetest.formspec_escape(label).."]",
"dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
"button[0.5,2.5;1,0.8;yes;OK]",
}
local formspec_string = table.concat(formspec, "")
-- present to players
minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
end
-- receive fields
minetest.register_on_player_receive_fields(function (player, formname, fields)
if formname == "modpol:dropdown_query" then
local pname = player:get_player_name()
if _contexts[pname] then
_contexts[pname](fields.input)
end
minetest.close_formspec(pname, formname)
end
end)
-- SECONDARY INTERACTIONS
-- ======================
-- 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)
-- COMPLEX INTERACTIONS
-- ====================
-- Function: modpol.interactions.binary_poll_org
-- input: initator (user string), org (number)
-- output: interaction begins
function modpol.interactions.binary_poll_org(initiator, org)
-- start formspec
modpol.interactions.text_query(initiator, "Poll question (yes/no):")
-- set user's context to followup function
_contexts[initiator] =
function(input)
local users = modpol.list_users()
for k,v in ipairs(users) do
modpol.interactions.binary_poll_user(v, input)
end
_contexts[initiator] = nil
end
end
-- Function: modpol.interactions.add_org
-- input: initator (user string), base_org_id (ID)
-- output: interaction begins
function modpol.interactions.add_org(initiator, base_org_id)
-- start formspec
modpol.interactions.text_query(initiator, "Org name:")
-- set user's context to followup function
_contexts[initiator] = function(input)
if input then
local base_org = modpol.orgs.get_org(base_org_id)
local result = base_org:add_org(input, initiator)
if result then
local message = input .. " created"
modpol.interactions.message(initiator, message)
end
end
_contexts[initiator] = nil
modpol.interactions.dashboard(initiator)
end
end
-- Function: modpol.interactions.remove_org
-- input: initator (user string)
-- output: interaction begins
function modpol.interactions.remove_org(initiator)
-- start formspec
local orgs_list = modpol.orgs.list_all()
local label = "Choose an org to remove:"
modpol.interactions.dropdown_query(initiator, label, orgs_list)
-- set user's context to followup function
_contexts[initiator] = function(input)
if input then
local target_org = modpol.orgs.get_org(input)
local result = target_org:delete()
if result then
local message = input .. " deleted"
modpol.interactions.message(initiator, message)
end
end
_contexts[initiator] = nil
modpol.interactions.dashboard(initiator)
end
end