From 41e7b4efaf55395366bfc7b0abbcd965aec26e5f Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Mon, 13 Jul 2015 23:22:02 +0200 Subject: [PATCH] Merged all command timers in a single mod : action_timers - Merged all command timers in a single mod - Activated mod - Updated news.txt --- minetestforfun_game/mods/sethome/depends.txt | 1 + minetestforfun_game/mods/sethome/init.lua | 74 ++++++++++++-------- mods/action_timers/init.lua | 62 ++++++++++++++++ mods/irc/botcmds.lua | 32 +++++---- mods/irc/depends.txt | 1 + mods/spawn/depends.txt | 1 + mods/spawn/init.lua | 40 ++++++----- worlds/minetestforfun/news.txt | 3 + worlds/minetestforfun/world.mt | 2 + 9 files changed, 156 insertions(+), 60 deletions(-) create mode 100755 minetestforfun_game/mods/sethome/depends.txt create mode 100755 mods/action_timers/init.lua create mode 100755 mods/irc/depends.txt create mode 100755 mods/spawn/depends.txt diff --git a/minetestforfun_game/mods/sethome/depends.txt b/minetestforfun_game/mods/sethome/depends.txt new file mode 100755 index 00000000..d06a3848 --- /dev/null +++ b/minetestforfun_game/mods/sethome/depends.txt @@ -0,0 +1 @@ +action_timers diff --git a/minetestforfun_game/mods/sethome/init.lua b/minetestforfun_game/mods/sethome/init.lua index 1cf7f427..c6181abe 100755 --- a/minetestforfun_game/mods/sethome/init.lua +++ b/minetestforfun_game/mods/sethome/init.lua @@ -1,13 +1,9 @@ home = {} home.homes_file = {["real"] = minetest.get_worldpath() .. "/realhomes", - ["nether"] = minetest.get_worldpath() .. "/netherhomes"} + ["nether"] = minetest.get_worldpath() .. "/netherhomes"} home.homepos = {["real"] = {}, ["nether"] = {}} -home.g_timers = {["real"] = {}, ["nether"] = {}} -home.s_timers = {["real"] = {}, ["nether"] = {}} - -home.GET_HOME_INTERVAL = 20*60 -home.SET_HOME_INTERVAL = 20*60 +home.time = 20 * 60 home.sethome = function(name) local player = minetest.get_player_by_name(name) @@ -16,25 +12,33 @@ home.sethome = function(name) if pos.y < -19600 then p_status = "nether" end - if home.s_timers[p_status][name] ~= nil then - local timer_player = os.difftime(os.time(),home.s_timers[p_status][name]) - if timer_player < home.SET_HOME_INTERVAL then -- less than x minutes - minetest.chat_send_player(name, "Please retry later, you used sethome last time less than ".. home.SET_HOME_INTERVAL .." seconds ago.") - minetest.chat_send_player(name, "Retry in: ".. home.SET_HOME_INTERVAL-timer_player .." seconds.") + + local function assign_home() + home.homepos[p_status][name] = pos + minetest.chat_send_player(name, "Home set!") + local output = io.open(home.homes_file[p_status], "w") + output:write(minetest.serialize(home.homepos[p_status])) + io.close(output) + return true + end + + if not action_timers.api.get_timer("sethome_" .. name) then + if home.homepos[p_status][name] ~= pos then + home.homepos[p_status][name] = pos + end + action_timers.api.register_timer("sethome_" .. name, home.time) + return assign_home() + else + local res = action_timers.api.do_action("sethome_" .. name, assign_home) + if tonumber(res) then + minetest.chat_send_player(name, "Please retry later, you used sethome last time less than ".. home.time .." seconds ago.") + minetest.chat_send_player(name, "Retry in: ".. math.floor(res) .." seconds.") minetest.log("action","Player ".. name .." tried to sethome within forbidden interval.") return false + elseif res == true then + return res end end - if home.homepos[p_status][player:get_player_name()] ~= pos then - home.s_timers[p_status][name] = os.time() - home.homepos[p_status][player:get_player_name()] = pos - minetest.chat_send_player(name, "Home set!") - local output = io.open(home.homes_file[p_status], "w") - output:write(minetest.serialize(home.homepos[p_status])) - io.close(output) - return true - end - return false end home.tohome = function(name) @@ -48,20 +52,28 @@ home.tohome = function(name) p_status = "nether" end if home.homepos[p_status][name] then - if home.g_timers[p_status][name] ~= nil then - local timer_player = os.difftime(os.time(),home.g_timers[p_status][name]) - if timer_player < home.GET_HOME_INTERVAL then -- less than x minutes - minetest.chat_send_player(name, "Please retry later, you used home last time less than ".. home.GET_HOME_INTERVAL .." seconds ago.") - minetest.chat_send_player(name, "Retry in: ".. home.GET_HOME_INTERVAL-timer_player .." seconds.") + + local function go_to_home() + player:setpos(home.homepos[p_status][player:get_player_name()]) + minetest.chat_send_player(name, "Teleported to home!") + minetest.log("action","Player ".. name .." teleported to home. Next teleportation allowed in ".. home.time .." seconds.") + return true + end + + if not action_timers.api.get_timer("home_" .. name) then + action_timers.api.register_timer("home_" .. name, home.time) + return go_to_home() + else + local res = action_timers.api.do_action("home_" .. name, go_to_home) + if tonumber(res) then + minetest.chat_send_player(name, "Please retry later, you used home last time less than ".. home.time .." seconds ago.") + minetest.chat_send_player(name, "Retry in: ".. math.floor(res) .." seconds.") minetest.log("action","Player ".. name .." tried to teleport home within forbidden interval.") return false + elseif res == true then + return res end end - player:setpos(home.homepos[p_status][player:get_player_name()]) - minetest.chat_send_player(name, "Teleported to home!") - minetest.log("action","Player ".. name .." teleported to home. Last teleportation allowed in ".. home.GET_HOME_INTERVAL .." seconds.") - home.g_timers[p_status][name] = os.time() - return true else minetest.chat_send_player(name, "Set a home using /sethome") return false diff --git a/mods/action_timers/init.lua b/mods/action_timers/init.lua new file mode 100755 index 00000000..b7dd0f2a --- /dev/null +++ b/mods/action_timers/init.lua @@ -0,0 +1,62 @@ +------------------------------- +-- Action timers +-- Mod handling action timers +-- + +action_timers = {} +action_timers.timers = {} +action_timers.limits = {} + +action_timers.api = {} + + +function action_timers.api.register_timer(name, limit) + if action_timers.timers[name] then + minetest.log("error", "[ACTimers] Cannot register timer " .. name .. " a second time") + return + elseif not limit then + minetest.log("error", "[ACTimers] Cannot register timer " .. name .. " without limit") + return + end + + action_timers.timers[name] = limit + action_timers.limits[name] = limit + minetest.log("action", "[ACTimers] Timer " .. name .. " registered with time limit " .. limit) +end + +function action_timers.api.do_action(name, func, params) + if not action_timers.timers[name] then + minetest.log("error", "[ACTimers] Timer " .. name .. " doesn't exist") + return + elseif not func then + minetest.log("error", "[ACTimers] Function passed to time checker for " .. name .. " is nil") + return + elseif action_timers.timers[name] > 0 then + minetest.log("error", "[ACTimers] Timer " .. name .. " is still up to 0") + return action_timers.timers[name] + end + + action_timers.timers[name] = action_timers.limits[name] + if not params then + print("func()") + return func() + else + print("func(unpack(params))") + return func(unpack(params)) + end +end + +function action_timers.api.get_timer(name) + return action_timers.timers[name] +end + +minetest.register_globalstep(function(dtime) + for name, _ in pairs(action_timers.timers) do + action_timers.timers[name] = action_timers.timers[name] - dtime + if action_timers.timers[name] < 0 then + action_timers.timers[name] = 0 + end + end +end) + +minetest.log("action", "[ACTimers] Loaded") diff --git a/mods/irc/botcmds.lua b/mods/irc/botcmds.lua index 44077407..19858237 100755 --- a/mods/irc/botcmds.lua +++ b/mods/irc/botcmds.lua @@ -1,5 +1,4 @@ -irc.whereis_timer = {} -irc.whereis_timer_max_limit = 120 +local whereis_interval = 120 irc.bot_commands = {} function irc:check_botcmd(msg) @@ -117,19 +116,28 @@ irc:register_bot_command("whereis", { if not player then return false, "There is no player named '"..args.."'" end - if irc.whereis_timer[user.nick] ~= nil then - local timer_player = os.difftime(os.time(),irc.whereis_timer[user.nick]) - if timer_player < irc.whereis_timer_max_limit then + + local function say_where_is() + local fmt = "Player %s is at (%.2f,%.2f,%.2f)" + local pos = player:getpos() + minetest.log("action","IRC user ".. user.nick.."!"..user.username.."@"..user.host.." asked for position of player "..player:get_player_name()) + minetest.chat_send_player(player:get_player_name(),"IRC user ".. user.nick.."!"..user.username.."@"..user.host.." asked for your position") + return true, fmt:format(args, pos.x, pos.y, pos.z) + end + + if not action_timers.api.get_timer("whereis_" .. user.nick) then + action_timers.api.register_timer("whereis_" .. user.nick, whereis_interval) + return say_where_is() + else + local res = action_timers.api.do_action("whereis_" .. user.nick, say_where_is) + if tonumber(res) then local answer = "Command used too often, retry in %d seconds." - return false,answer:format(irc.whereis_timer_max_limit - timer_player) + return false,answer:format(math.floor(res)) + else + print(res) + return res end end - local fmt = "Player %s is at (%.2f,%.2f,%.2f)" - local pos = player:getpos() - irc.whereis_timer[user.nick] = os.time() - minetest.log("action","IRC user ".. user.nick.."!"..user.username.."@"..user.host.." asked for position of player "..player:get_player_name()) - minetest.chat_send_player(player:get_player_name(),"IRC user ".. user.nick.."!"..user.username.."@"..user.host.." asked for your position") - return true, fmt:format(args, pos.x, pos.y, pos.z) end }) diff --git a/mods/irc/depends.txt b/mods/irc/depends.txt new file mode 100755 index 00000000..d06a3848 --- /dev/null +++ b/mods/irc/depends.txt @@ -0,0 +1 @@ +action_timers diff --git a/mods/spawn/depends.txt b/mods/spawn/depends.txt new file mode 100755 index 00000000..d06a3848 --- /dev/null +++ b/mods/spawn/depends.txt @@ -0,0 +1 @@ +action_timers diff --git a/mods/spawn/init.lua b/mods/spawn/init.lua index fbc5692a..4c0f50ae 100755 --- a/mods/spawn/init.lua +++ b/mods/spawn/init.lua @@ -1,32 +1,38 @@ -local timers = {} local SPAWN_INTERVAL = 5*60 -minetest.register_on_chat_message(function(name, message, playername, player) - local cmd = "/spawn" - if message:sub(0, #cmd) == cmd then - if message == '/spawn' then - local player = minetest.get_player_by_name(name) - - -- Checking timers - local timer_player = os.difftime(os.time(),timers[name]) - if timer_player ~= nil and timer_player < SPAWN_INTERVAL then -- less than x minutes - minetest.chat_send_player(name, "Please retry later, you used spawn last time less than ".. SPAWN_INTERVAL .." seconds ago.") - minetest.chat_send_player(name, "Retry in: ".. SPAWN_INTERVAL-timer_player .." seconds.") - minetest.log("action","Player ".. name .." tried to respawn within forbidden interval.") - return false - end +minetest.register_chatcommand("spawn", { + description = "Teleport a player to the defined spawnpoint", + func = function(name) + local player = minetest.get_player_by_name(name) + local function go_to_spawn() if minetest.setting_get_pos("static_spawnpoint") then minetest.chat_send_player(player:get_player_name(), "Teleporting to spawn...") player:setpos(minetest.setting_get_pos("static_spawnpoint")) minetest.log("action","Player ".. name .." respawned. Next allowed respawn in ".. SPAWN_INTERVAL .." seconds.") - timers[name] = os.time() return true else minetest.chat_send_player(player:get_player_name(), "ERROR: No spawn point is set on this server!") return false end end + + -- Checking timers + if not action_timers.api.get_timer("spawn_" .. name) then + action_timers.api.register_timer("spawn_" .. name, SPAWN_INTERVAL) + return go_to_spawn() + else + local res = action_timers.api.do_action("spawn_" .. name, go_to_spawn) + print(res) + if tonumber(res) then + minetest.chat_send_player(name, "Please retry later, you used spawn last time less than ".. SPAWN_INTERVAL .." seconds ago.") + minetest.chat_send_player(name, "Retry in: ".. math.floor(res) .." seconds.") + minetest.log("action","Player ".. name .." tried to respawn within forbidden interval.") + return false + else + return res + end + end end -end) +}) diff --git a/worlds/minetestforfun/news.txt b/worlds/minetestforfun/news.txt index 7782f719..a554e4fd 100755 --- a/worlds/minetestforfun/news.txt +++ b/worlds/minetestforfun/news.txt @@ -7,6 +7,9 @@ News de FR - MinetestForFun (Survival - PVP - Hardcore) ---Informations--- Le générateur de map actuel est maintenant STABLE, il ne sera plus réinitialisé/reset ! +---??/??/2015--- (Remerciements : ???) +Ajout du mod "action_timers" (centralisation de la gestion des compteurs de temps pour spawn, sethome, home, et whereis) + ---09/07/2015--- (Remerciements : Gael-de-Sailly, gravgun, Obani, LeMagnesium/Mg, , crabman77/crabman, mgl512/Le_Docteur) MAJ du mod "broomstick" (le Balai Volant/Broomstick est maintenant disponible ! Je vous invite à aller consulter son craft, vous pourrez voler 2 minutes en échange de 190 de mana, voila de quoi s'installer sur une île flottant dnas le ciel !) /!\ Ajout de la SuperVision/Monitoring du serveur, pour y accéder allez sur : Site officiel > LE JEU > Statut du Serveur /!\ diff --git a/worlds/minetestforfun/world.mt b/worlds/minetestforfun/world.mt index 8e81c381..b9f72c55 100755 --- a/worlds/minetestforfun/world.mt +++ b/worlds/minetestforfun/world.mt @@ -214,3 +214,5 @@ load_mod_future_ban = false load_mod_textures = false load_mod_snowdrift = false load_mod_sounds = false + +load_mod_action_timers = true