1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-11-05 01:50:25 +01:00

[random_messages] Update

- For #397
This commit is contained in:
LeMagnesium 2016-02-23 18:25:12 +01:00
parent 57086dee54
commit 39950ac653
3 changed files with 103 additions and 9 deletions

View File

@ -1,6 +1,13 @@
RandomMessages mod by arsdragonfly.
Put your messages in (world directory)/random_messages,1 message per line.
Messages can be all kinds of hints, mod usage, etc.
Add/Remove messages on the fly:
/rmessages viewmessages
to see all the messages.
/rmessages addmessage blah blah blah
to add the random message blah blah blah.
/rmessages removemessage 2
to remove the 2nd random message in /rmessages viewmessages .
In minetest.conf, random_messages_interval decides how often a message is sent.
Released under CC0.
Special thanks to:

View File

@ -1 +0,0 @@
default

View File

@ -11,6 +11,21 @@ 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();
@ -21,6 +36,15 @@ function random_messages.set_interval() --Read the interval from minetest.conf(s
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()
local line_number = 1
local input = io.open(minetest.get_worldpath().."/random_messages","r")
@ -39,22 +63,86 @@ function random_messages.read_messages()
end
function random_messages.display_message(message_number)
if random_messages.messages[message_number] then
minetest.chat_send_all(random_messages.messages[message_number])
local msg = random_messages.messages[message_number] or message_number
if msg then
minetest.chat_send_all(msg)
end
end
function random_messages.show_message()
random_messages.display_message(math.random(#random_messages.messages))
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()
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)
end
--When server starts:
random_messages.set_interval()
random_messages.read_messages()
local function tick()
random_messages.show_message()
minetest.after(MESSAGE_INTERVAL, tick)
end
minetest.after(MESSAGE_INTERVAL, tick)
local TIMER = 0
minetest.register_globalstep(function(dtime)
TIMER = TIMER + dtime;
if TIMER > MESSAGE_INTERVAL then
random_messages.show_message()
TIMER = 0
end
end)
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)