Kaynağa Gözat

Added minetest modules and am stuck on interactions and module flow

Nathan Schneider 2 yıl önce
ebeveyn
işleme
af6e639e27

+ 1 - 0
modpol_core/api.lua

@@ -14,5 +14,6 @@ dofile (localdir .. "/interactions/interactions.lua")
 dofile (localdir .. "/modules/add_child_org.lua")
 dofile (localdir .. "/modules/consent.lua")
 dofile (localdir .. "/modules/join_org_consent.lua")
+dofile (localdir .. "/modules/leave_org.lua")
 dofile (localdir .. "/modules/remove_org_consent.lua")
 dofile (localdir .. "/modules/remove_org.lua")

+ 42 - 9
modpol_core/modules/add_child_org.lua

@@ -1,29 +1,62 @@
 --- @module add_child_org
 -- Adds a child org
+-- Depends on `consent`
 
 local add_child_org = {
     name = "Add child org",
     slug = "add_child_org",
-    desc = "Create a child org within the current one"
+    desc = "Create a child org within the current one with consent"
 }
 add_child_org.data = {
+   child_name = ""
 }
 add_child_org.config = {
 }
 
 -- @function initiate
-function add_child_org:initiate(result)
+function add_child_org:initiate(config, result)
+   self.org:add_pending_action(
+      self.id, self.initiator,
+      "name_child_org")
+end
+
+function add_child_org:name_child_org()
    modpol.interactions.text_query(
       self.initiator,"Child org name: ",
       function(input)
-         self.org:add_org(input, self.initiator)
-         modpol.interactions.message_org(
-            self.initiator,
-            self.org.id,
-            "Child org created: "..input)
-         modpol.interactions.dashboard(self.initiator)
-   end)
+         if input == "" then
+            modpol.interactions.message(
+               self.initiator,
+               "No name entered for child org")
+            self.org:wipe_pending_actions(self.id)
+            return end
+         self.data.child_name = input
+         modpol.interactions.message(
+               self.initiator,
+               "Proposed child org: " .. input)
+         self.org:wipe_pending_actions(self.id)
+         self:propose_child_org()
+      end
+   )
 end
 
+function add_child_org:propose_child_org()
+   self.org:call_module(
+      "consent",
+      self.initiator, 
+      {
+         prompt = "Create child org " .. self.data.child_name .. "?",
+         votes_required = #self.org.members
+      },
+      function()
+            self.org:add_org(self.data.child_name, self.initiator)
+            modpol.interactions.message_org(
+               self.initiator,
+               self.org.id,
+               "Child org created: "..self.data.child_name)
+      end
+   )
+end
+      
 --- (Required) Add to module table
 modpol.modules.add_child_org = add_child_org

+ 3 - 3
modpol_core/modules/consent.lua

@@ -16,7 +16,7 @@ consent.config = {
     votes_required = 1
 }
 
-function consent:initiate(result)
+function consent:initiate(config, result)
    self.result = result
    -- if org is empty, consent is given automatically
    if self.org:get_member_count() == 0 then
@@ -39,10 +39,10 @@ function consent:callback(member)
            if resp == "Yes" then
               self.data.votes = self.data.votes + 1
            end
-            
+           
            if self.data.votes >= self.config.votes_required then
-              if self.result then self.result() end
               self.org:wipe_pending_actions(self.id)
+              if self.result then self.result() end
            end
         end
     )

+ 43 - 0
modpol_core/modules/leave_org.lua

@@ -0,0 +1,43 @@
+--- leave_org
+-- @module leave_org
+
+local leave_org = {
+    name = "Leave org",
+    slug = "leave_org",
+    desc = "Leave this org"
+}
+
+leave_org.data = {
+}
+
+leave_org.config = {
+}
+
+--- (Required): initiate function
+-- Modules have access to the following instance variables:
+-- <li><code>self.org</code> (the org the module was called in),</li>
+-- <li><code>self.initiator</code> (the user that callced the module),</li>
+-- <li><code>self.id</code> (the process id of the module instance)</li>
+-- @param config (optional) If user wants to override fields in the config table
+-- @param result (optional) Callback if this module is embedded in other modules
+-- @function initiate
+function leave_org:initiate(config, result)
+   if self.org == modpol.instance then
+   modpol.interactions.message(
+      self.initiator,
+      "You cannot leave the root org")
+   else
+      self.org:remove_member(self.initiator)
+      modpol.interactions.message_org(
+         self.initiator,self.org.id,
+         self.initiator .. " has left org " .. self.org.name)
+      modpol.interactions.message(
+         self.initiator,
+         "You have left org " .. self.org.name)
+      -- call result function
+   end
+   if result then result() end
+end
+
+--- (Required) Add to module table
+modpol.modules.leave_org = leave_org

