From 486d310f52e513ecab942967b4602b11289d90e3 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 17 Apr 2021 23:37:56 -0600 Subject: [PATCH] Began creating complex interactions in Minetest with _context --- modpol/interactions/interactions.lua | 6 +- modpol_minetest/chatcommands/chatcommands.lua | 2 +- .../overrides/interactions/interactions.lua | 154 ++++++++++++++---- 3 files changed, 125 insertions(+), 37 deletions(-) diff --git a/modpol/interactions/interactions.lua b/modpol/interactions/interactions.lua index dbf1248..4eb9c26 100644 --- a/modpol/interactions/interactions.lua +++ b/modpol/interactions/interactions.lua @@ -11,7 +11,7 @@ modpol.interactions = {} -- Q: Should this return a menu of commands relevant to the specific user? -- Output: Displays a menu of commands to the user -- TKTK currently just prints all of modpol---needs major improvement -modpol.dashboard = function(user) +function modpol.dashboard(user) local output = "" -- Org status output = output .. "Orgs:\n" .. @@ -31,7 +31,7 @@ end -- Function: modpol.interactions.message -- input: user (string), message (string) -- output: prints message to CLI -modpol.interactions.message = function(user, message) +function modpol.interactions.message(user, message) print(user .. ": " .. message) end @@ -39,7 +39,7 @@ end -- Function: modpol.interactions.text_query -- input: Query (string) -- output: User response (string) -modpol.interactions.text_query = function(query) +function modpol.interactions.text_query(query) -- TODO end diff --git a/modpol_minetest/chatcommands/chatcommands.lua b/modpol_minetest/chatcommands/chatcommands.lua index f5cb4f2..33c0ca6 100644 --- a/modpol_minetest/chatcommands/chatcommands.lua +++ b/modpol_minetest/chatcommands/chatcommands.lua @@ -21,7 +21,7 @@ regchat( "modpol", { privs = {}, func = function(user) - modpol.dashboard(user) + modpol.interactions.dashboard(user) end, }) diff --git a/modpol_minetest/overrides/interactions/interactions.lua b/modpol_minetest/overrides/interactions/interactions.lua index 242ba2f..71fcd51 100644 --- a/modpol_minetest/overrides/interactions/interactions.lua +++ b/modpol_minetest/overrides/interactions/interactions.lua @@ -15,17 +15,16 @@ minetest.register_on_leaveplayer(function(player) end) -- table of formspec field responses - local formspec_fields = {} -- =================================================================== -- MAIN MODPOL DASHBOARD --- Function: modpol.modpol(user) +-- Function: modpol.interactions.dashboard(user) -- Params: user (string) -- Q: Should this return a menu of commands relevant to the specific user? -- Output: Displays a menu of commands to the user -- TKTK currently a manually curated list---needs major improvement -modpol.dashboard = function(user) +function modpol.interactions.dashboard(user) -- prepare data -- to add: my orgs, nested orgs map local commands = "Command list: " @@ -44,25 +43,30 @@ modpol.dashboard = function(user) "label[0.5,1.5;", minetest.formspec_escape(orgs), "]", "label[0.5,2.5;", minetest.formspec_escape(users), "]", "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]", } local formspec_string = table.concat(formspec, "") -- present to player minetest.show_formspec(user, "modpol:dashboard", formspec_string) end - +-- receive input minetest.register_on_player_receive_fields(function (player, formname, fields) if formname == "modpol:dashboard" then local pname = player:get_player_name() 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) -- BASIC INTERACTION FUNCTIONS -- --- =================================================================== -- Function: modpol.interactions.message -- input: message (string) -- output @@ -70,14 +74,12 @@ modpol.interactions.message = function(user, message) minetest.chat_send_player(user, message) end - --- =================================================================== -- Function: modpol.interactions.text_query -- Overrides function at modpol/interactions.lua -- input: Query (string), User (string) -- output: User response (string) -- 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 local formspec = { "formspec_version[4]", @@ -89,46 +91,67 @@ modpol.interactions.text_query = function(user, query) local formspec_string = table.concat(formspec, "") -- present to players minetest.show_formspec(user, "modpol:text_query", formspec_string) - -- receive input - -- return output end - +-- receive fields minetest.register_on_player_receive_fields(function (player, formname, fields) if formname == "modpol:text_query" then local pname = player:get_player_name() - local users = modpol.list_users() - for k,v in ipairs(users) do - modpol.interactions.binary_poll_user(v, fields.input) + if _contexts[pname] then + _contexts[pname](fields.input) end minetest.close_formspec(pname, formname) 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) --- =================================================================== --- BASIC PARTICIPATION FUNCTIONS +-- SECONDARY INTERACTIONS +-- ====================== - --- =================================================================== -- Function: modpol.binary_poll_user(user, question) -- Overrides function at modpol/interactions.lua -- presents a yes/no poll to a user, returns answer -- function modpol.interactions.binary_poll_user(user, question) - -- set up formspec - local text = "Poll: " .. question - local formspec = { - "formspec_version[4]", - "size[5,3]", - "label[0.375,0.5;", minetest.formspec_escape(text), "]", - "button[1,1.5;1,0.8;yes;Yes]", - "button[2,1.5;1,0.8;no;No]", - --TKTK can we enable text wrapping? - --TKTK we could use scroll boxes to contain the text - } - local formspec_string = table.concat(formspec, "") - -- present to player - minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string) + -- set up formspec + local text = "Poll: " .. question + local formspec = { + "formspec_version[4]", + "size[5,3]", + "label[0.375,0.5;", minetest.formspec_escape(text), "]", + "button[1,1.5;1,0.8;yes;Yes]", + "button[2,1.5;1,0.8;no;No]", + --TKTK can we enable text wrapping? + --TKTK we could use scroll boxes to contain the text + } + local formspec_string = table.concat(formspec, "") + -- present to player + minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string) end 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) +-- 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 +