Browse Source

Completed checkbox_query and rewrite of change_modules module with checkboxes

Nathan Schneider 2 years ago
parent
commit
2989bd34bd

+ 0 - 3
modpol_core/api.lua

@@ -30,6 +30,3 @@ dofile (localdir .. "/modules/remove_process.lua")
 dofile (localdir .. "/modules/rename_org_consent.lua")
 dofile (localdir .. "/modules/send_token.lua")
 dofile (localdir .. "/modules/tokenomics.lua")
-
-
-dofile (localdir .. "/modules/checkbox_test.lua")

+ 11 - 4
modpol_core/interactions/interactions.lua

@@ -187,7 +187,12 @@ function modpol.interactions.org_dashboard(user, org_string)
       print("Interact with which one (use [id] number)?")
       local to_interact = io.read()
       local process = org.processes[tonumber(to_interact)]
-      if not process then return end
+      if not process then
+         modpol.interactions.message(
+            user, "Not a pending process")
+         modpol.interactions.org_dashboard(user, org.id)
+         return
+      end
       if org:has_pending_actions(user) then
          if org.pending[user][process.id] then
             org:interact(process.id, user)
@@ -381,8 +386,10 @@ function modpol.interactions.checkbox_query(
    end
    local result_table = modpol.util.copy_table(options)
    for i,v in ipairs(answer_table) do
-      -- flip the boolean on selected options
-      result_table[v][2] = not result_table[v][2]
+      if result_table[v] then
+         -- flip the boolean on selected options
+         result_table[v][2] = not result_table[v][2]
+      end
    end
    func(result_table)
 end
@@ -433,5 +440,5 @@ end
 
 --testing command
 function modpol.msg(text)
-   modpol.interactions.message("TEST MSG: ",text)
+   modpol.interactions.message("TEST MSG",text)
 end

+ 169 - 0
modpol_core/modules/change_modules-dropdown.lua

@@ -0,0 +1,169 @@
+--- 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.util.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.util.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
+            local i = 0
+            for k,v in pairs(self.org.modules) do
+               i = i + 1
+               if v.name == mod_text then
+                  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

+ 3 - 5
modpol_core/modules/change_modules.lua

@@ -49,16 +49,14 @@ function change_modules:initiate(result)
             if v[2] ~= modules_before[i][2] then
                if v[2] then
                   table.insert(self.data.add_modules, v[1])
-                  modpol.msg("add-insert: "..v[1])
                else
                   table.insert(self.data.remove_modules, v[1])
-                  modpol.msg("remove-insert: "..v[1])
                end
             end
          end
          -- abort if no changes
-         if self.data.add_modules == {}
-            and self.data.remove_modules == {} then
+         if #self.data.add_modules == 0
+            and #self.data.remove_modules == 0 then
             modpol.interactions.message(
                self.initiator, "No module changes proposed")
             modpol.interactions.org_dashboard(
@@ -74,7 +72,7 @@ function change_modules:initiate(result)
             self.data.summary = self.data.summary.."\nAdd: "..
                table.concat(self.data.add_modules,", ")
          elseif #self.data.remove_modules > 0 then
-            summary = "\nRemove: "..
+            self.data.summary = "\nRemove: "..
                table.concat(self.data.remove_modules,", ")
          end
          self.org:call_module(

+ 5 - 1
modpol_core/util/misc.lua

@@ -7,7 +7,11 @@ function modpol.util.copy_table(t)
    local t2 = {}
    if pairs(t) then
       for k,v in pairs(t) do
-         t2[k] = v
+         if type(v) == "table" then
+            t2[k] = modpol.util.copy_table(v)
+         else
+            t2[k] = v
+         end
       end
    end
    return t2

+ 7 - 10
modpol_minetest/overrides/interactions.lua

@@ -501,7 +501,8 @@ function modpol.interactions.checkbox_query(
       local fs_line = ""
       vertical = i * .5
       fs_line = "checkbox[0,"..vertical..";checkbox_"..i..";"..
-         v[1]..";"..tostring(v[2]).."]"
+         minetest.formspec_escape(v[1])..";"..
+         tostring(v[2]).."]"
       table.insert(checkbox_options, fs_line)
    end
    local max = vertical * 4
@@ -514,14 +515,9 @@ function modpol.interactions.checkbox_query(
       "scrollbar[9,1;0.3,5.5;vertical;scroller;0]",
       "scroll_container[0.5,1;9,5.5;scroller;vertical]",
    }
-   -- prepare options
-   for i,v in ipairs(options) do
-      local fs_line = ""
-      local vertical = i * .5
-      fs_line = "checkbox[0,"..vertical..";checkbox_"..i..";"..
-         minetest.formspec_escape(v[1])..";"
-         ..tostring(v[2]).."]"
-      table.insert(formspec, fs_line)
+   -- insert options
+   for i,v in ipairs(checkbox_options) do
+      table.insert(formspec, v)
    end
    table.insert(formspec,"scroll_container_end[]")
    table.insert(formspec,"button[0.5,7;1.5,0.8;submit;Submit]")
@@ -533,7 +529,8 @@ function modpol.interactions.checkbox_query(
    -- put func in _contexts
    if _contexts[user] == nil then _contexts[user] = {} end
    _contexts[user]["checkbox_query_func"] = func
-   _contexts[user]["checkbox_query_result"] = options
+   _contexts[user]["checkbox_query_result"] =
+      modpol.util.copy_table(options)
 end
 -- receive fields
 minetest.register_on_player_receive_fields(function (player, formname, fields)