+ 12 - 6
modpol_core/modules/remove_org.lua

@@ -15,12 +15,18 @@ remove_org.data = {}
 --- Initiate function
 -- @function initiate
 function remove_org:initiate(config, result)
-   modpol.interactions.message_org(
-      self.initiator,self.org.id,
-      "Removing org: "..self.org.name)
-   self.org:delete()
-   modpol.interactions.dashboard(self.initiator)
-   -- call result function 
+   if self.org == modpol.instance then
+   modpol.interactions.message(
+      self.initiator,
+      "You cannot remove the root org")
+   else
+      modpol.interactions.message_org(
+         self.initiator,self.org.id,
+         "Removing org: "..self.org.name)
+      self.org:delete()
+      modpol.interactions.dashboard(self.initiator)
+      -- call result function 
+   end
    if result then result() end
 end
 

+ 0 - 1
modpol_core/modules/remove_org_consent.lua

@@ -33,7 +33,6 @@ function remove_org_consent:complete()
       self.initiator, self.org.id,
       "Removing org: " .. self.org.name)
    self.org:delete()
-   modpol.interactions.dashboard(self.initiator)
 end
 
 modpol.modules.remove_org_consent = remove_org_consent

+ 0 - 3
modpol_core/orgs/process.lua

@@ -90,12 +90,9 @@ end
 
 function modpol.orgs:interact(process_id, user)
    local process = self.processes[process_id]
-   modpol.interactions.message(user,"hi!")
    if self.pending[user] then
-      modpol.interactions.message(user,"id: "..process_id)
       local callback = self.pending[user][process_id]
       if callback then
-         modpol.interactions.message(user,"la!")
          process[callback](process, user)
       end
    end

+ 49 - 0
modpol_minetest/modules/priv_to_org.lua

@@ -0,0 +1,49 @@
+--- Set privilege to org members
+-- @module priv_to_org
+-- Allows initiator to grant a priv they have to all members of an org
+
+local priv_to_org = {
+    name = "Set privilege to org members",
+    slug = "priv_to_org",
+    desc = "Allows initiator to grant a priv they have to all members of an org"
+}
+
+priv_to_org.data = {
+}
+
+priv_to_org.config = {
+}
+
+--- (Required): initiate function
+-- Modules have access to the following instance variables:
+-- <li><code>self.org</code> (the org the module was called in),</li>
+-- <li><code>self.initiator</code> (the user that callced the module),</li>
+-- <li><code>self.id</code> (the process id of the module instance)</li>
+-- @param config (optional) If user wants to override fields in the config table
+-- @param result (optional) Callback if this module is embedded in other modules
+-- @function initiate
+function priv_to_org:initiate(config, result) 
+   local player_privs = minetest.get_player_privs(self.initiator)
+   for i,v in ipairs(player_privs) do
+      if not v then table.remove(player_privs,i) end
+   end
+   modpol.interactions.dropdown_query(
+      self.initiator,
+      "Which privilege do you want to share with members of "..self.org.name.."?",
+      player_privs,
+      function(input)
+         for member in self.org.members do
+            local member_privs = minetest.get_player_privs(member.name)
+            member_privs[input] = true
+            minetest.set_player_privs(member.name, member_privs)
+         end
+         local message = self.initiator .. " has set " .. input ..
+         " privilege to all members of " .. self.org.name
+         modpol.interactions.message_org(self.initiator,self.org.id, message)
+   end)
+    -- call result function 
+    if result then result() end
+end
+
+--- (Required) Add to module table
+modpol.modules.priv_to_org = priv_to_org

+ 6 - 0
modpol_minetest/overrides/interactions.lua

@@ -176,6 +176,12 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
       if formname == "modpol:org_dashboard" then
          local pname = player:get_player_name()
          local org = modpol.orgs.get_org(_contexts[pname].current_org)
+         -- just confirm the org still exists:
+         if not org then
+            modpol.interactions.message(pname, "Org no longer exists")
+            modpol.interactions.dashboard(pname)
+            return end
+         -- okay, onward
          if nil then
          elseif fields.join then
             org:add_member(pname)