2016-02-05 21:55:45 +00:00
|
|
|
--[[
|
|
|
|
RandomMessages mod by arsdragonfly.
|
|
|
|
arsdragonfly@gmail.com
|
|
|
|
6/19/2013
|
|
|
|
--]]
|
|
|
|
--Time between two subsequent messages.
|
|
|
|
local MESSAGE_INTERVAL = 0
|
|
|
|
|
|
|
|
math.randomseed(os.time())
|
|
|
|
|
|
|
|
random_messages = {}
|
|
|
|
random_messages.messages = {} --This table contains all messages.
|
|
|
|
|
|
|
|
function table.count( t )
|
|
|
|
local i = 0
|
|
|
|
for k in pairs( t ) do i = i + 1 end
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
|
|
|
function table.random( t )
|
|
|
|
local rk = math.random( 1, table.count( t ) )
|
|
|
|
local i = 1
|
|
|
|
for k, v in pairs( t ) do
|
|
|
|
if ( i == rk ) then return v, k end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.initialize() --Set the interval in minetest.conf.
|
|
|
|
minetest.setting_set("random_messages_interval",120)
|
|
|
|
minetest.setting_save();
|
|
|
|
return 120
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.set_interval() --Read the interval from minetest.conf(set it if it doesn'st exist)
|
|
|
|
MESSAGE_INTERVAL = tonumber(minetest.setting_get("random_messages_interval")) or random_messages.initialize()
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.check_params(name,func,params)
|
|
|
|
local stat,msg = func(params)
|
|
|
|
if not stat then
|
|
|
|
minetest.chat_send_player(name,msg)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.read_messages()
|
2016-02-15 01:27:20 +00:00
|
|
|
random_messages.messages = {
|
|
|
|
"To talk to only your team, start your messages with /t. For example, /t Hello team!",
|
|
|
|
"Eat apples to restore health quickly.",
|
2017-10-12 14:40:02 +00:00
|
|
|
"Steel swords do more damage than guns, but you need to be up close.",
|
2017-10-12 23:31:39 +00:00
|
|
|
"Gain more score by killing more than you die, or by capturing the flag.",
|
|
|
|
"You gain more score the better the opponent you defeat.",
|
2017-11-07 00:35:46 +00:00
|
|
|
"Players are immune for 10 seconds after they respawn.",
|
2018-01-09 03:00:29 +00:00
|
|
|
"Access the pro section of the chest by achieving a 1k+ score and killing 2 people for every death.",
|
2018-01-03 02:50:20 +00:00
|
|
|
"Use team doors (steel) to stop the enemy walking into your base.",
|
|
|
|
"Sprint by pressing the fast key (E) when you have stamina.",
|
2018-01-02 20:13:30 +00:00
|
|
|
"Like CTF? Give feedback using /report, and consider donating at rubenwardy.com/donate",
|
2016-02-15 01:27:20 +00:00
|
|
|
}
|
2016-02-05 21:55:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.display_message(message_number)
|
2016-02-14 19:28:45 +00:00
|
|
|
local msg = random_messages.messages[message_number] or message_number
|
|
|
|
if msg then
|
|
|
|
minetest.chat_send_all(msg)
|
2016-02-05 21:55:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.show_message()
|
|
|
|
random_messages.display_message(table.random(random_messages.messages))
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.list_messages()
|
|
|
|
local str = ""
|
|
|
|
for k,v in pairs(random_messages.messages) do
|
|
|
|
str = str .. k .. " | " .. v .. "\n"
|
|
|
|
end
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.remove_message(k)
|
|
|
|
table.remove(random_messages.messages,k)
|
|
|
|
random_messages.save_messages()
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.add_message(t)
|
|
|
|
table.insert(random_messages.messages,table.concat(t," ",2))
|
|
|
|
random_messages.save_messages()
|
|
|
|
end
|
|
|
|
|
|
|
|
function random_messages.save_messages()
|
2017-10-12 14:40:02 +00:00
|
|
|
local output = io.open(minetest.get_worldpath().."/random_messages","w")
|
|
|
|
for k,v in pairs(random_messages.messages) do
|
|
|
|
output:write(v .. "\n")
|
|
|
|
end
|
|
|
|
io.close(output)
|
2016-02-05 21:55:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--When server starts:
|
|
|
|
random_messages.set_interval()
|
|
|
|
random_messages.read_messages()
|
|
|
|
|
2018-01-02 20:13:30 +00:00
|
|
|
local function step(dtime)
|
|
|
|
random_messages.show_message()
|
|
|
|
minetest.after(MESSAGE_INTERVAL, step)
|
|
|
|
end
|
|
|
|
minetest.after(MESSAGE_INTERVAL, step)
|
2016-02-05 21:55:45 +00:00
|
|
|
|
|
|
|
local register_chatcommand_table = {
|
|
|
|
params = "viewmessages | removemessage <number> | addmessage <number>",
|
|
|
|
privs = {server = true},
|
|
|
|
description = "View and/or alter the server's random messages",
|
|
|
|
func = function(name,param)
|
|
|
|
local t = string.split(param, " ")
|
|
|
|
if t[1] == "viewmessages" then
|
|
|
|
minetest.chat_send_player(name,random_messages.list_messages())
|
|
|
|
elseif t[1] == "removemessage" then
|
|
|
|
if not random_messages.check_params(
|
|
|
|
name,
|
|
|
|
function (params)
|
|
|
|
if not tonumber(params[2]) or
|
|
|
|
random_messages.messages[tonumber(params[2])] == nil then
|
|
|
|
return false,"ERROR: No such message."
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
t) then return end
|
|
|
|
random_messages.remove_message(t[2])
|
|
|
|
elseif t[1] == "addmessage" then
|
|
|
|
if not t[2] then
|
|
|
|
minetest.chat_send_player(name,"ERROR: No message.")
|
|
|
|
else
|
|
|
|
random_messages.add_message(t)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(name,"ERROR: Invalid command.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
minetest.register_chatcommand("random_messages", register_chatcommand_table)
|
|
|
|
minetest.register_chatcommand("rmessages", register_chatcommand_table)
|