From 4557e9c7d833af2ae014d2bba4b36f6487add537 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Sat, 10 Apr 2021 21:09:29 -0400 Subject: [PATCH] completed refactor --- modpol/orgs/new_orgs.lua | 99 +++++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/modpol/orgs/new_orgs.lua b/modpol/orgs/new_orgs.lua index 4d466da..586f656 100644 --- a/modpol/orgs/new_orgs.lua +++ b/modpol/orgs/new_orgs.lua @@ -9,16 +9,40 @@ modpol.orgs.__index = modpol.orgs -- ================================================== -- returns org when given its id - function modpol.orgs.get_org(id) return modpol.orgs.list[id] end +-- =============================================== +-- returns a string list of all orgs +function modpol.orgs.list_orgs() + local str + for k, v in ipairs(modpol.orgs.list) do + if type(v) == 'table' then + if str then + str = str .. '\n' .. v.name + else + str = v.name + end + end + end + return str +end + +-- =========================================== +-- deletes all orgs except for the instance +function modpol.orgs.reset() + for k, v in ipairs(modpol.orgs.list) do + if k > 1 then + modpol.orgs.list[k] = nil + end + end +end + -- =================================================== -- initializes the instance (root org) -- can only be run once, as only one instance can exist - -function modpol.orgs:init_instance() +function modpol.orgs.init_instance() local error_msg if modpol.orgs.list[1] then error_msg = 'Error: instance has already been initialized' @@ -44,9 +68,11 @@ function modpol.orgs:init_instance() return instance end + +-- FUNCTIONS BEYOND HERE OPERATE ON ORG OBJECTS + -- ======================================================= -- records a log message to the modpol ledger - function modpol.orgs:record(msg, entry_type) local entry = { timestamp = '', @@ -81,7 +107,6 @@ end -- ================================================== -- adds a new sub org to the org it is called on -- ex: instance:add_org('town hall') - function modpol.orgs:add_org(name) if self.id == nil then error_msg = 'Error: add_org can only be called by another org' @@ -119,8 +144,8 @@ end -- ======================================== -- recursively deletes an org and its suborgs +-- leaves entry in modpol.orgs.list as a string "removed" -- note: "reason" param was removed, can be added back - function modpol.orgs:delete() if self.id == 1 then return false, 'Error: cannot delete instance' @@ -134,15 +159,67 @@ function modpol.orgs:delete() end end - modpol.orgs.list[self.id] = nil + modpol.orgs.list[self.id] = 'removed' print('Removed ' .. self.name .. ': ' .. self.id) end -function modpol.orgs:add_member(user) - table.insert(self.members, user) + +-- =========================================== +-- internal function to get the index of a member name +function modpol.orgs:get_member_index(member) + for k, v in ipairs(self.members) do + if v == member then + return k + end + end end -function modpol.orgs:list_member() - print(self.members[1]) +-- =========================================== +-- adds a user to an org +function modpol.orgs:add_member(user) + -- trys to fill in empty spots first + empty_index = self:get_member_index('') + if empty_index then + self.members[empty_index] = user + else + -- adds to end if no empty spots + table.insert(self.members, user) + end +end + +-- ======================================= +-- removes a user from an org +function modpol.orgs:remove_member(user) + -- sets the array index to an empty string so that consecutive list is preserved + -- empty spots will get filled in by new members + user_index = self:get_member_index(user) + if user_index then + self.members[user_index] = '' + end +end + +-- =========================================== +-- boolean check whether user is an org +function modpol.orgs:has_member(user) + user_index = self:get_member_index(user) + if user_index then + return true + else + return false + end +end + +-- ================================== +-- returns a list of users in an org +function modpol.orgs:list_member() + local str + for k, v in ipairs(self.members) do + if str then + str = str .. '\n' .. v + else + str = v + end + end + return str end