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

@@ -95,7 +95,6 @@ function modpol.interactions.org_dashboard(user, org_name)
process:interact(user)
end
-- ===================================================================
-- Function: modpol.interactions.message
-- input: user (string), message (string)
-- output: prints message to CLI
@@ -103,7 +102,6 @@ function modpol.interactions.message(user, message)
print(user .. ": " .. message)
end
-- ===================================================================
-- Function: modpol.interactions.text_query
-- input: User (string), Query (string), func (function)
-- func input: user input (string)
@@ -114,12 +112,49 @@ function modpol.interactions.text_query(user, query, func)
func(answer)
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)
-- Params: user (string), question (string), func (function)
-- func input: user input (string: y/n)
-- 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)
local query = "Poll for " .. user .. " (y/n): ".. question
local answer