diff --git a/.gitignore b/.gitignore index 249cda9..4ecb204 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/data \ No newline at end of file +*/data \ No newline at end of file diff --git a/modpol/interactions/interactions.lua b/modpol/interactions/interactions.lua index 9e64603..d51f47b 100644 --- a/modpol/interactions/interactions.lua +++ b/modpol/interactions/interactions.lua @@ -9,46 +9,40 @@ modpol.interactions = {} -- DASHBOARDS -- ========== -function modpol.interactions.login() - print("Log in as which user?") - local username = io.read() - - while true do - local org = modpol.interactions.dashboard(username) - if org then - modpol.interactions.org_dashboard(username, org) - else - break - end - end -end - -- 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 -- TKTK currently just prints all of modpol---needs major improvement function modpol.interactions.dashboard(user) - print() - local org_list = modpol.orgs.list_all() - - print('Select an org') - for i, org_name in ipairs(org_list) do - local org = modpol.orgs.get_org(org_name) - local indicator = "" - if org:has_pending_actions(user) then - indicator = "*" - end - print('['..i..']'..' '..org_name..indicator) + -- adds user to root org if not already in it + if not modpol.instance:has_member(user) then + modpol.instance:add_member(user) end + local all_users = modpol.list_users() + + print('All orgs: (user orgs indicated by *)') + for id, org in ipairs(modpol.orgs.array) do + if type(org) == "table" then + local indicator = "" + if org:has_member(user) then indicator = "*" end + print('['..id..'] '..indicator..org.name) + end + end + + print('All users: ' .. table.concat(all_users, ', ')) + print() + + print('Access which org?') + local sel = io.read() - local sel_org = org_list[tonumber(sel)] + print() + local sel_org = modpol.orgs.array[tonumber(sel)].name if not sel_org then return end modpol.interactions.org_dashboard(user, sel_org) - end @@ -56,49 +50,93 @@ end -- 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) - print() local org = modpol.orgs.get_org(org_name) if not org then return nil end - - + + local parent = "" + if org.id == 1 then + parent = "none" + else + parent = modpol.orgs.get_org(org.parent).name + end + 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 processes = {} - for k,v in ipairs(org.processes) do - print(k, v) - 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 - local req_str = v.id .. " (" .. this_request.type .. " -> " .. table.concat(this_request.params, ", ") .. ")" .. active - table.insert(processes, req_str) - end + local process_msg = #org.processes .. " total" + + 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: " .. table.concat(processes, ", ")) + print("Processes: " .. process_msg) + print() + print("Commands: (L)eave, (J)oin, (P)rocesses, (A)dd child, (D)elete org") - print('Interact with which process?') local sel = io.read() - local process = org.processes[tonumber(sel)] - if not process then return end - process:interact(user) + 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}}) + + 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 + 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() + print("Interact with which one?") + local to_interact = io.read() + local process = org.processes[tonumber(to_interact)] + if not process then return end + if org:has_pending_actions(user) then + if org.pending[user][process.id] then + process:interact(user) + end + end + end + + + + end -- Function: modpol.interactions.policy_dashboard diff --git a/modpol/interactions/login.lua b/modpol/interactions/login.lua new file mode 100644 index 0000000..efb0063 --- /dev/null +++ b/modpol/interactions/login.lua @@ -0,0 +1,7 @@ +dofile("../modpol.lua") + +print("Log in as which user?") +local username = io.read() + +print() +modpol.interactions.dashboard(username) \ No newline at end of file diff --git a/modpol/modules/consent.lua b/modpol/modules/consent.lua index 9be2b53..1cbcbf8 100644 --- a/modpol/modules/consent.lua +++ b/modpol/modules/consent.lua @@ -13,6 +13,7 @@ function temp_consent_process() request_id = nil, total_votes = 0, majority_to_pass = 0.51, + votes_needed = nil, votes_yes = {}, votes_no = {} } @@ -35,6 +36,8 @@ function modpol.modules.consent:new_process(id, request_id, org_id) 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 @@ -82,13 +85,11 @@ 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) - local eligible_voters = process_org:get_member_count() - local votes_needed = math.ceil(self.majority_to_pass * eligible_voters) - if #self.votes_yes >= votes_needed then + 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 >= votes_needed then + 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 diff --git a/modpol/orgs/requests.lua b/modpol/orgs/requests.lua index 06d2daf..aacc085 100644 --- a/modpol/orgs/requests.lua +++ b/modpol/orgs/requests.lua @@ -255,3 +255,18 @@ function modpol.orgs:make_request(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/users/users.lua b/modpol/users/users.lua index b4aa337..8c94eec 100644 --- a/modpol/users/users.lua +++ b/modpol/users/users.lua @@ -13,10 +13,10 @@ modpol.list_users = function(org) local users = {} if (org == nil) then -- no specified org; all players - if modpol.orgs["instance"] - and modpol.orgs["instance"]["members"] then + if modpol.instance + and modpol.instance.members then -- if instance exists and has membership - users = modpol.orgs["instance"]["members"] + users = modpol.instance.members else users = {} end