*Reorganized code so that further expansion is possible in a very organized manner.

*modpol.add_member() -- added a check if the member is already a member of that organized

*storage -- fixed bug by moving storageref to top of file

*added on_joinplayer to add players to instance

*moved minetest specific code in init.lua to modpol_minetest, organized in folders.
All overrides in one folder, all chatcommands in another, the on_joinplayer in modpol_minetest/orgs/instance.lua
This commit is contained in:
MisterE123
2021-02-10 23:40:28 -05:00
parent c05023ad20
commit f19796f82d
20 changed files with 395 additions and 286 deletions

15
modpol/api.lua Normal file
View File

@@ -0,0 +1,15 @@
--call all files in this directory
local localdir = modpol.get_script_dir() .. "/modpol"
--Users
dofile (localdir .. "/users/users.lua")
--orgs
dofile (localdir .. "/orgs/orgs.lua")
--interactions
dofile (localdir .. "/interactions/interactions.lua")
-- messaging functions
dofile (localdir .. "/processes/processes.lua")

View File

@@ -0,0 +1,25 @@
-- ===================================================================
-- /orgs.lua
-- User interaction functions for Modular Politics
-- Called by modpol.lua
-- ===================================================================
-- Function: modpol.binary_poll_user(user, question)
-- Params: user (string), question (string)
-- Output:
-- presents a yes/no/abstain poll to a user, returns answer
modpol.binary_poll_user = function(user, question)
local query = "Poll for " .. user .. " (y/n/a): ".. question
local answer
repeat
print(query)
answer = io.read()
until answer == "y" or answer == "n" or answer == "a"
if answer == "y" then
return "Yes"
elseif answer == "n" then
return "No"
else
return "Abstain"
end
end

194
modpol/orgs/orgs.lua Normal file
View File

@@ -0,0 +1,194 @@
-- ===================================================================
-- /orgs.lua
-- Org-related functions for Modular Politics
-- Called by modpol/modpol/api.lua
-- ===================================================================
-- Function: modpol.record
-- Params: strings msg, org
-- Outputs:
-- "msg" specifies an event and/or status message.
-- "org" specifies an "org" name.
-- 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.
modpol.record = function (org_name, msg)
-- Record in global ledger
table.insert (modpol.ledger, msg)
-- Record in "org"-specific ledger
if modpol.orgs [org_name] ~= nil then
local org_ledger = modpol.orgs [org_name]["ledger"]
if org_ledger == nil then
modpol.orgs [org_name]["ledger"] = { msg }
else
modpol.orgs [org_name]["ledger"] =
table.insert (org_ledger, msg)
end
end
modpol.store_data() -- Copy data to disk
end
-- ===================================================================
-- Function: modpol.add_org
-- Params: string name, table members
-- Output:
--
-- This function creates an "org". It returns either an error message
-- starting with "-!-" or a success message.
--
-- The string parameter specifies the "org" name.
--
-- The specified table should be an integer-indexed array of member
-- names.
modpol.add_org = function (org_name, members)
local str
if modpol.ocutil.str_empty (org_name) then
return "Error: Org needs a name"
end
modpol.ocutil.log ("Adding org " .. org_name)
if modpol.orgs [org_name] ~= nil then
str = "Error: Org " .. org_name .. " already exists"
modpol.ocutil.log (str)
return str
end
modpol.orgs [org_name] = { members=members }
local msg = "New org: " .. org_name ..
" (" .. table.concat (members, ", ") .. ")"
modpol.record (org_name, msg)
return msg
end
-- ===================================================================
-- 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.
modpol.list_orgs = function()
local outbuf = ""
local str
for org_name, org_data in pairs (modpol.orgs) do
-- Process next "org"
-- Build string version of member list
local memcat = org_data ["members"]
if modpol.ocutil.str_empty (memcat) then
memcat = "(empty)"
else
memcat = "(" .. table.concat (memcat, ", ") .. ")"
end
-- Build output string
outbuf = outbuf .. org_name .. " " .. memcat .. "\n"
end
return outbuf
end
-- ===================================================================
-- Function: modpol.reset_orgs
-- Params: None
-- Removes all orgs and recreates blank org "instance" with all
-- current users
-- returns confirmation message
modpol.reset_orgs = function()
local users = modpol.list_users()
modpol.orgs = {}
local message = "Orgs purged"
modpol.record(nil, message)
modpol.add_org("instance", users)
return message
end
-- ===================================================================
-- Function: modpol.add_member
-- Params: org (string), member (string)
-- Output:
-- Adds the specified member to the specified org
-- Returns confirmation or error message
modpol.add_member = function(org, member)
if (modpol.orgs[org] == nil) then
return "Error: No such org"
else
if modpol.is_member(org,member) then
return "Error: " .. member .. " is already a member of " .. org
else
table.insert(modpol.orgs[org]["members"], member)
local message = member .. " added to org " .. org
modpol.record(message, org)
return message
end
end
end
-- ===================================================================
-- Function: modpol.is_member
-- Params: org (string), member (string)
-- Output: Boolean depending on membership or nil/error message
modpol.is_member = function(org, member)
if (modpol.orgs[org] == nil) then
return nil, "Error: No such org"
else
local member_table = modpol.orgs[org]["members"]
if member_table then
for index, value in pairs(member_table) do
if value == member then
-- match found
return true
end
end
-- no match found
return false
end
return false
end
end
-- ===================================================================
-- Function: modpol.remove_member
-- Params: org (string), member (string)
-- Output:
-- Removes the specified member from the specified org
-- Returns confirmation or error message
function modpol.remove_member(org, member)
local message = "Error: No such org"
if (modpol.orgs[org] == nil) then
return nil, message
else
local member_table = modpol.orgs[org]["members"]
if member_table then
for index, value in pairs(member_table) do
if value == member then
-- match found, set to nil
value = nil
message = member .. " removed from org " .. org
return message
end
end
end
-- no match found (or org has no members)
message = member .. " not found in org " .. org
return nil, message
end
end
-- ===================================================================
-- TKTK:
-- + rename_org(old_name, new_name)
-- +
-- ===================================================================
-- End of file.

