From 55c3ee9f4618b656da5cf0b22d1ad87e757d764d Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 17 Apr 2021 21:46:14 -0600 Subject: [PATCH] Added basic modpol.interactions.message --- modpol/interactions/interactions.lua | 23 ++- modpol_minetest/chatcommands/chatcommands.lua | 6 +- .../overrides/interactions/interactions.lua | 138 +++++++++++++----- 3 files changed, 129 insertions(+), 38 deletions(-) diff --git a/modpol/interactions/interactions.lua b/modpol/interactions/interactions.lua index 6f5ffde..dbf1248 100644 --- a/modpol/interactions/interactions.lua +++ b/modpol/interactions/interactions.lua @@ -3,6 +3,8 @@ -- User interaction functions for Modular Politics -- Called by modpol.lua +modpol.interactions = {} + -- =================================================================== -- Function: modpol.dashboard(user) -- Params: user (string) @@ -24,13 +26,30 @@ modpol.dashboard = function(user) print(output) end + +-- =================================================================== +-- Function: modpol.interactions.message +-- input: user (string), message (string) +-- output: prints message to CLI +modpol.interactions.message = function(user, message) + print(user .. ": " .. message) +end + +-- =================================================================== +-- Function: modpol.interactions.text_query +-- input: Query (string) +-- output: User response (string) +modpol.interactions.text_query = function(query) + -- TODO +end + -- =================================================================== -- Function: modpol.binary_poll_user(user, question) -- Params: user (string), question (string) -- Output: -- presents a yes/no/abstain poll to a user, returns answer -modpol.binary_poll_user = function(user, question) - local query = "Poll for " .. user .. " (y/n/a): ".. question +function modpol.interactions.binary_poll_user(user, question) + local query = "Poll for " .. user .. " (y/n): ".. question local answer repeat print(query) diff --git a/modpol_minetest/chatcommands/chatcommands.lua b/modpol_minetest/chatcommands/chatcommands.lua index 8110507..f5cb4f2 100644 --- a/modpol_minetest/chatcommands/chatcommands.lua +++ b/modpol_minetest/chatcommands/chatcommands.lua @@ -15,10 +15,10 @@ regchat = function(name, command_table) end -- =================================================================== --- /dashboard +-- /modpol -- Presents a menu of options to users regchat( - "dashboard", { + "modpol", { privs = {}, func = function(user) modpol.dashboard(user) @@ -102,7 +102,7 @@ regchat( "pollself", { privs = {}, func = function(user, param) - modpol.binary_poll_user(user, param) + modpol.interactions.binary_poll_user(user, param) return true, result end, }) diff --git a/modpol_minetest/overrides/interactions/interactions.lua b/modpol_minetest/overrides/interactions/interactions.lua index 5508eec..242ba2f 100644 --- a/modpol_minetest/overrides/interactions/interactions.lua +++ b/modpol_minetest/overrides/interactions/interactions.lua @@ -1,6 +1,26 @@ +-- =================================================================== +-- CONTEXTUAL STUFF + +-- First, set up contexts to enable passing across formspecs +-- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts + +local _contexts = {} +local function get_context(name) + local context = _contexts[name] or {} + _contexts[name] = context + return context +end +minetest.register_on_leaveplayer(function(player) + _contexts[player:get_player_name()] = nil +end) + +-- table of formspec field responses + +local formspec_fields = {} -- =================================================================== --- Function: modpol.dashboard(user) +-- MAIN MODPOL DASHBOARD +-- Function: modpol.modpol(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 @@ -23,18 +43,78 @@ modpol.dashboard = function(user) "label[0.5,0.5;", minetest.formspec_escape(commands), "]", "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;yes;Done]", + "button[0.5,7;1,0.8;test_poll;Test poll]", + "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) + minetest.show_formspec(user, "modpol:dashboard", formspec_string) end +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):") + end + end +end) + +-- BASIC INTERACTION FUNCTIONS -- + +-- =================================================================== +-- Function: modpol.interactions.message +-- input: message (string) +-- output +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) + -- set up formspec + local formspec = { + "formspec_version[4]", + "size[10,4]", + "label[0.5,1;", minetest.formspec_escape(query), "]", + "field[0.5,1.25;9,0.8;input;;]", + "button[0.5,2.5;1,0.8;yes;OK]", + } + local formspec_string = table.concat(formspec, "") + -- present to players + minetest.show_formspec(user, "modpol:text_query", formspec_string) + -- receive input + -- return output +end + +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) + end + minetest.close_formspec(pname, formname) + end +end) + + +-- =================================================================== +-- BASIC PARTICIPATION FUNCTIONS + + -- =================================================================== -- Function: modpol.binary_poll_user(user, question) --- Overwrites function at /interactions.lua --- presents a yes/no/abstain poll to a user, returns answer -modpol.binary_poll_user = function(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 = { @@ -42,39 +122,31 @@ modpol.binary_poll_user = function(user, question) "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]", - "button[3,1.5;1,0.8;abstain;Abstain]" + "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", formspec_string) + minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string) end --- =================================================================== --- Register input fields from forms --- Minetest-specific; does not overwrite --- separate this out into discrete functions? --- how do we ensure this is maximally modular? --- Perhaps create a table of possible formnames and their associated functions --- Then we can easily add to the table of possible options minetest.register_on_player_receive_fields(function (player, formname, fields) - -- modpol:binary_poll - if formname == "modpol:binary_poll" then - local pname = player:get_player_name() - local vote = nil - if fields.yes then vote = fields.yes - elseif fields.no then vote = fields.no - elseif fields.abstain then vote = fields.abstain - end - if vote then - minetest.chat_send_all(pname .. " voted " .. vote) - --TKTK : we should send the message to all in that org, definately not to all players - end - minetest.close_formspec(pname, formname) - return vote - else -- if the form is not a recognized name - return - end + local pname = player:get_player_name() + -- modpol:binary_poll + if formname == "modpol:binary_poll_user" then + local vote = nil + if fields.yes then vote = fields.yes + elseif fields.no then vote = fields.no + elseif fields.abstain then vote = fields.abstain + end + if vote then + modpol.interactions.message(pname, "Vote recorded") + minetest.chat_send_all(pname .. " voted " .. vote) + --TODO : we should send the message to all in that org, not to all players + end + minetest.close_formspec(pname, formname) + return vote + end end) +