*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

177
init.lua
View File

@ -1,3 +1,31 @@
modpol = {}
-- ===================================================================
--preoverrides: certain things must be predefined in the global table for them to have effect (or cmd line version defualts are used)
-- ===================================================================
-- ===================================================================
-- currently:
-- --get_script_dir: get modpol_minetest's version of directory
-- -- Persistent storage
-- -- -- must implement modpol.load_storage() and modpol.store_data()
-- -- -- defines the path for the lua file
-- get modpol_minetest's version of directory
modpol.get_script_dir = function()
return minetest.get_modpath("modpol")
end
-- TKTK: Implement minetest settingtypes for this... for now, the default is to use minetest mod storage.
-- However, the default for if modpol.lua is called from the cmd line is to use local storage
-- Any other implementation may with to make their own persistent storage file and declare it befoe calling modpol.lua in a similar manner
-- works with cmd line: "/storage/storage-local.lua", works with Minetest 5.0 and up: "/storage/storage-mod_storage.lua"
modpol.storage_file_path = minetest.get_modpath("modpol").."/storage/storage-mod_storage.lua"
-- ===================================================================
-- /init.lua
-- Modular Politics (modpol) for Minetest
@ -9,156 +37,11 @@
dofile(minetest.get_modpath("modpol") .. "/modpol.lua")
-- ===================================================================
-- Modular Politics functions
-- Overwriting default API functions with platform-specific ones
-- ===================================================================
-- ===================================================================
-- 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(users,name)
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
-- Modular Politics Minetest Specific Code
-- ===================================================================
-- Function: modpol.binary_poll_user(user, question)
-- Overwrites function at /interactions.lua
-- presents a yes/no/abstain poll to a user, returns answer
modpol.binary_poll_user = function(user, question)
-- set up formspec
local text = "Poll: " .. question
local formspec = {
"formspec_version[4]",
"size[5,3]",
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
"button[1,1.5;1,0.8;yes;Yes]",
"button[2,1.5;1,0.8;no;No]",
"button[3,1.5;1,0.8;abstain;Abstain]"
--TKTK can we enable text wrapping?
}
local formspec_string = table.concat(formspec, "")
-- present to player
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
if formname == "modpol:binary_poll" then
local pname = player:get_player_name()
local vote = nil
if fields.yes then vote = fields.yes
elseif fields.no then vote = fields.no
elseif fields.abstain then vote = fields.abstain
end
if vote then
minetest.chat_send_all(pname .. " voted " .. vote)
end
minetest.close_formspec(pname, formname)
return vote
else -- if the form is not a recognized name
return
end
end)
dofile(minetest.get_modpath("modpol") .. "/modpol_minetest/api.lua")
-- ===================================================================
-- End of file.