-- =================================================================== -- /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 -- =================================================================== -- Load modpol system 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 members end -- =================================================================== -- 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) -- =================================================================== -- End of file.