Major refactoring (big thanks to OldCoder) enabling CLI and local storage and cleaner modpol/MT split
This commit is contained in:
363
init.lua
363
init.lua
@ -1,227 +1,45 @@
|
||||
--[[ INITIALIZING: basics ]]--
|
||||
-- ===================================================================
|
||||
-- /init.lua
|
||||
-- Modular Politics (modpol) for Minetest
|
||||
-- TKTK Maybe make this just a quick ref file and locate MT files elsewhere?
|
||||
-- TKTK need to add player to orgs.instance with on_joinplayer
|
||||
|
||||
-- global API table
|
||||
modpol = {
|
||||
}
|
||||
-- ===================================================================
|
||||
-- Load modpol system
|
||||
|
||||
-- table for all active governance data
|
||||
modpol.orgs = {
|
||||
}
|
||||
dofile(minetest.get_modpath("modpol") .. "/modpol.lua")
|
||||
|
||||
-- record of governance interactions
|
||||
-- every state change should appear here
|
||||
modpol.ledger = {
|
||||
}
|
||||
-- ===================================================================
|
||||
-- Modular Politics functions
|
||||
-- Overwriting default API functions with platform-specific ones
|
||||
-- ===================================================================
|
||||
|
||||
-- update from mod_storage
|
||||
-- https://dev.minetest.net/StorageRef
|
||||
local mod_storage = minetest.get_mod_storage()
|
||||
-- load orgs
|
||||
local stored_orgs = minetest.deserialize(mod_storage:get_string("orgs"))
|
||||
if (stored_orgs ~= nil) then
|
||||
modpol.orgs = stored_orgs
|
||||
end
|
||||
-- load orgs
|
||||
local stored_ledger = minetest.deserialize(mod_storage:get_string("ledger"))
|
||||
if (stored_ledger ~= nil) then
|
||||
modpol.ledger = stored_ledger
|
||||
end
|
||||
|
||||
|
||||
--[[ FUNCTIONS:basics ]]--
|
||||
|
||||
-- record(message, org)
|
||||
-- writes all governance events to storage and ledger
|
||||
function modpol.record(message, org)
|
||||
-- record to ledger
|
||||
table.insert(modpol.ledger, message)
|
||||
-- record to org_ledger
|
||||
if (modpol.orgs[org] ~= nil) then
|
||||
local org_ledg = modpol.orgs[org]["ledger"]
|
||||
if (org_ledg == nil) then
|
||||
modpol.orgs[org]["ledger"] = {message}
|
||||
else
|
||||
modpol.orgs[org]["ledger"] = table.insert(org_ledg,message)
|
||||
end
|
||||
end
|
||||
-- record to storage
|
||||
mod_storage:set_string("orgs", minetest.serialize(modpol.orgs))
|
||||
mod_storage:set_string("ledger", minetest.serialize(modpol.ledger))
|
||||
end
|
||||
|
||||
--[[ FUNCTIONS:orgs ]]--
|
||||
|
||||
-- new_org()
|
||||
-- create an org and add it to the list
|
||||
function modpol.new_org(orgName, orgMembers)
|
||||
if (orgName == "") then -- blank orgName input
|
||||
return "-!- Org needs a name"
|
||||
end
|
||||
if (modpol.orgs[orgName] == nil) then -- copy check
|
||||
modpol.orgs[orgName] = {members = orgMembers}
|
||||
else return "-!- Org already exists"
|
||||
end
|
||||
local message = "New org: " .. orgName ..
|
||||
" (" .. table.concat(orgMembers, ", ") .. ")"
|
||||
modpol.record(message, orgName)
|
||||
return message
|
||||
end
|
||||
|
||||
-- /neworg [name]
|
||||
-- creates a new org with the current user as sole member
|
||||
minetest.register_chatcommand(
|
||||
"neworg", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
local result =
|
||||
modpol.new_org(param,{user})
|
||||
return true, result
|
||||
end,
|
||||
})
|
||||
|
||||
-- rename_org()
|
||||
function modpol.rename_org(oldName, newName)
|
||||
-- TKTK
|
||||
local message = "Org renamed: " .. oldName .. " > " .. newName
|
||||
modpol.record(message, newName)
|
||||
end
|
||||
|
||||
|
||||
-- /listorgs
|
||||
-- lists the orgs currently in the game
|
||||
minetest.register_chatcommand(
|
||||
"listorgs", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
local orglist = ""
|
||||
for key, value in pairs(modpol.orgs) do
|
||||
-- first, set up member list
|
||||
local membs = modpol.orgs[key]["members"]
|
||||
if (membs == nil) then membs = ""
|
||||
else membs = " (" .. table.concat(membs, ", ") .. ")"
|
||||
end
|
||||
-- now, assemble the list
|
||||
if (orglist == "") -- first element only
|
||||
then orglist = key .. membs
|
||||
else orglist = orglist .. ", " .. key .. membs
|
||||
end
|
||||
end
|
||||
return true, "Orgs: " .. orglist
|
||||
end,
|
||||
})
|
||||
|
||||
-- rm_orgs()
|
||||
-- removes all orgs
|
||||
function modpol.rm_orgs()
|
||||
modpol.orgs["instance"] = {members = modpol.list_members()}
|
||||
local message = "Orgs purged"
|
||||
modpol.record(message, nil)
|
||||
return message
|
||||
end
|
||||
|
||||
-- /rmorgs
|
||||
minetest.register_chatcommand(
|
||||
"rmorgs", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
modpol.orgs = {}
|
||||
return true, modpol.rm_orgs()
|
||||
end,
|
||||
})
|
||||
|
||||
--[[ FUNCTIONS:users ]]--
|
||||
|
||||
-- list_members(domain)
|
||||
-- produces a table with names of players currently in the game
|
||||
-- if empty, lists all players; if an org name, lists its members
|
||||
function modpol.list_members(domain)
|
||||
local members = {}
|
||||
if (domain == nil) then -- no specified domain; all players
|
||||
-- ===================================================================
|
||||
-- Function: modpol.list_users(org)
|
||||
-- Overwrites function at /users.lua
|
||||
-- Params:
|
||||
-- if nil, lists instance members; if an org name, lists its members
|
||||
-- Output: a table with names of players currently in the game
|
||||
modpol.list_users = function(org)
|
||||
local users = {}
|
||||
if (org == nil) then -- no specified org; all players
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
local name = player:get_player_name()
|
||||
table.insert(members,name)
|
||||
table.insert(users,name)
|
||||
end
|
||||
else -- if an org is specified
|
||||
if (modpol.orgs[domain] ~= nil) then -- org exists
|
||||
members = modpol.orgs[domain]["members"]
|
||||
if (modpol.orgs[org] ~= nil) then -- org exists
|
||||
users = modpol.orgs[org]["members"]
|
||||
end
|
||||
end
|
||||
return members
|
||||
end
|
||||
|
||||
-- /listplayers
|
||||
minetest.register_chatcommand(
|
||||
"listplayers", {
|
||||
privs = {},
|
||||
func = function(user)
|
||||
local result = table.concat(modpol.list_members(),", ")
|
||||
return true, "All players: " .. result
|
||||
end,
|
||||
})
|
||||
|
||||
function modpol.add_member(org, member)
|
||||
if (modpol.orgs.org == nil) then
|
||||
return "-!- No such org"
|
||||
else
|
||||
table.insert(modpol.orgs.org["members"], member)
|
||||
local message = member .. " added to org " .. org
|
||||
modpol.record(message, org)
|
||||
return message
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-- /joinorg
|
||||
minetest.register_chatcommand(
|
||||
"joinorg", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
local result = modpol.add_member(param, user)
|
||||
return true, result
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
function modpol.remove_member(org, member)
|
||||
-- remove from all child orgs also
|
||||
local message = member .. " removed from org " .. org
|
||||
modpol.record(message, org)
|
||||
return message
|
||||
end
|
||||
|
||||
-- /listmembers [org]
|
||||
-- lists the members of an org
|
||||
minetest.register_chatcommand(
|
||||
"listmembers", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
local orglist = modpol.list_members(param)
|
||||
return true, param .. ": " .. table.concat(orglist,", ")
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
-- PERMISSIONS FUNCTIONS
|
||||
|
||||
|
||||
-- PRIVILEGE FUNCTIONS
|
||||
-- Minetest-specific
|
||||
-- manages user privileges according to org membership
|
||||
|
||||
function modpol.assign_privilege(org, privilege)
|
||||
-- add privilege to all members of an org
|
||||
end
|
||||
|
||||
|
||||
function modpol.remove_privilege(org, privilege)
|
||||
-- remove privilege from all members of an org, unless they have it from other orgs
|
||||
end
|
||||
|
||||
--[[ USER INTERACTIONS ]]--
|
||||
|
||||
-- modpol.binary_poll_user(user, question)
|
||||
-- ===================================================================
|
||||
-- Function: modpol.binary_poll_user(user, question)
|
||||
-- Overwrites function at /interactions.lua
|
||||
-- presents a yes/no/abstain poll to a user, returns answer
|
||||
function modpol.binary_poll_user(user, question)
|
||||
modpol.binary_poll_user = function(user, question)
|
||||
-- set up formspec
|
||||
local text = "Poll: " .. question
|
||||
local formspec = {
|
||||
@ -238,6 +56,90 @@ function modpol.binary_poll_user(user, question)
|
||||
minetest.show_formspec(user, "modpol:binary_poll", formspec_string)
|
||||
end
|
||||
|
||||
-- ===================================================================
|
||||
-- Minetest commands
|
||||
-- ===================================================================
|
||||
|
||||
local chat_table -- MT chat command definitions table
|
||||
local regchat -- Chat-command registration function
|
||||
|
||||
regchat = minetest.register_chatcommand
|
||||
|
||||
-- ===================================================================
|
||||
-- /addorg /add_org
|
||||
-- This code defines a chat command which creates a new
|
||||
-- "org". Presently, the command makes the user the sole member of the
|
||||
-- "org".
|
||||
|
||||
chat_table = {
|
||||
privs = {} ,
|
||||
func = function (user, param)
|
||||
local result = modpol.add_org (param, { user })
|
||||
return true, result
|
||||
end
|
||||
}
|
||||
regchat ("addorg" , chat_table)
|
||||
regchat ("add_org" , chat_table)
|
||||
|
||||
-- ===================================================================
|
||||
-- /listorg /listorgs /list_org /list_orgs
|
||||
-- In Minetest mode, this code defines a chat command which lists the
|
||||
-- existing "orgs".
|
||||
-- The list shows one "org" per line in the following format:
|
||||
-- org_name (member, member, ...)
|
||||
|
||||
chat_table = {
|
||||
privs = {} ,
|
||||
func = function (user, param)
|
||||
return true, "Orgs:\n" .. modpol.list_orgs()
|
||||
end
|
||||
}
|
||||
|
||||
regchat ("listorg" , chat_table)
|
||||
regchat ("listorgs" , chat_table)
|
||||
regchat ("list_org" , chat_table)
|
||||
regchat ("list_orgs" , chat_table)
|
||||
|
||||
-- ===================================================================
|
||||
-- /listplayers
|
||||
minetest.register_chatcommand(
|
||||
"listplayers", {
|
||||
privs = {},
|
||||
func = function(user)
|
||||
local result = table.concat(modpol.list_users(),", ")
|
||||
return true, "All players: " .. result
|
||||
end,
|
||||
})
|
||||
|
||||
-- ===================================================================
|
||||
-- /joinorg
|
||||
minetest.register_chatcommand(
|
||||
"joinorg", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
local result = modpol.add_member(param, user)
|
||||
return true, result
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
-- ===================================================================
|
||||
-- /pollself [question]
|
||||
-- asks the user a question specified in param
|
||||
minetest.register_chatcommand(
|
||||
"pollself", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
modpol.binary_poll_user(user, param)
|
||||
return true, result
|
||||
end,
|
||||
})
|
||||
|
||||
-- ===================================================================
|
||||
-- Minetest events
|
||||
-- ===================================================================
|
||||
|
||||
-- ===================================================================
|
||||
-- Receiving fields
|
||||
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
||||
-- modpol:poll
|
||||
@ -258,42 +160,5 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
|
||||
end
|
||||
end)
|
||||
|
||||
-- /pollself [question]
|
||||
-- asks the user a question
|
||||
minetest.register_chatcommand(
|
||||
"pollself", {
|
||||
privs = {},
|
||||
func = function(user, param)
|
||||
modpol.binary_poll_user(user, param)
|
||||
return true, result
|
||||
end,
|
||||
})
|
||||
|
||||
--[[ TKTK need to enable more complex ineractions
|
||||
- checkboxes, radio
|
||||
- write-in
|
||||
]]--
|
||||
|
||||
-- MESSAGE FUNCTIONS
|
||||
|
||||
function modpol.org_message(org, speaker, message)
|
||||
-- If org doesn't exit, broadcast to all
|
||||
-- If org doesn't exist, don't broadcast
|
||||
-- use: minetest.chat_send_player("player1", "This is a chat message for player1")
|
||||
end
|
||||
-- register at chat command for this
|
||||
|
||||
|
||||
-- Add HUD interface that shows status: orgs, privileges https://rubenwardy.com/minetest_modding_book/en/players/hud.html
|
||||
|
||||
-- toggle it on and off
|
||||
|
||||
|
||||
|
||||
--[[ INITIALIZING: post-functions ]]--
|
||||
|
||||
-- create instance if not present
|
||||
if (modpol.orgs["instance"] == nil) then
|
||||
modpol.new_org("instance", modpol.list_members())
|
||||
end
|
||||
-- TKTK does this need to be on_joinplayer? still isn't adding the singleplayer
|
||||
-- ===================================================================
|
||||
-- End of file.
|
||||
|
Reference in New Issue
Block a user