Shifting minetest interactions to nested functions

This commit is contained in:
Nathan Schneider
2021-07-31 00:00:35 -06:00
parent 3885f9af78
commit 74263b252b
2 changed files with 91 additions and 58 deletions

View File

@ -16,10 +16,6 @@ minetest.register_on_leaveplayer(function(player)
_contexts[player:get_player_name()] = nil
end)
-- table of formspec field responses
local formspec_fields = {}
-- UTILITIES
-- =========
@ -38,7 +34,6 @@ local function formspec_list(array)
return table.concat(escaped,",")
end
-- DASHBOARDS
-- ==========
@ -78,18 +73,19 @@ 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
-- buttons first
elseif fields.test_poll then
local func_announce_vote = function(vote)
modpol.interacts.message(
pname, pname .. " voted " .. vote)
end
local func_start_poll = function(input)
modpol.interactions.binary_poll_user(
pname, input, func_announce_vote)
end
-- FOR TESTING PURPOSES ONLY
modpol.interactions.text_query(
pname,"Poll question:",func_start_poll)
pname,"Poll question:",
function(input)
modpol.interactions.binary_poll_user(
pname, input,
function(vote)
modpol.interacts.message(
pname, pname .. " voted " .. vote)
end)
end)
elseif fields.add_org then
modpol.interactions.add_org(pname, 1)
elseif fields.remove_org then
@ -230,7 +226,7 @@ end)
-- Function: modpol.interactions.message
-- input: message (string)
-- output
modpol.interactions.message = function(user, message)
function modpol.interactions.message(user, message)
minetest.chat_send_player(user, message)
end
@ -261,8 +257,8 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
local pname = player:get_player_name()
local input = fields.input
minetest.close_formspec(pname, formname)
if _contexts[pname]["text_query_func"] then
local func = _contexts[pname]["text_query_func"]
local func = _contexts[pname]["text_query_func"]
if func then
func(input)
else
modpol.interactions.message(pname, "text_query: " .. input)
@ -272,8 +268,10 @@ end)
-- Function: dropdown_query
-- input: user (string), label (string), options (table of strings)
function modpol.interactions.dropdown_query(user, label, options)
-- input: user (string), label (string), options (table of strings), func (function)
-- func input: choice (string)
-- output: calls func on user choice
function modpol.interactions.dropdown_query(user, label, options, func)
-- set up formspec
local formspec = {
"formspec_version[4]",
@ -285,15 +283,24 @@ function modpol.interactions.dropdown_query(user, label, options)
local formspec_string = table.concat(formspec, "")
-- present to players
minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
-- put func in _contexts
_contexts[user] = {}
_contexts[user]["dropdown_query_func"] = func
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
local choice = fields.input
minetest.close_formspec(pname, formname)
local func = _contexts[pname]["dropdown_query_func"]
if not choice then
-- no choice, do nothing
elseif func then
func(choice)
else
modpol.interactions.message(pname, "dropdown_query: " .. choice)
end
end
end)
@ -363,47 +370,38 @@ function modpol.interactions.binary_poll_org(initiator, org)
end
-- Function: modpol.interactions.add_org
-- TESTING PURPOSES. SHOULD BE DEPRICATED
-- TODO: NEEDS TO BE MADE INTO A REQUEST
-- 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
function modpol.interactions.add_org(user, base_org_id)
modpol.interactions.text_query(
user,"Org name:",
function(input)
local base_org = modpol.orgs.get_org(1)
local result = base_org:add_org(input, user)
local message = input .. " created"
modpol.interactions.message(user, message)
modpol.interactions.dashboard(user)
end)
end
-- Function: modpol.interactions.remove_org
-- TODO: NEEDS TO BE MADE INTO A REQUEST
-- input: initator (user string)
-- output: interaction begins
function modpol.interactions.remove_org(initiator)
function modpol.interactions.remove_org(user)
-- 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
modpol.interactions.dropdown_query(
user, label, orgs_list,
function(input)
if input then
local target_org = modpol.orgs.get_org(input)
local result = target_org:delete()
local message = input .. " deleted"
modpol.interactions.message(initiator, message)
modpol.interactions.message(user, message)
end
end
_contexts[initiator] = nil
modpol.interactions.dashboard(initiator)
end
modpol.interactions.dashboard(user)
end)
end