Further implementation of nested functions in interactions, including in consent module

This commit is contained in:
Nathan Schneider 2021-07-31 17:11:26 -06:00
parent 74263b252b
commit 4e10d74bff
3 changed files with 66 additions and 40 deletions

View File

@ -172,3 +172,16 @@ function modpol.interactions.binary_poll_user(user, question, func)
modpol.interactions.message(user, "Error: invalid response")
end
end
-- COMPLEX INTERACTIONS
-- ====================
-- Function: modpol.interactions.message_org
-- input: initiator (string), org_id (number), message (string)
-- output: broadcasts message to all org members
-- Function: modpol.interactions.binary_poll_org
-- input: initator (user string), org_id (number)
-- output: gets question from initiator, asks all org members, broadcasts answers

View File

@ -41,12 +41,16 @@ end
-- ============================
-- interact function for the consent module
function modpol.modules.consent:interact(user)
local resp = modpol.interactions.binary_poll_user(user, "How do you vote?")
if resp == 'yes' then
self:approve(user, true)
elseif resp == 'no' then
self:approve(user, false)
end
-- TODO this needs more context on the vote at hand
modpol.interactions.binary_poll_user(
user, "How do you vote?",
function(vote)
if vote == 'yes' then
self:approve(user, true)
elseif vote == 'no' then
self:approve(user, false)
end
end)
end
-- =========================================
@ -100,4 +104,4 @@ function modpol.modules.consent:update_status()
else
modpol.ocutil.log('Waiting for more votes...')
end
end
end

View File

@ -82,7 +82,7 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
modpol.interactions.binary_poll_user(
pname, input,
function(vote)
modpol.interacts.message(
modpol.interactions.message(
pname, pname .. " voted " .. vote)
end)
end)
@ -196,21 +196,17 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
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):")
modpol.interactions.binary_poll_org(
pname, _contexts[pname].current_org.id,
"Poll question (yes/no):",
function(input)
modpol.interactions.message_org(
_contexts[pname].current_org.id,
pname .. " voted " .. input)
end)
elseif fields.add_child then
--DEPRICATED pre-request version to remove:
--modpol.interactions.add_org(pname, org.id)
modpol.interactions.text_query(pname, "Org name:")
-- local new_org_name = _contexts[pname]["new_org_name"]
_contexts[pname] = function(input)
local new_request = {
user = pname,
type = "add_org",
params = {input}
}
org:make_request(new_request)
_contexts[pname] = nil
end
modpol.interactions.add_org(
pname, _contexts[pname].current_org.id)
elseif fields.remove_org then
modpol.interactions.remove_org(pname)
elseif fields.back then
@ -257,16 +253,19 @@ 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)
local func = _contexts[pname]["text_query_func"]
if func then
func(input)
if not input then
-- no input, do nothing
else
modpol.interactions.message(pname, "text_query: " .. input)
local func = _contexts[pname]["text_query_func"]
if func then
func(input)
else
modpol.interactions.message(pname, "text_query: " .. input)
end
end
end
end)
-- Function: dropdown_query
-- input: user (string), label (string), options (table of strings), func (function)
-- func input: choice (string)
@ -330,7 +329,7 @@ function modpol.interactions.binary_poll_user(user, question, func)
_contexts[user]["binary_poll_func"] = func
-- present to player
minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
modpol.interactions.message(user,"should have showed formspec: " .. question)
modpol.interactions.message(user,"should have showed formspec: " .. question)
end
minetest.register_on_player_receive_fields(function (player, formname, fields)
local pname = player:get_player_name()
@ -342,8 +341,8 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
end
minetest.close_formspec(pname, formname)
if vote then
modpol.interactions.message(pname, "Vote recorded")
local func = _contexts[user]["binary_poll_func"]
modpol.interactions.message(pname, "Voted " .. vote)
local func = _contexts[pname]["binary_poll_func"]
func(vote)
end
end
@ -352,21 +351,31 @@ end)
-- COMPLEX INTERACTIONS
-- ====================
-- Function: modpol.interactions.message_org
-- input: initiator (string), org_id (number), message (string)
-- output: broadcasts message to all org members
function modpol.interactions.message_org(initiator, org_id, message)
local org = modpol.orgs.get_org(org_id)
local users = org:list_member()
for k,v in ipairs(users) do
modpol.interactions.message(v, message)
end
end
-- 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] =
-- input: initator (user string), org_id (number)
-- output: gets question from initiator, asks all org members, broadcasts answers
function modpol.interactions.binary_poll_org(initiator, org_id)
local org = modpol.orgs.get_org(org_id)
local users = org:list_member()
modpol.interactions.text_query(
initiator, "Yes/no poll question:",
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)
end
-- Function: modpol.interactions.add_org