From 2028f1ee856e2b5c787a7d6a28be22a330aa696e Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Wed, 9 Feb 2022 22:14:26 -0700 Subject: [PATCH] Refactored policy structure Previously, all modules in an org were fully copied into that org. Now, the only copy of the modules is at modpol.modules, and orgs have a policy table at [org].policies, which overrides the config table in any given module. --- modpol_core/interactions/interactions.lua | 33 ++++++++++++------- modpol_core/modules/change_modules.lua | 11 +++---- modpol_core/modules/change_policy.lua | 14 ++++---- modpol_core/modules/display_policies.lua | 38 ++++++++++++---------- modpol_core/orgs/base.lua | 7 ++-- modpol_core/orgs/process.lua | 7 +++- modpol_minetest/overrides/interactions.lua | 18 +++++----- 7 files changed, 76 insertions(+), 52 deletions(-) diff --git a/modpol_core/interactions/interactions.lua b/modpol_core/interactions/interactions.lua index 58c54eb..229e9d8 100644 --- a/modpol_core/interactions/interactions.lua +++ b/modpol_core/interactions/interactions.lua @@ -18,13 +18,23 @@ function modpol.interactions.get_policy_string( local this_org = modpol.orgs.get_org(org) local this_module = modpol.modules[module_slug] local output = {} - if modpol.util.num_pairs(this_module.config) > 0 then - for k, v in pairs(this_module.config) do - local this_policy = k .. " - " .. tostring(v) - table.insert(output,this_policy) - end - end - return "Policies:\n" .. table.concat(output, sep) + if not this_org.policies[module_slug] then + return "Module not in org" + elseif modpol.util.num_pairs(this_module.config) > 0 then + for k, v in pairs(this_module.config) do + local this_policy = "[Policy error]" + if this_org.policies[module_slug][k] then + this_policy = + tostring(this_org.policies[module_slug][k]) + else + this_policy = tostring(v) + end + table.insert(output, k.." - "..this_policy) + end + return "Policies:\n" .. table.concat(output, sep) + else + return "No policies" + end end -- DASHBOARDS @@ -143,9 +153,10 @@ function modpol.interactions.org_dashboard(user, org_string) -- prepare modules menu local modules = {} - if org.modules then - for k,v in pairs(org.modules) do - if not v.hide then -- hide utility modules + if modpol.modules then + for k,v in pairs(modpol.modules) do + if not v.hide and -- hide utility modules + org.policies[k] then -- org includes it local module_entry = v.slug table.insert(modules, module_entry) end @@ -186,7 +197,7 @@ function modpol.interactions.org_dashboard(user, org_string) module_result = true end end - local module = org.modules[module_sel] + local module = modpol.modules[module_sel] if module_result then modpol.interactions.binary_poll_user( user, diff --git a/modpol_core/modules/change_modules.lua b/modpol_core/modules/change_modules.lua index beec34d..7cab39a 100644 --- a/modpol_core/modules/change_modules.lua +++ b/modpol_core/modules/change_modules.lua @@ -33,7 +33,7 @@ function change_modules:initiate(result) for k, module in pairs(modpol.modules) do if not modpol.modules[module.slug].hide then local in_org = false - if self.org.modules[module.slug] then + if self.org.policies[module.slug] then in_org = true end table.insert( @@ -97,14 +97,13 @@ end function change_modules:implement_change() for i,v in ipairs(self.data.add_modules) do local slug = string.match(v,"%[(.+)%]") - self.org.modules[slug] = - modpol.util.copy_table(modpol.modules[slug]) - table.sort(self.org.modules) + self.org.policies[slug] = {} + table.sort(self.org.policies) end for i,v in ipairs(self.data.remove_modules) do local slug = string.match(v,"%[(.+)%]") - self.org.modules[slug] = nil - table.sort(self.org.modules) + self.org.policies[slug] = nil + table.sort(self.org.policies) end -- announce and shut down modpol.interactions.message_org( diff --git a/modpol_core/modules/change_policy.lua b/modpol_core/modules/change_policy.lua index 250bd4d..a677ae5 100644 --- a/modpol_core/modules/change_policy.lua +++ b/modpol_core/modules/change_policy.lua @@ -22,8 +22,9 @@ change_policy.config = { function change_policy:initiate(result) -- prepare module options local available_modules = {} - for k,org_mod in pairs(self.org.modules) do - if not org_mod.hide then + for k,org_mod in pairs(modpol.modules) do + if not org_mod.hide and + self.org.policies[k] then available_modules[org_mod.slug] = modpol.util.copy_table(org_mod) end end local modules_list = {} @@ -48,7 +49,7 @@ function change_policy:initiate(result) self.initiator, "Choose a module to change policies for:", modules_list, function(mod_choice) - local this_module = self.org.modules[mod_choice] + local this_module = modpol.modules[mod_choice] local module_policies = this_module.config local policy_list = {} for k,v in pairs(module_policies) do @@ -70,10 +71,9 @@ function change_policy:initiate(result) function(policy_choice) modpol.interactions.text_query( self.initiator, - "Use carefully!\n" .. "Current " .. policy_choice .. " value: " .. - tostring(self.org.modules[mod_choice][policy_choice]) - .. "\nChange value to:", + tostring(modpol.modules[mod_choice][policy_choice]) + .. "\nChange value to (be careful!):", function(policy_input) self:approve_change( mod_choice, @@ -99,7 +99,7 @@ function change_policy:approve_change(module_slug, policy, input) self.initiator, "Updating " .. policy .. " policy on module " .. module_slug .. " with: " .. input) - self.org.modules[module_slug].config[policy] = input + self.org.policies[module_slug][policy] = input modpol.interactions.org_dashboard( self.initiator, self.org.id) if self.data.result then self.data.result() end diff --git a/modpol_core/modules/display_policies.lua b/modpol_core/modules/display_policies.lua index f6a105e..f4af1ae 100644 --- a/modpol_core/modules/display_policies.lua +++ b/modpol_core/modules/display_policies.lua @@ -19,26 +19,30 @@ display_policies.config = { -- @param result Callback if this module is embedded in other modules function display_policies:initiate(result) local display_table = {} - for k,v in pairs(self.org.modules) do - local input = v.name - table.insert(display_table, input) - if v.config - and modpol.util.num_pairs(v.config) > 0 then - for k2,v2 in pairs(v.config) do - local v2_string = "" - if type(v2) == "string" then - v2_string = v2 - elseif type(v2) == "table" - or type(v2) == "number" then - v2_string = tostring(v2) - else - v2_string = "Could not render" + for k,v in pairs(self.org.policies) do + if v then -- check the module is allowed + local input = modpol.modules[k].name + table.insert(display_table, input) + if modpol.modules[k].config then + for k2,v2 in pairs(modpol.modules[k].config) do + if self.org.policies[k][k2] then + v2 = self.org.policies[k][k2] + end + local v2_string = "" + if type(v2) == "string" then + v2_string = v2 + elseif type(v2) == "table" + or type(v2) == "number" then + v2_string = tostring(v2) + else + v2_string = "Could not render" + end + input = k2..": "..v2_string + table.insert(display_table, input) end - input = k2..": "..v2_string - table.insert(display_table, input) end + table.insert(display_table, "---") end - table.insert(display_table, "---") end local output = table.concat(display_table,"\n") if #display_table == 0 then diff --git a/modpol_core/orgs/base.lua b/modpol_core/orgs/base.lua index b534d67..a596b98 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.util.copy_table(modpol.modules), + policies = {}, processes = {}, pending = {}, members = {}, @@ -105,6 +105,9 @@ function modpol.orgs.init_instance() local instance = temp_org() instance.id = 1 instance.name = "Root" + for i,v in pairs(modpol.modules) do + instance.policies[i] = {} + end setmetatable(instance, modpol.orgs) @@ -183,7 +186,7 @@ function modpol.orgs:add_org(name, user) child_org.name = name child_org.parent = self.id child_org.processes = {} - child_org.modules = modpol.util.copy_table(self.modules) + child_org.policies = modpol.util.copy_table(self.policies) setmetatable(child_org, modpol.orgs) diff --git a/modpol_core/orgs/process.lua b/modpol_core/orgs/process.lua index d3eca52..7a12de3 100644 --- a/modpol_core/orgs/process.lua +++ b/modpol_core/orgs/process.lua @@ -15,7 +15,7 @@ function modpol.orgs:call_module(module_slug, initiator, config, result, parent_ local index = #self.processes + 1 - local module = self.modules[module_slug] + local module = modpol.modules[module_slug] -- first applies any relevant org policies -- then overrides with the config values given on input @@ -23,6 +23,11 @@ function modpol.orgs:call_module(module_slug, initiator, config, result, parent_ if modpol.util.num_pairs(module.config) > 0 then for k, v in pairs(module.config) do new_config[k] = v + -- overrides with org policies + if self.policies[module_slug] + and self.policies[module_slug][k] then + new_config[k] = self.policies[module_slug][k] + end -- overrides with input settings if config and config[k] then new_config[k] = config[k] diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index 82d71fb..8b7d2f6 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/modpol_minetest/overrides/interactions.lua @@ -145,9 +145,10 @@ function modpol.interactions.org_dashboard(user, org_string) -- prepare modules menu local modules = {} - if org.modules then - for k,v in pairs(org.modules) do - if not v.hide then -- hide utility modules + if modpol.modules then + for k,v in pairs(modpol.modules) do + if not v.hide and -- hide utility modules + org.policies[k] then -- org includes it local module_entry = v.name table.insert(modules, module_entry) end @@ -228,8 +229,9 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) elseif fields.modules and fields.modules ~= "View..." then local module = nil - for k,v in pairs(org.modules) do - if fields.modules == v.name then + for k,v in pairs(modpol.modules) do + if fields.modules == v.name + and org.policies[v.slug] then module = v end end if module then @@ -411,10 +413,10 @@ function modpol.interactions.text_query(user, query, func) -- set up formspec local formspec = { "formspec_version[4]", - "size[10,4]", + "size[10,6]", "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]", + "field[0.5,3.25;9,0.8;input;;]", + "button[0.5,4.5;1,0.8;yes;OK]", } local formspec_string = table.concat(formspec, "") -- present to player