Shifting minetest interactions to nested functions
This commit is contained in:
@ -95,7 +95,6 @@ function modpol.interactions.org_dashboard(user, org_name)
|
|||||||
process:interact(user)
|
process:interact(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ===================================================================
|
|
||||||
-- Function: modpol.interactions.message
|
-- Function: modpol.interactions.message
|
||||||
-- input: user (string), message (string)
|
-- input: user (string), message (string)
|
||||||
-- output: prints message to CLI
|
-- output: prints message to CLI
|
||||||
@ -103,7 +102,6 @@ function modpol.interactions.message(user, message)
|
|||||||
print(user .. ": " .. message)
|
print(user .. ": " .. message)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ===================================================================
|
|
||||||
-- Function: modpol.interactions.text_query
|
-- Function: modpol.interactions.text_query
|
||||||
-- input: User (string), Query (string), func (function)
|
-- input: User (string), Query (string), func (function)
|
||||||
-- func input: user input (string)
|
-- func input: user input (string)
|
||||||
@ -114,12 +112,49 @@ function modpol.interactions.text_query(user, query, func)
|
|||||||
func(answer)
|
func(answer)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ===================================================================
|
-- Function: dropdown_query
|
||||||
|
-- input: user (string), label (string), options (table of strings), func(choice) (function)
|
||||||
|
-- func input: choice (string)
|
||||||
|
-- output: calls func on choice
|
||||||
|
function modpol.interactions.dropdown_query(user, label, options, func)
|
||||||
|
-- set up options
|
||||||
|
local options_display = ""
|
||||||
|
local options_number = 0
|
||||||
|
for k,v in ipairs(options) do
|
||||||
|
options_display = options_display .. k .. ". " ..
|
||||||
|
options[k] .. "\n"
|
||||||
|
options_number = options_number + 1
|
||||||
|
end
|
||||||
|
options_display = options_display .. "Select number:"
|
||||||
|
if options_number == 0 then
|
||||||
|
print("Error: No options given for dropdown")
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
-- begin displaying
|
||||||
|
print(user .. ": " .. label)
|
||||||
|
print(options_display)
|
||||||
|
-- read input and produce output
|
||||||
|
local answer
|
||||||
|
answer = io.read()
|
||||||
|
answer = tonumber(answer)
|
||||||
|
if answer then
|
||||||
|
if answer >= 1 and answer <= options_number then
|
||||||
|
print("Selection: " .. options[answer])
|
||||||
|
func(options[answer])
|
||||||
|
else
|
||||||
|
print("Error: Not in dropdown range")
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("Error: Must be a number")
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Function: modpol.binary_poll_user(user, question)
|
-- Function: modpol.binary_poll_user(user, question)
|
||||||
-- Params: user (string), question (string), func (function)
|
-- Params: user (string), question (string), func (function)
|
||||||
-- func input: user input (string: y/n)
|
-- func input: user input (string: y/n)
|
||||||
-- Output: Applies "func" to user input
|
-- Output: Applies "func" to user input
|
||||||
-- presents a yes/no poll to a user, returns answer
|
|
||||||
function modpol.interactions.binary_poll_user(user, question, func)
|
function modpol.interactions.binary_poll_user(user, question, func)
|
||||||
local query = "Poll for " .. user .. " (y/n): ".. question
|
local query = "Poll for " .. user .. " (y/n): ".. question
|
||||||
local answer
|
local answer
|
||||||
|
@ -16,10 +16,6 @@ minetest.register_on_leaveplayer(function(player)
|
|||||||
_contexts[player:get_player_name()] = nil
|
_contexts[player:get_player_name()] = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- table of formspec field responses
|
|
||||||
local formspec_fields = {}
|
|
||||||
|
|
||||||
|
|
||||||
-- UTILITIES
|
-- UTILITIES
|
||||||
-- =========
|
-- =========
|
||||||
|
|
||||||
@ -38,7 +34,6 @@ local function formspec_list(array)
|
|||||||
return table.concat(escaped,",")
|
return table.concat(escaped,",")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- DASHBOARDS
|
-- DASHBOARDS
|
||||||
-- ==========
|
-- ==========
|
||||||
|
|
||||||
@ -80,16 +75,17 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|||||||
if nil then
|
if nil then
|
||||||
-- buttons first
|
-- buttons first
|
||||||
elseif fields.test_poll then
|
elseif fields.test_poll then
|
||||||
local func_announce_vote = function(vote)
|
-- FOR TESTING PURPOSES ONLY
|
||||||
|
modpol.interactions.text_query(
|
||||||
|
pname,"Poll question:",
|
||||||
|
function(input)
|
||||||
|
modpol.interactions.binary_poll_user(
|
||||||
|
pname, input,
|
||||||
|
function(vote)
|
||||||
modpol.interacts.message(
|
modpol.interacts.message(
|
||||||
pname, pname .. " voted " .. vote)
|
pname, pname .. " voted " .. vote)
|
||||||
end
|
end)
|
||||||
local func_start_poll = function(input)
|
end)
|
||||||
modpol.interactions.binary_poll_user(
|
|
||||||
pname, input, func_announce_vote)
|
|
||||||
end
|
|
||||||
modpol.interactions.text_query(
|
|
||||||
pname,"Poll question:",func_start_poll)
|
|
||||||
elseif fields.add_org then
|
elseif fields.add_org then
|
||||||
modpol.interactions.add_org(pname, 1)
|
modpol.interactions.add_org(pname, 1)
|
||||||
elseif fields.remove_org then
|
elseif fields.remove_org then
|
||||||
@ -230,7 +226,7 @@ end)
|
|||||||
-- Function: modpol.interactions.message
|
-- Function: modpol.interactions.message
|
||||||
-- input: message (string)
|
-- input: message (string)
|
||||||
-- output
|
-- output
|
||||||
modpol.interactions.message = function(user, message)
|
function modpol.interactions.message(user, message)
|
||||||
minetest.chat_send_player(user, message)
|
minetest.chat_send_player(user, message)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -261,8 +257,8 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|||||||
local pname = player:get_player_name()
|
local pname = player:get_player_name()
|
||||||
local input = fields.input
|
local input = fields.input
|
||||||
minetest.close_formspec(pname, formname)
|
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)
|
func(input)
|
||||||
else
|
else
|
||||||
modpol.interactions.message(pname, "text_query: " .. input)
|
modpol.interactions.message(pname, "text_query: " .. input)
|
||||||
@ -272,8 +268,10 @@ end)
|
|||||||
|
|
||||||
|
|
||||||
-- Function: dropdown_query
|
-- Function: dropdown_query
|
||||||
-- input: user (string), label (string), options (table of strings)
|
-- input: user (string), label (string), options (table of strings), func (function)
|
||||||
function modpol.interactions.dropdown_query(user, label, options)
|
-- func input: choice (string)
|
||||||
|
-- output: calls func on user choice
|
||||||
|
function modpol.interactions.dropdown_query(user, label, options, func)
|
||||||
-- set up formspec
|
-- set up formspec
|
||||||
local formspec = {
|
local formspec = {
|
||||||
"formspec_version[4]",
|
"formspec_version[4]",
|
||||||
@ -285,15 +283,24 @@ function modpol.interactions.dropdown_query(user, label, options)
|
|||||||
local formspec_string = table.concat(formspec, "")
|
local formspec_string = table.concat(formspec, "")
|
||||||
-- present to players
|
-- present to players
|
||||||
minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
|
minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
|
||||||
|
-- put func in _contexts
|
||||||
|
_contexts[user] = {}
|
||||||
|
_contexts[user]["dropdown_query_func"] = func
|
||||||
end
|
end
|
||||||
-- receive fields
|
-- receive fields
|
||||||
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
||||||
if formname == "modpol:dropdown_query" then
|
if formname == "modpol:dropdown_query" then
|
||||||
local pname = player:get_player_name()
|
local pname = player:get_player_name()
|
||||||
if _contexts[pname] then
|
local choice = fields.input
|
||||||
_contexts[pname](fields.input)
|
|
||||||
end
|
|
||||||
minetest.close_formspec(pname, formname)
|
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
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -363,47 +370,38 @@ function modpol.interactions.binary_poll_org(initiator, org)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Function: modpol.interactions.add_org
|
-- 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)
|
-- input: initator (user string), base_org_id (ID)
|
||||||
-- output: interaction begins
|
-- output: interaction begins
|
||||||
function modpol.interactions.add_org(initiator, base_org_id)
|
function modpol.interactions.add_org(user, base_org_id)
|
||||||
-- start formspec
|
modpol.interactions.text_query(
|
||||||
modpol.interactions.text_query(initiator, "Org name:")
|
user,"Org name:",
|
||||||
-- set user's context to followup function
|
function(input)
|
||||||
_contexts[initiator] = function(input)
|
local base_org = modpol.orgs.get_org(1)
|
||||||
if input then
|
local result = base_org:add_org(input, user)
|
||||||
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"
|
local message = input .. " created"
|
||||||
modpol.interactions.message(initiator, message)
|
modpol.interactions.message(user, message)
|
||||||
end
|
modpol.interactions.dashboard(user)
|
||||||
end
|
end)
|
||||||
_contexts[initiator] = nil
|
|
||||||
modpol.interactions.dashboard(initiator)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Function: modpol.interactions.remove_org
|
-- Function: modpol.interactions.remove_org
|
||||||
|
-- TODO: NEEDS TO BE MADE INTO A REQUEST
|
||||||
-- input: initator (user string)
|
-- input: initator (user string)
|
||||||
-- output: interaction begins
|
-- output: interaction begins
|
||||||
function modpol.interactions.remove_org(initiator)
|
function modpol.interactions.remove_org(user)
|
||||||
-- start formspec
|
-- start formspec
|
||||||
local orgs_list = modpol.orgs.list_all()
|
local orgs_list = modpol.orgs.list_all()
|
||||||
local label = "Choose an org to remove:"
|
local label = "Choose an org to remove:"
|
||||||
modpol.interactions.dropdown_query(initiator, label, orgs_list)
|
modpol.interactions.dropdown_query(
|
||||||
-- set user's context to followup function
|
user, label, orgs_list,
|
||||||
_contexts[initiator] = function(input)
|
function(input)
|
||||||
if input then
|
if input then
|
||||||
local target_org = modpol.orgs.get_org(input)
|
local target_org = modpol.orgs.get_org(input)
|
||||||
local result = target_org:delete()
|
local result = target_org:delete()
|
||||||
if result then
|
|
||||||
local message = input .. " deleted"
|
local message = input .. " deleted"
|
||||||
modpol.interactions.message(initiator, message)
|
modpol.interactions.message(user, message)
|
||||||
end
|
|
||||||
end
|
|
||||||
_contexts[initiator] = nil
|
|
||||||
modpol.interactions.dashboard(initiator)
|
|
||||||
end
|
end
|
||||||
|
modpol.interactions.dashboard(user)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user