Got modules basically working in CLI and Minetest, still having issues with Minetest interactions in module:initiate()

This commit is contained in:
Nathan Schneider committed 2021-12-18 13:35:18 -07:00
1 parent 4411c01ebb
commit 721eb26777
12 files changed
+187 -240

No files matched your search

+3 -1
View File
@@ -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")
+13 -6
View File
@@ -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)
+29
View File
@@ -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
-32
View File
@@ -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)
+30 -20
View File
@@ -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
+7 -3
View File
@@ -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",
+14 -9
View File
@@ -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
-- <li><code>self.org</code> (the org the module was called in),</li>
-- <li><code>self.initiator</code> (the user that callced the module),</li>
-- <li><code>self.id</code> (the process id of the module instance)</li>
-- @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
+18 -10
View File
@@ -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
+20 -12
View File
@@ -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
+5 -1
View File
@@ -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)
+20 -19
View File
@@ -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