2016-09-14 19:22:09 +02: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.
2016-10-11 11:09:32 +02:00
minetest.setting_set ( " random_messages_interval " , 7200 )
2016-09-14 19:22:09 +02:00
minetest.setting_save ( ) ;
2016-09-14 20:21:00 +02:00
return 1800
2016-09-14 19:22:09 +02:00
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-11-01 13:05:12 +01:00
local mc = core.colorize
2017-09-14 22:28:15 +02:00
local base = " #A3B5CB "
local url = " #54a3a6 " --[[good color but too agressive in this case "#44be72"]]
local highlight = " #4f8abd "
2016-09-14 19:22:09 +02:00
random_messages.messages = {
2017-09-14 22:28:15 +02:00
mc ( base , " # Illuna-Notes: Teamspeak? Mumble! Talk together on our " ) .. mc ( highlight , " Mumbleserver " ) .. mc ( base , " at " ) .. mc ( url , " tchncs.de! " ) ,
mc ( base , " # Illuna-Notes: Enjoy Illuna? Tell your friends and followers about it! " ) ,
mc ( base , " # Illuna-Notes: Have something to share? Join us at " ) .. mc ( url , " https://community.illuna.rocks " ) .. mc ( base , " today! " ) ,
mc ( base , " # Illuna-Notes: Chat together! Even from outside the game! Join our connected Illuna " ) .. mc ( highlight , " [ matrix ] " ) .. mc ( base , " room: " ) .. mc ( url , " #illuna:tchncs.de " ) ,
mc ( base , " # Illuna-Notes: Chat together! Even from outside the game! Join our connected Illuna IRC room: " ) .. mc ( highlight , " #illuna " ) .. mc ( base , " on " ) .. mc ( highlight , " Freenode " ) ,
mc ( base , " # Illuna-Notes: Chat together! Even from outside the game! Join our connected " ) .. mc ( highlight , " Telegram " ) .. mc ( base , " Group: " ) .. mc ( url , " https://t.me/illunaminetest " ) ,
mc ( base , " # Illuna-Notes: Did you know? Illuna is powered by donations, you can help letting it survive: " ) .. mc ( url , " https://illuna.rocks/donate " ) .. mc ( base , " . Each single microdonation helps a bunch. :3 " ) ,
2016-09-14 19:22:09 +02:00
}
end
function random_messages . display_message ( 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 ( 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 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 )