From 2365eb83d050c4b2ed630ab97b44164cf9fd3719 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sun, 19 Dec 2021 23:23:23 -0700 Subject: [PATCH] Added change_modules mod and lots of bugfixes. Now merging to master. --- modpol_core/api.lua | 1 + modpol_core/modpol.lua | 3 + modpol_core/modules/change_modules.lua | 171 +++++++++++++++++++ modpol_core/modules/remove_child_consent.lua | 4 +- modpol_core/modules/template.lua | 2 +- modpol_core/orgs/base.lua | 2 +- modpol_core/util/misc.lua | 9 + modpol_minetest/api.lua | 4 + modpol_minetest/overrides/interactions.lua | 41 +++-- 9 files changed, 219 insertions(+), 18 deletions(-) create mode 100644 modpol_core/modules/change_modules.lua create mode 100644 modpol_core/util/misc.lua diff --git a/modpol_core/api.lua b/modpol_core/api.lua index 4f9bca6..b50bf18 100644 --- a/modpol_core/api.lua +++ b/modpol_core/api.lua @@ -12,6 +12,7 @@ dofile (localdir .. "/interactions/interactions.lua") --modules --TODO make this automatic and directory-based dofile (localdir .. "/modules/add_child_org_consent.lua") +dofile (localdir .. "/modules/change_modules.lua") dofile (localdir .. "/modules/consent.lua") dofile (localdir .. "/modules/join_org_consent.lua") dofile (localdir .. "/modules/leave_org.lua") diff --git a/modpol_core/modpol.lua b/modpol_core/modpol.lua index 9e779d3..6533ade 100644 --- a/modpol_core/modpol.lua +++ b/modpol_core/modpol.lua @@ -44,6 +44,9 @@ print (topdir) -- OldCoder utilities dofile (topdir .. "/util/ocutil/ocutil.lua") +-- Misc. utilities +dofile (topdir .. "/util/misc.lua") + -- =================================================================== -- Persistent storage -- must implement modpol.load_storage() and modpol.store_data() diff --git a/modpol_core/modules/change_modules.lua b/modpol_core/modules/change_modules.lua new file mode 100644 index 0000000..d5c97da --- /dev/null +++ b/modpol_core/modules/change_modules.lua @@ -0,0 +1,171 @@ +--- change_modules +-- @module change_modules +-- Depends on consent + +local change_modules = { + name = "Change modules (consent)", + slug = "change_modules", + desc = "Add or remove modules from the org with member consent", + hide = false; +} + +change_modules.data = { + result = nil +} + +change_modules.config = { +} + +function change_modules:initiate(result) + self.data.result = result + -- Step 1: add or remove? + modpol.interactions.dropdown_query( + self.initiator, "Module change options:", + {"Add module","Remove module"}, + function(input) + if input == "Add module" then + self:add_module() + elseif input == "Remove module" then + self:remove_module() + end + end + ) +end + +function change_modules:add_module() + -- prepare module options + local available_modules = modpol.copy_table(modpol.modules) + for k,org_mod in pairs(self.org.modules) do + if available_modules[org_mod.slug] then + available_modules[org_mod.slug] = nil + end end + -- present module options + local modules_list = {} + for k,v in pairs(available_modules) do + table.insert(modules_list,v.name) + end + if #modules_list == 0 then + modpol.interactions.message( + self.initiator, "Org has all modules") + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) + return + end + table.sort(modules_list) + -- now ask which to add + modpol.interactions.dropdown_query( + self.initiator, "Choose a module to add:", + modules_list, + function(mod_choice) + -- confirm choice + modpol.interactions.binary_poll_user( + self.initiator, + "Confirm: propose to add module \"" .. + mod_choice .. "\"?", + function(input) + if input == "Yes" then + self:propose_change("add",mod_choice) + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + else + self:add_module() + end + end + ) + end + ) +end + +function change_modules:remove_module() + -- prepare module options + local available_modules = {} + for k,org_mod in pairs(self.org.modules) do + if not org_mod.hide then + available_modules[org_mod.slug] = modpol.copy_table(org_mod) + end end + local modules_list = {} + local modules_count = 0 + for k,v in pairs(available_modules) do + table.insert(modules_list,v.name) + modules_count = modules_count + 1 + end + -- abort if no modules to remove + if modules_count == 0 then + modpol.interactions.message( + self.initiator, "Org has no modules") + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) + return + end + table.sort(modules_list) + -- now ask which to remove + modpol.interactions.dropdown_query( + self.initiator, "Choose a module to remove:", + modules_list, + function(mod_choice) + -- confirm choice + modpol.interactions.binary_poll_user( + self.initiator, + "Confirm: propose to remove module \"" .. mod_choice .. "\"?", + function(input) + if input == "Yes" then + self:propose_change("remove",mod_choice) + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + else + self:remove_module() + end + end + ) + end + ) +end + +--- propose_change +-- @field type "add" or "remove" +function change_modules:propose_change(type, mod_text) + self.org:call_module( + "consent",self.initiator, + { + prompt = "Do you consent to "..type.. + " this module in org "..self.org.name.. + ":\n"..mod_text, + votes_required = #self.org.members + }, + function() + if type == "add" then + for k,v in pairs(modpol.modules) do + if v.name == mod_text then + table.insert(self.org.modules,v) + end + end + modpol.interactions.message_org( + self.initiator,self.org.id, + "Consent reached:\nAdding \"" + ..mod_text.."\" to org "..self.org.name) + elseif type == "remove" then + modpol.msg("Removing!") + local i = 0 + for k,v in pairs(self.org.modules) do + i = i + 1 + if v.name == mod_text then + modpol.msg("got it!") + self.org.modules[k] = nil + end + end + modpol.interactions.message_org( + self.initiator,self.org.id, + "Consent reached:\nRemoving \"" + ..mod_text.."\" from org "..self.org.name) + end + end) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +--- (Required) Add to module table +modpol.modules.change_modules = change_modules diff --git a/modpol_core/modules/remove_child_consent.lua b/modpol_core/modules/remove_child_consent.lua index 3968b8b..ba3255e 100644 --- a/modpol_core/modules/remove_child_consent.lua +++ b/modpol_core/modules/remove_child_consent.lua @@ -33,8 +33,8 @@ function remove_child_consent:initiate(result) self.data.result = result modpol.interactions.dropdown_query( self.initiator, - "Which child of org "..self.org.name.. - " do you want to remove?", + "Choose a child of org ".. + self.org.name.." to remove:", children, function(input) self.data.child_to_remove = modpol.orgs.get_org(input) diff --git a/modpol_core/modules/template.lua b/modpol_core/modules/template.lua index 91773e7..ae63712 100644 --- a/modpol_core/modules/template.lua +++ b/modpol_core/modules/template.lua @@ -2,7 +2,7 @@ -- @module module_template --- (Required): data table containing name and description of the module --- @field name "Human-readable name" +-- @field name "Human-readable name (parens OK, no brackets)" -- @field slug "Same as module class name" -- @field desc "Description of the module" -- @field hide "Whether this is a hidden utility module" diff --git a/modpol_core/orgs/base.lua b/modpol_core/orgs/base.lua index 27acf66..908144a 100644 --- a/modpol_core/orgs/base.lua +++ b/modpol_core/orgs/base.lua @@ -14,7 +14,7 @@ function temp_org() return { id = nil, name = nil, - modules = modpol.modules, + modules = modpol.copy_table(modpol.modules), processes = {}, pending = {}, members = {}, diff --git a/modpol_core/util/misc.lua b/modpol_core/util/misc.lua new file mode 100644 index 0000000..bfb3bc9 --- /dev/null +++ b/modpol_core/util/misc.lua @@ -0,0 +1,9 @@ +--- @function modpol.copy_table +-- Returns a copy of the table inputted +function modpol.copy_table(t) + local t2 = {} + for k,v in pairs(t) do + t2[k] = v + end + return t2 +end diff --git a/modpol_minetest/api.lua b/modpol_minetest/api.lua index 7547f5e..d1ea9d4 100644 --- a/modpol_minetest/api.lua +++ b/modpol_minetest/api.lua @@ -10,6 +10,10 @@ local localdir = minetest.get_modpath("modpol") .. "/modpol_minetest" --overrides dofile (localdir .. "/overrides/interactions.lua") +--testing command for "singleplayer" +function modpol.msg(text) + modpol.interactions.message("singleplayer",text) +end -- =================================================================== -- Minetest Chatcommands diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index 3000f15..de24dc0 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/modpol_minetest/overrides/interactions.lua @@ -54,11 +54,11 @@ function modpol.interactions.dashboard(user) "size[10,6]", "label[0.5,0.5;M O D U L A R P O L I T I C S]", "label[0.5,2;All orgs:]", - "dropdown[2,1.5;5,0.8;all_orgs;"..formspec_list(all_orgs)..";;]", + "dropdown[2,1.5;7,0.8;all_orgs;"..formspec_list(all_orgs)..";;]", "label[0.5,3;Your orgs:]", - "dropdown[2,2.5;5,0.8;user_orgs;"..formspec_list(user_orgs)..";;]", + "dropdown[2,2.5;7,0.8;user_orgs;"..formspec_list(user_orgs)..";;]", "label[0.5,4;All users:]", - "dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]", + "dropdown[2,3.5;7,0.8;all_users;"..formspec_list(all_users)..";;]", "button_exit[8.5,5;1,0.8;close;Close]", } local formspec_string = table.concat(formspec, "") @@ -108,14 +108,16 @@ function modpol.interactions.org_dashboard(user, org_string) else parent = "none" end -- prepare children menu - local children = {"View..."} + local children = {} for k,v in ipairs(org.children) do local this_child = modpol.orgs.get_org(v) table.insert(children, this_child.name) end + table.sort(children) + table.insert(children,1,"View...") -- prepare modules menu - local modules = {"View..."} + local modules = {} if org.modules then for k,v in pairs(org.modules) do if not v.hide then -- hide utility modules @@ -125,9 +127,11 @@ function modpol.interactions.org_dashboard(user, org_string) end end end + table.sort(modules) + table.insert(modules,1,"View...") -- prepare pending menu - local pending = {"View..."} + local pending = {} local num_pending = 0 if org.pending[user] then for k,v in pairs(org.pending[user]) do @@ -137,6 +141,8 @@ function modpol.interactions.org_dashboard(user, org_string) num_pending = num_pending + 1 end end + table.sort(pending) + table.insert(pending,1,"View...") -- set player context local user_context = {} @@ -150,13 +156,13 @@ function modpol.interactions.org_dashboard(user, org_string) minetest.formspec_escape(org.name)..membership_toggle(org.name).."]", "label[0.5,1;Parent: "..parent..membership_toggle(parent).."]", "label[0.5,2;Members:]", - "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]", + "dropdown[2,1.5;7,0.8;user_orgs;"..formspec_list(org.members)..";;]", "label[0.5,3;Children:]", - "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]", + "dropdown[2,2.5;7,0.8;children;"..formspec_list(children)..";;]", "label[0.5,4;Modules:]", - "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]", + "dropdown[2,3.5;7,0.8;modules;"..formspec_list(modules)..";;]", "label[0.5,5;Pending ("..num_pending.."):]", - "dropdown[2,4.5;5,0.8;pending;"..formspec_list(pending)..";;]", + "dropdown[2,4.5;7,0.8;pending;"..formspec_list(pending)..";;]", "button[8.5,7;1,0.8;back;Back]", } local formspec_string = table.concat(formspec, "") @@ -186,7 +192,7 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) fields.modules,"%[(.*)%]") modpol.interactions.binary_poll_user( pname, - modpol.modules[module].name.."\n".. + modpol.modules[module].name..":\n".. modpol.modules[module].desc.."\n".. "Proceed?", function(input) @@ -283,6 +289,8 @@ end) -- func input: choice (string) -- output: calls func on user choice function modpol.interactions.dropdown_query(user, label, options, func) + -- Add "View..." to the top of the list + table.insert(options,1,"View...") -- set up formspec local formspec = { "formspec_version[4]", @@ -302,20 +310,25 @@ end minetest.register_on_player_receive_fields(function (player, formname, fields) if formname == "modpol:dropdown_query" then local pname = player:get_player_name() - if fields.cancel == "cancel" then - -- cancel, do nothing + if fields.cancel then + minetest.close_formspec(pname, formname) + elseif fields.input == "View..." then + -- "View...", do nothing else local choice = fields.input local func = _contexts[pname]["dropdown_query_func"] if not choice then -- empty, do nothing elseif func then + --not sure if we should close + --breaks sequential dropdown_queries: + --minetest.close_formspec(pname, formname) func(choice) else + minetest.close_formspec(pname, formname) modpol.interactions.message(pname, "dropdown_query: " .. choice) end end - minetest.close_formspec(pname, formname) end end)