View File

@@ -0,0 +1,55 @@
-- ===================================================================
-- /processes.lua
-- Process-related functions for Modular Politics
-- Called by modpol.lua
-- TKTK may need to create high-level modpol.processes table to hold ongoing processes
-- TKTK in order to enable async processes. Rewrite all this as listeners
-- ===================================================================
-- Function: modpol.consent
-- Params: org (string), proposal (string)
-- Outputs: boolean - true if consent achieved or false; nil on error
-- Also includes a table of responses
-- This is the default decision-making routine for Modular Politics
-- Stops at the first "No" vote
modpol.consent = function(org, query)
-- Check that org exists
if modpol.orgs[org] == nil then
return nil, "Error: Org does not exist"
end
-- Poll all members
local responses = {}
for index, value in ipairs(modpol.orgs[org]["members"]) do
local response = modpol.binary_poll_user(value, query)
responses[value] = response
if response == "No" then
return false, responses
end
end
return true, responses
end
-- TKTK exploring modpol.initiate functions, which have no args
-- Need to properly document these
modpol.initiate = {}
modpol.initiate.consent = function(org)
print("What is your query?")
local query = io.read()
return modpol.consent(org, query)
end
-- Function: modpol.switchboard
-- Params: org (string), routine (string)
-- Outputs: Checks the org for any policies related to a given function;
-- Calls the modpol.initiate.* function(org) based on policy
-- Defaults to modpol.initiate.consent()
modpol.switchboard = function(org, routine)
if modpol.orgs[org]["policies"]
and modpol.orgs[org]["policies"][routine] then
-- TKTK if there exists a policy
else
modpol.initiate.consent(org)
end
end

29
modpol/users/users.lua Normal file
View File

@@ -0,0 +1,29 @@
-- ===================================================================
-- /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.orgs["instance"]
and modpol.orgs["instance"]["members"] then
-- if instance exists and has membership
users = modpol.orgs["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