From a03528d8fefe5e1ec4c9b619b06c3539a03a9b04 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Sat, 18 Dec 2021 14:57:11 -0700 Subject: [PATCH] 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]",