*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

40
modpol_minetest/api.lua Normal file
View File

@ -0,0 +1,40 @@
--call all files in this directory
-- ===================================================================
-- Minetest Specific Overrides of modpol functions
-- ===================================================================
local localdir = modpol.get_script_dir() .. "/modpol_minetest"
--Users
dofile (localdir .. "/overrides/users/users.lua")
--orgs
--dofile (localdir .. "/overrides/orgs/orgs.lua")
--interactions
dofile (localdir .. "/overrides/interactions/interactions.lua")
-- messaging functions
-- dofile (localdir .. "/overrides/processes/processes.lua")
-- ===================================================================
-- Minetest Chatcommands
-- ===================================================================
dofile (localdir .. "/chatcommands/chatcommands.lua")
-- ===================================================================
-- Minetest Specific code
-- ===================================================================
-- orgs
-- ===================
dofile (localdir .. "/orgs/instance.lua") --add players to the instance when they join.

View File

@ -0,0 +1,78 @@
-- ===================================================================
-- 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,
})

View File

@ -0,0 +1,6 @@
--add members to the instance, if they are not already there.
minetest.register_on_joinplayer(function(player)
local p_name = player:get_player_name()
modpol.add_member("instance", p_name)
end)

View File

@ -0,0 +1,44 @@
-- ===================================================================
-- 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?
--TKTK we could use scroll boxes to contain the text
}
local formspec_string = table.concat(formspec, "")
-- present to player
minetest.show_formspec(user, "modpol:binary_poll", formspec_string)
end
--what to do
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)
--TKTK : we should send the message to all in that org, definately not to all players
end
minetest.close_formspec(pname, formname)
return vote
else -- if the form is not a recognized name
return
end
end)

View File

@ -0,0 +1,21 @@
-- ===================================================================
-- 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