Began creating complex interactions in Minetest with _context
This commit is contained in:
parent
55c3ee9f46
commit
486d310f52
@ -11,7 +11,7 @@ modpol.interactions = {}
|
|||||||
-- Q: Should this return a menu of commands relevant to the specific user?
|
-- Q: Should this return a menu of commands relevant to the specific user?
|
||||||
-- Output: Displays a menu of commands to the user
|
-- Output: Displays a menu of commands to the user
|
||||||
-- TKTK currently just prints all of modpol---needs major improvement
|
-- TKTK currently just prints all of modpol---needs major improvement
|
||||||
modpol.dashboard = function(user)
|
function modpol.dashboard(user)
|
||||||
local output = ""
|
local output = ""
|
||||||
-- Org status
|
-- Org status
|
||||||
output = output .. "Orgs:\n" ..
|
output = output .. "Orgs:\n" ..
|
||||||
@ -31,7 +31,7 @@ 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
|
||||||
modpol.interactions.message = function(user, message)
|
function modpol.interactions.message(user, message)
|
||||||
print(user .. ": " .. message)
|
print(user .. ": " .. message)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ end
|
|||||||
-- Function: modpol.interactions.text_query
|
-- Function: modpol.interactions.text_query
|
||||||
-- input: Query (string)
|
-- input: Query (string)
|
||||||
-- output: User response (string)
|
-- output: User response (string)
|
||||||
modpol.interactions.text_query = function(query)
|
function modpol.interactions.text_query(query)
|
||||||
-- TODO
|
-- TODO
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ regchat(
|
|||||||
"modpol", {
|
"modpol", {
|
||||||
privs = {},
|
privs = {},
|
||||||
func = function(user)
|
func = function(user)
|
||||||
modpol.dashboard(user)
|
modpol.interactions.dashboard(user)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -15,17 +15,16 @@ minetest.register_on_leaveplayer(function(player)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
-- table of formspec field responses
|
-- table of formspec field responses
|
||||||
|
|
||||||
local formspec_fields = {}
|
local formspec_fields = {}
|
||||||
|
|
||||||
-- ===================================================================
|
-- ===================================================================
|
||||||
-- MAIN MODPOL DASHBOARD
|
-- MAIN MODPOL DASHBOARD
|
||||||
-- Function: modpol.modpol(user)
|
-- Function: modpol.interactions.dashboard(user)
|
||||||
-- Params: user (string)
|
-- Params: user (string)
|
||||||
-- Q: Should this return a menu of commands relevant to the specific user?
|
-- Q: Should this return a menu of commands relevant to the specific user?
|
||||||
-- Output: Displays a menu of commands to the user
|
-- Output: Displays a menu of commands to the user
|
||||||
-- TKTK currently a manually curated list---needs major improvement
|
-- TKTK currently a manually curated list---needs major improvement
|
||||||
modpol.dashboard = function(user)
|
function modpol.interactions.dashboard(user)
|
||||||
-- prepare data
|
-- prepare data
|
||||||
-- to add: my orgs, nested orgs map
|
-- to add: my orgs, nested orgs map
|
||||||
local commands = "Command list: "
|
local commands = "Command list: "
|
||||||
@ -44,25 +43,30 @@ modpol.dashboard = function(user)
|
|||||||
"label[0.5,1.5;", minetest.formspec_escape(orgs), "]",
|
"label[0.5,1.5;", minetest.formspec_escape(orgs), "]",
|
||||||
"label[0.5,2.5;", minetest.formspec_escape(users), "]",
|
"label[0.5,2.5;", minetest.formspec_escape(users), "]",
|
||||||
"button[0.5,7;1,0.8;test_poll;Test poll]",
|
"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]",
|
"button_exit[8.5,7;1,0.8;close;Close]",
|
||||||
}
|
}
|
||||||
local formspec_string = table.concat(formspec, "")
|
local formspec_string = table.concat(formspec, "")
|
||||||
-- present to player
|
-- present to player
|
||||||
minetest.show_formspec(user, "modpol:dashboard", formspec_string)
|
minetest.show_formspec(user, "modpol:dashboard", formspec_string)
|
||||||
end
|
end
|
||||||
|
-- receive input
|
||||||
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
||||||
if formname == "modpol:dashboard" then
|
if formname == "modpol:dashboard" then
|
||||||
local pname = player:get_player_name()
|
local pname = player:get_player_name()
|
||||||
if fields.test_poll then
|
if fields.test_poll then
|
||||||
modpol.interactions.text_query(pname, "Poll question (yes/no):")
|
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)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- BASIC INTERACTION FUNCTIONS --
|
-- BASIC INTERACTION FUNCTIONS --
|
||||||
|
|
||||||
-- ===================================================================
|
|
||||||
-- Function: modpol.interactions.message
|
-- Function: modpol.interactions.message
|
||||||
-- input: message (string)
|
-- input: message (string)
|
||||||
-- output
|
-- output
|
||||||
@ -70,14 +74,12 @@ modpol.interactions.message = function(user, message)
|
|||||||
minetest.chat_send_player(user, message)
|
minetest.chat_send_player(user, message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- ===================================================================
|
|
||||||
-- Function: modpol.interactions.text_query
|
-- Function: modpol.interactions.text_query
|
||||||
-- Overrides function at modpol/interactions.lua
|
-- Overrides function at modpol/interactions.lua
|
||||||
-- input: Query (string), User (string)
|
-- input: Query (string), User (string)
|
||||||
-- output: User response (string)
|
-- output: User response (string)
|
||||||
-- TODO Need to switch "user" to index not name
|
-- TODO Need to switch "user" to index not name
|
||||||
modpol.interactions.text_query = function(user, query)
|
function modpol.interactions.text_query(user, query)
|
||||||
-- set up formspec
|
-- set up formspec
|
||||||
local formspec = {
|
local formspec = {
|
||||||
"formspec_version[4]",
|
"formspec_version[4]",
|
||||||
@ -89,46 +91,67 @@ modpol.interactions.text_query = function(user, query)
|
|||||||
local formspec_string = table.concat(formspec, "")
|
local formspec_string = table.concat(formspec, "")
|
||||||
-- present to players
|
-- present to players
|
||||||
minetest.show_formspec(user, "modpol:text_query", formspec_string)
|
minetest.show_formspec(user, "modpol:text_query", formspec_string)
|
||||||
-- receive input
|
|
||||||
-- return output
|
|
||||||
end
|
end
|
||||||
|
-- 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:text_query" then
|
if formname == "modpol:text_query" then
|
||||||
local pname = player:get_player_name()
|
local pname = player:get_player_name()
|
||||||
local users = modpol.list_users()
|
if _contexts[pname] then
|
||||||
for k,v in ipairs(users) do
|
_contexts[pname](fields.input)
|
||||||
modpol.interactions.binary_poll_user(v, fields.input)
|
|
||||||
end
|
end
|
||||||
minetest.close_formspec(pname, formname)
|
minetest.close_formspec(pname, formname)
|
||||||
end
|
end
|
||||||
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 options_string = table.concat(options,",")
|
||||||
|
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;".. minetest.formspec_escape(options_string)..";;]",
|
||||||
|
"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
|
||||||
-- BASIC PARTICIPATION FUNCTIONS
|
-- ======================
|
||||||
|
|
||||||
|
|
||||||
-- ===================================================================
|
|
||||||
-- Function: modpol.binary_poll_user(user, question)
|
-- Function: modpol.binary_poll_user(user, question)
|
||||||
-- Overrides function at modpol/interactions.lua
|
-- Overrides function at modpol/interactions.lua
|
||||||
-- presents a yes/no poll to a user, returns answer
|
-- presents a yes/no poll to a user, returns answer
|
||||||
--
|
--
|
||||||
function modpol.interactions.binary_poll_user(user, question)
|
function modpol.interactions.binary_poll_user(user, question)
|
||||||
-- set up formspec
|
-- set up formspec
|
||||||
local text = "Poll: " .. question
|
local text = "Poll: " .. question
|
||||||
local formspec = {
|
local formspec = {
|
||||||
"formspec_version[4]",
|
"formspec_version[4]",
|
||||||
"size[5,3]",
|
"size[5,3]",
|
||||||
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
|
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
|
||||||
"button[1,1.5;1,0.8;yes;Yes]",
|
"button[1,1.5;1,0.8;yes;Yes]",
|
||||||
"button[2,1.5;1,0.8;no;No]",
|
"button[2,1.5;1,0.8;no;No]",
|
||||||
--TKTK can we enable text wrapping?
|
--TKTK can we enable text wrapping?
|
||||||
--TKTK we could use scroll boxes to contain the text
|
--TKTK we could use scroll boxes to contain the text
|
||||||
}
|
}
|
||||||
local formspec_string = table.concat(formspec, "")
|
local formspec_string = table.concat(formspec, "")
|
||||||
-- present to player
|
-- present to player
|
||||||
minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
|
minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
||||||
@ -150,3 +173,68 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|||||||
end
|
end
|
||||||
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
|
||||||
|
modpol.interactions.dashboard(initiator)
|
||||||
|
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)
|
||||||
|
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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user