From e96b8641dcfe023a56768a878318a2f99dba9103 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sun, 5 Sep 2021 17:55:18 -0600 Subject: [PATCH 01/39] Non-functioning start on module stratey #1 --- modpol/orgs/requests.lua | 60 ++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/modpol/orgs/requests.lua b/modpol/orgs/requests.lua index aacc085..f312a3f 100644 --- a/modpol/orgs/requests.lua +++ b/modpol/orgs/requests.lua @@ -1,10 +1,47 @@ -modpol.orgs.request_params = { - add_org = 1, - delete = 0, - add_member = 1, - remove_member = 1 +-- REQUESTS +-- Provides request functionality to org + +-- TODO +-- include consent functionality, available to modules +-- load available modules in modpol.lua +-- make policies a set of options for consent functions +-- create simple modules for core functions: change_policy, add_member, start_org, join_org, close_org + +-- CONSENT +-- Provides consent functionality for modules + +-- define default params +modpol.default_policy = { + target_org = nil, + time_limit = nil, + vote_threshold = 0 } +-- Function: modpol.orgs:consent(params, result_function) +-- params: table of optional preferences, e.g.: + -- target_org + -- time_limit + -- vote_threshold (0-100 percent, 0 is automatic approval) +-- result_function: function(input) that takes result boolean +function modpol.orgs:consent(params, result_function) + -- if appropriate, pass on to target org + if params.target_org then + local target_org = params.target_org + params.target_org = nil + params.target_org:consent(params, result_function) + return + end + STUCK ON HOW TO IMPLEMENT THIS WITHOUT REQUIRING THE FULL PROCESS FUNCTIONALITY + +end + + + + + +-- PROCESSES +-- functions that enable requests to create processes + -- ================================ -- creates a new process linked to a request id function modpol.orgs:create_process(process_type, request_id) @@ -171,19 +208,6 @@ end -- ================================ -- tries to make a request to the org function modpol.orgs:make_request(request) - -- makes sure the request has the valid number of parameters - local num_params = modpol.orgs.request_params[request.type] - - if num_params == nil then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request type is invalid') - return false - end - - -- request.params should match the num of params for that type - if #request.params ~= num_params then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has invalid number of parameters') - return false - end -- checking to see if identical request already exists for k, v in ipairs(self.requests) do From a1a6203bab558ec3833a176b2ff504435a64e57f Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Mon, 27 Sep 2021 15:35:12 -0600 Subject: [PATCH 02/39] More very messy work on drafting module structure --- modpol/api.lua | 6 ++- modpol/modpol.lua | 8 ++- modpol/modules/consent.lua | 98 ------------------------------------- modpol/modules/defer_to.lua | 0 4 files changed, 12 insertions(+), 100 deletions(-) delete mode 100644 modpol/modules/consent.lua delete mode 100644 modpol/modules/defer_to.lua diff --git a/modpol/api.lua b/modpol/api.lua index 9565302..2b11371 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -8,9 +8,13 @@ dofile (localdir .. "/users/users.lua") --orgs dofile (localdir .. "/orgs/base.lua") dofile (localdir .. "/orgs/requests.lua") +dofile (localdir .. "/orgs/consent.lua") +dofile (localdir .. "/orgs/defer_to.lua") --interactions dofile (localdir .. "/interactions/interactions.lua") --modules -dofile (localdir .. "/modules/consent.lua") +dofile (loaldir .. "/modules/join_org.lua") +dofile (loaldir .. "/modules/remove_org.lua") +dofile (loaldir .. "/modules/child_org.lua") diff --git a/modpol/modpol.lua b/modpol/modpol.lua index 844c34a..21a63b2 100644 --- a/modpol/modpol.lua +++ b/modpol/modpol.lua @@ -64,10 +64,16 @@ dofile (modpol.storage_file_path) modpol.load_storage() -- =================================================================== --- ModPol core features +-- Modpol core features dofile (topdir .. "/api.lua") +-- =================================================================== +-- Modpol modules + +modpol.modules = modpol.modules or {} + +-- TKTK need to specify modules to include -- =================================================================== -- Final checks diff --git a/modpol/modules/consent.lua b/modpol/modules/consent.lua deleted file mode 100644 index 1cbcbf8..0000000 --- a/modpol/modules/consent.lua +++ /dev/null @@ -1,98 +0,0 @@ -modpol.modules = modpol.modules or {} - -modpol.modules.consent = {} - --- sets consent to its own callback -modpol.modules.consent.__index = modpol.modules.consent - -function temp_consent_process() - return { - type = "consent", - id = nil, - org_id = nil, - request_id = nil, - total_votes = 0, - majority_to_pass = 0.51, - votes_needed = nil, - votes_yes = {}, - votes_no = {} - } -end - --- =============================================== --- function to create a new consent process to resolve a pending process -function modpol.modules.consent:new_process(id, request_id, org_id) - local process = temp_consent_process() - process.id = id - process.request_id = request_id - process.org_id = org_id - - setmetatable(process, modpol.modules.consent) - modpol.ocutil.log('Created new process #' .. id .. ' for request id #' .. request_id) - - local p_org = modpol.orgs.get_org(org_id) - - for i, member in ipairs(p_org.members) do - p_org:add_pending_action(id, member) - end - - process.votes_needed = math.ceil(process.majority_to_pass * p_org:get_member_count()) - - return process -end - --- ============================ --- interact function for the consent module --- input: user (string) -function modpol.modules.consent:interact(user) - -- TODO this needs more context on the vote at hand - modpol.interactions.binary_poll_user( - user, "Do you consent?", - function(vote) - if vote == 'Yes' then - self:approve(user, true) - elseif vote == 'No' then - self:approve(user, false) - end - end) -end - --- ====================================================== --- function for users to vote on a pending request -function modpol.modules.consent:approve(user, decision) - if not modpol.orgs.get_org(self.org_id):has_member(user) then - modpol.ocutil.log('Error in consent:approve -> user not a member of the org') - return - end - - if decision then - table.insert(self.votes_yes, user) - modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id) - else - table.insert(self.votes_no, user) - modpol.ocutil.log('User ' .. user .. ' voted no on request #' .. self.request_id) - end - - self.total_votes = self.total_votes + 1 - - local p_org = modpol.orgs.get_org(self.org_id) - p_org:remove_pending_action(self.id, user) - - self:update_status() -end - --- =================================================== --- determines whether process has finished and resolves request if it has (unfinished) -function modpol.modules.consent:update_status() - local process_org = modpol.orgs.get_org(self.org_id) - - if #self.votes_yes >= self.votes_needed then - modpol.ocutil.log('Request #' .. self.request_id .. ' passes') - process_org:resolve_request(self.request_id, true) - elseif #self.votes_no >= self.votes_needed then - modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass') - process_org:resolve_request(self.request_id, false) - else - modpol.ocutil.log('Waiting for more votes...') - end -end diff --git a/modpol/modules/defer_to.lua b/modpol/modules/defer_to.lua deleted file mode 100644 index e69de29..0000000 From dc13e543d5bb5a8d9ae72d3583f02b3c311a1949 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Mon, 27 Sep 2021 15:36:19 -0600 Subject: [PATCH 03/39] Moved some files around --- modpol/modules/child_org.lua | 28 ++++++++++ modpol/modules/join_org.lua | 0 modpol/modules/leave_org.lua | 0 modpol/modules/remove_org.lua | 0 modpol/orgs/consent.lua | 98 +++++++++++++++++++++++++++++++++++ modpol/orgs/defer_to.lua | 0 6 files changed, 126 insertions(+) create mode 100644 modpol/modules/child_org.lua create mode 100644 modpol/modules/join_org.lua create mode 100644 modpol/modules/leave_org.lua create mode 100644 modpol/modules/remove_org.lua create mode 100644 modpol/orgs/consent.lua create mode 100644 modpol/orgs/defer_to.lua diff --git a/modpol/modules/child_org.lua b/modpol/modules/child_org.lua new file mode 100644 index 0000000..e76abf5 --- /dev/null +++ b/modpol/modules/child_org.lua @@ -0,0 +1,28 @@ +-- CHILD_ORG +-- Module that enables user to create child in current org + +-- Initial configuration +modpol.modules.child_org = {} +modpol.modules.child_org.name = "Create child org" + + +-- == REQUEST == + +-- gather data from initiator: child org name, comment + +-- function: modpol.modules.child_org.request + + + +-- == CONSENT == + +-- function: modpol.orgs:consent(process_id, result_function) +-- the result function should begin the completion process below + + + +-- == COMPLETION == + +-- if approved/if failed functions? + +-- function: modpol.modules.child_org.create(initiator, parent_org, child_org) diff --git a/modpol/modules/join_org.lua b/modpol/modules/join_org.lua new file mode 100644 index 0000000..e69de29 diff --git a/modpol/modules/leave_org.lua b/modpol/modules/leave_org.lua new file mode 100644 index 0000000..e69de29 diff --git a/modpol/modules/remove_org.lua b/modpol/modules/remove_org.lua new file mode 100644 index 0000000..e69de29 diff --git a/modpol/orgs/consent.lua b/modpol/orgs/consent.lua new file mode 100644 index 0000000..243b0b0 --- /dev/null +++ b/modpol/orgs/consent.lua @@ -0,0 +1,98 @@ +-- TODO: NEEDS TO BE REWRITTEN AS A LIBRARY NOT A MODULE + +modpol.orgs.consent = {} + +-- sets consent to its own callback +modpol.orgs.consent.__index = modpol.orgs.consent + +function temp_consent_process() + return { + type = "consent", + id = nil, + org_id = nil, + request_id = nil, + total_votes = 0, + majority_to_pass = 0.51, + votes_needed = nil, + votes_yes = {}, + votes_no = {} + } +end + +-- =============================================== +-- function to create a new consent process to resolve a pending process +function modpol.orgs.consent:new_process(id, request_id, org_id) + local process = temp_consent_process() + process.id = id + process.request_id = request_id + process.org_id = org_id + + setmetatable(process, modpol.orgs.consent) + modpol.ocutil.log('Created new process #' .. id .. ' for request id #' .. request_id) + + local p_org = modpol.orgs.get_org(org_id) + + for i, member in ipairs(p_org.members) do + p_org:add_pending_action(id, member) + end + + process.votes_needed = math.ceil(process.majority_to_pass * p_org:get_member_count()) + + return process +end + +-- ============================ +-- interact function for the consent module +-- input: user (string) +function modpol.orgs.consent:interact(user) + -- TODO this needs more context on the vote at hand + modpol.interactions.binary_poll_user( + user, "Do you consent?", + function(vote) + if vote == 'Yes' then + self:approve(user, true) + elseif vote == 'No' then + self:approve(user, false) + end + end) +end + +-- ====================================================== +-- function for users to vote on a pending request +function modpol.orgs.consent:approve(user, decision) + if not modpol.orgs.get_org(self.org_id):has_member(user) then + modpol.ocutil.log('Error in consent:approve -> user not a member of the org') + return + end + + if decision then + table.insert(self.votes_yes, user) + modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id) + else + table.insert(self.votes_no, user) + modpol.ocutil.log('User ' .. user .. ' voted no on request #' .. self.request_id) + end + + self.total_votes = self.total_votes + 1 + + local p_org = modpol.orgs.get_org(self.org_id) + p_org:remove_pending_action(self.id, user) + + self:update_status() +end + +-- =================================================== +-- determines whether process has finished and resolves request if it has (unfinished) +function modpol.orgs.consent:update_status() + local process_org = modpol.orgs.get_org(self.org_id) + + if #self.votes_yes >= self.votes_needed then + modpol.ocutil.log('Request #' .. self.request_id .. ' passes') + process_org:resolve_request(self.request_id, true) + elseif #self.votes_no >= self.votes_needed then + modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass') + process_org:resolve_request(self.request_id, false) + else + modpol.ocutil.log('Waiting for more votes...') + end +end diff --git a/modpol/orgs/defer_to.lua b/modpol/orgs/defer_to.lua new file mode 100644 index 0000000..e69de29 From 0d384da48cc0915a4571ad5061b2800c547ea5bd Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Tue, 28 Sep 2021 09:12:38 -0600 Subject: [PATCH 04/39] A little more file-moving, still much work to do and totally broken on action-module refactor --- modpol/modules/child_org.lua | 6 +++++- modpol/orgs/{defer_to.lua => defer.lua} | 0 modpol/orgs/requests.lua | 23 +---------------------- 3 files changed, 6 insertions(+), 23 deletions(-) rename modpol/orgs/{defer_to.lua => defer.lua} (100%) diff --git a/modpol/modules/child_org.lua b/modpol/modules/child_org.lua index e76abf5..8a1631a 100644 --- a/modpol/modules/child_org.lua +++ b/modpol/modules/child_org.lua @@ -8,9 +8,13 @@ modpol.modules.child_org.name = "Create child org" -- == REQUEST == +-- Initialization function +-- function: modpol.modules.child_org:initialize + + -- gather data from initiator: child org name, comment --- function: modpol.modules.child_org.request +-- function: modpol.modules.child_org:request diff --git a/modpol/orgs/defer_to.lua b/modpol/orgs/defer.lua similarity index 100% rename from modpol/orgs/defer_to.lua rename to modpol/orgs/defer.lua diff --git a/modpol/orgs/requests.lua b/modpol/orgs/requests.lua index f312a3f..4d8b545 100644 --- a/modpol/orgs/requests.lua +++ b/modpol/orgs/requests.lua @@ -14,30 +14,9 @@ modpol.default_policy = { target_org = nil, time_limit = nil, - vote_threshold = 0 + vote_threshold = 1 -- a ratio of the total number of members } --- Function: modpol.orgs:consent(params, result_function) --- params: table of optional preferences, e.g.: - -- target_org - -- time_limit - -- vote_threshold (0-100 percent, 0 is automatic approval) --- result_function: function(input) that takes result boolean -function modpol.orgs:consent(params, result_function) - -- if appropriate, pass on to target org - if params.target_org then - local target_org = params.target_org - params.target_org = nil - params.target_org:consent(params, result_function) - return - end - STUCK ON HOW TO IMPLEMENT THIS WITHOUT REQUIRING THE FULL PROCESS FUNCTIONALITY - -end - - - - -- PROCESSES -- functions that enable requests to create processes From 7163480581b8e526eddd2435f47cc1bfeb2fd0d3 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Tue, 28 Sep 2021 19:01:59 -0400 Subject: [PATCH 05/39] prototyping for new consent system --- modpol/orgs/process.lua | 55 ++++++++++++++++++++++++++++++++++++++++ modpol/orgs/requests.lua | 3 +++ 2 files changed, 58 insertions(+) create mode 100644 modpol/orgs/process.lua diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua new file mode 100644 index 0000000..4babcf4 --- /dev/null +++ b/modpol/orgs/process.lua @@ -0,0 +1,55 @@ +old_request_format = { + user=user, -- requesting user + type="add_member", -- action + params={user} -- action params +} + +old_process_format = { + type = "consent", -- delete + id = nil, + org_id = nil, + request_id = nil, -- delete + + -- consent config + majority_to_pass = 0.51, -- voting threshold + votes_needed = nil, + + -- consent data + total_votes = 0, + votes_yes = {}, + votes_no = {} +} + +new_process_format = { + initiator = "user", + org_id = 12314, + module = "create_child_org", -- policy table lookup + process_id = 8347, + -- used to freeze voter eligibility + timestamp = 1632850133, -- look into supporting other formats, overrides (turn based, etc.) + + + data = { + child_org_name = "oligarchy" + } + +} + +--[[ +voting configuration: + +minimum voters (ratio or const) +maximum voters (ratio or const) + + +minimum duration +maximum duration +--]] + +policy_table_format = { + "create_child_org": { + consent_threshold = 0.51, + max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit + defer = nil, -- org id to defer to, or nil + } +} \ No newline at end of file diff --git a/modpol/orgs/requests.lua b/modpol/orgs/requests.lua index 4d8b545..bcebab5 100644 --- a/modpol/orgs/requests.lua +++ b/modpol/orgs/requests.lua @@ -1,3 +1,6 @@ +-- THIS IS NOW DEPRECATED, TO MERGE INTO PROCESS + + -- REQUESTS -- Provides request functionality to org From a28c3139826a6eb9c16999ecb53f262a358deadf Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 4 Oct 2021 17:50:24 -0400 Subject: [PATCH 06/39] updated process and policy table --- modpol/orgs/process.lua | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 4babcf4..ddd478a 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -22,31 +22,43 @@ old_process_format = { new_process_format = { initiator = "user", + status = "request", org_id = 12314, module = "create_child_org", -- policy table lookup process_id = 8347, - -- used to freeze voter eligibility timestamp = 1632850133, -- look into supporting other formats, overrides (turn based, etc.) - data = { child_org_name = "oligarchy" + }, + + consent = { + -- voter eligibilty frozen by action table invites + start_time = 384179234, + member_count = 14, + votes_yes = {}, + votes_no = {} } } ---[[ -voting configuration: - -minimum voters (ratio or const) -maximum voters (ratio or const) - -minimum duration -maximum duration ---]] policy_table_format = { + "create_child_org": { + defer_to = nil, + + -- duration + duration = nil, -- evaluates end conditions when reached + + -- thesholds + no_threshold = nil, -- fails if reached + yes_threshold = nil, -- succeeds if reached + + --ratios + consent_ratio = nil, -- % of voters + quorum = nil, -- % of members that vote + } "create_child_org": { consent_threshold = 0.51, max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit From 2386c0e9294e5ae69cbbaf8fd6e7922b15ad3936 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 11 Oct 2021 09:58:56 -0400 Subject: [PATCH 07/39] first draft of consent system --- modpol/orgs/process.lua | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index ddd478a..52a7029 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -42,6 +42,90 @@ new_process_format = { } +-- initialize values +function init_consent(policy) { + self.start_time = os.time() + self.member_count = modpol.orgs.get_org(self.org_id):get_member_count() + + if policy.duration then + register_callback(self.start_time + policy.duration) + end + + if (duration and (consent_ratio or yes_threshold or no_threshold)) or (yes_threshold) or (consent_ratio) then + -- well formed policy + else + -- invalid + end + +} + +-- update vote count +function update_consent(user, approve) { + if approve then + table.insert(yes_votes, user) + else + table.insert(no_votes, user) + + if not duration then + eval_consent() + end + +} + +-- evaluate state of vote +function eval_consent() { + consent_ratio = #yes_votes / (#yes_votes + #no_votes) + quorum = (#yes_votes + #no_votes) / member_count + + if policy.duration then + + if policy.consent_ratio then + if policy.quorum then + if quorum < policy.quorum then + fail() + end + end + if consent_ratio >= policy.consent_ratio then + pass() + else + fail() + end + + elseif policy.yes_threshold then + if #yes_votes >= policy.yes_threshold then + pass() + else + fail() + end + + elseif policy.no_threshold then + if #no_votes <= policy.no_threshold then + fail() + else + pass() + end + end + + elseif policy.yes_threshold then + if policy.no_threshold then + if #no_votes >= policy.no_threshold then + fail() + end + if #yes_votes >= policy.yes_threshold then + pass() + end + + elseif policy.consent_ratio and policy.quorum then + if quorum >= policy.quorum then + if consent_ratio >= policy.consent_ratio then + pass() + else + fail() + end + end + end + +} policy_table_format = { From 8bc5c5ba4e226b479257d9b4a948bdfd93693d77 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 11 Oct 2021 15:26:15 -0400 Subject: [PATCH 08/39] mintest notes --- modpol/orgs/process.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 52a7029..0105f71 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -48,6 +48,8 @@ function init_consent(policy) { self.member_count = modpol.orgs.get_org(self.org_id):get_member_count() if policy.duration then + -- mintest.after(time, func, ...args) + -- should override modpol callback function register_callback(self.start_time + policy.duration) end @@ -65,7 +67,7 @@ function update_consent(user, approve) { table.insert(yes_votes, user) else table.insert(no_votes, user) - + if not duration then eval_consent() end From 0ca04294b7a2c3c3ad77e92b5a47f6c2de95caa5 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 22 Nov 2021 16:24:44 -0500 Subject: [PATCH 09/39] working version of join_org module demonstrated in tests/new_module_test.lua --- modpol/api.lua | 8 +++---- modpol/modpol.lua | 9 ++++---- modpol/modules/join_org.lua | 18 +++++++++++++++ modpol/modules/join_org_class.lua | 37 +++++++++++++++++++++++++++++++ modpol/tests/new_module_test.lua | 10 +++++++++ 5 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 modpol/modules/join_org_class.lua create mode 100644 modpol/tests/new_module_test.lua diff --git a/modpol/api.lua b/modpol/api.lua index 2b11371..d20c88f 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -9,12 +9,12 @@ dofile (localdir .. "/users/users.lua") dofile (localdir .. "/orgs/base.lua") dofile (localdir .. "/orgs/requests.lua") dofile (localdir .. "/orgs/consent.lua") -dofile (localdir .. "/orgs/defer_to.lua") +dofile (localdir .. "/orgs/defer.lua") --interactions dofile (localdir .. "/interactions/interactions.lua") --modules -dofile (loaldir .. "/modules/join_org.lua") -dofile (loaldir .. "/modules/remove_org.lua") -dofile (loaldir .. "/modules/child_org.lua") +dofile (localdir .. "/modules/join_org.lua") +dofile (localdir .. "/modules/remove_org.lua") +dofile (localdir .. "/modules/child_org.lua") diff --git a/modpol/modpol.lua b/modpol/modpol.lua index 21a63b2..e1f62cb 100644 --- a/modpol/modpol.lua +++ b/modpol/modpol.lua @@ -63,10 +63,6 @@ dofile (modpol.storage_file_path) -- If available, load persistent storage into active tables modpol.load_storage() --- =================================================================== --- Modpol core features - -dofile (topdir .. "/api.lua") -- =================================================================== -- Modpol modules @@ -75,6 +71,11 @@ modpol.modules = modpol.modules or {} -- TKTK need to specify modules to include +-- =================================================================== +-- Modpol core features + +dofile (topdir .. "/api.lua") + -- =================================================================== -- Final checks diff --git a/modpol/modules/join_org.lua b/modpol/modules/join_org.lua index e69de29..a608481 100644 --- a/modpol/modules/join_org.lua +++ b/modpol/modules/join_org.lua @@ -0,0 +1,18 @@ + +join_org = {} + +function join_org.initiate(initiator, org, result) + modpol.interactions.binary_poll_user( + initiator, + "Would you like to join " .. org.name, + function (resp) + if resp == "Yes" then + org:add_member(initiator) + end + end + ) + + if result then result() end +end + +modpol.modules.join_org = join_org diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua new file mode 100644 index 0000000..ccb29c2 --- /dev/null +++ b/modpol/modules/join_org_class.lua @@ -0,0 +1,37 @@ +-- JOIN ORG +-- Module that enables a user to join an org + +modpol.modules.join_org = {} +module = modpol.modules.join_org + +JoinOrg = {} +JoinOrg_mt = { __index = JoinOrg } + + +function JoinOrg:create(initiator, org) + local inst = { + name = "Join an org", + desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org." + } + setmetatable(inst, JoinOrg_mt) + return inst + +end + +function JoinOrg:initiate(initiator, org, result) + modpol.interactions.binary_poll_user(initiator, "Would you like to join", ) +end + +function JoinOrg:request() + +end + +function JoinOrg:implement() + self.org:add_member(self.initiator) +end + +-- =================================== +-- When calling a module internally + +test = JoinOrg.create() +test:initiate("luke") \ No newline at end of file diff --git a/modpol/tests/new_module_test.lua b/modpol/tests/new_module_test.lua new file mode 100644 index 0000000..3a0201f --- /dev/null +++ b/modpol/tests/new_module_test.lua @@ -0,0 +1,10 @@ +dofile('../modpol.lua'); + +modpol.orgs.reset() + +test_org = modpol.instance:add_org('test_org', 'luke') +test_org:add_member('nathan') + +print(table.concat(test_org:list_members(), ", ")) + +modpol.modules.join_org.initiate("paul", test_org, function () print("callback") end) \ No newline at end of file From db43df6ce2a3240e12103bb15ec8be84699bda1b Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 22 Nov 2021 16:49:03 -0500 Subject: [PATCH 10/39] adding class version of join org module --- modpol/api.lua | 1 + modpol/modules/join_org.lua | 15 +++++++++++++++ modpol/modules/join_org_class.lua | 27 ++++++++++++++++++--------- modpol/tests/new_module_test.lua | 9 ++++++++- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/modpol/api.lua b/modpol/api.lua index d20c88f..5e278ec 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -16,5 +16,6 @@ dofile (localdir .. "/interactions/interactions.lua") --modules dofile (localdir .. "/modules/join_org.lua") +dofile (localdir .. "/modules/join_org_class.lua") dofile (localdir .. "/modules/remove_org.lua") dofile (localdir .. "/modules/child_org.lua") diff --git a/modpol/modules/join_org.lua b/modpol/modules/join_org.lua index a608481..9b20287 100644 --- a/modpol/modules/join_org.lua +++ b/modpol/modules/join_org.lua @@ -12,6 +12,21 @@ function join_org.initiate(initiator, org, result) end ) + -- for i, member in ipairs(org.members) do + -- org:add_pending_action( + -- member, + -- function () + -- modpol.interactions.binary_poll_user( + -- member, + -- "Let " .. initiator .. " join " .. org.name .. "?", + -- function (resp) + + -- end + -- ) + -- end + -- ) + -- end + if result then result() end end diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua index ccb29c2..5d8305c 100644 --- a/modpol/modules/join_org_class.lua +++ b/modpol/modules/join_org_class.lua @@ -1,25 +1,35 @@ -- JOIN ORG -- Module that enables a user to join an org -modpol.modules.join_org = {} -module = modpol.modules.join_org - JoinOrg = {} JoinOrg_mt = { __index = JoinOrg } -function JoinOrg:create(initiator, org) +function JoinOrg.create(initiator, org) local inst = { name = "Join an org", - desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org." + desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.", + initiator = initiator, + org = org } setmetatable(inst, JoinOrg_mt) return inst end -function JoinOrg:initiate(initiator, org, result) - modpol.interactions.binary_poll_user(initiator, "Would you like to join", ) +function JoinOrg:initiate(result) + modpol.interactions.binary_poll_user( + self.initiator, + "Would you like to join", + function (resp) + if resp == "Yes" then + self:implement() + end + end + ) + + if result then result() end + end function JoinOrg:request() @@ -33,5 +43,4 @@ end -- =================================== -- When calling a module internally -test = JoinOrg.create() -test:initiate("luke") \ No newline at end of file +modpol.modules.join_org_class = JoinOrg \ No newline at end of file diff --git a/modpol/tests/new_module_test.lua b/modpol/tests/new_module_test.lua index 3a0201f..2f83e9a 100644 --- a/modpol/tests/new_module_test.lua +++ b/modpol/tests/new_module_test.lua @@ -7,4 +7,11 @@ test_org:add_member('nathan') print(table.concat(test_org:list_members(), ", ")) -modpol.modules.join_org.initiate("paul", test_org, function () print("callback") end) \ No newline at end of file +function completion() + print("completed") +end + +-- modpol.modules.join_org.initiate("paul", test_org, completion) + +process = modpol.modules.join_org_class.create("paul", test_org) +process:initiate(completion) \ No newline at end of file From f381845d21a17baff86b500ac69f888207ba773d Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 29 Nov 2021 14:36:18 -0500 Subject: [PATCH 11/39] added some notes to the modules, some not currently functional. rewriting process.lua to work with new modules, support pending actions --- modpol/modules/join_org.lua | 28 +++--- modpol/modules/join_org_class.lua | 37 ++++++- modpol/orgs/base.lua | 2 +- modpol/orgs/process.lua | 160 ++++-------------------------- modpol/orgs/process_old.lua | 153 ++++++++++++++++++++++++++++ modpol/tests/new_module_test.lua | 6 +- 6 files changed, 226 insertions(+), 160 deletions(-) create mode 100644 modpol/orgs/process_old.lua diff --git a/modpol/modules/join_org.lua b/modpol/modules/join_org.lua index 9b20287..3353dae 100644 --- a/modpol/modules/join_org.lua +++ b/modpol/modules/join_org.lua @@ -12,20 +12,20 @@ function join_org.initiate(initiator, org, result) end ) - -- for i, member in ipairs(org.members) do - -- org:add_pending_action( - -- member, - -- function () - -- modpol.interactions.binary_poll_user( - -- member, - -- "Let " .. initiator .. " join " .. org.name .. "?", - -- function (resp) - - -- end - -- ) - -- end - -- ) - -- end + for i, member in ipairs(org.members) do + org:add_pending_action( + member, + function () + modpol.interactions.binary_poll_user( + member, + "Let " .. initiator .. " join " .. org.name .. "?", + function (resp) + + end + ) + end + ) + end if result then result() end end diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua index 5d8305c..5e574c9 100644 --- a/modpol/modules/join_org_class.lua +++ b/modpol/modules/join_org_class.lua @@ -23,11 +23,44 @@ function JoinOrg:initiate(result) "Would you like to join", function (resp) if resp == "Yes" then - self:implement() + + modpol.call_module( + "consent", + "", + org, + result + + ) + + + modpol.modules.consent( + "Let " .. initiator .. " join " .. org.name .. "?", + org, + params, + self:on_failure, + self:on_success + ) + + -- for i, member in ipairs(org.members) do + -- org:add_pending_action( + -- member, + -- function () + -- modpol.interactions.binary_poll_user( + -- member, + -- "Let " .. initiator .. " join " .. org.name .. "?", + -- function (resp) + -- self:vote() + -- end + -- ) + -- end + -- ) + -- end end end ) + + if result then result() end end @@ -36,7 +69,7 @@ function JoinOrg:request() end -function JoinOrg:implement() +function JoinOrg:on_success() self.org:add_member(self.initiator) end diff --git a/modpol/orgs/base.lua b/modpol/orgs/base.lua index b86d7bf..f26523c 100644 --- a/modpol/orgs/base.lua +++ b/modpol/orgs/base.lua @@ -18,7 +18,7 @@ function temp_org() remove_member={process_type='consent', must_be_member=false} }, processes = {}, - requests = {}, + -- requests = {}, pending = {}, members = {}, parent = nil, diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 0105f71..c1b2f60 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -1,153 +1,33 @@ -old_request_format = { - user=user, -- requesting user - type="add_member", -- action - params={user} -- action params -} - -old_process_format = { - type = "consent", -- delete - id = nil, - org_id = nil, - request_id = nil, -- delete - - -- consent config - majority_to_pass = 0.51, -- voting threshold - votes_needed = nil, - - -- consent data - total_votes = 0, - votes_yes = {}, - votes_no = {} -} - -new_process_format = { - initiator = "user", - status = "request", - org_id = 12314, - module = "create_child_org", -- policy table lookup - process_id = 8347, - timestamp = 1632850133, -- look into supporting other formats, overrides (turn based, etc.) - - data = { - child_org_name = "oligarchy" - }, - - consent = { - -- voter eligibilty frozen by action table invites - start_time = 384179234, - member_count = 14, - votes_yes = {}, - votes_no = {} - } - -} - --- initialize values -function init_consent(policy) { - self.start_time = os.time() - self.member_count = modpol.orgs.get_org(self.org_id):get_member_count() - - if policy.duration then - -- mintest.after(time, func, ...args) - -- should override modpol callback function - register_callback(self.start_time + policy.duration) +function modpol.orgs:call_module(module_name, initiator) + if not modpol.modules[module_name] then + modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. name .. '" not found') + return end - if (duration and (consent_ratio or yes_threshold or no_threshold)) or (yes_threshold) or (consent_ratio) then - -- well formed policy - else - -- invalid - end + local module = modpol.modules[module_name] + local new_process = module.create(initiator, self) -} + table.insert(self.processes, new_process) + + return new_process +end --- update vote count -function update_consent(user, approve) { - if approve then - table.insert(yes_votes, user) - else - table.insert(no_votes, user) +function modpol.orgs:create_process() - if not duration then - eval_consent() - end +end -} +function modpol.orgs:add_pending_action() --- evaluate state of vote -function eval_consent() { - consent_ratio = #yes_votes / (#yes_votes + #no_votes) - quorum = (#yes_votes + #no_votes) / member_count +end - if policy.duration then +function mopdol.orgs:remove_pending_action() - if policy.consent_ratio then - if policy.quorum then - if quorum < policy.quorum then - fail() - end - end - if consent_ratio >= policy.consent_ratio then - pass() - else - fail() - end +end - elseif policy.yes_threshold then - if #yes_votes >= policy.yes_threshold then - pass() - else - fail() - end +function modpol.orgs:wipe_pending_actions() - elseif policy.no_threshold then - if #no_votes <= policy.no_threshold then - fail() - else - pass() - end - end +end - elseif policy.yes_threshold then - if policy.no_threshold then - if #no_votes >= policy.no_threshold then - fail() - end - if #yes_votes >= policy.yes_threshold then - pass() - end +function modpol.orgs:has_pending_actions() - elseif policy.consent_ratio and policy.quorum then - if quorum >= policy.quorum then - if consent_ratio >= policy.consent_ratio then - pass() - else - fail() - end - end - end - -} - - -policy_table_format = { - "create_child_org": { - defer_to = nil, - - -- duration - duration = nil, -- evaluates end conditions when reached - - -- thesholds - no_threshold = nil, -- fails if reached - yes_threshold = nil, -- succeeds if reached - - --ratios - consent_ratio = nil, -- % of voters - quorum = nil, -- % of members that vote - } - "create_child_org": { - consent_threshold = 0.51, - max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit - defer = nil, -- org id to defer to, or nil - } -} \ No newline at end of file +end \ No newline at end of file diff --git a/modpol/orgs/process_old.lua b/modpol/orgs/process_old.lua new file mode 100644 index 0000000..0105f71 --- /dev/null +++ b/modpol/orgs/process_old.lua @@ -0,0 +1,153 @@ +old_request_format = { + user=user, -- requesting user + type="add_member", -- action + params={user} -- action params +} + +old_process_format = { + type = "consent", -- delete + id = nil, + org_id = nil, + request_id = nil, -- delete + + -- consent config + majority_to_pass = 0.51, -- voting threshold + votes_needed = nil, + + -- consent data + total_votes = 0, + votes_yes = {}, + votes_no = {} +} + +new_process_format = { + initiator = "user", + status = "request", + org_id = 12314, + module = "create_child_org", -- policy table lookup + process_id = 8347, + timestamp = 1632850133, -- look into supporting other formats, overrides (turn based, etc.) + + data = { + child_org_name = "oligarchy" + }, + + consent = { + -- voter eligibilty frozen by action table invites + start_time = 384179234, + member_count = 14, + votes_yes = {}, + votes_no = {} + } + +} + +-- initialize values +function init_consent(policy) { + self.start_time = os.time() + self.member_count = modpol.orgs.get_org(self.org_id):get_member_count() + + if policy.duration then + -- mintest.after(time, func, ...args) + -- should override modpol callback function + register_callback(self.start_time + policy.duration) + end + + if (duration and (consent_ratio or yes_threshold or no_threshold)) or (yes_threshold) or (consent_ratio) then + -- well formed policy + else + -- invalid + end + +} + +-- update vote count +function update_consent(user, approve) { + if approve then + table.insert(yes_votes, user) + else + table.insert(no_votes, user) + + if not duration then + eval_consent() + end + +} + +-- evaluate state of vote +function eval_consent() { + consent_ratio = #yes_votes / (#yes_votes + #no_votes) + quorum = (#yes_votes + #no_votes) / member_count + + if policy.duration then + + if policy.consent_ratio then + if policy.quorum then + if quorum < policy.quorum then + fail() + end + end + if consent_ratio >= policy.consent_ratio then + pass() + else + fail() + end + + elseif policy.yes_threshold then + if #yes_votes >= policy.yes_threshold then + pass() + else + fail() + end + + elseif policy.no_threshold then + if #no_votes <= policy.no_threshold then + fail() + else + pass() + end + end + + elseif policy.yes_threshold then + if policy.no_threshold then + if #no_votes >= policy.no_threshold then + fail() + end + if #yes_votes >= policy.yes_threshold then + pass() + end + + elseif policy.consent_ratio and policy.quorum then + if quorum >= policy.quorum then + if consent_ratio >= policy.consent_ratio then + pass() + else + fail() + end + end + end + +} + + +policy_table_format = { + "create_child_org": { + defer_to = nil, + + -- duration + duration = nil, -- evaluates end conditions when reached + + -- thesholds + no_threshold = nil, -- fails if reached + yes_threshold = nil, -- succeeds if reached + + --ratios + consent_ratio = nil, -- % of voters + quorum = nil, -- % of members that vote + } + "create_child_org": { + consent_threshold = 0.51, + max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit + defer = nil, -- org id to defer to, or nil + } +} \ No newline at end of file diff --git a/modpol/tests/new_module_test.lua b/modpol/tests/new_module_test.lua index 2f83e9a..5dcf4a4 100644 --- a/modpol/tests/new_module_test.lua +++ b/modpol/tests/new_module_test.lua @@ -11,7 +11,7 @@ function completion() print("completed") end --- modpol.modules.join_org.initiate("paul", test_org, completion) +modpol.modules.join_org.initiate("paul", test_org, completion) -process = modpol.modules.join_org_class.create("paul", test_org) -process:initiate(completion) \ No newline at end of file +-- process = modpol.modules.join_org_class.create("paul", test_org) +-- process:initiate(completion) \ No newline at end of file From 1d45dc38b09a0f93947edba4d886e7375ede909a Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 29 Nov 2021 17:53:32 -0500 Subject: [PATCH 12/39] first pass of new process system --- modpol/modules/join_org_class.lua | 6 ++- modpol/orgs/process.lua | 66 +++++++++++++++++++++++++------ 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua index 5e574c9..7b15446 100644 --- a/modpol/modules/join_org_class.lua +++ b/modpol/modules/join_org_class.lua @@ -5,12 +5,13 @@ JoinOrg = {} JoinOrg_mt = { __index = JoinOrg } -function JoinOrg.create(initiator, org) +function JoinOrg.create(initiator, org, id) local inst = { name = "Join an org", desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.", initiator = initiator, - org = org + org = org, + id = id } setmetatable(inst, JoinOrg_mt) return inst @@ -71,6 +72,7 @@ end function JoinOrg:on_success() self.org:add_member(self.initiator) + self.org:delete_process(self.id) end -- =================================== diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index c1b2f60..1c66c16 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -4,30 +4,72 @@ function modpol.orgs:call_module(module_name, initiator) return end + local empty_index = nil + -- linear search for empty process slots (lazy deletion) + for k, v in ipairs(self.processes) do + if v == 'deleted' then + empty_index = k + break + end + end + + local index + -- attempts to fill empty spots in list, otherwise appends to end + if empty_index then + index = empty_index + else + index = #self.processes + 1 + end + local module = modpol.modules[module_name] - local new_process = module.create(initiator, self) + local new_process = module.create(initiator, self, index) - table.insert(self.processes, new_process) - - return new_process + self.processes[index] = new_process + + return index end -function modpol.orgs:create_process() - +function modpol.orgs:delete_process(id) + self.processes[id] = 'deleted' end -function modpol.orgs:add_pending_action() - +function modpol.orgs:add_pending_action(process_id, user, callback) + self.pending[user] = self.pending[user] or {} + self.pending[user][process_id] = callback end -function mopdol.orgs:remove_pending_action() - +function mopdol.orgs:remove_pending_action(process_id, user) + if self.pending[user] then + self.pending[user][process_id] = nil + end end -function modpol.orgs:wipe_pending_actions() - +function modpol.orgs:wipe_pending_actions(process_id) + for user in pairs(self.pending) do + self.pending[user][process_id] = nil + end end function modpol.orgs:has_pending_actions() + -- next() will return the next pair in a table + -- if next() returns nil, the table is empty + if not self.pending[user] then + return false + else + if not next(self.pending[user]) then + return false + else + return true + end + end +end +function modpol.orgs:interact(process_id, user) + local process = self.processes[process_id] + if self.pending[user] then + local callback = self.pending[user][process_id] + if callback then + process[callback](process) + end + end end \ No newline at end of file From 08151ad9d9172b3534ac7ff2770f915cacf8b9e5 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 29 Nov 2021 18:29:39 -0500 Subject: [PATCH 13/39] notes from meeting --- modpol/modules/join_org_class.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua index 7b15446..cda1647 100644 --- a/modpol/modules/join_org_class.lua +++ b/modpol/modules/join_org_class.lua @@ -60,16 +60,37 @@ function JoinOrg:initiate(result) end ) + for user in pairs(self.org.users) { + self.org:add_pending_action(self.id, user, "callback") + + + org.pending.user[2348] = "callback" + + self:callback(user) + } if result then result() end end -function JoinOrg:request() +function JoinOrg:callback(user) + modpol.interactions.binary_poll_user( + self.initiator, + "Do you want this" .. self.user_to_add .. "to join?", + function (resp) + if resp == "yes" then + self.votes_yes += 1 + end + self:evaluate_vote() + + end + ) end + + function JoinOrg:on_success() self.org:add_member(self.initiator) self.org:delete_process(self.id) From fa7a0f82f64b2c4beba6e7f201deb63ee899db7b Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 6 Dec 2021 16:45:52 -0500 Subject: [PATCH 14/39] functional test of call_module and new pending action/interact version --- modpol/api.lua | 4 +- modpol/modules/join_org_class.lua | 66 ++++++++----------------------- modpol/orgs/example.lua | 48 ++++++++++++++++++++++ modpol/orgs/process.lua | 6 ++- modpol/tests/new_module_test.lua | 12 +++--- 5 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 modpol/orgs/example.lua diff --git a/modpol/api.lua b/modpol/api.lua index 5e278ec..e1daaec 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -7,9 +7,7 @@ dofile (localdir .. "/users/users.lua") --orgs dofile (localdir .. "/orgs/base.lua") -dofile (localdir .. "/orgs/requests.lua") -dofile (localdir .. "/orgs/consent.lua") -dofile (localdir .. "/orgs/defer.lua") +dofile (localdir .. "/orgs/process.lua") --interactions dofile (localdir .. "/interactions/interactions.lua") diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua index cda1647..0533e8a 100644 --- a/modpol/modules/join_org_class.lua +++ b/modpol/modules/join_org_class.lua @@ -11,7 +11,8 @@ function JoinOrg.create(initiator, org, id) desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.", initiator = initiator, org = org, - id = id + id = id, + votes_yes = 0 } setmetatable(inst, JoinOrg_mt) return inst @@ -24,63 +25,27 @@ function JoinOrg:initiate(result) "Would you like to join", function (resp) if resp == "Yes" then - - modpol.call_module( - "consent", - "", - org, - result - - ) - - - modpol.modules.consent( - "Let " .. initiator .. " join " .. org.name .. "?", - org, - params, - self:on_failure, - self:on_success - ) - - -- for i, member in ipairs(org.members) do - -- org:add_pending_action( - -- member, - -- function () - -- modpol.interactions.binary_poll_user( - -- member, - -- "Let " .. initiator .. " join " .. org.name .. "?", - -- function (resp) - -- self:vote() - -- end - -- ) - -- end - -- ) - -- end + for id, member in pairs(self.org.members) do + self.org:add_pending_action(self.id, member, "callback") + + end end end ) - for user in pairs(self.org.users) { - self.org:add_pending_action(self.id, user, "callback") - - - org.pending.user[2348] = "callback" - - self:callback(user) - } if result then result() end end -function JoinOrg:callback(user) +function JoinOrg:callback(member) modpol.interactions.binary_poll_user( - self.initiator, - "Do you want this" .. self.user_to_add .. "to join?", + member, + "Do you want " .. self.initiator .. " to join?", function (resp) - if resp == "yes" then - self.votes_yes += 1 + if resp == "Yes" then + self.votes_yes = self.votes_yes + 1 end self:evaluate_vote() @@ -91,9 +56,12 @@ end -function JoinOrg:on_success() - self.org:add_member(self.initiator) - self.org:delete_process(self.id) +function JoinOrg:evaluate_vote() + if self.votes_yes >= 1 then + print('added user') + self.org:add_member(self.initiator) + self.org:wipe_pending_actions(self.id) + end end -- =================================== diff --git a/modpol/orgs/example.lua b/modpol/orgs/example.lua new file mode 100644 index 0000000..b16333b --- /dev/null +++ b/modpol/orgs/example.lua @@ -0,0 +1,48 @@ +--[[ +modpol calls this function when someone starts a new request for this module +--]] + +function module:initiate(org) { + + form = { + { + "name": "to_remove", + "display": "Which user should be removed?", + "type": "drop-down", + "values": org:list_members() + }, + { + "name": "reason", + "type": "text-box", + "prompt": "Reason for removing member:" + } + } + + return form +} + +--[[ +modpol prompts the user with this form, and after receiving the data asynchronously +the returned form would look like: + +{ + "to_remove": luke, + "reason": stealing food +} + +based on provided "name" fields and the input given by the user +now module:request is called +--]] + +function module:request(form) { + self.data = form +} + +--[[ +after the module request function runs, modpol will initiate the consent process +if consent is approved, the implement function is called +--]] + +function module:implement() { + org:remove_member(self.data.to_remove) +} \ No newline at end of file diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 1c66c16..d514f5b 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -26,6 +26,8 @@ function modpol.orgs:call_module(module_name, initiator) self.processes[index] = new_process + self.processes[index]:initiate() + return index end @@ -38,7 +40,7 @@ function modpol.orgs:add_pending_action(process_id, user, callback) self.pending[user][process_id] = callback end -function mopdol.orgs:remove_pending_action(process_id, user) +function modpol.orgs:remove_pending_action(process_id, user) if self.pending[user] then self.pending[user][process_id] = nil end @@ -69,7 +71,7 @@ function modpol.orgs:interact(process_id, user) if self.pending[user] then local callback = self.pending[user][process_id] if callback then - process[callback](process) + process[callback](process, user) end end end \ No newline at end of file diff --git a/modpol/tests/new_module_test.lua b/modpol/tests/new_module_test.lua index 5dcf4a4..48bc292 100644 --- a/modpol/tests/new_module_test.lua +++ b/modpol/tests/new_module_test.lua @@ -7,11 +7,9 @@ test_org:add_member('nathan') print(table.concat(test_org:list_members(), ", ")) -function completion() - print("completed") -end +-- modpol.modules.join_org.initiate("paul", test_org) -modpol.modules.join_org.initiate("paul", test_org, completion) - --- process = modpol.modules.join_org_class.create("paul", test_org) --- process:initiate(completion) \ No newline at end of file +test_org:call_module( + "join_org_class", + "paul" +) \ No newline at end of file From 826f9600b52f0dad123bf48241470f2eca96ec49 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Thu, 16 Dec 2021 13:32:30 -0500 Subject: [PATCH 15/39] cleaned up module template! --- modpol/modules/join_org_class.lua | 22 +++++----------------- modpol/orgs/process.lua | 14 +++++++++++++- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua index 0533e8a..9160425 100644 --- a/modpol/modules/join_org_class.lua +++ b/modpol/modules/join_org_class.lua @@ -2,22 +2,12 @@ -- Module that enables a user to join an org JoinOrg = {} -JoinOrg_mt = { __index = JoinOrg } - -function JoinOrg.create(initiator, org, id) - local inst = { - name = "Join an org", - desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.", - initiator = initiator, - org = org, - id = id, - votes_yes = 0 - } - setmetatable(inst, JoinOrg_mt) - return inst - -end +JoinOrg.setup = { + name = "Join an org", + desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.", + votes_yes = 0 +} function JoinOrg:initiate(result) modpol.interactions.binary_poll_user( @@ -33,8 +23,6 @@ function JoinOrg:initiate(result) end ) - - if result then result() end end diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index d514f5b..5ede0ed 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -22,7 +22,19 @@ function modpol.orgs:call_module(module_name, initiator) end local module = modpol.modules[module_name] - local new_process = module.create(initiator, self, index) + + local new_process = { + metatable = {__index = module}, + initiator = initiator, + org = self, + id = index + } + + for k, v in pairs(module.setup) do + new_process[k] = v + end + + setmetatable(new_process, new_process.metatable) self.processes[index] = new_process From 6e02e254aa8c827236e092bd373e36e61d667a72 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Thu, 16 Dec 2021 14:03:42 -0500 Subject: [PATCH 16/39] added module template, added support for module parameters and default values through the config table --- modpol/modules/template.lua | 29 +++++++++++++++++++++++++++++ modpol/orgs/process.lua | 15 ++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 modpol/modules/template.lua diff --git a/modpol/modules/template.lua b/modpol/modules/template.lua new file mode 100644 index 0000000..52144ec --- /dev/null +++ b/modpol/modules/template.lua @@ -0,0 +1,29 @@ + + +Module = {} + +Module.setup = { + name = "Module Name", + desc = "Description of the module" +} + +-- defines the input parameters to the module initiate function +-- when calling a module from within another module +-- variables not defined in config will be ignored +-- default values set in config can be overridden +Module.config = { + votes_required = 5 + voting_type = "majority" +} + +-- Modules have access to the following instance variables +-- self.org (the org the module was called in) +-- self.initiator (the user that callced the module) +-- self.id (the process id of the module instance) + +function Module:initiate(config, result) + -- call interaction functions here! + + -- call result function + if result then result() end +end \ No newline at end of file diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 5ede0ed..a24dd6b 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -1,4 +1,4 @@ -function modpol.orgs:call_module(module_name, initiator) +function modpol.orgs:call_module(module_name, initiator, config, result) if not modpol.modules[module_name] then modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. name .. '" not found') return @@ -23,13 +23,15 @@ function modpol.orgs:call_module(module_name, initiator) local module = modpol.modules[module_name] + -- setting default params local new_process = { metatable = {__index = module}, initiator = initiator, org = self, id = index } - + + -- copying default fields from setup for k, v in pairs(module.setup) do new_process[k] = v end @@ -38,7 +40,14 @@ function modpol.orgs:call_module(module_name, initiator) self.processes[index] = new_process - self.processes[index]:initiate() + -- sets default values for undeclared config variables + for k, v in pairs(module.config) do + if config[k] == nil then + config[k] = v + end + end + + self.processes[index]:initiate(config, result)) return index end From 0b265d7be5bff69095b1dfeea789ddea1dac222f Mon Sep 17 00:00:00 2001 From: Skylar Hew Date: Thu, 16 Dec 2021 14:07:58 -0700 Subject: [PATCH 17/39] Added ldoc comments and slug variable for setup --- modpol/modules/template.lua | 48 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/modpol/modules/template.lua b/modpol/modules/template.lua index 52144ec..7a3fbc6 100644 --- a/modpol/modules/template.lua +++ b/modpol/modules/template.lua @@ -1,29 +1,39 @@ +--- Template for modules +-- function initiate and table setup are required as a base. +-- @module Template +Template = {} - -Module = {} - -Module.setup = { - name = "Module Name", +--- (Required): setup table containing name and description of the module +-- @field name "Module Human-Readable Name" +-- @field slug "Template" same as module name +-- @field desc "Description of the module" +Template.setup = { + name = "Module Human-Readable Name", + slug = "Template", desc = "Description of the module" } --- defines the input parameters to the module initiate function --- when calling a module from within another module --- variables not defined in config will be ignored --- default values set in config can be overridden -Module.config = { - votes_required = 5 - voting_type = "majority" +--- (Optional): config for module +-- Defines the input parameters to the module initiate function. +-- When calling a module from within another module, +-- variables not defined in config will be ignored. +-- Default values set in config can be overridden +-- @field field_1 ex: votes_required, default = 5 +-- @field field_2 ex: voting_type, default = "majority" +Template.config = { + field_1 = 5 + field_2 = "majority" } --- Modules have access to the following instance variables --- self.org (the org the module was called in) --- self.initiator (the user that callced the module) --- self.id (the process id of the module instance) - -function Module:initiate(config, result) +--- (Required): initiate function +-- Modules have access to the following instance variables: +--
  • self.org (the org the module was called in),
  • +--
  • self.initiator (the user that callced the module),
  • +--
  • self.id (the process id of the module instance)
  • +-- @function initiate +function Template:initiate(config, result) -- call interaction functions here! -- call result function if result then result() end -end \ No newline at end of file +end From 48123b3496c52ebc37479409cc9fcf74cd9665ee Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Thu, 16 Dec 2021 16:52:06 -0500 Subject: [PATCH 18/39] added consent module, nesting mdoules now functional! --- modpol/api.lua | 2 ++ modpol/modules/consent.lua | 42 +++++++++++++++++++++++++++++ modpol/modules/join_org_consent.lua | 30 +++++++++++++++++++++ modpol/orgs/process.lua | 24 +++++++++-------- modpol/tests/nested_functions.lua | 31 --------------------- modpol/tests/nested_module_test.lua | 13 +++++++++ 6 files changed, 100 insertions(+), 42 deletions(-) create mode 100644 modpol/modules/consent.lua create mode 100644 modpol/modules/join_org_consent.lua delete mode 100644 modpol/tests/nested_functions.lua create mode 100644 modpol/tests/nested_module_test.lua diff --git a/modpol/api.lua b/modpol/api.lua index e1daaec..9fa3c03 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -15,5 +15,7 @@ dofile (localdir .. "/interactions/interactions.lua") --modules dofile (localdir .. "/modules/join_org.lua") dofile (localdir .. "/modules/join_org_class.lua") +dofile (localdir .. "/modules/join_org_consent.lua") +dofile (localdir .. "/modules/consent.lua") dofile (localdir .. "/modules/remove_org.lua") dofile (localdir .. "/modules/child_org.lua") diff --git a/modpol/modules/consent.lua b/modpol/modules/consent.lua new file mode 100644 index 0000000..27b1cd8 --- /dev/null +++ b/modpol/modules/consent.lua @@ -0,0 +1,42 @@ + + +Consent = {} + +Consent.setup = { + name = "Consent", + slug = "consent", + desc = "Other modules can use to implement consent based decision making", + votes = 0 +} + +Consent.config = { + prompt = "Would you like to approve this action?", + votes_required = 1 +} + +function Consent:initiate(result) + self.result = result + for id, member in pairs(self.org.members) do + self.org:add_pending_action(self.id, member, "callback") + end +end + +function Consent:callback(member) + modpol.interactions.binary_poll_user( + member, + self.config.prompt, + function (resp) + if resp == "Yes" then + self.votes = self.votes + 1 + end + + if self.votes >= self.config.votes_required then + self.result() + self.org:wipe_pending_actions(self.id) + end + end + ) +end + + +modpol.modules.consent = Consent \ No newline at end of file diff --git a/modpol/modules/join_org_consent.lua b/modpol/modules/join_org_consent.lua new file mode 100644 index 0000000..cc02d4e --- /dev/null +++ b/modpol/modules/join_org_consent.lua @@ -0,0 +1,30 @@ + + +JoinOrg = {} + +JoinOrg.setup = { + name = "Join an org", + slug = "join_org_consent", + desc = "Consent based join org module" +} + +function JoinOrg:initiate() + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Allow " .. self.initiator .. " to join?", + votes_required = 1 + }, + function () + self:complete() + end + ) +end + +function JoinOrg:complete() + self.org:add_member(self.initiator) + print("Added " .. self.initiator .. " to the org!") +end + +modpol.modules.join_org_consent = JoinOrg \ No newline at end of file diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index a24dd6b..1b963ed 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -1,6 +1,6 @@ function modpol.orgs:call_module(module_name, initiator, config, result) if not modpol.modules[module_name] then - modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. name .. '" not found') + modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_name .. '" not found') return end @@ -23,12 +23,22 @@ function modpol.orgs:call_module(module_name, initiator, config, result) local module = modpol.modules[module_name] + -- sets default values for undeclared config variables + if module.config then + for k, v in pairs(module.config) do + if config[k] == nil then + config[k] = v + end + end + end + -- setting default params local new_process = { metatable = {__index = module}, initiator = initiator, org = self, - id = index + id = index, + config = config } -- copying default fields from setup @@ -39,15 +49,7 @@ function modpol.orgs:call_module(module_name, initiator, config, result) setmetatable(new_process, new_process.metatable) self.processes[index] = new_process - - -- sets default values for undeclared config variables - for k, v in pairs(module.config) do - if config[k] == nil then - config[k] = v - end - end - - self.processes[index]:initiate(config, result)) + self.processes[index]:initiate(result) return index end diff --git a/modpol/tests/nested_functions.lua b/modpol/tests/nested_functions.lua deleted file mode 100644 index 914d68f..0000000 --- a/modpol/tests/nested_functions.lua +++ /dev/null @@ -1,31 +0,0 @@ -dofile("../modpol.lua") - -print('\nRemoving existing orgs') -modpol.orgs.reset() - -print('\nCreating an org called "test_org"') -test_org = modpol.instance:add_org('test_org', 'luke') - -print('\nAdding user "nathan" to test_org') -test_org:add_member('nathan') - -print("\nOkay, let's start with requests. Setting up an org...") -modpol.instance:add_org('test_org', 'luke') -test_org:add_member('nathan') - -print('\nMaking consent the add_member policy') -test_org:set_policy("add_member", "consent", false); - -print('\nMaking a request to add Josh') -add_josh = { - user = "josh", - type = "add_member", - params = {"josh"} -} -request_id = test_org:make_request(add_josh) - - -print('\nHave the two members vote on it') -modpol.interactions.org_dashboard("nathan","test_org") -modpol.interactions.org_dashboard("luke","test_org") - diff --git a/modpol/tests/nested_module_test.lua b/modpol/tests/nested_module_test.lua new file mode 100644 index 0000000..611a616 --- /dev/null +++ b/modpol/tests/nested_module_test.lua @@ -0,0 +1,13 @@ +dofile('../modpol.lua'); + +modpol.orgs.reset() + +test_org = modpol.instance:add_org('test_org', 'luke') +test_org:add_member('nathan') + +print(table.concat(test_org:list_members(), ", ")) + +test_org:call_module( + "join_org_consent", + "paul" +) \ No newline at end of file From 28e2710efc3b84f4b634347b92d4c0745e886393 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Thu, 16 Dec 2021 15:07:26 -0700 Subject: [PATCH 19/39] CLI interactions updated for modules-as-actions --- README.md | 7 +-- modpol/interactions/interactions.lua | 80 ++++++++++++++-------------- modpol/orgs/base.lua | 8 +-- 3 files changed, 45 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 954d1a6..ca858a4 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,12 @@ In the game, open the Modular Politics interface with the command `/modpol`. Modular Politics can also be used independently of Minetest as a command-line tool. Currently command-line use of modpol requires a Unix-style system, but it is intended to become more fully platform independent. -The command-line version is in the `modpol` subdirectory. To interact with the interpreter on Unix systems in CLI mode, install lua or luajit and execute the following command in this directory: +The command-line version is in the `modpol` subdirectory. To interact with the interpreter on Unix systems in CLI mode, install lua or luajit and execute the following in this directory: ``` -$ cd modpol/ +$ cd modpol/interacctions/ $ lua [or luajit] -> dofile("modpol.lua") +> dofile("login.lua") ``` For a list of global functions and tables, use `modpol.menu()`. @@ -45,6 +45,7 @@ Other contributors include: * [Luke Miller](https://gitlab.com/lukvmil) (main control flow, object orientation, module spec) * [MisterE](https://gitlab.com/gbrrudmin) (project refactoring, core feature development) * Robert Kiraly [[OldCoder](https://github.com/oldcoder/)] (ocutils.lua, storage-local.lua, project refactoring) +* Skylar Hew (documentation) We'd love to welcome more contributors, particularly from the Minetest community! Please join the conversation in the [Issues](https://gitlab.com/medlabboulder/modpol/-/issues) or the [Minetest.net forum](https://forum.minetest.net/viewtopic.php?f=47&t=26037). diff --git a/modpol/interactions/interactions.lua b/modpol/interactions/interactions.lua index d51f47b..5e2bddf 100644 --- a/modpol/interactions/interactions.lua +++ b/modpol/interactions/interactions.lua @@ -38,10 +38,12 @@ function modpol.interactions.dashboard(user) local sel = io.read() print() - local sel_org = modpol.orgs.array[tonumber(sel)].name - if not sel_org then return end - + if modpol.orgs.array[tonumber(sel)] then + local sel_org = modpol.orgs.array[tonumber(sel)].name + else + return + end modpol.interactions.org_dashboard(user, sel_org) end @@ -53,6 +55,7 @@ function modpol.interactions.org_dashboard(user, org_name) local org = modpol.orgs.get_org(org_name) if not org then return nil end + -- identify parent local parent = "" if org.id == 1 then parent = "none" @@ -60,66 +63,65 @@ function modpol.interactions.org_dashboard(user, org_name) parent = modpol.orgs.get_org(org.parent).name end + -- identify children 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 - local process_msg = #org.processes .. " total" + -- list available modules + local org_modules = {} + for k,v in ipairs(org.modules) do + table.insert(org_modules, org.modules[k].slug) + end + -- list pending actions + local process_msg = #org.processes .. " total actions" if org.pending[user] then process_msg = process_msg .. " (" .. #org.pending[user] .. " pending)" else process_msg = process_msg .. " (0 pending)" end - -- set up output print("Org: " .. org_name) print("Parent: " .. parent) print("Members: " .. table.concat(org.members, ", ")) print("Children: " .. table.concat(children, ", ")) - print("Processes: " .. process_msg) + print("Modules: " .. table.concat(org_modules, ", ")) + print("Actions: " .. process_msg) print() - print("Commands: (L)eave, (J)oin, (P)rocesses, (A)dd child, (D)elete org") + print("Commands: (M)odules, (A)ctions") local sel = io.read() print() - if sel == 'l' or sel == 'L' then - org:remove_member(user) - - elseif sel == 'j' or sel == 'J' then - org:make_request({user=user, type="add_member", params={user}}) - + if sel == 'm' or sel == 'M' then + print("Type module name: ") + local module_sel = io.read() + print() + local module_result = false + for k,v in ipairs(org_modules) do + if v == module_sel then + module_result = true + end + end + if module_result then + org:call_module(module_sel, user) + else + print("Error: Module not found.") + end + elseif sel == 'a' or sel == 'A' then - print("What should the new org be named?") - local new_org_name = io.read() - org:make_request({user=user, type="add_org", params={new_org_name}}) - - elseif sel == 'd' or sel == 'D' then - org:make_request({user=user, type="delete", params={}}) - - elseif sel == 'p' or sel == 'P' then local processes = {} print("All processes: (* indicates pending action)") for k,v in ipairs(org.processes) do - local this_request = org.requests[v.request_id] - if type(this_request) == "table" then - local active = '' - if org.pending[user] then - if org.pending[user][v.id] then - active = '*' - end + local active = '' + if org.pending[user] then + if org.pending[user][v.id] then + active = '*' end - local req_str = "[" .. v.id .. "] " .. - active .. this_request.type - if this_request.params[1] then - req_str = req_str .. ": " .. - table.concat(this_request.params, ", ") - end - print(req_str) end end print() @@ -129,14 +131,12 @@ function modpol.interactions.org_dashboard(user, org_name) if not process then return end if org:has_pending_actions(user) then if org.pending[user][process.id] then - process:interact(user) + org:interact(process.id, user) end end + else + print("Command not found") end - - - - end -- Function: modpol.interactions.policy_dashboard diff --git a/modpol/orgs/base.lua b/modpol/orgs/base.lua index f26523c..2211e6e 100644 --- a/modpol/orgs/base.lua +++ b/modpol/orgs/base.lua @@ -11,14 +11,8 @@ function temp_org() return { id = nil, name = nil, - policies = { - add_org={process_type='consent', must_be_member=false}, - delete={process_type='consent', must_be_member=false}, - add_member={process_type='consent', must_be_member=false}, - remove_member={process_type='consent', must_be_member=false} - }, + modules = modpol.modules, processes = {}, - -- requests = {}, pending = {}, members = {}, parent = nil, From ccc8c40cd77f597d7c659d37ecc5b57f6c97ee1c Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Thu, 16 Dec 2021 15:18:10 -0700 Subject: [PATCH 20/39] Removed old DOCS folder and moved GOVERNANCE to root; added provision to it on university ownership. --- modpol/DOCS/GOVERNANCE.md | 15 ---- modpol/DOCS/Predefinitions.txt | 35 --------- modpol/DOCS/ledgers | 50 ------------ modpol/DOCS/orgs | 137 --------------------------------- 4 files changed, 237 deletions(-) delete mode 100644 modpol/DOCS/GOVERNANCE.md delete mode 100644 modpol/DOCS/Predefinitions.txt delete mode 100644 modpol/DOCS/ledgers delete mode 100644 modpol/DOCS/orgs diff --git a/modpol/DOCS/GOVERNANCE.md b/modpol/DOCS/GOVERNANCE.md deleted file mode 100644 index 33b26c7..0000000 --- a/modpol/DOCS/GOVERNANCE.md +++ /dev/null @@ -1,15 +0,0 @@ -One administrator, @ntnsndr, holds ultimate decision-making power over the project. This is a temporary arrangement while we build trust as a community and adopt a more inclusive structure. - -* **Autocracy** The administrator holds ultimate decision-making power in the community. - * **Executive** The administrator is responsible for implementing—or delegating implementation of—policies and other decisions. - * **Lobbying** If participants are not happy with the administrator's leadership, they may voice their concerns or leave the community. -* **Do-ocracy** Those who step forward to do a given task can decide how it should be done, in ongoing consultation with each other and the administrator. - * **Membership** Participation is open to anyone who wants to contribute. The administrator can remove misbehaving participants at will for the sake of the common good. -* **Code of Conduct** Participants are expected to abide by the Contributor Covenant (contributor-covenant.org). - ---- - -Created by [Nathan Schneider](https://gitlab.com/medlabboulder/modpol) - -[![CommunityRule derived](https://communityrule.info/assets/CommunityRule-derived-000000.svg)](https://communityrule.info) -[Creative Commons BY-SA](https://creativecommons.org/licenses/by-sa/4.0/) diff --git a/modpol/DOCS/Predefinitions.txt b/modpol/DOCS/Predefinitions.txt deleted file mode 100644 index 8498435..0000000 --- a/modpol/DOCS/Predefinitions.txt +++ /dev/null @@ -1,35 +0,0 @@ -When calling modpol.lua from another file, one may wish to use -different functions than those included by default. - -So far, 2 definitions need to be made BEFORE calling modpol.lua, the rest can be overwritten -after calling modpol.lua . - -setup: ----------------- -Define the global modpol table: -modpol = {} - -the following 2 items *may* be defined, if they are not, then defaults will be used: -======================== --------------------- -modpol.get_script_dir() --------------------- -type: function - -defaults to: a function that can only work in linux - -usage: must return the file path of the modpol directory. If modpol is in a -larger directory, such as a mod filesystem, make sure that the path returned is the "base" -modpol folder, i.e. the folder with all the original modpol files. -======================== --------------------- -modpol.storage_file_path --------------------- -type: string - -defaults to: topdir .. "/storage/storage-local.lua", -where topdir is the directory defined by modpol.storage_file_path - -usage: if you wish to use another method of storage than that provided by default, -then you can save a filepath to the storage script here. - diff --git a/modpol/DOCS/ledgers b/modpol/DOCS/ledgers deleted file mode 100644 index dedf78a..0000000 --- a/modpol/DOCS/ledgers +++ /dev/null @@ -1,50 +0,0 @@ -Ledger -************************** - -a legder is a table of records that holds the history of an org, and of modpol itself. - -its structure is a table of legder entries. - -Every org has one, starting with the entry recording its creation. - -Ledger Entry -************************** -a legder entry is a table with the following properties: - -timestamp = nil, -entrytype = nil, -action_msg = '', - -the timestamp will hold the output of os.time at the moment the entry was created. - -the entrytype is optional; it can hold an identifier of the kind of entry it is (e.g. 'org_init', 'new_user', etc etc.) - -action_msg is the record output itself. - - - -Entrytypes -************************** - -These are the entrytypes that modpol uses. Each additional module should document whatever entrytypes it adds. - -'org_init' -type used when an org is created - -'org_purge' -type used when all orgs are purged. -- will only be found in the main modpol ledger. - -'org_remove' -type used when removing an individual org - -'add_member' -type used when adding a member to an org - -'remove_member' -type used when removing a member from an org - - - -Old orgs -************************** -when an org is removed, its ledger is stored in modpol.old_legders = {} Note that multiple ledgers may exist here with the same org name. \ No newline at end of file diff --git a/modpol/DOCS/orgs b/modpol/DOCS/orgs deleted file mode 100644 index a360bd7..0000000 --- a/modpol/DOCS/orgs +++ /dev/null @@ -1,137 +0,0 @@ -== Dev note === -*note, see doc ledgers for info about org ledgers. -they are *not* just a message in a table anymore... they have a timestamp and more. -=============== - - -Orgs are stored in modpol.orgs by the key 'org_id', a unique integer - -Old ledgers of deleted orgs are stored in modpol.old_ledgers, -if preserve records is enabled - -An org is a table with the following properties: - -{ - name = '', -- a unique name... - this is a reference to the key used to store the org table. Changing this does not change the org name. - policies = {}, -- a table to store the org's policies - members = {}, -- a table that contains the member names - or the org, in no particular order (TKTK: maybe change it so that members have a name:properties table?) - ledger = {}, -- a table of ledger entries. See the ledger docs - parent = nil, -- the name of the org that spawned this org. - removing a parent org removes its children - children = {}, -- a table of strings of org names of child orgs - properties = { -- good for modules to store arbitrary info about the org here. - - }, - -} - - -API functions for orgs -====================== - -a local variable setting 'preserve_records' determines whether -to store old ledgers when an org is deleted. It is set to true. -It would be possible to extend this to a settings file later. - ---==oo888888888888888888888888888888oo==-- -Function: modpol.record(org_id, msg, type) --- Params: strings msg, type, org_id (number) --- Outputs: --- "msg" specifies an event and/or status message. --- "org" specifies an "org" name. --- "type" -- optional type of legder entry. Could be e.g. 'election_result', 'add_member', etc - - --- This function adds the message to a global ledger and, if "org" --- specifies a valid "org", to an "org"-specific ledger. Both the mem- --- ory-resident and on-disk copies of the data structures used are up- --- dated. --- the type input is used to be able to search ledgers for specific events - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.add_org(org_id, members, parent, properties) --- Params: org)id (number), table members, string parent, table properties --- Parent must be an existing org, defaults to 'instance', properties --- are arbitrary properties to initialize the org with - --- Output: --- This function creates an "org". It returns a boolean success indicator and --- either an error message starting with "Error:" or a success message. --- --- --- The string parameter specifies the "org" name. --- --- The members table should be an integer-indexed array of member --- names. - --- parent should be nil or a valid existing org name. If nil, defaults to --- 'instance' - --- properties defaults to an empty table. Use it to initialize the org --- with arbitrary properties - ---==oo888888888888888888888888888888oo==-- - --- Function: modpol.remove_org(org_id, reason) --- Params: org_id (number), opt string reason --- Output: --- --- This function removes an "org". It returns a boolean --- success indicator and either an error message --- starting with "Error:" or a success message. It also recursively --- removes all child orgs, with 'remove parent org' as reason. If --- preserve_records is enabled, it logs the removal in the org ledger, --- and stores the ledger in modpol.old_legders --- --- The string parameter specifies the "org" name. --- --- The reason is an optional string to additionally include in the --- log as reason for removal - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.list_orgs() --- Params: None --- Output: --- This function returns a text-format list of "orgs". The list shows --- one "org" per line in the following format: --- org_name (member, member, ...) --- If there are no "orgs", the output is an empty string. - ---==oo888888888888888888888888888888oo==-- - - -Function: modpol.reset_orgs() --- Params: None --- Removes all orgs and recreates blank org "instance" with all --- current users --- returns confirmation message --- if preserve_records is enabled, stores all org ledgers --- in modpol.old_ledgers - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.add_member(org_id, member) --- Params: org_id (number), member (string) --- Output: --- Adds the specified member to the specified org --- Returns a boolean success indicator and --- either a confirmation or error message - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.is_member --- Params: org (number), member (string) --- Output: boolean, or nil and error message if not applicable. (org nonexistent) - - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.remove_member --- Params: org (number), member (string) --- Output: --- Removes the specified member from the specified org --- Returns confirmation or error message \ No newline at end of file From 73515beaea7b21809ae8680d8da5cfce0423de58 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Thu, 16 Dec 2021 15:23:35 -0700 Subject: [PATCH 21/39] Cleaned up modpol_minetest directory structure --- modpol_minetest/api.lua | 22 +- modpol_minetest/chatcommands/chatcommands.lua | 108 ---- modpol_minetest/orgs/instance.lua | 6 - .../overrides/interactions/interactions.lua | 487 ------------------ modpol_minetest/overrides/users/users.lua | 21 - 5 files changed, 9 insertions(+), 635 deletions(-) delete mode 100644 modpol_minetest/chatcommands/chatcommands.lua delete mode 100644 modpol_minetest/orgs/instance.lua delete mode 100644 modpol_minetest/overrides/interactions/interactions.lua delete mode 100644 modpol_minetest/overrides/users/users.lua diff --git a/modpol_minetest/api.lua b/modpol_minetest/api.lua index 800b8c5..b469924 100644 --- a/modpol_minetest/api.lua +++ b/modpol_minetest/api.lua @@ -10,31 +10,27 @@ local localdir = minetest.get_modpath("modpol") .. "/modpol_minetest" --Users -dofile (localdir .. "/overrides/users/users.lua") +dofile (localdir .. "/overrides/users.lua") --orgs ---dofile (localdir .. "/overrides/orgs/orgs.lua") +--dofile (localdir .. "/overrides/orgs.lua") --interactions -dofile (localdir .. "/overrides/interactions/interactions.lua") - --- messaging functions --- dofile (localdir .. "/overrides/processes/processes.lua") - +dofile (localdir .. "/overrides/interactions.lua") -- =================================================================== -- Minetest Chatcommands -- =================================================================== -dofile (localdir .. "/chatcommands/chatcommands.lua") +dofile (localdir .. "/chatcommands.lua") -- =================================================================== -- Minetest Specific code -- =================================================================== - --- orgs --- =================== - -dofile (localdir .. "/orgs/instance.lua") --add players to the instance when they join. \ No newline at end of file +--add members to the instance, if they are not already there. +minetest.register_on_joinplayer(function(player) + local p_name = player:get_player_name() + modpol.instance:add_member(p_name) +end) diff --git a/modpol_minetest/chatcommands/chatcommands.lua b/modpol_minetest/chatcommands/chatcommands.lua deleted file mode 100644 index 33c0ca6..0000000 --- a/modpol_minetest/chatcommands/chatcommands.lua +++ /dev/null @@ -1,108 +0,0 @@ --- =================================================================== --- Minetest commands --- =================================================================== - -command_list = {} -- user-facing table of commands - -local chat_table -- MT chat command definitions table -local regchat -- Chat-command registration function - -regchat = minetest.register_chatcommand - -regchat = function(name, command_table) - minetest.register_chatcommand(name, command_table) - table.insert(command_list, name) -end - --- =================================================================== --- /modpol --- Presents a menu of options to users -regchat( - "modpol", { - privs = {}, - func = function(user) - modpol.interactions.dashboard(user) - end, -}) - --- =================================================================== --- /reset --- For testing only --- Clears the system and recreates instance with all players -regchat( - "reset", { - privs = {}, - func = function(user) - modpol.orgs.reset(); - return true, "Reset orgs" - end, -}) - - - --- =================================================================== --- /addorg --- This code defines a chat command which creates a new --- "org". Presently, the command makes the user the sole member of the --- "org". - -regchat( - "addorg", { - privs = {} , - func = function (user, param) - local success, message = modpol.instance:add_org (param) - return true, message - end -}) - --- =================================================================== --- /listorgs --- In Minetest mode, this code defines a chat command which lists --- existing "orgs". --- The list shows one "org" per line in the following format: --- org_name (member, member, ...) - -regchat( - "listorgs", { - privs = {} , - func = function (user, param) - return true, "Orgs: " .. - table.concat(modpol.orgs.list_all(), ", ") - end -}) - --- =================================================================== --- /listplayers -regchat( - "listplayers", { - privs = {}, - func = function(user) - local result = table.concat(modpol.list_users(),", ") - return true, "All players: " .. result - end, -}) - --- =================================================================== --- /joinorg -regchat( - "joinorg", { - privs = {}, - func = function(user, param) - local org = modpol.orgs.get_org(param) - local success, result = org:add_member(user) - return true, result - end, -}) - - --- =================================================================== --- /pollself [question] --- asks the user a question specified in param -regchat( - "pollself", { - privs = {}, - func = function(user, param) - modpol.interactions.binary_poll_user(user, param) - return true, result - end, -}) diff --git a/modpol_minetest/orgs/instance.lua b/modpol_minetest/orgs/instance.lua deleted file mode 100644 index 667ad64..0000000 --- a/modpol_minetest/orgs/instance.lua +++ /dev/null @@ -1,6 +0,0 @@ ---add members to the instance, if they are not already there. - -minetest.register_on_joinplayer(function(player) - local p_name = player:get_player_name() - modpol.instance:add_member(p_name) -end) diff --git a/modpol_minetest/overrides/interactions/interactions.lua b/modpol_minetest/overrides/interactions/interactions.lua deleted file mode 100644 index 5057730..0000000 --- a/modpol_minetest/overrides/interactions/interactions.lua +++ /dev/null @@ -1,487 +0,0 @@ --- INTERACTIONS.LUA (for Minetest) - --- CONTEXTUAL STUFF --- ================ - --- _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) - --- UTILITIES --- ========= - --- Function: formspec_list --- for use generating option lists in formspecs from tables --- input: table of strings --- output: a formspec-ready list of the strings -local function formspec_list(array) - local escaped = {} - if not array then - return "" - end - for i = 1, #array do - escaped[i] = minetest.formspec_escape(array[i]) - end - return table.concat(escaped,",") -end - --- DASHBOARDS --- ========== - --- Function: modpol.interactions.dashboard(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 --- TODO currently a manually curated list---needs major improvement -function modpol.interactions.dashboard(user) - -- prepare data - -- to add: nested orgs map - local all_orgs = modpol.orgs.list_all() - local user_orgs = modpol.orgs.user_orgs(user) - local all_users = modpol.list_users() - -- set up formspec - local formspec = { - "formspec_version[4]", - "size[10,8]", - "label[0.5,0.5;MODPOL DASHBOARD]", - "label[0.5,2;All orgs:]", - "dropdown[2,1.5;5,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)..";;]", - "label[0.5,4;All users:]", - "dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]", - "button[0.5,7;1,0.8;test_poll;Test poll]", - "button[2,7;1,0.8;add_org;Add org]", - "button[3.5,7;1.5,0.8;remove_org;Remove org]", - "button[5.5,7;1.5,0.8;reset_orgs;Reset orgs]", - "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) -end --- receive input -minetest.register_on_player_receive_fields(function (player, formname, fields) - if formname == "modpol:dashboard" then - local pname = player:get_player_name() - if nil then - -- buttons first - elseif fields.test_poll then - -- FOR TESTING PURPOSES ONLY - modpol.interactions.text_query( - pname,"Poll question:", - function(input) - modpol.interactions.binary_poll_user( - pname, input, - function(vote) - modpol.interactions.message( - pname, pname .. " voted " .. vote) - end) - end) - elseif fields.add_org then - modpol.interactions.add_org(pname, 1) - elseif fields.remove_org then - modpol.interactions.remove_org(pname) - elseif fields.reset_orgs then - modpol.orgs.reset() - modpol.instance:add_member(pname) - modpol.interactions.dashboard(pname) - - -- Put all dropdowns at the end - elseif fields.close then - minetest.close_formspec(pname, formname) - elseif fields.all_orgs or fields.user_orgs then - local org_name = fields.all_orgs or fields.user_orgs - modpol.interactions.org_dashboard(pname, org_name) - end - end -end) - - --- Function: modpol.interactions.org_dashboard --- Params: user (string), org_name (string) --- Output: Displays a menu of org-specific commands to the user -function modpol.interactions.org_dashboard(user, org_name) - -- prepare data - local org = modpol.orgs.get_org(org_name) - if not org then return nil end - local is_member = org:has_member(user) - local membership_toggle = function() - local toggle_code = "" - if is_member then - toggle_code = toggle_code - ..minetest.formspec_escape("leave")..";" - ..minetest.formspec_escape("Leave").."]" - else - toggle_code = toggle_code - ..minetest.formspec_escape("join")..";" - ..minetest.formspec_escape("Join").."]" - end - return toggle_code - end - -- identify parent - local parent = modpol.orgs.get_org(org.parent) - if parent then parent = parent.name - else parent = "none" end - -- prepare children menu - local children = {"View..."} - for k,v in ipairs(org.children) do - local this_child = modpol.orgs.get_org(v) - table.insert(children, this_child.name) - end - -- prepare policies menu - local policies = {"View..."} - for k,v in pairs(org.policies) do - table.insert(policies, k .. ": " .. - org.policies[k].process_type) - end - table.insert(policies, "Add policy") - -- prepare processes menu - local processes = {"View..."} - for k,v in ipairs(org.processes) do - local this_request = org.requests[v.request_id] - if type(this_request) == "table" then - local active = '' - if org.pending[user] then - if org.pending[user][v.id] then - active = '*' - end - end - local req_str = "[" .. v.id .. "] " .. - active .. this_request.type - if this_request.params[1] then - req_str = req_str .. ": " .. - table.concat(this_request.params, ", ") - end - table.insert(processes, req_str) - end - end - -- set player context - local user_context = {} - user_context["current_org"] = org_name - _contexts[user] = user_context - -- set up formspec - local formspec = { - "formspec_version[4]", - "size[10,8]", - "label[0.5,0.5;Org: ".. - minetest.formspec_escape(org_name).."]", - "label[0.5,1;Parent: "..parent.."]", - "button[8.5,0.5;1,0.8;"..membership_toggle(), - "label[0.5,2;Members:]", - "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]", - "label[0.5,3;Children:]", - "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]", - "label[0.5,4;Policies:]", - "dropdown[2,3.5;5,0.8;policies;"..formspec_list(policies)..";;]", - "label[0.5,5;Processes:]", - "dropdown[2,4.5;5,0.8;processes;"..formspec_list(processes)..";;]", - "button[0.5,7;1,0.8;test_poll;Test poll]", - "button[2,7;1,0.8;add_child;Add child]", - "button[3.5,7;1.5,0.8;remove_org;Remove org]", - "button[8.5,7;1,0.8;back;Back]", - } - local formspec_string = table.concat(formspec, "") - -- present to player - minetest.show_formspec(user, "modpol:org_dashboard", formspec_string) -end --- receive input -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) - if nil then - elseif fields.join then - local new_request = { - user = pname, - type = "add_member", - params = {pname} - } - org:make_request(new_request) - modpol.interactions.org_dashboard(pname,org.name) - elseif fields.leave then - org:remove_member(pname) - modpol.interactions.dashboard(pname) - elseif fields.test_poll then - modpol.interactions.binary_poll_org( - pname, org.id, - function(input) - modpol.interactions.message_org( - pname, - org.id, - "New response: " .. input) - end) - elseif fields.add_child then - modpol.interactions.text_query( - pname, "Child org name:", - function(input) - local new_request = { - user = pname, - type = "add_org", - params = {input} - } - org:make_request(new_request) - modpol.interactions.message(pname,"requested") - modpol.interactions.org_dashboard( - pname,org.name) - end) - elseif fields.remove_org then - local new_request = { - user = pname, - type = "delete", - params = {} - } - org:make_request(new_request) - modpol.interactions.org_dashboard(pname,org.name) - elseif fields.back then - modpol.interactions.dashboard(pname) - - -- Put all dropdowns at the end - elseif fields.policies - and fields.policies ~= "View..." then - local policy - if fields.policies == "Add policy" then - policy = nil - elseif fields.policies == "View..." then - return - else - policy = string.match(fields.policies,"(%w+)%:") - end - modpol.interactions.policy_dashboard( - pname, org.id, policy) - elseif fields.processes - and fields.processes ~= "View..." then - local sel = string.match(fields.processes,"%[(%d)%]") - local process = org.processes[tonumber(sel)] - if process then - process:interact(pname) - end - elseif fields.children - and fields.children ~= "View..." then - local org_name = fields.children - modpol.interactions.org_dashboard(pname, org_name) - end - end -end) - - --- Function: modpol.interactions.policy_dashboard --- input: user (string), org_id (int), policy (string) --- output: opens a dashboard for viewing/editing policy details --- TODO -function modpol.interactions.policy_dashboard( - user, org_id, policy) - modpol.interactions.message( - user, - "Not yet implemented: " .. policy) -end - - --- BASIC INTERACTION FUNCTIONS --- =========================== - --- Function: modpol.interactions.message --- input: message (string) --- output -function modpol.interactions.message(user, message) - minetest.chat_send_player(user, message) -end - --- Function: modpol.interactions.text_query --- Overrides function at modpol/interactions.lua --- input: user (string), query (string), func (function) --- func input: user input (string) --- output: Applies "func" to user input -function modpol.interactions.text_query(user, query, func) - -- 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) - -- put func in _contexts - if _contexts[user] == nil then _contexts[user] = {} end - _contexts[user]["text_query_func"] = func -end --- receive fields -minetest.register_on_player_receive_fields(function (player, formname, fields) - if formname == "modpol:text_query" then - local pname = player:get_player_name() - local input = fields.input - if not input then - -- no input, do nothing - else - local func = _contexts[pname]["text_query_func"] - if func then - func(input) - else - modpol.interactions.message(pname, "text_query: " .. input) - end - end - minetest.close_formspec(pname, formname) - end -end) - --- Function: dropdown_query --- input: user (string), label (string), options (table of strings), func (function) --- func input: choice (string) --- output: calls func on user choice -function modpol.interactions.dropdown_query(user, label, options, func) - -- set up formspec - local formspec = { - "formspec_version[4]", - "size[10,4]", - "label[0.5,1;"..minetest.formspec_escape(label).."]", - "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]", - "button[0.5,2.5;1,0.8;cancel;Cancel]", - } - local formspec_string = table.concat(formspec, "") - -- present to players - minetest.show_formspec(user, "modpol:dropdown_query", formspec_string) - -- put func in _contexts - if _contexts[user] == nil then _contexts[user] = {} end - _contexts[user]["dropdown_query_func"] = func -end --- receive fields -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 - local choice = fields.input - local func = _contexts[pname]["dropdown_query_func"] - if not choice then - -- no choice, do nothing - elseif func then - func(choice) - else - modpol.interactions.message(pname, "dropdown_query: " .. choice) - end - end - minetest.close_formspec(pname, formname) - end -end) - - --- SECONDARY INTERACTIONS --- ====================== - --- Function: modpol.binary_poll_user(user, question, function) --- Overrides function at modpol/interactions.lua --- Params: user (string), question (string), func (function) --- func input: user input (string: y/n) --- Output: Applies "func" to user input -function modpol.interactions.binary_poll_user(user, question, func) - -- set up formspec - local formspec = { - "formspec_version[4]", - "size[5,3]", - "label[0.375,0.5;",minetest.formspec_escape(question), "]", - "button[1,1.5;1,0.8;yes;Yes]", - "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, "") - if _contexts[user] == nil then _contexts[user] = {} end - _contexts[user]["binary_poll_func"] = func - -- present to player - minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string) -end -minetest.register_on_player_receive_fields(function (player, formname, fields) - 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 - end - if vote then - modpol.interactions.message(pname, "Responded " .. vote) - local func = _contexts[pname]["binary_poll_func"] - if func then func(vote) end - end - minetest.close_formspec(pname, formname) - end -end) - --- COMPLEX INTERACTIONS --- ==================== - --- Function: modpol.interactions.message_org --- input: initiator (string), org_id (number), message (string) --- output: broadcasts message to all org members -function modpol.interactions.message_org(initiator, org_id, message) - local org = modpol.orgs.get_org(org_id) - local users = org:list_members() - for k,v in ipairs(users) do - modpol.interactions.message(v, message) - end -end - - --- Function: modpol.interactions.binary_poll_org --- input: initator (user string), org_id (number) --- output: gets question from initiator, asks all org members, broadcasts answers --- TODO for testing. This should be implemented as a request. -function modpol.interactions.binary_poll_org(initiator, org_id, func) - local org = modpol.orgs.get_org(org_id) - local users = org:list_members() - modpol.interactions.text_query( - initiator, "Yes/no poll question:", - function(input) - for k,v in ipairs(users) do - modpol.interactions.binary_poll_user(v, input, func) - end - end) -end - --- Function: modpol.interactions.add_org --- input: initator (user string), base_org_id (ID) --- output: interaction begins --- GODMODE -function modpol.interactions.add_org(user, base_org_id) - modpol.interactions.text_query( - user,"Org name:", - function(input) - local base_org = modpol.orgs.get_org(1) - local result = base_org:add_org(input, user) - local message = input .. " created" - modpol.interactions.message(user, message) - modpol.interactions.dashboard(user) - end) -end - --- Function: modpol.interactions.remove_org --- input: initator (user string) --- output: interaction begins --- GODMODE -function modpol.interactions.remove_org(user) - -- start formspec - local orgs_list = modpol.orgs.list_all() - local label = "Choose an org to remove:" - modpol.interactions.dropdown_query( - user, label, orgs_list, - function(input) - if input then - local target_org = modpol.orgs.get_org(input) - local result = target_org:delete() - local message = input .. " deleted" - modpol.interactions.message(user, message) - end - modpol.interactions.dashboard(user) - end) -end diff --git a/modpol_minetest/overrides/users/users.lua b/modpol_minetest/overrides/users/users.lua deleted file mode 100644 index 229ebfb..0000000 --- a/modpol_minetest/overrides/users/users.lua +++ /dev/null @@ -1,21 +0,0 @@ - --- =================================================================== --- Function: modpol.list_users(org) --- Overwrites function at /users.lua --- Params: --- if nil, lists instance members; if an org name, lists its members --- Output: a table with names of players currently in the game -modpol.list_users = function(org) - local users = {} - if (org == nil) then -- no specified org; all players - for _,player in ipairs(minetest.get_connected_players()) do - local name = player:get_player_name() - table.insert(users,name) - end - else -- if an org is specified - if (modpol.orgs[org] ~= nil) then -- org exists - users = modpol.orgs[org]["members"] - end - end - return users - end \ No newline at end of file From 9be019a839e8b7563e341acf8073862cf2465e38 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Thu, 16 Dec 2021 16:55:08 -0700 Subject: [PATCH 22/39] First draft of modules-as-actions Minetest interactions fix. Still not listing modules; may not be loading correctly. --- modpol/api.lua | 6 ++---- modpol/orgs/process.lua | 7 ++++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/modpol/api.lua b/modpol/api.lua index 9fa3c03..70e4eb2 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -14,8 +14,6 @@ dofile (localdir .. "/interactions/interactions.lua") --modules dofile (localdir .. "/modules/join_org.lua") -dofile (localdir .. "/modules/join_org_class.lua") -dofile (localdir .. "/modules/join_org_consent.lua") dofile (localdir .. "/modules/consent.lua") -dofile (localdir .. "/modules/remove_org.lua") -dofile (localdir .. "/modules/child_org.lua") +--dofile (localdir .. "/modules/remove_org.lua") +--dofile (localdir .. "/modules/child_org.lua") diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 1b963ed..38c10b6 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -38,9 +38,10 @@ function modpol.orgs:call_module(module_name, initiator, config, result) initiator = initiator, org = self, id = index, - config = config + config = config, + name = module_name } - + -- copying default fields from setup for k, v in pairs(module.setup) do new_process[k] = v @@ -97,4 +98,4 @@ function modpol.orgs:interact(process_id, user) process[callback](process, user) end end -end \ No newline at end of file +end From b9d2b73611ba0cf0d174bb464bb093524e935af8 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Thu, 16 Dec 2021 16:55:49 -0700 Subject: [PATCH 23/39] Correction: files added --- GOVERNANCE.md | 16 + modpol_minetest/chatcommands.lua | 108 +++++ modpol_minetest/overrides/interactions.lua | 479 +++++++++++++++++++++ modpol_minetest/overrides/users.lua | 21 + 4 files changed, 624 insertions(+) create mode 100644 GOVERNANCE.md create mode 100644 modpol_minetest/chatcommands.lua create mode 100644 modpol_minetest/overrides/interactions.lua create mode 100644 modpol_minetest/overrides/users.lua diff --git a/GOVERNANCE.md b/GOVERNANCE.md new file mode 100644 index 0000000..5283261 --- /dev/null +++ b/GOVERNANCE.md @@ -0,0 +1,16 @@ +One administrator, @ntnsndr, holds ultimate decision-making power over the project. This is a temporary arrangement while we build trust as a community and adopt a more inclusive structure. + +* **Autocracy** The administrator holds ultimate decision-making power in the community. + * **Executive** The administrator is responsible for implementing—or delegating implementation of—policies and other decisions. + * **Lobbying** If participants are not happy with the administrator's leadership, they may voice their concerns or leave the community. +* **Do-ocracy** Those who step forward to do a given task can decide how it should be done, in ongoing consultation with each other and the administrator. + * **Membership** Participation is open to anyone who wants to contribute. The administrator can remove misbehaving participants at will for the sake of the common good. +* **Code of Conduct** Participants are expected to abide by the Contributor Covenant (contributor-covenant.org). +* **Ownership** This is a project of the Media Enterprise Design Lab at the University of Colorado Boulder and is owned by the university. + +--- + +Created by [Nathan Schneider](https://gitlab.com/medlabboulder/modpol) + +[![CommunityRule derived](https://communityrule.info/assets/CommunityRule-derived-000000.svg)](https://communityrule.info) +[Creative Commons BY-SA](https://creativecommons.org/licenses/by-sa/4.0/) diff --git a/modpol_minetest/chatcommands.lua b/modpol_minetest/chatcommands.lua new file mode 100644 index 0000000..33c0ca6 --- /dev/null +++ b/modpol_minetest/chatcommands.lua @@ -0,0 +1,108 @@ +-- =================================================================== +-- Minetest commands +-- =================================================================== + +command_list = {} -- user-facing table of commands + +local chat_table -- MT chat command definitions table +local regchat -- Chat-command registration function + +regchat = minetest.register_chatcommand + +regchat = function(name, command_table) + minetest.register_chatcommand(name, command_table) + table.insert(command_list, name) +end + +-- =================================================================== +-- /modpol +-- Presents a menu of options to users +regchat( + "modpol", { + privs = {}, + func = function(user) + modpol.interactions.dashboard(user) + end, +}) + +-- =================================================================== +-- /reset +-- For testing only +-- Clears the system and recreates instance with all players +regchat( + "reset", { + privs = {}, + func = function(user) + modpol.orgs.reset(); + return true, "Reset orgs" + end, +}) + + + +-- =================================================================== +-- /addorg +-- This code defines a chat command which creates a new +-- "org". Presently, the command makes the user the sole member of the +-- "org". + +regchat( + "addorg", { + privs = {} , + func = function (user, param) + local success, message = modpol.instance:add_org (param) + return true, message + end +}) + +-- =================================================================== +-- /listorgs +-- In Minetest mode, this code defines a chat command which lists +-- existing "orgs". +-- The list shows one "org" per line in the following format: +-- org_name (member, member, ...) + +regchat( + "listorgs", { + privs = {} , + func = function (user, param) + return true, "Orgs: " .. + table.concat(modpol.orgs.list_all(), ", ") + end +}) + +-- =================================================================== +-- /listplayers +regchat( + "listplayers", { + privs = {}, + func = function(user) + local result = table.concat(modpol.list_users(),", ") + return true, "All players: " .. result + end, +}) + +-- =================================================================== +-- /joinorg +regchat( + "joinorg", { + privs = {}, + func = function(user, param) + local org = modpol.orgs.get_org(param) + local success, result = org:add_member(user) + return true, result + end, +}) + + +-- =================================================================== +-- /pollself [question] +-- asks the user a question specified in param +regchat( + "pollself", { + privs = {}, + func = function(user, param) + modpol.interactions.binary_poll_user(user, param) + return true, result + end, +}) diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua new file mode 100644 index 0000000..b109a36 --- /dev/null +++ b/modpol_minetest/overrides/interactions.lua @@ -0,0 +1,479 @@ +-- INTERACTIONS.LUA (for Minetest) + +-- CONTEXTUAL STUFF +-- ================ + +-- _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) + +-- UTILITIES +-- ========= + +-- Function: formspec_list +-- for use generating option lists in formspecs from tables +-- input: table of strings +-- output: a formspec-ready list of the strings +local function formspec_list(array) + local escaped = {} + if not array then + return "" + end + for i = 1, #array do + escaped[i] = minetest.formspec_escape(array[i]) + end + return table.concat(escaped,",") +end + +-- DASHBOARDS +-- ========== + +-- Function: modpol.interactions.dashboard(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 +-- TODO currently a manually curated list---needs major improvement +function modpol.interactions.dashboard(user) + -- prepare data + -- to add: nested orgs map + local all_orgs = modpol.orgs.list_all() + local user_orgs = modpol.orgs.user_orgs(user) + local all_users = modpol.list_users() + -- set up formspec + local formspec = { + "formspec_version[4]", + "size[10,8]", + "label[0.5,0.5;MODPOL DASHBOARD]", + "label[0.5,2;All orgs:]", + "dropdown[2,1.5;5,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)..";;]", + "label[0.5,4;All users:]", + "dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]", + "button[0.5,7;1,0.8;test_poll;Test poll]", + "button[2,7;1,0.8;add_org;Add org]", + "button[3.5,7;1.5,0.8;remove_org;Remove org]", + "button[5.5,7;1.5,0.8;reset_orgs;Reset orgs]", + "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) +end +-- receive input +minetest.register_on_player_receive_fields(function (player, formname, fields) + if formname == "modpol:dashboard" then + local pname = player:get_player_name() + if nil then + -- buttons first + elseif fields.test_poll then + -- FOR TESTING PURPOSES ONLY + modpol.interactions.text_query( + pname,"Poll question:", + function(input) + modpol.interactions.binary_poll_user( + pname, input, + function(vote) + modpol.interactions.message( + pname, pname .. " voted " .. vote) + end) + end) + elseif fields.add_org then + modpol.interactions.add_org(pname, 1) + elseif fields.remove_org then + modpol.interactions.remove_org(pname) + elseif fields.reset_orgs then + modpol.orgs.reset() + modpol.instance:add_member(pname) + modpol.interactions.dashboard(pname) + + -- Put all dropdowns at the end + elseif fields.close then + minetest.close_formspec(pname, formname) + elseif fields.all_orgs or fields.user_orgs then + local org_name = fields.all_orgs or fields.user_orgs + modpol.interactions.org_dashboard(pname, org_name) + end + end +end) + + +-- Function: modpol.interactions.org_dashboard +-- Params: user (string), org_name (string) +-- Output: Displays a menu of org-specific commands to the user +function modpol.interactions.org_dashboard(user, org_name) + -- prepare data + local org = modpol.orgs.get_org(org_name) + if not org then return nil end + local is_member = org:has_member(user) + local membership_toggle = function() + local toggle_code = "" + if is_member then + toggle_code = toggle_code + ..minetest.formspec_escape("leave")..";" + ..minetest.formspec_escape("Leave").."]" + else + toggle_code = toggle_code + ..minetest.formspec_escape("join")..";" + ..minetest.formspec_escape("Join").."]" + end + return toggle_code + end + + -- identify parent + local parent = modpol.orgs.get_org(org.parent) + if parent then parent = parent.name + else parent = "none" end + + -- prepare children menu + local children = {"View..."} + for k,v in ipairs(org.children) do + local this_child = modpol.orgs.get_org(v) + table.insert(children, this_child.name) + end + + -- prepare modules menu + local modules = {"View..."} + if org.modules then + for k,v in ipairs(org.modules) do + table.insert(modules, org.modules[k].slug) + end + end + + -- prepare actions menu + local actions = {"View..."} + if org.pending[user] then + for k,v in pairs(org.pending[user]) do + local action_string = "[" .. k .. "] " .. + org.processes[k].name + table.insert(actions, action_string) + end + end + + -- set player context + local user_context = {} + user_context["current_org"] = org_name + _contexts[user] = user_context + -- set up formspec + local formspec = { + "formspec_version[4]", + "size[10,8]", + "label[0.5,0.5;Org: ".. + minetest.formspec_escape(org_name).."]", + "label[0.5,1;Parent: "..parent.."]", + "button[8.5,0.5;1,0.8;"..membership_toggle(), + "label[0.5,2;Members:]", + "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]", + "label[0.5,3;Children:]", + "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]", + "label[0.5,4;Modules:]", + "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]", + "label[0.5,5;Actions:]", + "dropdown[2,4.5;5,0.8;actions;"..formspec_list(actions)..";;]", + "button[0.5,7;1,0.8;test_poll;Test poll]", + "button[2,7;1,0.8;add_child;Add child]", + "button[3.5,7;1.5,0.8;remove_org;Remove org]", + "button[8.5,7;1,0.8;back;Back]", + } + local formspec_string = table.concat(formspec, "") + -- present to player + minetest.show_formspec(user, "modpol:org_dashboard", formspec_string) +end +-- receive input +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) + if nil then + elseif fields.join then + local new_request = { + user = pname, + type = "add_member", + params = {pname} + } + org:make_request(new_request) + modpol.interactions.org_dashboard(pname,org.name) + elseif fields.leave then + org:remove_member(pname) + modpol.interactions.dashboard(pname) + elseif fields.test_poll then + modpol.interactions.binary_poll_org( + pname, org.id, + function(input) + modpol.interactions.message_org( + pname, + org.id, + "New response: " .. input) + end) + elseif fields.add_child then + modpol.interactions.text_query( + pname, "Child org name:", + function(input) + local new_request = { + user = pname, + type = "add_org", + params = {input} + } + org:make_request(new_request) + modpol.interactions.message(pname,"requested") + modpol.interactions.org_dashboard( + pname,org.name) + end) + elseif fields.remove_org then + local new_request = { + user = pname, + type = "delete", + params = {} + } + org:make_request(new_request) + modpol.interactions.org_dashboard(pname,org.name) + elseif fields.back then + modpol.interactions.dashboard(pname) + + -- Put all dropdowns at the end + -- Receiving modules + elseif fields.modules + and fields.modules ~= "View..." then + local module = fields.modules + org:call_module(module, user) + + -- Receiving actions + elseif fields.actions + and fields.actions ~= "View..." then + local action = string.match( + fields.actions,"%[(%d)%]") + local process = org.processes[tonumber(action)] + if process then + org:interact(process, user) + end + + -- Children + elseif fields.children + and fields.children ~= "View..." then + local org_name = fields.children + modpol.interactions.org_dashboard(pname, org_name) + end + end +end) + + +-- Function: modpol.interactions.policy_dashboard +-- input: user (string), org_id (int), policy (string) +-- output: opens a dashboard for viewing/editing policy details +-- TODO +function modpol.interactions.policy_dashboard( + user, org_id, policy) + modpol.interactions.message( + user, + "Not yet implemented: " .. policy) +end + + +-- BASIC INTERACTION FUNCTIONS +-- =========================== + +-- Function: modpol.interactions.message +-- input: message (string) +-- output +function modpol.interactions.message(user, message) + minetest.chat_send_player(user, message) +end + +-- Function: modpol.interactions.text_query +-- Overrides function at modpol/interactions.lua +-- input: user (string), query (string), func (function) +-- func input: user input (string) +-- output: Applies "func" to user input +function modpol.interactions.text_query(user, query, func) + -- 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) + -- put func in _contexts + if _contexts[user] == nil then _contexts[user] = {} end + _contexts[user]["text_query_func"] = func +end +-- receive fields +minetest.register_on_player_receive_fields(function (player, formname, fields) + if formname == "modpol:text_query" then + local pname = player:get_player_name() + local input = fields.input + if not input then + -- no input, do nothing + else + local func = _contexts[pname]["text_query_func"] + if func then + func(input) + else + modpol.interactions.message(pname, "text_query: " .. input) + end + end + minetest.close_formspec(pname, formname) + end +end) + +-- Function: dropdown_query +-- input: user (string), label (string), options (table of strings), func (function) +-- func input: choice (string) +-- output: calls func on user choice +function modpol.interactions.dropdown_query(user, label, options, func) + -- set up formspec + local formspec = { + "formspec_version[4]", + "size[10,4]", + "label[0.5,1;"..minetest.formspec_escape(label).."]", + "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]", + "button[0.5,2.5;1,0.8;cancel;Cancel]", + } + local formspec_string = table.concat(formspec, "") + -- present to players + minetest.show_formspec(user, "modpol:dropdown_query", formspec_string) + -- put func in _contexts + if _contexts[user] == nil then _contexts[user] = {} end + _contexts[user]["dropdown_query_func"] = func +end +-- receive fields +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 + local choice = fields.input + local func = _contexts[pname]["dropdown_query_func"] + if not choice then + -- no choice, do nothing + elseif func then + func(choice) + else + modpol.interactions.message(pname, "dropdown_query: " .. choice) + end + end + minetest.close_formspec(pname, formname) + end +end) + + +-- SECONDARY INTERACTIONS +-- ====================== + +-- Function: modpol.binary_poll_user(user, question, function) +-- Overrides function at modpol/interactions.lua +-- Params: user (string), question (string), func (function) +-- func input: user input (string: y/n) +-- Output: Applies "func" to user input +function modpol.interactions.binary_poll_user(user, question, func) + -- set up formspec + local formspec = { + "formspec_version[4]", + "size[5,3]", + "label[0.375,0.5;",minetest.formspec_escape(question), "]", + "button[1,1.5;1,0.8;yes;Yes]", + "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, "") + if _contexts[user] == nil then _contexts[user] = {} end + _contexts[user]["binary_poll_func"] = func + -- present to player + minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string) +end +minetest.register_on_player_receive_fields(function (player, formname, fields) + 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 + end + if vote then + modpol.interactions.message(pname, "Responded " .. vote) + local func = _contexts[pname]["binary_poll_func"] + if func then func(vote) end + end + minetest.close_formspec(pname, formname) + end +end) + +-- COMPLEX INTERACTIONS +-- ==================== + +-- Function: modpol.interactions.message_org +-- input: initiator (string), org_id (number), message (string) +-- output: broadcasts message to all org members +function modpol.interactions.message_org(initiator, org_id, message) + local org = modpol.orgs.get_org(org_id) + local users = org:list_members() + for k,v in ipairs(users) do + modpol.interactions.message(v, message) + end +end + + +-- Function: modpol.interactions.binary_poll_org +-- input: initator (user string), org_id (number) +-- output: gets question from initiator, asks all org members, broadcasts answers +-- TODO for testing. This should be implemented as a request. +function modpol.interactions.binary_poll_org(initiator, org_id, func) + local org = modpol.orgs.get_org(org_id) + local users = org:list_members() + modpol.interactions.text_query( + initiator, "Yes/no poll question:", + function(input) + for k,v in ipairs(users) do + modpol.interactions.binary_poll_user(v, input, func) + end + end) +end + +-- Function: modpol.interactions.add_org +-- input: initator (user string), base_org_id (ID) +-- output: interaction begins +-- GODMODE +function modpol.interactions.add_org(user, base_org_id) + modpol.interactions.text_query( + user,"Org name:", + function(input) + local base_org = modpol.orgs.get_org(1) + local result = base_org:add_org(input, user) + local message = input .. " created" + modpol.interactions.message(user, message) + modpol.interactions.dashboard(user) + end) +end + +-- Function: modpol.interactions.remove_org +-- input: initator (user string) +-- output: interaction begins +-- GODMODE +function modpol.interactions.remove_org(user) + -- start formspec + local orgs_list = modpol.orgs.list_all() + local label = "Choose an org to remove:" + modpol.interactions.dropdown_query( + user, label, orgs_list, + function(input) + if input then + local target_org = modpol.orgs.get_org(input) + local result = target_org:delete() + local message = input .. " deleted" + modpol.interactions.message(user, message) + end + modpol.interactions.dashboard(user) + end) +end diff --git a/modpol_minetest/overrides/users.lua b/modpol_minetest/overrides/users.lua new file mode 100644 index 0000000..229ebfb --- /dev/null +++ b/modpol_minetest/overrides/users.lua @@ -0,0 +1,21 @@ + +-- =================================================================== +-- Function: modpol.list_users(org) +-- Overwrites function at /users.lua +-- Params: +-- if nil, lists instance members; if an org name, lists its members +-- Output: a table with names of players currently in the game +modpol.list_users = function(org) + local users = {} + if (org == nil) then -- no specified org; all players + for _,player in ipairs(minetest.get_connected_players()) do + local name = player:get_player_name() + table.insert(users,name) + end + else -- if an org is specified + if (modpol.orgs[org] ~= nil) then -- org exists + users = modpol.orgs[org]["members"] + end + end + return users + end \ No newline at end of file From 9e492bb1f8347ca1a1e7243678c7b9a54fef92b9 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Thu, 16 Dec 2021 23:33:02 -0700 Subject: [PATCH 24/39] Added remove_org_consent module, a few other tweaks to modules --- README.md | 2 +- modpol/api.lua | 4 +- modpol/modpol.lua | 1 - modpol/modules/consent.lua | 2 +- modpol/modules/join_org.lua | 6 +++ modpol/modules/join_org_class.lua | 58 --------------------------- modpol/modules/join_org_consent.lua | 22 +++++----- modpol/modules/remove_org.lua | 22 ++++++++++ modpol/modules/remove_org_consent.lua | 31 ++++++++++++++ modpol/modules/template.lua | 2 + 10 files changed, 77 insertions(+), 73 deletions(-) delete mode 100644 modpol/modules/join_org_class.lua create mode 100644 modpol/modules/remove_org_consent.lua diff --git a/README.md b/README.md index ca858a4..353cfb4 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Modular Politics can also be used independently of Minetest as a command-line to The command-line version is in the `modpol` subdirectory. To interact with the interpreter on Unix systems in CLI mode, install lua or luajit and execute the following in this directory: ``` -$ cd modpol/interacctions/ +$ cd modpol/interactions/ $ lua [or luajit] > dofile("login.lua") ``` diff --git a/modpol/api.lua b/modpol/api.lua index 70e4eb2..8c2bc48 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -13,7 +13,7 @@ dofile (localdir .. "/orgs/process.lua") dofile (localdir .. "/interactions/interactions.lua") --modules -dofile (localdir .. "/modules/join_org.lua") dofile (localdir .. "/modules/consent.lua") ---dofile (localdir .. "/modules/remove_org.lua") +dofile (localdir .. "/modules/join_org_consent.lua") +dofile (localdir .. "/modules/remove_org_consent.lua") --dofile (localdir .. "/modules/child_org.lua") diff --git a/modpol/modpol.lua b/modpol/modpol.lua index e1f62cb..9e779d3 100644 --- a/modpol/modpol.lua +++ b/modpol/modpol.lua @@ -97,7 +97,6 @@ end -- create instance if not present modpol.instance = modpol.orgs.array[1] or modpol.orgs.init_instance() - modpol.ocutil.log ("modpol loaded") -- =================================================================== diff --git a/modpol/modules/consent.lua b/modpol/modules/consent.lua index 27b1cd8..e7d22b3 100644 --- a/modpol/modules/consent.lua +++ b/modpol/modules/consent.lua @@ -39,4 +39,4 @@ function Consent:callback(member) end -modpol.modules.consent = Consent \ No newline at end of file +modpol.modules.consent = Consent diff --git a/modpol/modules/join_org.lua b/modpol/modules/join_org.lua index 3353dae..3806685 100644 --- a/modpol/modules/join_org.lua +++ b/modpol/modules/join_org.lua @@ -1,6 +1,12 @@ join_org = {} +join_org.setup = { + name = "Join Org", + slug = "join_org", + desc = "If consent process is passed, initiator joins this org." +} + function join_org.initiate(initiator, org, result) modpol.interactions.binary_poll_user( initiator, diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua deleted file mode 100644 index 9160425..0000000 --- a/modpol/modules/join_org_class.lua +++ /dev/null @@ -1,58 +0,0 @@ --- JOIN ORG --- Module that enables a user to join an org - -JoinOrg = {} - -JoinOrg.setup = { - name = "Join an org", - desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.", - votes_yes = 0 -} - -function JoinOrg:initiate(result) - modpol.interactions.binary_poll_user( - self.initiator, - "Would you like to join", - function (resp) - if resp == "Yes" then - for id, member in pairs(self.org.members) do - self.org:add_pending_action(self.id, member, "callback") - - end - end - end - ) - - if result then result() end - -end - -function JoinOrg:callback(member) - modpol.interactions.binary_poll_user( - member, - "Do you want " .. self.initiator .. " to join?", - function (resp) - if resp == "Yes" then - self.votes_yes = self.votes_yes + 1 - end - - self:evaluate_vote() - - end - ) -end - - - -function JoinOrg:evaluate_vote() - if self.votes_yes >= 1 then - print('added user') - self.org:add_member(self.initiator) - self.org:wipe_pending_actions(self.id) - end -end - --- =================================== --- When calling a module internally - -modpol.modules.join_org_class = JoinOrg \ No newline at end of file diff --git a/modpol/modules/join_org_consent.lua b/modpol/modules/join_org_consent.lua index cc02d4e..d126fd5 100644 --- a/modpol/modules/join_org_consent.lua +++ b/modpol/modules/join_org_consent.lua @@ -1,20 +1,22 @@ +--- Join org (consent) +-- A simple module that calls a consent process on an org to add a member. +-- Depends on the Consent module. +join_org_consent = {} -JoinOrg = {} - -JoinOrg.setup = { - name = "Join an org", +join_org_consent.setup = { + name = "Join this org", slug = "join_org_consent", - desc = "Consent based join org module" + desc = "Adds member with consent of all members." } -function JoinOrg:initiate() +function join_org_consent:initiate() self.org:call_module( "consent", self.initiator, { prompt = "Allow " .. self.initiator .. " to join?", - votes_required = 1 + votes_required = #self.org.members }, function () self:complete() @@ -22,9 +24,9 @@ function JoinOrg:initiate() ) end -function JoinOrg:complete() +function join_org_consent:complete() self.org:add_member(self.initiator) - print("Added " .. self.initiator .. " to the org!") + print("Added " .. self.initiator .. " to the org.") end -modpol.modules.join_org_consent = JoinOrg \ No newline at end of file +modpol.modules.join_org_consent = join_org_consent diff --git a/modpol/modules/remove_org.lua b/modpol/modules/remove_org.lua index e69de29..ae9bbdc 100644 --- a/modpol/modules/remove_org.lua +++ b/modpol/modules/remove_org.lua @@ -0,0 +1,22 @@ +--- Remove Org +-- A simple module that calls a consent process on an org to remove it. +-- Depends on the Consent module. +remove_org = {} + +--- (Required): setup table containing name and description of the module +remove_org.setup = { + name = "Remove this org", + slug = "remove_org", + desc = "Removes an org if all members consent." +} + +--- Initiate function +--
  • self.org (the org the module was called in),
  • +--
  • self.initiator (the user that callced the module),
  • +--
  • self.id (the process id of the module instance)
  • +-- @function initiate +function remove_org:initiate(config, result) + + -- call result function + if result then result() end +end diff --git a/modpol/modules/remove_org_consent.lua b/modpol/modules/remove_org_consent.lua new file mode 100644 index 0000000..9574901 --- /dev/null +++ b/modpol/modules/remove_org_consent.lua @@ -0,0 +1,31 @@ +--- Remove org (consent) +-- A simple module that calls a consent process on an org to remove it. +-- Depends on the Consent module. +remove_org_consent = {} + +remove_org_consent.setup = { + name = "Remove this org", + slug = "remove_org", + desc = "Removes an org if all members consent." +} + +function remove_org_consent:initiate() + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Remove org " .. self.org.name .. "?", + votes_required = #self.org.members + }, + function () + self:complete() + end + ) +end + +function join_org_consent:complete() + self.org:delete() + print("Removed org " .. self.org.name .. ".") +end + +modpol.modules.remove_org_consent = remove_org_consent diff --git a/modpol/modules/template.lua b/modpol/modules/template.lua index 7a3fbc6..777f8f6 100644 --- a/modpol/modules/template.lua +++ b/modpol/modules/template.lua @@ -30,6 +30,8 @@ Template.config = { --
  • self.org (the org the module was called in),
  • --
  • self.initiator (the user that callced the module),
  • --
  • self.id (the process id of the module instance)
  • +-- @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 Template:initiate(config, result) -- call interaction functions here! From 4411c01ebbb50ba5c3a3318dc9cc023399539f8d Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 09:13:42 -0700 Subject: [PATCH 25/39] Corrected copyright statement on LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 4cf57e8..4653428 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Media Enterprise Design Lab +Copyright (c) 2021 University of Colorado Boulder. A project of the Media Enterprise Design Lab (colorado.edu/lab/medlab/). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 721eb267779cc35ae4c8f8949c867668adb4314f Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 13:35:18 -0700 Subject: [PATCH 26/39] Got modules basically working in CLI and Minetest, still having issues with Minetest interactions in module:initiate() --- modpol/api.lua | 4 +- modpol/interactions/interactions.lua | 19 ++- modpol/modules/add_child_org.lua | 29 ++++ modpol/modules/child_org.lua | 32 ----- modpol/modules/consent.lua | 50 ++++--- modpol/modules/join_org_consent.lua | 10 +- modpol/modules/remove_org.lua | 23 +-- modpol/modules/remove_org_consent.lua | 28 ++-- modpol/modules/template.lua | 32 +++-- modpol/orgs/base.lua | 6 +- modpol/orgs/process.lua | 39 +++--- modpol_minetest/overrides/interactions.lua | 155 ++++----------------- 12 files changed, 187 insertions(+), 240 deletions(-) create mode 100644 modpol/modules/add_child_org.lua delete mode 100644 modpol/modules/child_org.lua diff --git a/modpol/api.lua b/modpol/api.lua index 8c2bc48..f5e04e6 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -13,7 +13,9 @@ dofile (localdir .. "/orgs/process.lua") dofile (localdir .. "/interactions/interactions.lua") --modules +--TODO make this automatic and directory-based +dofile (localdir .. "/modules/add_child_org.lua") dofile (localdir .. "/modules/consent.lua") dofile (localdir .. "/modules/join_org_consent.lua") dofile (localdir .. "/modules/remove_org_consent.lua") ---dofile (localdir .. "/modules/child_org.lua") +dofile (localdir .. "/modules/remove_org.lua") diff --git a/modpol/interactions/interactions.lua b/modpol/interactions/interactions.lua index 5e2bddf..b198e4f 100644 --- a/modpol/interactions/interactions.lua +++ b/modpol/interactions/interactions.lua @@ -34,17 +34,16 @@ function modpol.interactions.dashboard(user) print('All users: ' .. table.concat(all_users, ', ')) print() - print('Access which org?') - + print('Access which org id?') local sel = io.read() print() if modpol.orgs.array[tonumber(sel)] then local sel_org = modpol.orgs.array[tonumber(sel)].name + modpol.interactions.org_dashboard(user, sel_org) else - return + print("Org id not found.") end - modpol.interactions.org_dashboard(user, sel_org) end @@ -72,8 +71,8 @@ function modpol.interactions.org_dashboard(user, org_name) -- list available modules local org_modules = {} - for k,v in ipairs(org.modules) do - table.insert(org_modules, org.modules[k].slug) + for k,v in pairs(org.modules) do + table.insert(org_modules, v.slug) end -- list pending actions @@ -136,6 +135,7 @@ function modpol.interactions.org_dashboard(user, org_name) end else print("Command not found") + modpol.interactions.org_dashboard(user, org_name) end end @@ -230,6 +230,13 @@ end -- Function: modpol.interactions.message_org -- input: initiator (string), org_id (number), message (string) -- output: broadcasts message to all org members +function modpol.interactions.message_org(initiator, org_id, message) + local org = modpol.orgs.get_org(org_id) + local users = org:list_members() + for k,v in ipairs(users) do + modpol.interactions.message(v, message) + end +end -- Function: modpol.interactions.binary_poll_org -- input: initator (user string), org_id (number) diff --git a/modpol/modules/add_child_org.lua b/modpol/modules/add_child_org.lua new file mode 100644 index 0000000..bad73b3 --- /dev/null +++ b/modpol/modules/add_child_org.lua @@ -0,0 +1,29 @@ +--- @module add_child_org +-- Adds a child org + +local add_child_org = { + name = "Add child org", + slug = "add_child_org", + desc = "Create a child org within the current one" +} +add_child_org.data = { +} +add_child_org.config = { +} + +-- @function initiate +function add_child_org:initiate(result) + 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) +end + +--- (Required) Add to module table +modpol.modules.add_child_org = add_child_org diff --git a/modpol/modules/child_org.lua b/modpol/modules/child_org.lua deleted file mode 100644 index 8a1631a..0000000 --- a/modpol/modules/child_org.lua +++ /dev/null @@ -1,32 +0,0 @@ --- CHILD_ORG --- Module that enables user to create child in current org - --- Initial configuration -modpol.modules.child_org = {} -modpol.modules.child_org.name = "Create child org" - - --- == REQUEST == - --- Initialization function --- function: modpol.modules.child_org:initialize - - --- gather data from initiator: child org name, comment - --- function: modpol.modules.child_org:request - - - --- == CONSENT == - --- function: modpol.orgs:consent(process_id, result_function) --- the result function should begin the completion process below - - - --- == COMPLETION == - --- if approved/if failed functions? - --- function: modpol.modules.child_org.create(initiator, parent_org, child_org) diff --git a/modpol/modules/consent.lua b/modpol/modules/consent.lua index e7d22b3..03983a6 100644 --- a/modpol/modules/consent.lua +++ b/modpol/modules/consent.lua @@ -1,42 +1,52 @@ +--- @module consent +-- A utility module for checking consent - -Consent = {} - -Consent.setup = { +local consent = { name = "Consent", slug = "consent", desc = "Other modules can use to implement consent based decision making", +} + +consent.data = { votes = 0 } -Consent.config = { - prompt = "Would you like to approve this action?", +consent.config = { + prompt = "Do you consent?", votes_required = 1 } -function Consent:initiate(result) - self.result = result - for id, member in pairs(self.org.members) do - self.org:add_pending_action(self.id, member, "callback") - end +function consent:initiate(result) + self.result = result + -- if org is empty, consent is given automatically + if self.org:get_member_count() == 0 then + self.result() + self.org:wipe_pending_actions(self.id) + else + -- otherwise, create poll + for id, member in pairs(self.org.members) do + self.org:add_pending_action(self.id, member, "callback") + end + end end -function Consent:callback(member) +function consent:callback(member) modpol.interactions.binary_poll_user( member, self.config.prompt, function (resp) - if resp == "Yes" then - self.votes = self.votes + 1 - end + self.org:remove_pending_action(self.id,member) + if resp == "Yes" then + self.data.votes = self.data.votes + 1 + end - if self.votes >= self.config.votes_required then - self.result() - self.org:wipe_pending_actions(self.id) - end + if self.data.votes >= self.config.votes_required then + if self.result then self.result() end + self.org:wipe_pending_actions(self.id) + end end ) end -modpol.modules.consent = Consent +modpol.modules.consent = consent diff --git a/modpol/modules/join_org_consent.lua b/modpol/modules/join_org_consent.lua index d126fd5..9228dc7 100644 --- a/modpol/modules/join_org_consent.lua +++ b/modpol/modules/join_org_consent.lua @@ -2,14 +2,18 @@ -- A simple module that calls a consent process on an org to add a member. -- Depends on the Consent module. -join_org_consent = {} - -join_org_consent.setup = { +local join_org_consent = { name = "Join this org", slug = "join_org_consent", desc = "Adds member with consent of all members." } +join_org_consent.data = { +} + +join_org_consent.config = { +} + function join_org_consent:initiate() self.org:call_module( "consent", diff --git a/modpol/modules/remove_org.lua b/modpol/modules/remove_org.lua index ae9bbdc..590771d 100644 --- a/modpol/modules/remove_org.lua +++ b/modpol/modules/remove_org.lua @@ -1,22 +1,27 @@ ---- Remove Org +--- @module Remove Org -- A simple module that calls a consent process on an org to remove it. --- Depends on the Consent module. -remove_org = {} ---- (Required): setup table containing name and description of the module -remove_org.setup = { + +--- Main module table +remove_org = { name = "Remove this org", slug = "remove_org", desc = "Removes an org if all members consent." } +remove_org.config = {} +remove_org.data = {} + --- Initiate function ---
  • self.org (the org the module was called in),
  • ---
  • self.initiator (the user that callced the module),
  • ---
  • self.id (the process id of the module instance)
  • -- @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 result then result() end end + +modpol.modules.remove_org = remove_org diff --git a/modpol/modules/remove_org_consent.lua b/modpol/modules/remove_org_consent.lua index 9574901..de5c305 100644 --- a/modpol/modules/remove_org_consent.lua +++ b/modpol/modules/remove_org_consent.lua @@ -1,31 +1,39 @@ --- Remove org (consent) -- A simple module that calls a consent process on an org to remove it. -- Depends on the Consent module. -remove_org_consent = {} -remove_org_consent.setup = { +local remove_org_consent = { name = "Remove this org", - slug = "remove_org", + slug = "remove_org_consent", desc = "Removes an org if all members consent." } +remove_org_consent.data = { +} + +remove_org_consent.config = { +} + function remove_org_consent:initiate() self.org:call_module( "consent", self.initiator, { - prompt = "Remove org " .. self.org.name .. "?", - votes_required = #self.org.members + prompt = "Remove org " .. self.org.name .. "?", + votes_required = #self.org.members }, - function () - self:complete() + function () + self:complete() end ) end -function join_org_consent:complete() - self.org:delete() - print("Removed org " .. self.org.name .. ".") +function remove_org_consent:complete() + modpol.interactions.message_org( + 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 diff --git a/modpol/modules/template.lua b/modpol/modules/template.lua index 777f8f6..7358e57 100644 --- a/modpol/modules/template.lua +++ b/modpol/modules/template.lua @@ -1,26 +1,31 @@ ---- Template for modules --- function initiate and table setup are required as a base. --- @module Template -Template = {} +--- module_template +-- @module module_template ---- (Required): setup table containing name and description of the module --- @field name "Module Human-Readable Name" --- @field slug "Template" same as module name +--- (Required): data table containing name and description of the module +-- @field name "Human-readable name" +-- @field slug "Same as module class name" -- @field desc "Description of the module" -Template.setup = { +local module_template = { name = "Module Human-Readable Name", - slug = "Template", + slug = "template", desc = "Description of the module" } ---- (Optional): config for module +--- (Required) Data for module +-- Variables that module uses during the course of a process +-- Can be blank +module_template.data = { +} + +--- (Required): config for module -- Defines the input parameters to the module initiate function. +-- Can be blank -- When calling a module from within another module, -- variables not defined in config will be ignored. -- Default values set in config can be overridden -- @field field_1 ex: votes_required, default = 5 -- @field field_2 ex: voting_type, default = "majority" -Template.config = { +module_template.config = { field_1 = 5 field_2 = "majority" } @@ -33,9 +38,12 @@ Template.config = { -- @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 Template:initiate(config, result) +function module_template:initiate(config, result) -- call interaction functions here! -- call result function if result then result() end end + +--- (Required) Add to module table +modpol.modules.module_template = module_template diff --git a/modpol/orgs/base.lua b/modpol/orgs/base.lua index 2211e6e..27acf66 100644 --- a/modpol/orgs/base.lua +++ b/modpol/orgs/base.lua @@ -1,3 +1,6 @@ +--- Orgs: Base +-- Basic functions for orgs + modpol.orgs = modpol.orgs or { count = 1, @@ -171,7 +174,8 @@ function modpol.orgs:add_org(name, user) child_org.id = modpol.orgs.count child_org.name = name child_org.parent = self.id - child_org.processes = self.processes + child_org.processes = {} + child_org.modules = self.modules setmetatable(child_org, modpol.orgs) diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 38c10b6..8f20761 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -1,6 +1,8 @@ -function modpol.orgs:call_module(module_name, initiator, config, result) - if not modpol.modules[module_name] then - modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_name .. '" not found') +--- Process functions for orgs + +function modpol.orgs:call_module(module_slug, initiator, config, result) + if not modpol.modules[module_slug] then + modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found') return end @@ -21,10 +23,10 @@ function modpol.orgs:call_module(module_name, initiator, config, result) index = #self.processes + 1 end - local module = modpol.modules[module_name] + local module = modpol.modules[module_slug] -- sets default values for undeclared config variables - if module.config then + if #module.config > 0 then for k, v in pairs(module.config) do if config[k] == nil then config[k] = v @@ -39,14 +41,10 @@ function modpol.orgs:call_module(module_name, initiator, config, result) org = self, id = index, config = config, - name = module_name + data = module.data, + slug = module_slug } - -- copying default fields from setup - for k, v in pairs(module.setup) do - new_process[k] = v - end - setmetatable(new_process, new_process.metatable) self.processes[index] = new_process @@ -76,7 +74,7 @@ function modpol.orgs:wipe_pending_actions(process_id) end end -function modpol.orgs:has_pending_actions() +function modpol.orgs:has_pending_actions(user) -- next() will return the next pair in a table -- if next() returns nil, the table is empty if not self.pending[user] then @@ -91,11 +89,14 @@ function modpol.orgs:has_pending_actions() end function modpol.orgs:interact(process_id, user) - local process = self.processes[process_id] - if self.pending[user] then - local callback = self.pending[user][process_id] - if callback then - process[callback](process, user) - end - end + 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 end diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index b109a36..5a12989 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/modpol_minetest/overrides/interactions.lua @@ -59,10 +59,7 @@ function modpol.interactions.dashboard(user) "dropdown[2,2.5;5,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)..";;]", - "button[0.5,7;1,0.8;test_poll;Test poll]", - "button[2,7;1,0.8;add_org;Add org]", - "button[3.5,7;1.5,0.8;remove_org;Remove org]", - "button[5.5,7;1.5,0.8;reset_orgs;Reset orgs]", + "button[0.5,7;1.5,0.8;reset_orgs;Reset orgs]", "button_exit[8.5,7;1,0.8;close;Close]", } local formspec_string = table.concat(formspec, "") @@ -75,22 +72,6 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) local pname = player:get_player_name() if nil then -- buttons first - elseif fields.test_poll then - -- FOR TESTING PURPOSES ONLY - modpol.interactions.text_query( - pname,"Poll question:", - function(input) - modpol.interactions.binary_poll_user( - pname, input, - function(vote) - modpol.interactions.message( - pname, pname .. " voted " .. vote) - end) - end) - elseif fields.add_org then - modpol.interactions.add_org(pname, 1) - elseif fields.remove_org then - modpol.interactions.remove_org(pname) elseif fields.reset_orgs then modpol.orgs.reset() modpol.instance:add_member(pname) @@ -144,18 +125,20 @@ function modpol.interactions.org_dashboard(user, org_name) -- prepare modules menu local modules = {"View..."} if org.modules then - for k,v in ipairs(org.modules) do - table.insert(modules, org.modules[k].slug) + for k,v in pairs(org.modules) do + table.insert(modules, v.slug) end end -- prepare actions menu local actions = {"View..."} + local num_actions = 0 if org.pending[user] then for k,v in pairs(org.pending[user]) do local action_string = "[" .. k .. "] " .. - org.processes[k].name + org.processes[k].name table.insert(actions, action_string) + num_actions = num_actions + 1 end end @@ -177,7 +160,7 @@ function modpol.interactions.org_dashboard(user, org_name) "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]", "label[0.5,4;Modules:]", "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]", - "label[0.5,5;Actions:]", + "label[0.5,5;Actions ("..num_actions.."):]", "dropdown[2,4.5;5,0.8;actions;"..formspec_list(actions)..";;]", "button[0.5,7;1,0.8;test_poll;Test poll]", "button[2,7;1,0.8;add_child;Add child]", @@ -195,12 +178,7 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) local org = modpol.orgs.get_org(_contexts[pname].current_org) if nil then elseif fields.join then - local new_request = { - user = pname, - type = "add_member", - params = {pname} - } - org:make_request(new_request) + org:add_member(pname) modpol.interactions.org_dashboard(pname,org.name) elseif fields.leave then org:remove_member(pname) @@ -217,24 +195,19 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) elseif fields.add_child then modpol.interactions.text_query( pname, "Child org name:", - function(input) - local new_request = { - user = pname, - type = "add_org", - params = {input} - } - org:make_request(new_request) - modpol.interactions.message(pname,"requested") - modpol.interactions.org_dashboard( - pname,org.name) + function(input) + org:add_org(input,pname) + modpol.interactions.message_org( + pname, + org.id, + "Child org created: " .. input) end) elseif fields.remove_org then - local new_request = { - user = pname, - type = "delete", - params = {} - } - org:make_request(new_request) + modpol.interactions.message_org( + pname, + org.id, + "Removing org: " .. org.name) + org:delete() modpol.interactions.org_dashboard(pname,org.name) elseif fields.back then modpol.interactions.dashboard(pname) @@ -244,8 +217,9 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) elseif fields.modules and fields.modules ~= "View..." then local module = fields.modules - org:call_module(module, user) - + org:call_module(module, pname) + modpol.interactions.org_dashboard(pname,org.name) + -- Receiving actions elseif fields.actions and fields.actions ~= "View..." then @@ -253,7 +227,7 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) fields.actions,"%[(%d)%]") local process = org.processes[tonumber(action)] if process then - org:interact(process, user) + org:interact(process.id, pname) end -- Children @@ -282,8 +256,8 @@ end -- =========================== -- Function: modpol.interactions.message --- input: message (string) --- output +-- input: user (string), message (string) +-- output: displays message to specified user function modpol.interactions.message(user, message) minetest.chat_send_player(user, message) end @@ -291,7 +265,6 @@ end -- Function: modpol.interactions.text_query -- Overrides function at modpol/interactions.lua -- input: user (string), query (string), func (function) --- func input: user input (string) -- output: Applies "func" to user input function modpol.interactions.text_query(user, query, func) -- set up formspec @@ -303,7 +276,7 @@ function modpol.interactions.text_query(user, query, func) "button[0.5,2.5;1,0.8;yes;OK]", } local formspec_string = table.concat(formspec, "") - -- present to players + -- present to player minetest.show_formspec(user, "modpol:text_query", formspec_string) -- put func in _contexts if _contexts[user] == nil then _contexts[user] = {} end @@ -367,10 +340,6 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) end end) - --- SECONDARY INTERACTIONS --- ====================== - -- Function: modpol.binary_poll_user(user, question, function) -- Overrides function at modpol/interactions.lua -- Params: user (string), question (string), func (function) @@ -384,8 +353,8 @@ function modpol.interactions.binary_poll_user(user, question, func) "label[0.375,0.5;",minetest.formspec_escape(question), "]", "button[1,1.5;1,0.8;yes;Yes]", "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 + --TODO can we enable text wrapping? + --TODO we could use scroll boxes to contain the text } local formspec_string = table.concat(formspec, "") if _contexts[user] == nil then _contexts[user] = {} end @@ -409,71 +378,3 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) minetest.close_formspec(pname, formname) end end) - --- COMPLEX INTERACTIONS --- ==================== - --- Function: modpol.interactions.message_org --- input: initiator (string), org_id (number), message (string) --- output: broadcasts message to all org members -function modpol.interactions.message_org(initiator, org_id, message) - local org = modpol.orgs.get_org(org_id) - local users = org:list_members() - for k,v in ipairs(users) do - modpol.interactions.message(v, message) - end -end - - --- Function: modpol.interactions.binary_poll_org --- input: initator (user string), org_id (number) --- output: gets question from initiator, asks all org members, broadcasts answers --- TODO for testing. This should be implemented as a request. -function modpol.interactions.binary_poll_org(initiator, org_id, func) - local org = modpol.orgs.get_org(org_id) - local users = org:list_members() - modpol.interactions.text_query( - initiator, "Yes/no poll question:", - function(input) - for k,v in ipairs(users) do - modpol.interactions.binary_poll_user(v, input, func) - end - end) -end - --- Function: modpol.interactions.add_org --- input: initator (user string), base_org_id (ID) --- output: interaction begins --- GODMODE -function modpol.interactions.add_org(user, base_org_id) - modpol.interactions.text_query( - user,"Org name:", - function(input) - local base_org = modpol.orgs.get_org(1) - local result = base_org:add_org(input, user) - local message = input .. " created" - modpol.interactions.message(user, message) - modpol.interactions.dashboard(user) - end) -end - --- Function: modpol.interactions.remove_org --- input: initator (user string) --- output: interaction begins --- GODMODE -function modpol.interactions.remove_org(user) - -- start formspec - local orgs_list = modpol.orgs.list_all() - local label = "Choose an org to remove:" - modpol.interactions.dropdown_query( - user, label, orgs_list, - function(input) - if input then - local target_org = modpol.orgs.get_org(input) - local result = target_org:delete() - local message = input .. " deleted" - modpol.interactions.message(user, message) - end - modpol.interactions.dashboard(user) - end) -end From bd95fcf81123c2d0b989d7d823537fb265ee665b Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 14:02:14 -0700 Subject: [PATCH 27/39] Moved login.lua to root and updated README --- README.md | 13 +++++++++---- login.lua | 7 +++++++ modpol/interactions/login.lua | 7 ------- 3 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 login.lua delete mode 100644 modpol/interactions/login.lua diff --git a/README.md b/README.md index 353cfb4..e4f918e 100644 --- a/README.md +++ b/README.md @@ -19,15 +19,20 @@ In the game, open the Modular Politics interface with the command `/modpol`. Modular Politics can also be used independently of Minetest as a command-line tool. Currently command-line use of modpol requires a Unix-style system, but it is intended to become more fully platform independent. -The command-line version is in the `modpol` subdirectory. To interact with the interpreter on Unix systems in CLI mode, install lua or luajit and execute the following in this directory: +The command-line version is in the `modpol` subdirectory. To run the program on Unix systems in CLI mode, install lua or luajit and execute the following in this directory: ``` -$ cd modpol/interactions/ -$ lua [or luajit] +$ lua[jit] login.lua +``` + +You can also interact with the interpreter by starting it this way: + +``` +$ lua[jit] > dofile("login.lua") ``` -For a list of global functions and tables, use `modpol.menu()`. +In the interpreter, for a list of global functions and tables, use `modpol.menu()`. ## Storage diff --git a/login.lua b/login.lua new file mode 100644 index 0000000..e2a8971 --- /dev/null +++ b/login.lua @@ -0,0 +1,7 @@ +dofile("modpol/modpol.lua") + +print("Log in as which user?") +local username = io.read() + +print() +modpol.interactions.dashboard(username) diff --git a/modpol/interactions/login.lua b/modpol/interactions/login.lua deleted file mode 100644 index efb0063..0000000 --- a/modpol/interactions/login.lua +++ /dev/null @@ -1,7 +0,0 @@ -dofile("../modpol.lua") - -print("Log in as which user?") -local username = io.read() - -print() -modpol.interactions.dashboard(username) \ No newline at end of file From c2852b1bcea1a4158f5d7b026efc49af8206827f Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 14:50:41 -0700 Subject: [PATCH 28/39] Renamed modpol/modpol directory to modpol_core for clarity and consistency --- README.md | 10 ++++------ init.lua | 2 +- login.lua | 2 +- {modpol => modpol_core}/api.lua | 0 .../interactions/interactions.lua | 0 {modpol => modpol_core}/modpol.lua | 0 {modpol => modpol_core}/modules/add_child_org.lua | 0 {modpol => modpol_core}/modules/consent.lua | 0 {modpol => modpol_core}/modules/join_org.lua | 0 .../modules/join_org_consent.lua | 0 {modpol => modpol_core}/modules/leave_org.lua | 0 {modpol => modpol_core}/modules/remove_org.lua | 0 .../modules/remove_org_consent.lua | 0 {modpol => modpol_core}/modules/template.lua | 0 {modpol => modpol_core}/orgs/base.lua | 0 {modpol => modpol_core}/orgs/consent.lua | 0 {modpol => modpol_core}/orgs/defer.lua | 0 {modpol => modpol_core}/orgs/example.lua | 0 {modpol => modpol_core}/orgs/process.lua | 0 {modpol => modpol_core}/orgs/process_old.lua | 0 {modpol => modpol_core}/orgs/requests.lua | 0 {modpol => modpol_core}/storage/storage-local.lua | 0 .../tests/nested_module_test.lua | 0 {modpol => modpol_core}/tests/new_module_test.lua | 0 {modpol => modpol_core}/tests/org_basic_test.lua | 0 {modpol => modpol_core}/tests/org_req_test.lua | 0 {modpol => modpol_core}/users/users.lua | 0 {modpol => modpol_core}/util/ocutil/ocutil.lua | 0 {modpol => modpol_core}/util/serpent/LICENSE.txt | 0 {modpol => modpol_core}/util/serpent/README.md | 0 .../util/serpent/serpent-git.tar.bz2 | Bin {modpol => modpol_core}/util/serpent/serpent.lua | 0 32 files changed, 6 insertions(+), 8 deletions(-) rename {modpol => modpol_core}/api.lua (100%) rename {modpol => modpol_core}/interactions/interactions.lua (100%) rename {modpol => modpol_core}/modpol.lua (100%) rename {modpol => modpol_core}/modules/add_child_org.lua (100%) rename {modpol => modpol_core}/modules/consent.lua (100%) rename {modpol => modpol_core}/modules/join_org.lua (100%) rename {modpol => modpol_core}/modules/join_org_consent.lua (100%) rename {modpol => modpol_core}/modules/leave_org.lua (100%) rename {modpol => modpol_core}/modules/remove_org.lua (100%) rename {modpol => modpol_core}/modules/remove_org_consent.lua (100%) rename {modpol => modpol_core}/modules/template.lua (100%) rename {modpol => modpol_core}/orgs/base.lua (100%) rename {modpol => modpol_core}/orgs/consent.lua (100%) rename {modpol => modpol_core}/orgs/defer.lua (100%) rename {modpol => modpol_core}/orgs/example.lua (100%) rename {modpol => modpol_core}/orgs/process.lua (100%) rename {modpol => modpol_core}/orgs/process_old.lua (100%) rename {modpol => modpol_core}/orgs/requests.lua (100%) rename {modpol => modpol_core}/storage/storage-local.lua (100%) rename {modpol => modpol_core}/tests/nested_module_test.lua (100%) rename {modpol => modpol_core}/tests/new_module_test.lua (100%) rename {modpol => modpol_core}/tests/org_basic_test.lua (100%) rename {modpol => modpol_core}/tests/org_req_test.lua (100%) rename {modpol => modpol_core}/users/users.lua (100%) rename {modpol => modpol_core}/util/ocutil/ocutil.lua (100%) rename {modpol => modpol_core}/util/serpent/LICENSE.txt (100%) rename {modpol => modpol_core}/util/serpent/README.md (100%) rename {modpol => modpol_core}/util/serpent/serpent-git.tar.bz2 (100%) rename {modpol => modpol_core}/util/serpent/serpent.lua (100%) diff --git a/README.md b/README.md index e4f918e..0ec0d9a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # Modular Politics Prototype for Minetest -This is a mod for [Minetest](https://minetest.net) that enables diverse governance mechanisms. It seeks to implement the [Modular Politics](https://metagov.org/modpol) proposal. In the future, it -will be possible to use this framework to simulate governance in a -number of platform contexts. +This is a mod for [Minetest](https://minetest.net) that enables diverse governance mechanisms. It seeks to implement the [Modular Politics](https://metagov.org/modpol) proposal. In the future, it will be possible to use this framework to simulate governance in a number of platform contexts. This mod produces an API that can serve as a dependency for other mods that add specific governance functionalities. @@ -10,7 +8,7 @@ For background information and project roadmap, see [the wiki](https://gitlab.co ## Installation in Minetest -To use this in Minetest, simply install it in your mods/ or worldmods/ folder. Minetest will load init.lua. +To use this in Minetest, simply install it in your `mods/` or `worldmods/` folder. Minetest will load `init.lua`. In the game, open the Modular Politics interface with the command `/modpol`. @@ -36,9 +34,9 @@ In the interpreter, for a list of global functions and tables, use `modpol.menu( ## Storage -By default, a data directory named "data" will be created in this directory. "/data" will contain a log file and serialized program data files. +By default, a data directory named "data" will be created in this directory. `/data` will contain a log file and serialized program data files. -Another storage method may be chosen in modpol.lua. A StorageRef-based method for Minetest 5.* is included: storage-mod_storage.lua. +Another storage method may be chosen in `modpol.lua`. A StorageRef-based method for Minetest 5.* is included: `storage-mod_storage.lua`. ## Credits diff --git a/init.lua b/init.lua index 08bbb39..869f55c 100644 --- a/init.lua +++ b/init.lua @@ -34,7 +34,7 @@ modpol.storage_file_path = minetest.get_modpath("modpol").."/modpol_minetest/sto -- =================================================================== -- Load modpol system -dofile(minetest.get_modpath("modpol") .. "/modpol/modpol.lua") +dofile(minetest.get_modpath("modpol") .. "/modpol_core/modpol.lua") -- =================================================================== diff --git a/login.lua b/login.lua index e2a8971..a29a58c 100644 --- a/login.lua +++ b/login.lua @@ -1,4 +1,4 @@ -dofile("modpol/modpol.lua") +dofile("modpol_core/modpol.lua") print("Log in as which user?") local username = io.read() diff --git a/modpol/api.lua b/modpol_core/api.lua similarity index 100% rename from modpol/api.lua rename to modpol_core/api.lua diff --git a/modpol/interactions/interactions.lua b/modpol_core/interactions/interactions.lua similarity index 100% rename from modpol/interactions/interactions.lua rename to modpol_core/interactions/interactions.lua diff --git a/modpol/modpol.lua b/modpol_core/modpol.lua similarity index 100% rename from modpol/modpol.lua rename to modpol_core/modpol.lua diff --git a/modpol/modules/add_child_org.lua b/modpol_core/modules/add_child_org.lua similarity index 100% rename from modpol/modules/add_child_org.lua rename to modpol_core/modules/add_child_org.lua diff --git a/modpol/modules/consent.lua b/modpol_core/modules/consent.lua similarity index 100% rename from modpol/modules/consent.lua rename to modpol_core/modules/consent.lua diff --git a/modpol/modules/join_org.lua b/modpol_core/modules/join_org.lua similarity index 100% rename from modpol/modules/join_org.lua rename to modpol_core/modules/join_org.lua diff --git a/modpol/modules/join_org_consent.lua b/modpol_core/modules/join_org_consent.lua similarity index 100% rename from modpol/modules/join_org_consent.lua rename to modpol_core/modules/join_org_consent.lua diff --git a/modpol/modules/leave_org.lua b/modpol_core/modules/leave_org.lua similarity index 100% rename from modpol/modules/leave_org.lua rename to modpol_core/modules/leave_org.lua diff --git a/modpol/modules/remove_org.lua b/modpol_core/modules/remove_org.lua similarity index 100% rename from modpol/modules/remove_org.lua rename to modpol_core/modules/remove_org.lua diff --git a/modpol/modules/remove_org_consent.lua b/modpol_core/modules/remove_org_consent.lua similarity index 100% rename from modpol/modules/remove_org_consent.lua rename to modpol_core/modules/remove_org_consent.lua diff --git a/modpol/modules/template.lua b/modpol_core/modules/template.lua similarity index 100% rename from modpol/modules/template.lua rename to modpol_core/modules/template.lua diff --git a/modpol/orgs/base.lua b/modpol_core/orgs/base.lua similarity index 100% rename from modpol/orgs/base.lua rename to modpol_core/orgs/base.lua diff --git a/modpol/orgs/consent.lua b/modpol_core/orgs/consent.lua similarity index 100% rename from modpol/orgs/consent.lua rename to modpol_core/orgs/consent.lua diff --git a/modpol/orgs/defer.lua b/modpol_core/orgs/defer.lua similarity index 100% rename from modpol/orgs/defer.lua rename to modpol_core/orgs/defer.lua diff --git a/modpol/orgs/example.lua b/modpol_core/orgs/example.lua similarity index 100% rename from modpol/orgs/example.lua rename to modpol_core/orgs/example.lua diff --git a/modpol/orgs/process.lua b/modpol_core/orgs/process.lua similarity index 100% rename from modpol/orgs/process.lua rename to modpol_core/orgs/process.lua diff --git a/modpol/orgs/process_old.lua b/modpol_core/orgs/process_old.lua similarity index 100% rename from modpol/orgs/process_old.lua rename to modpol_core/orgs/process_old.lua diff --git a/modpol/orgs/requests.lua b/modpol_core/orgs/requests.lua similarity index 100% rename from modpol/orgs/requests.lua rename to modpol_core/orgs/requests.lua diff --git a/modpol/storage/storage-local.lua b/modpol_core/storage/storage-local.lua similarity index 100% rename from modpol/storage/storage-local.lua rename to modpol_core/storage/storage-local.lua diff --git a/modpol/tests/nested_module_test.lua b/modpol_core/tests/nested_module_test.lua similarity index 100% rename from modpol/tests/nested_module_test.lua rename to modpol_core/tests/nested_module_test.lua diff --git a/modpol/tests/new_module_test.lua b/modpol_core/tests/new_module_test.lua similarity index 100% rename from modpol/tests/new_module_test.lua rename to modpol_core/tests/new_module_test.lua diff --git a/modpol/tests/org_basic_test.lua b/modpol_core/tests/org_basic_test.lua similarity index 100% rename from modpol/tests/org_basic_test.lua rename to modpol_core/tests/org_basic_test.lua diff --git a/modpol/tests/org_req_test.lua b/modpol_core/tests/org_req_test.lua similarity index 100% rename from modpol/tests/org_req_test.lua rename to modpol_core/tests/org_req_test.lua diff --git a/modpol/users/users.lua b/modpol_core/users/users.lua similarity index 100% rename from modpol/users/users.lua rename to modpol_core/users/users.lua diff --git a/modpol/util/ocutil/ocutil.lua b/modpol_core/util/ocutil/ocutil.lua similarity index 100% rename from modpol/util/ocutil/ocutil.lua rename to modpol_core/util/ocutil/ocutil.lua diff --git a/modpol/util/serpent/LICENSE.txt b/modpol_core/util/serpent/LICENSE.txt similarity index 100% rename from modpol/util/serpent/LICENSE.txt rename to modpol_core/util/serpent/LICENSE.txt diff --git a/modpol/util/serpent/README.md b/modpol_core/util/serpent/README.md similarity index 100% rename from modpol/util/serpent/README.md rename to modpol_core/util/serpent/README.md diff --git a/modpol/util/serpent/serpent-git.tar.bz2 b/modpol_core/util/serpent/serpent-git.tar.bz2 similarity index 100% rename from modpol/util/serpent/serpent-git.tar.bz2 rename to modpol_core/util/serpent/serpent-git.tar.bz2 diff --git a/modpol/util/serpent/serpent.lua b/modpol_core/util/serpent/serpent.lua similarity index 100% rename from modpol/util/serpent/serpent.lua rename to modpol_core/util/serpent/serpent.lua From a03528d8fefe5e1ec4c9b619b06c3539a03a9b04 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 14:57:11 -0700 Subject: [PATCH 29/39] A bunch of cleaning up of unused files --- modpol_core/api.lua | 3 - modpol_core/interactions/interactions.lua | 2 +- modpol_core/orgs/consent.lua | 98 -------- modpol_core/orgs/defer.lua | 0 modpol_core/orgs/example.lua | 48 ---- modpol_core/orgs/process_old.lua | 153 ------------ modpol_core/orgs/requests.lua | 278 --------------------- modpol_core/users/users.lua | 29 --- modpol_minetest/overrides/interactions.lua | 2 +- 9 files changed, 2 insertions(+), 611 deletions(-) delete mode 100644 modpol_core/orgs/consent.lua delete mode 100644 modpol_core/orgs/defer.lua delete mode 100644 modpol_core/orgs/example.lua delete mode 100644 modpol_core/orgs/process_old.lua delete mode 100644 modpol_core/orgs/requests.lua delete mode 100644 modpol_core/users/users.lua diff --git a/modpol_core/api.lua b/modpol_core/api.lua index f5e04e6..707cdcb 100644 --- a/modpol_core/api.lua +++ b/modpol_core/api.lua @@ -2,9 +2,6 @@ local localdir = modpol.topdir ---Users -dofile (localdir .. "/users/users.lua") - --orgs dofile (localdir .. "/orgs/base.lua") dofile (localdir .. "/orgs/process.lua") diff --git a/modpol_core/interactions/interactions.lua b/modpol_core/interactions/interactions.lua index b198e4f..1dc8b7c 100644 --- a/modpol_core/interactions/interactions.lua +++ b/modpol_core/interactions/interactions.lua @@ -20,7 +20,7 @@ function modpol.interactions.dashboard(user) modpol.instance:add_member(user) end - local all_users = modpol.list_users() + local all_users = modpol.instance:list_members() print('All orgs: (user orgs indicated by *)') for id, org in ipairs(modpol.orgs.array) do diff --git a/modpol_core/orgs/consent.lua b/modpol_core/orgs/consent.lua deleted file mode 100644 index 243b0b0..0000000 --- a/modpol_core/orgs/consent.lua +++ /dev/null @@ -1,98 +0,0 @@ --- TODO: NEEDS TO BE REWRITTEN AS A LIBRARY NOT A MODULE - -modpol.orgs.consent = {} - --- sets consent to its own callback -modpol.orgs.consent.__index = modpol.orgs.consent - -function temp_consent_process() - return { - type = "consent", - id = nil, - org_id = nil, - request_id = nil, - total_votes = 0, - majority_to_pass = 0.51, - votes_needed = nil, - votes_yes = {}, - votes_no = {} - } -end - --- =============================================== --- function to create a new consent process to resolve a pending process -function modpol.orgs.consent:new_process(id, request_id, org_id) - local process = temp_consent_process() - process.id = id - process.request_id = request_id - process.org_id = org_id - - setmetatable(process, modpol.orgs.consent) - modpol.ocutil.log('Created new process #' .. id .. ' for request id #' .. request_id) - - local p_org = modpol.orgs.get_org(org_id) - - for i, member in ipairs(p_org.members) do - p_org:add_pending_action(id, member) - end - - process.votes_needed = math.ceil(process.majority_to_pass * p_org:get_member_count()) - - return process -end - --- ============================ --- interact function for the consent module --- input: user (string) -function modpol.orgs.consent:interact(user) - -- TODO this needs more context on the vote at hand - modpol.interactions.binary_poll_user( - user, "Do you consent?", - function(vote) - if vote == 'Yes' then - self:approve(user, true) - elseif vote == 'No' then - self:approve(user, false) - end - end) -end - --- ====================================================== --- function for users to vote on a pending request -function modpol.orgs.consent:approve(user, decision) - if not modpol.orgs.get_org(self.org_id):has_member(user) then - modpol.ocutil.log('Error in consent:approve -> user not a member of the org') - return - end - - if decision then - table.insert(self.votes_yes, user) - modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id) - else - table.insert(self.votes_no, user) - modpol.ocutil.log('User ' .. user .. ' voted no on request #' .. self.request_id) - end - - self.total_votes = self.total_votes + 1 - - local p_org = modpol.orgs.get_org(self.org_id) - p_org:remove_pending_action(self.id, user) - - self:update_status() -end - --- =================================================== --- determines whether process has finished and resolves request if it has (unfinished) -function modpol.orgs.consent:update_status() - local process_org = modpol.orgs.get_org(self.org_id) - - if #self.votes_yes >= self.votes_needed then - modpol.ocutil.log('Request #' .. self.request_id .. ' passes') - process_org:resolve_request(self.request_id, true) - elseif #self.votes_no >= self.votes_needed then - modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass') - process_org:resolve_request(self.request_id, false) - else - modpol.ocutil.log('Waiting for more votes...') - end -end diff --git a/modpol_core/orgs/defer.lua b/modpol_core/orgs/defer.lua deleted file mode 100644 index e69de29..0000000 diff --git a/modpol_core/orgs/example.lua b/modpol_core/orgs/example.lua deleted file mode 100644 index b16333b..0000000 --- a/modpol_core/orgs/example.lua +++ /dev/null @@ -1,48 +0,0 @@ ---[[ -modpol calls this function when someone starts a new request for this module ---]] - -function module:initiate(org) { - - form = { - { - "name": "to_remove", - "display": "Which user should be removed?", - "type": "drop-down", - "values": org:list_members() - }, - { - "name": "reason", - "type": "text-box", - "prompt": "Reason for removing member:" - } - } - - return form -} - ---[[ -modpol prompts the user with this form, and after receiving the data asynchronously -the returned form would look like: - -{ - "to_remove": luke, - "reason": stealing food -} - -based on provided "name" fields and the input given by the user -now module:request is called ---]] - -function module:request(form) { - self.data = form -} - ---[[ -after the module request function runs, modpol will initiate the consent process -if consent is approved, the implement function is called ---]] - -function module:implement() { - org:remove_member(self.data.to_remove) -} \ No newline at end of file diff --git a/modpol_core/orgs/process_old.lua b/modpol_core/orgs/process_old.lua deleted file mode 100644 index 0105f71..0000000 --- a/modpol_core/orgs/process_old.lua +++ /dev/null @@ -1,153 +0,0 @@ -old_request_format = { - user=user, -- requesting user - type="add_member", -- action - params={user} -- action params -} - -old_process_format = { - type = "consent", -- delete - id = nil, - org_id = nil, - request_id = nil, -- delete - - -- consent config - majority_to_pass = 0.51, -- voting threshold - votes_needed = nil, - - -- consent data - total_votes = 0, - votes_yes = {}, - votes_no = {} -} - -new_process_format = { - initiator = "user", - status = "request", - org_id = 12314, - module = "create_child_org", -- policy table lookup - process_id = 8347, - timestamp = 1632850133, -- look into supporting other formats, overrides (turn based, etc.) - - data = { - child_org_name = "oligarchy" - }, - - consent = { - -- voter eligibilty frozen by action table invites - start_time = 384179234, - member_count = 14, - votes_yes = {}, - votes_no = {} - } - -} - --- initialize values -function init_consent(policy) { - self.start_time = os.time() - self.member_count = modpol.orgs.get_org(self.org_id):get_member_count() - - if policy.duration then - -- mintest.after(time, func, ...args) - -- should override modpol callback function - register_callback(self.start_time + policy.duration) - end - - if (duration and (consent_ratio or yes_threshold or no_threshold)) or (yes_threshold) or (consent_ratio) then - -- well formed policy - else - -- invalid - end - -} - --- update vote count -function update_consent(user, approve) { - if approve then - table.insert(yes_votes, user) - else - table.insert(no_votes, user) - - if not duration then - eval_consent() - end - -} - --- evaluate state of vote -function eval_consent() { - consent_ratio = #yes_votes / (#yes_votes + #no_votes) - quorum = (#yes_votes + #no_votes) / member_count - - if policy.duration then - - if policy.consent_ratio then - if policy.quorum then - if quorum < policy.quorum then - fail() - end - end - if consent_ratio >= policy.consent_ratio then - pass() - else - fail() - end - - elseif policy.yes_threshold then - if #yes_votes >= policy.yes_threshold then - pass() - else - fail() - end - - elseif policy.no_threshold then - if #no_votes <= policy.no_threshold then - fail() - else - pass() - end - end - - elseif policy.yes_threshold then - if policy.no_threshold then - if #no_votes >= policy.no_threshold then - fail() - end - if #yes_votes >= policy.yes_threshold then - pass() - end - - elseif policy.consent_ratio and policy.quorum then - if quorum >= policy.quorum then - if consent_ratio >= policy.consent_ratio then - pass() - else - fail() - end - end - end - -} - - -policy_table_format = { - "create_child_org": { - defer_to = nil, - - -- duration - duration = nil, -- evaluates end conditions when reached - - -- thesholds - no_threshold = nil, -- fails if reached - yes_threshold = nil, -- succeeds if reached - - --ratios - consent_ratio = nil, -- % of voters - quorum = nil, -- % of members that vote - } - "create_child_org": { - consent_threshold = 0.51, - max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit - defer = nil, -- org id to defer to, or nil - } -} \ No newline at end of file diff --git a/modpol_core/orgs/requests.lua b/modpol_core/orgs/requests.lua deleted file mode 100644 index bcebab5..0000000 --- a/modpol_core/orgs/requests.lua +++ /dev/null @@ -1,278 +0,0 @@ --- THIS IS NOW DEPRECATED, TO MERGE INTO PROCESS - - --- REQUESTS --- Provides request functionality to org - --- TODO --- include consent functionality, available to modules --- load available modules in modpol.lua --- make policies a set of options for consent functions --- create simple modules for core functions: change_policy, add_member, start_org, join_org, close_org - --- CONSENT --- Provides consent functionality for modules - --- define default params -modpol.default_policy = { - target_org = nil, - time_limit = nil, - vote_threshold = 1 -- a ratio of the total number of members -} - - --- PROCESSES --- functions that enable requests to create processes - --- ================================ --- creates a new process linked to a request id -function modpol.orgs:create_process(process_type, request_id) - if not modpol.modules[process_type] then - modpol.ocutil.log('Process type "' .. process_type .. '" does not exist') - return - end - - local empty_index = nil - -- linear search for empty process slots (lazy deletion) - for k, v in ipairs(self.processes) do - if v == 'deleted' then - empty_index = k - break - end - end - - local index - -- attempts to fill empty spots in list, otherwise appends to end - if empty_index then - index = empty_index - else - index = #self.processes + 1 - end - - -- retrieving requested module - local module = modpol.modules[process_type] - local new_process = module:new_process(index, request_id, self.id) - - self.processes[index] = new_process - - modpol.ocutil.log('Created process #' .. index .. ' - ' .. process_type .. ' for ' .. self.name) - self:record('Created process #' .. index .. ' - ' .. process_type, 'add_process') - - return index -end - --- =========================== --- adds a new pending action to the org's table -function modpol.orgs:add_pending_action(process_id, user) - -- adds tables if they don't exist already - self.pending[user] = self.pending[user] or {} - -- flagging actual action - self.pending[user][process_id] = true - - local message = - modpol.interactions.message(user, "New pending action in " .. self.name) - modpol.ocutil.log("Added pending action to " .. user .. " in process #" .. process_id .. ' from ' .. self.name) - self:record('Added pending action to ' .. user .. ' in process #' .. process_id, "add_pending_action") -end - --- ======================== --- removes a pending action from the org's table -function modpol.orgs:remove_pending_action(process_id, user) - - if self.pending[user] then - self.pending[user][process_id] = nil - modpol.ocutil.log("Removed pending action from " .. user .. " in process #" .. process_id .. ' from ' .. self.name) - self:record('Removed pending action from ' .. user .. ' in process #' .. process_id, "del_pending_action") - else - modpol.ocutil.log("Could not remove pending action from " .. user .. " in process #" .. process_id) - end -end - --- ===================== --- removes all pending actions for a given process id from all users -function modpol.orgs:wipe_pending_actions(process_id) - for user in pairs(self.pending) do - self.pending[user][process_id] = nil - end - modpol.ocutil.log("Removed all pending actions for process #" .. process_id) - self:record('Removed all pending actions for process #' .. process_id, "del_pending_action") -end - --- ====================== --- returns a boolean to indicate whether a user has any active pending actions -function modpol.orgs:has_pending_actions(user) - -- next() will return the next pair in a table - -- if next() returns nil, the table is empty - if not self.pending[user] then - return false - else - if not next(self.pending[user]) then - return false - else - return true - end - end -end - --- =========================== --- compares to requests to see if they are identical -function modpol.orgs.comp_req(req1, req2) - -- compares request type - if req1.type ~= req2.type then - return false - else - -- comparing parameters - -- we can assume the number of params is the same as this is checked in the make_request func - for k, v in ipairs(req1.params) do - if v ~= req2.params[k] then - return false - end - end - end - return true -end - --- =============================== --- returns string of all active requests -function modpol.orgs:list_request() - local str - for id, req in ipairs(self.requests) do - if req ~= "deleted" then - if str then - str = str .. '\n' .. req.type .. ' (' .. req.user .. ') ' - else - str = req.type .. ' (' .. req.user .. ') ' - end - end - end - return str -end - --- =============================== --- if the request was approved, the associated function is called, otherwise it is deleted --- TODO Rather than hard-coding functions below, this should be given an arbitrary result function based on the request -function modpol.orgs:resolve_request(request_id, approve) - - -- wipe actions - self:wipe_pending_actions(request_id) - - if approve then - local request = self.requests[request_id] - local p = request.params - - -- there's probably a way to clean this up, the issue is the varying number of commands - -- ex: self['add_member'](self, 'member_name') - -- not sure if this is safe, more testing to do - - -- self[request.type](self, p[1], p[2], p[3]) - - if request.type == "add_org" then - self:add_org(request.params[1], request.user) - elseif request.type == "delete" then - self:delete() - elseif request.type == "add_member" then - self:add_member(request.params[1]) - elseif request.type == "remove_member" then - self:remove_member(request.params[1]) - end - - end - - self.processes[request_id] = "deleted" - modpol.ocutil.log('Deleted process #' .. request_id) - - self.requests[request_id] = "deleted" - modpol.ocutil.log("Resolved request #" .. request_id .. ' in ' .. self.name) - - self:record("Resolved request #" .. request_id, "resolve_request") -end - --- ================================ --- tries to make a request to the org -function modpol.orgs:make_request(request) - - -- checking to see if identical request already exists - for k, v in ipairs(self.requests) do - if self.comp_req(request, v) == true then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made') - return false - end - end - - -- checking to see if user is able to make request - local requested_policy = self.policies[request.type] - - -- if not the instance org (instance's don't have parents) - if self.id ~= 1 then - local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type] - - -- tries to use org's policy table, defers to parent otherwise - if not requested_policy then - modpol.ocutil.log(request.type .. ' policy not found, deferring to parent org') - requested_policy = parent_policy - - if not parent_policy then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined') - return false - end - end - - -- fails if instance policy undefined - else - if not requested_policy then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> policy undefined') - return false - end - end - - -- make sure user is allowed to make request - if requested_policy.must_be_member and not self:has_member(request.user) then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request') - return false - end - - local empty_index = nil - -- linear search for empty process slots (lazy deletion) - for k, v in ipairs(self.requests) do - if v == 'deleted' then - empty_index = k - break - end - end - - -- attempts to fill empty spots in list, otherwise appends to end - local request_id = nil - if empty_index then - self.requests[empty_index] = request - request_id = empty_index - else - table.insert(self.requests, request) - request_id = #self.requests - end - - modpol.interactions.message(request.user, "Request made by " .. request.user .. " to " .. request.type .. " in " .. self.name) - modpol.ocutil.log("Request made by " .. request.user .. " to " .. request.type .. " in " .. self.name) - self:record("Request made by " .. request.user .. " to " .. request.type, "make_request") - - -- launching process tied to this request - local process_id = self:create_process(requested_policy.process_type, request_id) - - -- returns process id of processes launched by this request - return process_id -end - --- wrapper for process:interact function, ensures that user actually has a pending action for that process -function modpol.orgs:interact(process_id, user) - process = self.processes[process_id] - if self.pending[user] then - if self.pending[user][process_id] == true then - process:interact(user) - else - modpol.ocutil.log("Cannot interact with process, user does not have a valid pending action") - end - else - modpol.ocutil.log("Cannot interact with process, user does not have any pending actions") - end - -end - diff --git a/modpol_core/users/users.lua b/modpol_core/users/users.lua deleted file mode 100644 index 8c94eec..0000000 --- a/modpol_core/users/users.lua +++ /dev/null @@ -1,29 +0,0 @@ --- =================================================================== --- /users.lua --- User-related functions for Modular Politics --- Called by modpol.lua - --- =================================================================== --- Function: modpol.list_users --- Params: org --- Outputs: Table of user names --- --- This may be overwritten by the platform-specific interface - -modpol.list_users = function(org) - local users = {} - if (org == nil) then -- no specified org; all players - if modpol.instance - and modpol.instance.members then - -- if instance exists and has membership - users = modpol.instance.members - else - users = {} - end - else -- if an org is specified - if (modpol.orgs[org] ~= nil) then -- org exists - users = modpol.orgs[org]["members"] - end - end - return users -end diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index 5a12989..89e84c0 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/modpol_minetest/overrides/interactions.lua @@ -47,7 +47,7 @@ function modpol.interactions.dashboard(user) -- to add: nested orgs map local all_orgs = modpol.orgs.list_all() local user_orgs = modpol.orgs.user_orgs(user) - local all_users = modpol.list_users() + local all_users = modpol.instance:list_members() -- set up formspec local formspec = { "formspec_version[4]", From 3e3f737915ff91a547a2a9dce7c742092439831f Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 15:06:26 -0700 Subject: [PATCH 30/39] A little more cleaning up --- init.lua | 2 +- modpol_minetest/api.lua | 8 +------- modpol_minetest/overrides/users.lua | 21 --------------------- 3 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 modpol_minetest/overrides/users.lua diff --git a/init.lua b/init.lua index 869f55c..66cc4a6 100644 --- a/init.lua +++ b/init.lua @@ -14,7 +14,7 @@ modpol = {} -- get modpol_minetest's version of directory modpol.get_script_dir = function() - return minetest.get_modpath("modpol").."/modpol" + return minetest.get_modpath("modpol").."/modpol_core" end -- TKTK: Implement minetest settingtypes for this... for now, the default is to use minetest mod storage. diff --git a/modpol_minetest/api.lua b/modpol_minetest/api.lua index b469924..2d0aa39 100644 --- a/modpol_minetest/api.lua +++ b/modpol_minetest/api.lua @@ -9,13 +9,7 @@ local localdir = minetest.get_modpath("modpol") .. "/modpol_minetest" ---Users -dofile (localdir .. "/overrides/users.lua") - ---orgs ---dofile (localdir .. "/overrides/orgs.lua") - ---interactions +--overrides dofile (localdir .. "/overrides/interactions.lua") diff --git a/modpol_minetest/overrides/users.lua b/modpol_minetest/overrides/users.lua deleted file mode 100644 index 229ebfb..0000000 --- a/modpol_minetest/overrides/users.lua +++ /dev/null @@ -1,21 +0,0 @@ - --- =================================================================== --- Function: modpol.list_users(org) --- Overwrites function at /users.lua --- Params: --- if nil, lists instance members; if an org name, lists its members --- Output: a table with names of players currently in the game -modpol.list_users = function(org) - local users = {} - if (org == nil) then -- no specified org; all players - for _,player in ipairs(minetest.get_connected_players()) do - local name = player:get_player_name() - table.insert(users,name) - end - else -- if an org is specified - if (modpol.orgs[org] ~= nil) then -- org exists - users = modpol.orgs[org]["members"] - end - end - return users - end \ No newline at end of file From ac0c78795b0864876e4a247e66d076cf2a4ccb32 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 15:46:14 -0700 Subject: [PATCH 31/39] Drafted priv_to_org as first MT module, but it doesn't work yet b/c interaction issues --- README.md | 4 ++-- modpol_minetest/api.lua | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0ec0d9a..867df4e 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# Modular Politics Prototype for Minetest +# Modular Politics for Minetest This is a mod for [Minetest](https://minetest.net) that enables diverse governance mechanisms. It seeks to implement the [Modular Politics](https://metagov.org/modpol) proposal. In the future, it will be possible to use this framework to simulate governance in a number of platform contexts. This mod produces an API that can serve as a dependency for other mods that add specific governance functionalities. -For background information and project roadmap, see [the wiki](https://gitlab.com/medlabboulder/modpol/-/wikis/home). +For background information, documentation, and the project roadmap, see [the wiki](https://gitlab.com/medlabboulder/modpol/-/wikis/home). ## Installation in Minetest diff --git a/modpol_minetest/api.lua b/modpol_minetest/api.lua index 2d0aa39..7547f5e 100644 --- a/modpol_minetest/api.lua +++ b/modpol_minetest/api.lua @@ -1,6 +1,4 @@ ---call all files in this directory - - +--- Script for loading Minetest files -- =================================================================== -- Minetest Specific Overrides of modpol functions @@ -20,7 +18,13 @@ dofile (localdir .. "/chatcommands.lua") -- =================================================================== --- Minetest Specific code +-- Minetest-specific modules +-- =================================================================== + +dofile (localdir .. "/modules/priv_to_org.lua") + +-- =================================================================== +-- Minetest-specific code -- =================================================================== --add members to the instance, if they are not already there. From 51d5e404e960d961c22acd308ee6168722bed212 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 15:48:55 -0700 Subject: [PATCH 32/39] Tweak to description.txt --- description.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/description.txt b/description.txt index 4a6c332..7b5f707 100644 --- a/description.txt +++ b/description.txt @@ -1 +1 @@ -Provides an API for diverse governance mechanisms +Provides a basis for diverse, user-configurable modules for in-game governance. From af6e639e273603da68d03f0be393df46410eb43f Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 17:53:49 -0700 Subject: [PATCH 33/39] Added minetest modules and am stuck on interactions and module flow --- modpol_core/api.lua | 1 + modpol_core/modules/add_child_org.lua | 51 ++++++++++++++++++---- modpol_core/modules/consent.lua | 6 +-- modpol_core/modules/leave_org.lua | 43 ++++++++++++++++++ modpol_core/modules/remove_org.lua | 18 +++++--- modpol_core/modules/remove_org_consent.lua | 1 - modpol_core/orgs/process.lua | 3 -- modpol_minetest/modules/priv_to_org.lua | 49 +++++++++++++++++++++ modpol_minetest/overrides/interactions.lua | 6 +++ 9 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 modpol_minetest/modules/priv_to_org.lua diff --git a/modpol_core/api.lua b/modpol_core/api.lua index 707cdcb..45029c1 100644 --- a/modpol_core/api.lua +++ b/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") diff --git a/modpol_core/modules/add_child_org.lua b/modpol_core/modules/add_child_org.lua index bad73b3..b0cf608 100644 --- a/modpol_core/modules/add_child_org.lua +++ b/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 diff --git a/modpol_core/modules/consent.lua b/modpol_core/modules/consent.lua index 03983a6..885b847 100644 --- a/modpol_core/modules/consent.lua +++ b/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 ) diff --git a/modpol_core/modules/leave_org.lua b/modpol_core/modules/leave_org.lua index e69de29..ba5be73 100644 --- a/modpol_core/modules/leave_org.lua +++ b/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: +--
  • self.org (the org the module was called in),
  • +--
  • self.initiator (the user that callced the module),
  • +--
  • self.id (the process id of the module instance)
  • +-- @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 diff --git a/modpol_core/modules/remove_org.lua b/modpol_core/modules/remove_org.lua index 590771d..616c953 100644 --- a/modpol_core/modules/remove_org.lua +++ b/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 diff --git a/modpol_core/modules/remove_org_consent.lua b/modpol_core/modules/remove_org_consent.lua index de5c305..0e8b0ca 100644 --- a/modpol_core/modules/remove_org_consent.lua +++ b/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 diff --git a/modpol_core/orgs/process.lua b/modpol_core/orgs/process.lua index 8f20761..e132524 100644 --- a/modpol_core/orgs/process.lua +++ b/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 diff --git a/modpol_minetest/modules/priv_to_org.lua b/modpol_minetest/modules/priv_to_org.lua new file mode 100644 index 0000000..ac63a88 --- /dev/null +++ b/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: +--
  • self.org (the org the module was called in),
  • +--
  • self.initiator (the user that callced the module),
  • +--
  • self.id (the process id of the module instance)
  • +-- @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 diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index 89e84c0..03aa55e 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/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) From e994061f38eba333c9883616d42bb2eb94b020d7 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 20:41:49 -0700 Subject: [PATCH 34/39] A bunch of module bugfixes --- modpol_core/interactions/interactions.lua | 8 ++-- modpol_core/modules/add_child_org.lua | 48 +++++++++++----------- modpol_core/modules/consent.lua | 15 +++---- modpol_core/modules/join_org.lua | 8 ++-- modpol_core/modules/join_org_consent.lua | 3 +- modpol_core/modules/leave_org.lua | 7 +--- modpol_core/modules/remove_org.lua | 4 +- modpol_core/modules/remove_org_consent.lua | 5 ++- modpol_core/modules/template.lua | 3 +- modpol_core/orgs/process.lua | 2 + modpol_core/util/ocutil/ocutil.lua | 2 +- modpol_minetest/modules/priv_to_org.lua | 27 ++++++------ modpol_minetest/overrides/interactions.lua | 33 ++++++++++----- 13 files changed, 87 insertions(+), 78 deletions(-) diff --git a/modpol_core/interactions/interactions.lua b/modpol_core/interactions/interactions.lua index 1dc8b7c..81de00a 100644 --- a/modpol_core/interactions/interactions.lua +++ b/modpol_core/interactions/interactions.lua @@ -228,11 +228,11 @@ end -- ==================== -- Function: modpol.interactions.message_org --- input: initiator (string), org_id (number), message (string) +-- input: initiator (string), org (number or string), message (string) -- output: broadcasts message to all org members -function modpol.interactions.message_org(initiator, org_id, message) - local org = modpol.orgs.get_org(org_id) - local users = org:list_members() +function modpol.interactions.message_org(initiator, org, message) + local this_org = modpol.orgs.get_org(org) + local users = this_org:list_members() for k,v in ipairs(users) do modpol.interactions.message(v, message) end diff --git a/modpol_core/modules/add_child_org.lua b/modpol_core/modules/add_child_org.lua index b0cf608..933b087 100644 --- a/modpol_core/modules/add_child_org.lua +++ b/modpol_core/modules/add_child_org.lua @@ -15,12 +15,6 @@ add_child_org.config = { -- @function initiate 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) @@ -28,34 +22,38 @@ function add_child_org:name_child_org() modpol.interactions.message( self.initiator, "No name entered for child org") - self.org:wipe_pending_actions(self.id) + self.org:delete_process(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() + -- initiate consent process + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Create child org " .. + self.data.child_name .. "?", + votes_required = #self.org.members + }, + function() + self:create_child_org() + end + ) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) 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 - ) +function add_child_org:create_child_org() + self.org:add_org(self.data.child_name, self.initiator) + modpol.interactions.message_org( + self.initiator, + self.org.name, + "Child org created: "..self.data.child_name) + self.org:delete_process(self.id) end --- (Required) Add to module table diff --git a/modpol_core/modules/consent.lua b/modpol_core/modules/consent.lua index 885b847..e936b39 100644 --- a/modpol_core/modules/consent.lua +++ b/modpol_core/modules/consent.lua @@ -8,7 +8,7 @@ local consent = { } consent.data = { - votes = 0 + votes = 0 } consent.config = { @@ -16,11 +16,12 @@ consent.config = { votes_required = 1 } -function consent:initiate(config, result) - self.result = result +function consent:initiate(result) + self.data.result = result -- if org is empty, consent is given automatically if self.org:get_member_count() == 0 then - self.result() + if self.data.result then + self.data.result() end self.org:wipe_pending_actions(self.id) else -- otherwise, create poll @@ -39,14 +40,14 @@ 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.data.result then + self.data.result() end self.org:wipe_pending_actions(self.id) - if self.result then self.result() end + self.org:delete_process(self.id) end end ) end - modpol.modules.consent = consent diff --git a/modpol_core/modules/join_org.lua b/modpol_core/modules/join_org.lua index 3806685..23d277b 100644 --- a/modpol_core/modules/join_org.lua +++ b/modpol_core/modules/join_org.lua @@ -7,19 +7,19 @@ join_org.setup = { desc = "If consent process is passed, initiator joins this org." } -function join_org.initiate(initiator, org, result) +function join_org.initiate(result) modpol.interactions.binary_poll_user( initiator, "Would you like to join " .. org.name, function (resp) if resp == "Yes" then - org:add_member(initiator) + self.org:add_member(self.initiator) end end ) - for i, member in ipairs(org.members) do - org:add_pending_action( + for i, member in ipairs(self.org.members) do + self.org:add_pending_action( member, function () modpol.interactions.binary_poll_user( diff --git a/modpol_core/modules/join_org_consent.lua b/modpol_core/modules/join_org_consent.lua index 9228dc7..e40e023 100644 --- a/modpol_core/modules/join_org_consent.lua +++ b/modpol_core/modules/join_org_consent.lua @@ -14,7 +14,7 @@ join_org_consent.data = { join_org_consent.config = { } -function join_org_consent:initiate() +function join_org_consent:initiate(result) self.org:call_module( "consent", self.initiator, @@ -26,6 +26,7 @@ function join_org_consent:initiate() self:complete() end ) + if result then result() end end function join_org_consent:complete() diff --git a/modpol_core/modules/leave_org.lua b/modpol_core/modules/leave_org.lua index ba5be73..bbc0c42 100644 --- a/modpol_core/modules/leave_org.lua +++ b/modpol_core/modules/leave_org.lua @@ -15,13 +15,9 @@ leave_org.config = { --- (Required): initiate function -- Modules have access to the following instance variables: ---
  • self.org (the org the module was called in),
  • ---
  • self.initiator (the user that callced the module),
  • ---
  • self.id (the process id of the module instance)
  • --- @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) +function leave_org:initiate(result) if self.org == modpol.instance then modpol.interactions.message( self.initiator, @@ -34,7 +30,6 @@ function leave_org:initiate(config, result) modpol.interactions.message( self.initiator, "You have left org " .. self.org.name) - -- call result function end if result then result() end end diff --git a/modpol_core/modules/remove_org.lua b/modpol_core/modules/remove_org.lua index 616c953..9c3bbc6 100644 --- a/modpol_core/modules/remove_org.lua +++ b/modpol_core/modules/remove_org.lua @@ -1,5 +1,5 @@ --- @module Remove Org --- A simple module that calls a consent process on an org to remove it. +-- A simple module that removes an org. --- Main module table @@ -14,7 +14,7 @@ remove_org.data = {} --- Initiate function -- @function initiate -function remove_org:initiate(config, result) +function remove_org:initiate(result) if self.org == modpol.instance then modpol.interactions.message( self.initiator, diff --git a/modpol_core/modules/remove_org_consent.lua b/modpol_core/modules/remove_org_consent.lua index 0e8b0ca..d5f5973 100644 --- a/modpol_core/modules/remove_org_consent.lua +++ b/modpol_core/modules/remove_org_consent.lua @@ -14,7 +14,7 @@ remove_org_consent.data = { remove_org_consent.config = { } -function remove_org_consent:initiate() +function remove_org_consent:initiate(result) self.org:call_module( "consent", self.initiator, @@ -26,6 +26,9 @@ function remove_org_consent:initiate() self:complete() end ) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + if result then result() end end function remove_org_consent:complete() diff --git a/modpol_core/modules/template.lua b/modpol_core/modules/template.lua index 7358e57..1232e52 100644 --- a/modpol_core/modules/template.lua +++ b/modpol_core/modules/template.lua @@ -35,10 +35,9 @@ module_template.config = { --
  • self.org (the org the module was called in),
  • --
  • self.initiator (the user that callced the module),
  • --
  • self.id (the process id of the module instance)
  • --- @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 module_template:initiate(config, result) +function module_template:initiate(result) -- call interaction functions here! -- call result function diff --git a/modpol_core/orgs/process.lua b/modpol_core/orgs/process.lua index e132524..8961a1a 100644 --- a/modpol_core/orgs/process.lua +++ b/modpol_core/orgs/process.lua @@ -60,6 +60,8 @@ end function modpol.orgs:add_pending_action(process_id, user, callback) self.pending[user] = self.pending[user] or {} self.pending[user][process_id] = callback + modpol.interactions.message( + user, "New pending action in org "..self.name) end function modpol.orgs:remove_pending_action(process_id, user) diff --git a/modpol_core/util/ocutil/ocutil.lua b/modpol_core/util/ocutil/ocutil.lua index f943be4..940d7a2 100644 --- a/modpol_core/util/ocutil/ocutil.lua +++ b/modpol_core/util/ocutil/ocutil.lua @@ -97,7 +97,7 @@ modpol.ocutil.log = function (s) if modpol.ocutil.logdir ~= nil and modpol.ocutil.logname ~= nil then - s = s .. "\n" -- Add trailing newline + s = s .. "\n" -- Add trailing newline local logpath = modpol.ocutil.logdir .. "/" .. modpol.ocutil.logname modpol.ocutil.file_append (logpath, s) diff --git a/modpol_minetest/modules/priv_to_org.lua b/modpol_minetest/modules/priv_to_org.lua index ac63a88..03e19a3 100644 --- a/modpol_minetest/modules/priv_to_org.lua +++ b/modpol_minetest/modules/priv_to_org.lua @@ -15,31 +15,30 @@ priv_to_org.config = { } --- (Required): initiate function --- Modules have access to the following instance variables: ---
  • self.org (the org the module was called in),
  • ---
  • self.initiator (the user that callced the module),
  • ---
  • self.id (the process id of the module instance)
  • --- @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) +function priv_to_org:initiate(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 + -- construct table for display + local player_privs_table = {"View..."} + for k,v in pairs(player_privs) do + if player_privs[k] then + table.insert(player_privs_table,k) + end end modpol.interactions.dropdown_query( self.initiator, "Which privilege do you want to share with members of "..self.org.name.."?", - player_privs, + player_privs_table, function(input) - for member in self.org.members do - local member_privs = minetest.get_player_privs(member.name) + for i,member in ipairs(self.org.members) do + local member_privs = minetest.get_player_privs(member) member_privs[input] = true - minetest.set_player_privs(member.name, member_privs) + minetest.set_player_privs(member, member_privs) end - local message = self.initiator .. " has set " .. input .. + local message = self.initiator .. " added " .. input .. " privilege to all members of " .. self.org.name - modpol.interactions.message_org(self.initiator,self.org.id, message) + modpol.interactions.message_org(self.initiator, self.org.id, message) end) -- call result function if result then result() end diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index 03aa55e..7346bc5 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/modpol_minetest/overrides/interactions.lua @@ -223,8 +223,16 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) elseif fields.modules and fields.modules ~= "View..." then local module = fields.modules - org:call_module(module, pname) - modpol.interactions.org_dashboard(pname,org.name) + modpol.interactions.binary_poll_user( + pname, + modpol.modules[module].name.."\n".. + modpol.modules[module].desc.."\n".. + "Proceed?", + function(input) + if input == "Yes" then + org:call_module(module, pname) + end + end) -- Receiving actions elseif fields.actions @@ -258,14 +266,16 @@ function modpol.interactions.policy_dashboard( end --- BASIC INTERACTION FUNCTIONS --- =========================== +-- INTERACTION FUNCTIONS +-- ===================== -- Function: modpol.interactions.message -- input: user (string), message (string) -- output: displays message to specified user function modpol.interactions.message(user, message) - minetest.chat_send_player(user, message) + if message then + minetest.chat_send_player(user, message) + end end -- Function: modpol.interactions.text_query @@ -331,11 +341,13 @@ 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 + if fields.cancel == "cancel" then + -- cancel, do nothing + else local choice = fields.input local func = _contexts[pname]["dropdown_query_func"] if not choice then - -- no choice, do nothing + -- empty, do nothing elseif func then func(choice) else @@ -355,10 +367,10 @@ function modpol.interactions.binary_poll_user(user, question, func) -- set up formspec local formspec = { "formspec_version[4]", - "size[5,3]", + "size[8,4]", "label[0.375,0.5;",minetest.formspec_escape(question), "]", - "button[1,1.5;1,0.8;yes;Yes]", - "button[2,1.5;1,0.8;no;No]", + "button[1,2.5;1,0.8;yes;Yes]", + "button[2,2.5;1,0.8;no;No]", --TODO can we enable text wrapping? --TODO we could use scroll boxes to contain the text } @@ -377,7 +389,6 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) elseif fields.no then vote = fields.no end if vote then - modpol.interactions.message(pname, "Responded " .. vote) local func = _contexts[pname]["binary_poll_func"] if func then func(vote) end end From d4599e9bbe85a26f5b12bbd020e1678499ca7386 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 21:01:33 -0700 Subject: [PATCH 35/39] Removing the testing buttons from formspecs now that modules do all that work --- modpol_core/modules/remove_org.lua | 2 +- modpol_minetest/overrides/interactions.lua | 71 +++++----------------- 2 files changed, 15 insertions(+), 58 deletions(-) diff --git a/modpol_core/modules/remove_org.lua b/modpol_core/modules/remove_org.lua index 9c3bbc6..23fc07e 100644 --- a/modpol_core/modules/remove_org.lua +++ b/modpol_core/modules/remove_org.lua @@ -6,7 +6,7 @@ remove_org = { name = "Remove this org", slug = "remove_org", - desc = "Removes an org if all members consent." + desc = "Eliminates the org and all child orgs." } remove_org.config = {} diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index 7346bc5..a4a5d2a 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/modpol_minetest/overrides/interactions.lua @@ -59,7 +59,6 @@ function modpol.interactions.dashboard(user) "dropdown[2,2.5;5,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)..";;]", - "button[0.5,7;1.5,0.8;reset_orgs;Reset orgs]", "button_exit[8.5,7;1,0.8;close;Close]", } local formspec_string = table.concat(formspec, "") @@ -72,10 +71,7 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) local pname = player:get_player_name() if nil then -- buttons first - elseif fields.reset_orgs then - modpol.orgs.reset() - modpol.instance:add_member(pname) - modpol.interactions.dashboard(pname) + -- none here right now! -- Put all dropdowns at the end elseif fields.close then @@ -95,21 +91,18 @@ function modpol.interactions.org_dashboard(user, org_name) -- prepare data local org = modpol.orgs.get_org(org_name) if not org then return nil end - local is_member = org:has_member(user) - local membership_toggle = function() - local toggle_code = "" - if is_member then - toggle_code = toggle_code - ..minetest.formspec_escape("leave")..";" - ..minetest.formspec_escape("Leave").."]" - else - toggle_code = toggle_code - ..minetest.formspec_escape("join")..";" - ..minetest.formspec_escape("Join").."]" - end - return toggle_code - end + local function membership_toggle(org_display) + local current_org = modpol.orgs.get_org(org_display) + if current_org then + if current_org:has_member(user) then + return " (member)" + end + else + return "" + end + end + -- identify parent local parent = modpol.orgs.get_org(org.parent) if parent then parent = parent.name @@ -151,9 +144,8 @@ function modpol.interactions.org_dashboard(user, org_name) "formspec_version[4]", "size[10,8]", "label[0.5,0.5;Org: ".. - minetest.formspec_escape(org_name).."]", - "label[0.5,1;Parent: "..parent.."]", - "button[8.5,0.5;1,0.8;"..membership_toggle(), + 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)..";;]", "label[0.5,3;Children:]", @@ -162,9 +154,6 @@ function modpol.interactions.org_dashboard(user, org_name) "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]", "label[0.5,5;Actions ("..num_actions.."):]", "dropdown[2,4.5;5,0.8;actions;"..formspec_list(actions)..";;]", - "button[0.5,7;1,0.8;test_poll;Test poll]", - "button[2,7;1,0.8;add_child;Add child]", - "button[3.5,7;1.5,0.8;remove_org;Remove org]", "button[8.5,7;1,0.8;back;Back]", } local formspec_string = table.concat(formspec, "") @@ -183,38 +172,6 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) return end -- okay, onward if nil then - elseif fields.join then - org:add_member(pname) - modpol.interactions.org_dashboard(pname,org.name) - elseif fields.leave then - org:remove_member(pname) - modpol.interactions.dashboard(pname) - elseif fields.test_poll then - modpol.interactions.binary_poll_org( - pname, org.id, - function(input) - modpol.interactions.message_org( - pname, - org.id, - "New response: " .. input) - end) - elseif fields.add_child then - modpol.interactions.text_query( - pname, "Child org name:", - function(input) - org:add_org(input,pname) - modpol.interactions.message_org( - pname, - org.id, - "Child org created: " .. input) - end) - elseif fields.remove_org then - modpol.interactions.message_org( - pname, - org.id, - "Removing org: " .. org.name) - org:delete() - modpol.interactions.org_dashboard(pname,org.name) elseif fields.back then modpol.interactions.dashboard(pname) From 53d2a25f4212f3ca0522d59f3d2c01fe1623247e Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sun, 19 Dec 2021 16:00:26 -0700 Subject: [PATCH 36/39] Adding and fixing basic modules for renaming, removing, and adding orgs --- README.md | 4 +- modpol_core/api.lua | 5 +- modpol_core/interactions/interactions.lua | 24 +++--- ...hild_org.lua => add_child_org_consent.lua} | 39 +++++++--- modpol_core/modules/consent.lua | 7 +- modpol_core/modules/join_org_consent.lua | 32 +++++--- modpol_core/modules/leave_org.lua | 9 ++- modpol_core/modules/remove_child_consent.lua | 71 +++++++++++++++++ modpol_core/modules/remove_member_consent.lua | 64 +++++++++++++++ modpol_core/modules/remove_org.lua | 2 +- modpol_core/modules/remove_org_consent.lua | 29 ++++--- modpol_core/modules/rename_org_consent.lua | 77 +++++++++++++++++++ modpol_core/modules/template.lua | 28 ++++--- modpol_minetest/chatcommands.lua | 52 +++---------- modpol_minetest/overrides/interactions.lua | 58 +++++++------- 15 files changed, 371 insertions(+), 130 deletions(-) rename modpol_core/modules/{add_child_org.lua => add_child_org_consent.lua} (54%) create mode 100644 modpol_core/modules/remove_child_consent.lua create mode 100644 modpol_core/modules/remove_member_consent.lua create mode 100644 modpol_core/modules/rename_org_consent.lua diff --git a/README.md b/README.md index 867df4e..640d943 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,9 @@ For background information, documentation, and the project roadmap, see [the wik To use this in Minetest, simply install it in your `mods/` or `worldmods/` folder. Minetest will load `init.lua`. -In the game, open the Modular Politics interface with the command `/modpol`. +In the game, open the Modular Politics dashboard with the command `/mp`. + +For testing purposes, players with the `privs` privilege (generally admins) can use the `/mp` command, which resets all the orgs and opens a dashboard. ## Standalone Version on the Command Line diff --git a/modpol_core/api.lua b/modpol_core/api.lua index 45029c1..4f9bca6 100644 --- a/modpol_core/api.lua +++ b/modpol_core/api.lua @@ -11,9 +11,12 @@ dofile (localdir .. "/interactions/interactions.lua") --modules --TODO make this automatic and directory-based -dofile (localdir .. "/modules/add_child_org.lua") +dofile (localdir .. "/modules/add_child_org_consent.lua") dofile (localdir .. "/modules/consent.lua") dofile (localdir .. "/modules/join_org_consent.lua") dofile (localdir .. "/modules/leave_org.lua") +dofile (localdir .. "/modules/remove_child_consent.lua") +dofile (localdir .. "/modules/remove_member_consent.lua") dofile (localdir .. "/modules/remove_org_consent.lua") dofile (localdir .. "/modules/remove_org.lua") +dofile (localdir .. "/modules/rename_org_consent.lua") diff --git a/modpol_core/interactions/interactions.lua b/modpol_core/interactions/interactions.lua index 81de00a..664841b 100644 --- a/modpol_core/interactions/interactions.lua +++ b/modpol_core/interactions/interactions.lua @@ -48,10 +48,10 @@ end -- Function: modpol.interactions.org_dashboard --- Params: user (string), org_name (string) +-- Params: user (string), org_string (string or id) -- Output: Displays a menu of org-specific commands to the user -function modpol.interactions.org_dashboard(user, org_name) - local org = modpol.orgs.get_org(org_name) +function modpol.interactions.org_dashboard(user, org_string) + local org = modpol.orgs.get_org(org_string) if not org then return nil end -- identify parent @@ -72,11 +72,13 @@ function modpol.interactions.org_dashboard(user, org_name) -- list available modules local org_modules = {} for k,v in pairs(org.modules) do - table.insert(org_modules, v.slug) + if not v.hide then + table.insert(org_modules, v.slug) + end end - -- list pending actions - local process_msg = #org.processes .. " total actions" + -- list pending + local process_msg = #org.processes .. " total processes" if org.pending[user] then process_msg = process_msg .. " (" .. #org.pending[user] .. " pending)" else @@ -84,14 +86,14 @@ function modpol.interactions.org_dashboard(user, org_name) end -- set up output - print("Org: " .. org_name) + print("Org: " .. org.name) print("Parent: " .. parent) print("Members: " .. table.concat(org.members, ", ")) print("Children: " .. table.concat(children, ", ")) print("Modules: " .. table.concat(org_modules, ", ")) - print("Actions: " .. process_msg) + print("Pending: " .. process_msg) print() - print("Commands: (M)odules, (A)ctions") + print("Commands: (M)odules, (P)ending") local sel = io.read() print() @@ -114,7 +116,7 @@ function modpol.interactions.org_dashboard(user, org_name) elseif sel == 'a' or sel == 'A' then local processes = {} - print("All processes: (* indicates pending action)") + print("All processes: (* indicates pending)") for k,v in ipairs(org.processes) do local active = '' if org.pending[user] then @@ -135,7 +137,7 @@ function modpol.interactions.org_dashboard(user, org_name) end else print("Command not found") - modpol.interactions.org_dashboard(user, org_name) + modpol.interactions.org_dashboard(user, org.name) end end diff --git a/modpol_core/modules/add_child_org.lua b/modpol_core/modules/add_child_org_consent.lua similarity index 54% rename from modpol_core/modules/add_child_org.lua rename to modpol_core/modules/add_child_org_consent.lua index 933b087..e323e19 100644 --- a/modpol_core/modules/add_child_org.lua +++ b/modpol_core/modules/add_child_org_consent.lua @@ -1,20 +1,21 @@ ---- @module add_child_org +--- @module add_child_org_consent -- 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 with consent" +local add_child_org_consent = { + name = "Add child org (consent)", + slug = "add_child_org_consent", + desc = "Create a child org in the current one with member consent" } -add_child_org.data = { - child_name = "" +add_child_org_consent.data = { + child_name = "", + result = nil } -add_child_org.config = { +add_child_org_consent.config = { } -- @function initiate -function add_child_org:initiate(config, result) +function add_child_org_consent:initiate(result) modpol.interactions.text_query( self.initiator,"Child org name: ", function(input) @@ -22,8 +23,21 @@ function add_child_org:initiate(config, result) modpol.interactions.message( self.initiator, "No name entered for child org") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + self.org:delete_process(self.id) + if result then result() end + return + elseif modpol.orgs.get_org(input) then + modpol.interactions.message( + self.initiator, + "Org name already in use") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) self.org:delete_process(self.id) - return end + if result then result() end + return + end self.data.child_name = input modpol.interactions.message( self.initiator, @@ -47,14 +61,15 @@ function add_child_org:initiate(config, result) ) end -function add_child_org:create_child_org() +function add_child_org_consent:create_child_org() self.org:add_org(self.data.child_name, self.initiator) modpol.interactions.message_org( self.initiator, self.org.name, "Child org created: "..self.data.child_name) + if self.data.result then self.data.result() end self.org:delete_process(self.id) end --- (Required) Add to module table -modpol.modules.add_child_org = add_child_org +modpol.modules.add_child_org_consent = add_child_org_consent diff --git a/modpol_core/modules/consent.lua b/modpol_core/modules/consent.lua index e936b39..78deca1 100644 --- a/modpol_core/modules/consent.lua +++ b/modpol_core/modules/consent.lua @@ -2,9 +2,10 @@ -- A utility module for checking consent local consent = { - name = "Consent", + name = "Consent process utility", slug = "consent", - desc = "Other modules can use to implement consent based decision making", + desc = "A module other modules use for consent decisions", + hide = true } consent.data = { @@ -46,6 +47,8 @@ function consent:callback(member) self.org:wipe_pending_actions(self.id) self.org:delete_process(self.id) end + modpol.interactions.org_dashboard( + member, self.org.name) end ) end diff --git a/modpol_core/modules/join_org_consent.lua b/modpol_core/modules/join_org_consent.lua index e40e023..9bfa437 100644 --- a/modpol_core/modules/join_org_consent.lua +++ b/modpol_core/modules/join_org_consent.lua @@ -3,21 +3,30 @@ -- Depends on the Consent module. local join_org_consent = { - name = "Join this org", + name = "Join this org (consent)", slug = "join_org_consent", - desc = "Adds member with consent of all members." + desc = "Adds member with consent of all members" } join_org_consent.data = { + result = nil } join_org_consent.config = { } -function join_org_consent:initiate(result) - self.org:call_module( - "consent", - self.initiator, +function join_org_consent:initiate(result) + if self.org:has_member(self.initiator) then + modpol.interactions.message( + self.initiator, + "You are already a member of this org") + if result then result() end + self.org:delete_process(self.id) + else + self.data.result = result + self.org:call_module( + "consent", + self.initiator, { prompt = "Allow " .. self.initiator .. " to join?", votes_required = #self.org.members @@ -26,12 +35,17 @@ function join_org_consent:initiate(result) self:complete() end ) - if result then result() end + end end function join_org_consent:complete() - self.org:add_member(self.initiator) - print("Added " .. self.initiator .. " to the org.") + self.org:add_member(self.initiator) + modpol.interactions.message_org( + self.initiator,self.org.name, + "Consent reached: " .. self.initiator .. + " joined org " .. self.org.name) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) end modpol.modules.join_org_consent = join_org_consent diff --git a/modpol_core/modules/leave_org.lua b/modpol_core/modules/leave_org.lua index bbc0c42..d8e685d 100644 --- a/modpol_core/modules/leave_org.lua +++ b/modpol_core/modules/leave_org.lua @@ -4,7 +4,7 @@ local leave_org = { name = "Leave org", slug = "leave_org", - desc = "Leave this org" + desc = "Remove yourself from the current org" } leave_org.data = { @@ -19,9 +19,9 @@ leave_org.config = { -- @function initiate function leave_org:initiate(result) if self.org == modpol.instance then - modpol.interactions.message( - self.initiator, - "You cannot leave the root org") + modpol.interactions.message( + self.initiator, + "You cannot leave the root org") else self.org:remove_member(self.initiator) modpol.interactions.message_org( @@ -32,6 +32,7 @@ function leave_org:initiate(result) "You have left org " .. self.org.name) end if result then result() end + self.org:delete_process(self.id) end --- (Required) Add to module table diff --git a/modpol_core/modules/remove_child_consent.lua b/modpol_core/modules/remove_child_consent.lua new file mode 100644 index 0000000..3968b8b --- /dev/null +++ b/modpol_core/modules/remove_child_consent.lua @@ -0,0 +1,71 @@ +--- Remove child (consent) +-- A simple module that calls a consent process on an org to remove its child +-- Depends on the Consent module. + +local remove_child_consent = { + name = "Remove child (consent)", + slug = "remove_child_consent", + desc = "Removes a child org if all members of this org consent." +} + +remove_child_consent.data = { + result = nil, + child_to_remove = nil +} + +remove_child_consent.config = { +} + +function remove_child_consent:initiate(result) + local children = {} + for i,v in ipairs(self.org.children) do + local child = modpol.orgs.get_org(v) + if child then table.insert(children, child.name) end + end + -- Abort if no child orgs + if #children == 0 then + modpol.interactions.message( + self.initiator, + "Org has no children") + if result then result() end + self.org:delete_process(self.id) + else + self.data.result = result + modpol.interactions.dropdown_query( + self.initiator, + "Which child of org "..self.org.name.. + " do you want to remove?", + children, + function(input) + self.data.child_to_remove = modpol.orgs.get_org(input) + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Remove child org "..input.."?", + votes_required = #self.org.members + }, + function() + self:complete() + end) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end) + end +end + +function remove_child_consent:complete() + modpol.interactions.message_org( + self.initiator, self.data.child_to_remove.id, + "Removing org " .. self.data.child_to_remove.name .. + " by parent org consent") + modpol.interactions.message_org( + self.initiator, self.org.id, + "Consent reached: removing org " .. + self.data.child_to_remove.name) + self.data.child_to_remove:delete() + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +modpol.modules.remove_child_consent = remove_child_consent diff --git a/modpol_core/modules/remove_member_consent.lua b/modpol_core/modules/remove_member_consent.lua new file mode 100644 index 0000000..fc118e1 --- /dev/null +++ b/modpol_core/modules/remove_member_consent.lua @@ -0,0 +1,64 @@ +--- remove_member_consent +-- @module remove_member_consent + +local remove_member_consent = { + name = "Remove a member (consent)", + slug = "remove_member_consent", + desc = "Removes org member with consent of other members" +} + +remove_member_consent.data = { + member_to_remove = "", + result = nil +} + +remove_member_consent.config = { +} + +function remove_member_consent:initiate(result) + -- Abort if in root org + if self.org == modpol.instance then + modpol.interactions.message( + self.initiator, + "Members cannot be removed from the root org") + if result then result() end + self.org:delete_process(self.id) + else -- proceed if not root + self.data.result = result + modpol.interactions.dropdown_query( + self.initiator, + "Which member of org "..self.org.name.. + " do you want to remove?", + self.org.members, + function(input) + self.data.member_to_remove = input + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Remove "..input.. + " from org "..self.org.name.."?", + votes_required = #self.org.members - 1 + }, + function() + self:complete() + end) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end) + end +end + +function remove_member_consent:complete() + modpol.interactions.message_org( + self.initiator, self.org.id, + "Consent reached: removing ".. + self.data.member_to_remove.. + " from org "..self.org.name) + self.org:remove_member(self.data.member_to_remove) + self.org:delete_process(self.id) + if self.data.result then self.data.result() end +end + +--- (Required) Add to module table +modpol.modules.remove_member_consent = remove_member_consent diff --git a/modpol_core/modules/remove_org.lua b/modpol_core/modules/remove_org.lua index 23fc07e..b2118d1 100644 --- a/modpol_core/modules/remove_org.lua +++ b/modpol_core/modules/remove_org.lua @@ -18,7 +18,7 @@ function remove_org:initiate(result) if self.org == modpol.instance then modpol.interactions.message( self.initiator, - "You cannot remove the root org") + "Cannot remove the root org") else modpol.interactions.message_org( self.initiator,self.org.id, diff --git a/modpol_core/modules/remove_org_consent.lua b/modpol_core/modules/remove_org_consent.lua index d5f5973..f9e8513 100644 --- a/modpol_core/modules/remove_org_consent.lua +++ b/modpol_core/modules/remove_org_consent.lua @@ -3,19 +3,28 @@ -- Depends on the Consent module. local remove_org_consent = { - name = "Remove this org", + name = "Remove this org (consent)", slug = "remove_org_consent", desc = "Removes an org if all members consent." } remove_org_consent.data = { + result = nil } remove_org_consent.config = { } -function remove_org_consent:initiate(result) - self.org:call_module( +function remove_org_consent:initiate(result) + if self.org == modpol.instance then + modpol.interactions.message( + self.initiator, + "Cannot remove root org") + if result then result() end + self.org:delete_process(self.id) + else + self.data.result = result + self.org:call_module( "consent", self.initiator, { @@ -25,17 +34,19 @@ function remove_org_consent:initiate(result) function () self:complete() end - ) - modpol.interactions.org_dashboard( - self.initiator, self.org.name) - if result then result() end + ) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end end function remove_org_consent:complete() modpol.interactions.message_org( self.initiator, self.org.id, - "Removing org: " .. self.org.name) - self.org:delete() + "Consent reached: removing org " .. self.org.name) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) + self.org:delete() end modpol.modules.remove_org_consent = remove_org_consent diff --git a/modpol_core/modules/rename_org_consent.lua b/modpol_core/modules/rename_org_consent.lua new file mode 100644 index 0000000..1b9aa47 --- /dev/null +++ b/modpol_core/modules/rename_org_consent.lua @@ -0,0 +1,77 @@ +--- Rename org (consent) +-- A simple module that calls a consent process on an org to rename it. +-- Depends on the Consent module. + +local rename_org_consent = { + name = "Rename this org (consent)", + slug = "rename_org_consent", + desc = "Renames an org if all members consent." +} + +rename_org_consent.data = { + result = nil, + new_name = nil +} + +rename_org_consent.config = { +} + +function rename_org_consent:initiate(result) + modpol.interactions.text_query( + self.initiator,"New org name: ", + function(input) + if input == "" then + modpol.interactions.message( + self.initiator, + "No name entered for child org") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + self.org:delete_process(self.id) + if result then result() end + return + elseif modpol.orgs.get_org(input) then + modpol.interactions.message( + self.initiator, + "Org name already in use") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + self.org:delete_process(self.id) + if result then result() end + return + end + self.data.new_name = input + modpol.interactions.message( + self.initiator, + "Proposed to change name of org " .. + self.org.name .. " to " .. input) + -- initiate consent process + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Change name of org " .. + self.org.name .. " to " .. input .. "?", + votes_required = #self.org.members + }, + function() + self:complete() + end + ) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end + ) +end + +function rename_org_consent:complete() + modpol.interactions.message_org( + self.initiator, + self.org.name, + "Changing name of org " .. self.org.name .. + " to " .. self.data.new_name) + self.org.name = self.data.new_name + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +modpol.modules.rename_org_consent = rename_org_consent diff --git a/modpol_core/modules/template.lua b/modpol_core/modules/template.lua index 1232e52..ec20f7e 100644 --- a/modpol_core/modules/template.lua +++ b/modpol_core/modules/template.lua @@ -1,20 +1,22 @@ ---- module_template --- @module module_template +--- change_modules +-- @module change_modules --- (Required): data table containing name and description of the module -- @field name "Human-readable name" -- @field slug "Same as module class name" -- @field desc "Description of the module" -local module_template = { +-- @field hide "Whether this is a hidden utility module" +local change_modules = { name = "Module Human-Readable Name", slug = "template", - desc = "Description of the module" + desc = "Description of the module", + hide = false; } --- (Required) Data for module -- Variables that module uses during the course of a process -- Can be blank -module_template.data = { +change_modules.data = { } --- (Required): config for module @@ -25,7 +27,7 @@ module_template.data = { -- Default values set in config can be overridden -- @field field_1 ex: votes_required, default = 5 -- @field field_2 ex: voting_type, default = "majority" -module_template.config = { +change_modules.config = { field_1 = 5 field_2 = "majority" } @@ -37,12 +39,16 @@ module_template.config = { --
  • self.id (the process id of the module instance)
  • -- @param result (optional) Callback if this module is embedded in other modules -- @function initiate -function module_template:initiate(result) - -- call interaction functions here! +function change_modules:initiate(result) + -- call interaction functions here! - -- call result function - if result then result() end + -- concluding functions: + -- call these wherever process might end; + -- may need to put result in self.data.result + -- if process ends in another function + if result then result() end + self.org:delete_process(self.id) end --- (Required) Add to module table -modpol.modules.module_template = module_template +modpol.modules.change_modules = change_modules diff --git a/modpol_minetest/chatcommands.lua b/modpol_minetest/chatcommands.lua index 33c0ca6..103de31 100644 --- a/modpol_minetest/chatcommands.lua +++ b/modpol_minetest/chatcommands.lua @@ -15,10 +15,10 @@ regchat = function(name, command_table) end -- =================================================================== --- /modpol +-- /mp -- Presents a menu of options to users regchat( - "modpol", { + "mp", { privs = {}, func = function(user) modpol.interactions.dashboard(user) @@ -26,14 +26,17 @@ regchat( }) -- =================================================================== --- /reset --- For testing only +-- /mptest +-- For testing only, accessible to admin users -- Clears the system and recreates instance with all players +-- opens dashboard too for fun. regchat( - "reset", { - privs = {}, + "mptest", { + privs = {privs=true}, func = function(user) - modpol.orgs.reset(); + modpol.orgs.reset() + modpol.instance:add_member(user) + modpol.interactions.dashboard(user) return true, "Reset orgs" end, }) @@ -71,38 +74,3 @@ regchat( end }) --- =================================================================== --- /listplayers -regchat( - "listplayers", { - privs = {}, - func = function(user) - local result = table.concat(modpol.list_users(),", ") - return true, "All players: " .. result - end, -}) - --- =================================================================== --- /joinorg -regchat( - "joinorg", { - privs = {}, - func = function(user, param) - local org = modpol.orgs.get_org(param) - local success, result = org:add_member(user) - return true, result - end, -}) - - --- =================================================================== --- /pollself [question] --- asks the user a question specified in param -regchat( - "pollself", { - privs = {}, - func = function(user, param) - modpol.interactions.binary_poll_user(user, param) - return true, result - end, -}) diff --git a/modpol_minetest/overrides/interactions.lua b/modpol_minetest/overrides/interactions.lua index a4a5d2a..3000f15 100644 --- a/modpol_minetest/overrides/interactions.lua +++ b/modpol_minetest/overrides/interactions.lua @@ -51,15 +51,15 @@ function modpol.interactions.dashboard(user) -- set up formspec local formspec = { "formspec_version[4]", - "size[10,8]", - "label[0.5,0.5;MODPOL DASHBOARD]", + "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)..";;]", "label[0.5,3;Your orgs:]", "dropdown[2,2.5;5,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)..";;]", - "button_exit[8.5,7;1,0.8;close;Close]", + "button_exit[8.5,5;1,0.8;close;Close]", } local formspec_string = table.concat(formspec, "") -- present to player @@ -85,11 +85,11 @@ end) -- Function: modpol.interactions.org_dashboard --- Params: user (string), org_name (string) +-- Params: user (string), org_string (string or num) -- Output: Displays a menu of org-specific commands to the user -function modpol.interactions.org_dashboard(user, org_name) +function modpol.interactions.org_dashboard(user, org_string) -- prepare data - local org = modpol.orgs.get_org(org_name) + local org = modpol.orgs.get_org(org_string) if not org then return nil end local function membership_toggle(org_display) @@ -98,9 +98,8 @@ function modpol.interactions.org_dashboard(user, org_name) if current_org:has_member(user) then return " (member)" end - else - return "" end + return "" end -- identify parent @@ -119,32 +118,36 @@ function modpol.interactions.org_dashboard(user, org_name) local modules = {"View..."} if org.modules then for k,v in pairs(org.modules) do - table.insert(modules, v.slug) + if not v.hide then -- hide utility modules + local module_entry = v.name.. + " ["..v.slug.."]" + table.insert(modules, module_entry) + end end end - -- prepare actions menu - local actions = {"View..."} - local num_actions = 0 + -- prepare pending menu + local pending = {"View..."} + local num_pending = 0 if org.pending[user] then for k,v in pairs(org.pending[user]) do - local action_string = "[" .. k .. "] " .. - org.processes[k].name - table.insert(actions, action_string) - num_actions = num_actions + 1 + local pending_string = org.processes[k].name + .." ["..k.."]" + table.insert(pending, pending_string) + num_pending = num_pending + 1 end end -- set player context local user_context = {} - user_context["current_org"] = org_name + user_context["current_org"] = org.name _contexts[user] = user_context -- set up formspec local formspec = { "formspec_version[4]", "size[10,8]", "label[0.5,0.5;Org: ".. - minetest.formspec_escape(org_name)..membership_toggle(org_name).."]", + 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)..";;]", @@ -152,8 +155,8 @@ function modpol.interactions.org_dashboard(user, org_name) "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]", "label[0.5,4;Modules:]", "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]", - "label[0.5,5;Actions ("..num_actions.."):]", - "dropdown[2,4.5;5,0.8;actions;"..formspec_list(actions)..";;]", + "label[0.5,5;Pending ("..num_pending.."):]", + "dropdown[2,4.5;5,0.8;pending;"..formspec_list(pending)..";;]", "button[8.5,7;1,0.8;back;Back]", } local formspec_string = table.concat(formspec, "") @@ -179,7 +182,8 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) -- Receiving modules elseif fields.modules and fields.modules ~= "View..." then - local module = fields.modules + local module = string.match( + fields.modules,"%[(.*)%]") modpol.interactions.binary_poll_user( pname, modpol.modules[module].name.."\n".. @@ -191,12 +195,12 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) end end) - -- Receiving actions - elseif fields.actions - and fields.actions ~= "View..." then - local action = string.match( - fields.actions,"%[(%d)%]") - local process = org.processes[tonumber(action)] + -- Receiving pending + elseif fields.pending + and fields.pending ~= "View..." then + local pending = string.match( + fields.pending,"%[(%d)%]") + local process = org.processes[tonumber(pending)] if process then org:interact(process.id, pname) end From a59ad2237003adeca81a4cd3ad96586f90576563 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sun, 19 Dec 2021 16:42:34 -0700 Subject: [PATCH 37/39] Updated documentation --- README.md | 22 +++++++++++++++++----- modpol_core/modules/template.lua | 14 +++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 640d943..05c4adf 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ # Modular Politics for Minetest -This is a mod for [Minetest](https://minetest.net) that enables diverse governance mechanisms. It seeks to implement the [Modular Politics](https://metagov.org/modpol) proposal. In the future, it will be possible to use this framework to simulate governance in a number of platform contexts. +Modular Politics is an extension that enables diverse governance processes on multi-user platforms. It seeks to implement a theoretical framework, also called [Modular Politics](https://metagov.org/modpol), which proposes these design goals: -This mod produces an API that can serve as a dependency for other mods that add specific governance functionalities. +* *Modularity*: Platform operators and community members should have the ability to construct systems by creating, importing, and arranging composable parts together as a coherent whole. +* *Expressiveness*: The governance layer should be able to implement as wide a range of processes as possible. +* *Portability*: Governance tools developed for one platform should be portable to another platform for reuse and adaptation. +* *Interoperability*: Governance systems operating on different platforms and protocols should have the ability to interact with each other, sharing data and influencing each other's processes. + +Modular Politics produces a library that enables users to create or adapt their own modules that add specific governance functionalities. + +This implementation is a mod for [Minetest](https://minetest.net), a free/open-source voxel game. It is designed to be easily adapted to other multi-user platforms that also employ Lua as an extension language. -For background information, documentation, and the project roadmap, see [the wiki](https://gitlab.com/medlabboulder/modpol/-/wikis/home). ## Installation in Minetest @@ -52,9 +58,15 @@ Other contributors include: * Robert Kiraly [[OldCoder](https://github.com/oldcoder/)] (ocutils.lua, storage-local.lua, project refactoring) * Skylar Hew (documentation) -We'd love to welcome more contributors, particularly from the Minetest community! Please join the conversation in the [Issues](https://gitlab.com/medlabboulder/modpol/-/issues) or the [Minetest.net forum](https://forum.minetest.net/viewtopic.php?f=47&t=26037). +We are grateful for initial support for this project from a residency with [The Bentway Conservancy](https://www.thebentway.ca/). Read about us in _[The Field Guide to Digital and/as Public Space](https://www.thebentway.ca/stories/field-guide/)_. + + +## Contributing + +We'd love to welcome more contributors. Please join the conversation in the [Issues](https://gitlab.com/medlabboulder/modpol/-/issues), our [Matrix.org channel](https://matrix.to/#/#minetest-modpol:matrix.org), and the [Minetest.net forum](https://forum.minetest.net/viewtopic.php?f=47&t=26037). + +Learn more about the project and how to develop your own modules in [the wiki](https://gitlab.com/medlabboulder/modpol/-/wikis/home). -We are grateful for support for this project from a residency with [The Bentway Conservancy](https://www.thebentway.ca/). ## Licenses diff --git a/modpol_core/modules/template.lua b/modpol_core/modules/template.lua index ec20f7e..91773e7 100644 --- a/modpol_core/modules/template.lua +++ b/modpol_core/modules/template.lua @@ -1,12 +1,12 @@ ---- change_modules --- @module change_modules +--- module_template +-- @module module_template --- (Required): data table containing name and description of the module -- @field name "Human-readable name" -- @field slug "Same as module class name" -- @field desc "Description of the module" -- @field hide "Whether this is a hidden utility module" -local change_modules = { +local module_template = { name = "Module Human-Readable Name", slug = "template", desc = "Description of the module", @@ -16,7 +16,7 @@ local change_modules = { --- (Required) Data for module -- Variables that module uses during the course of a process -- Can be blank -change_modules.data = { +module_template.data = { } --- (Required): config for module @@ -27,7 +27,7 @@ change_modules.data = { -- Default values set in config can be overridden -- @field field_1 ex: votes_required, default = 5 -- @field field_2 ex: voting_type, default = "majority" -change_modules.config = { +module_template.config = { field_1 = 5 field_2 = "majority" } @@ -39,7 +39,7 @@ change_modules.config = { --
  • self.id (the process id of the module instance)
  • -- @param result (optional) Callback if this module is embedded in other modules -- @function initiate -function change_modules:initiate(result) +function module_template:initiate(result) -- call interaction functions here! -- concluding functions: @@ -51,4 +51,4 @@ function change_modules:initiate(result) end --- (Required) Add to module table -modpol.modules.change_modules = change_modules +modpol.modules.module_template = module_template From 1d6d0f21e28d9760220400913ad20f78aaaaf880 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sun, 19 Dec 2021 17:20:30 -0700 Subject: [PATCH 38/39] Added more conceptual background to README --- README.md | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 05c4adf..2e9cc95 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,25 @@ # Modular Politics for Minetest -Modular Politics is an extension that enables diverse governance processes on multi-user platforms. It seeks to implement a theoretical framework, also called [Modular Politics](https://metagov.org/modpol), which proposes these design goals: +Modular Politics is an extension that enables diverse governance processes on multi-user platforms. It offers a library that enables users to create or adapt their own modules that add specific governance functionalities. + +This implementation is a mod for [Minetest](https://minetest.net), a free/open-source voxel game. It is designed to be easily adapted to other multi-user platforms that also employ Lua as an extension language. + +## Design philosophy + +Modular Politics seeks to implement a theoretical framework, also called "[modular politics](https://metagov.org/modpol)," which proposes these design goals: * *Modularity*: Platform operators and community members should have the ability to construct systems by creating, importing, and arranging composable parts together as a coherent whole. * *Expressiveness*: The governance layer should be able to implement as wide a range of processes as possible. * *Portability*: Governance tools developed for one platform should be portable to another platform for reuse and adaptation. * *Interoperability*: Governance systems operating on different platforms and protocols should have the ability to interact with each other, sharing data and influencing each other's processes. -Modular Politics produces a library that enables users to create or adapt their own modules that add specific governance functionalities. +Additionally, Modular Politics seeks to counteract the tendency for "[implicit feudalism](https://ntnsndr.in/ImplicitFeudalism)," according to which rigid, top-down power structures are the norm in online spaces. To this end, some design patterns include: -This implementation is a mod for [Minetest](https://minetest.net), a free/open-source voxel game. It is designed to be easily adapted to other multi-user platforms that also employ Lua as an extension language. +* *Groups, not roles*: While most platforms assign powers through particular permissions given to individuals, in Modular Politics, power lies in groups (which Modular Politics calls "orgs"). +* *Consent, not oligarchy*: Rather than assuming that decisions will be made by a few power-holders, the software assumes that consent by all affected users is the norm. +* *Inheritance, not blank slates*: When a new group is formed, it inherits the patterns of what preceded it, rather than imagining that it is starting from scratch. +It is certainly possible to use Modular Politics to replicate practices of implicit feudalism, such as all-powerful admins, but doing so requires extra work to overcome these defaults. ## Installation in Minetest @@ -49,12 +58,12 @@ Another storage method may be chosen in `modpol.lua`. A StorageRef-based method ## Credits -Initiated by [Nathan Schneider](https://nathanschneider.info) of the [Media Enterprise Design Lab](https://colorado.edu/lab/medlab) at the University of Colorado Boulder, as part of the [Metagovernance Project](https://metagov.org). Based on the paper "[Modular Politics: Toward a Governance Layer for Online Communities](https://metagov.org/modpol)." +This project is led by [Nathan Schneider](https://nathanschneider.info) of the [Media Enterprise Design Lab](https://colorado.edu/lab/medlab) at the University of Colorado Boulder, as part of the [Metagovernance Project](https://metagov.org). -Other contributors include: +Contributors include: -* [Luke Miller](https://gitlab.com/lukvmil) (main control flow, object orientation, module spec) -* [MisterE](https://gitlab.com/gbrrudmin) (project refactoring, core feature development) +* [Luke Miller](https://gitlab.com/lukvmil) (co-leadership, main control flow, object orientation, module spec) +* [MisterE](https://gitlab.com/gbrrudmin) (early project refactoring, core feature development) * Robert Kiraly [[OldCoder](https://github.com/oldcoder/)] (ocutils.lua, storage-local.lua, project refactoring) * Skylar Hew (documentation) From 2365eb83d050c4b2ed630ab97b44164cf9fd3719 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sun, 19 Dec 2021 23:23:23 -0700 Subject: [PATCH 39/39] 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)