ctf_classes: Implement cooldown after class change (#637)

Defaults to a 30s cooldown. Players with ctf_admin priv are exempt from this restriction.
This commit is contained in:
ANAND 2020-06-04 22:15:13 +05:30 committed by GitHub
parent 45aac6268f
commit cafd16878e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 17 deletions

View file

@ -78,6 +78,8 @@ function ctf_classes.set(player, new_name)
meta:set_string("ctf_classes:class", new_name) meta:set_string("ctf_classes:class", new_name)
ctf_classes.update(player) ctf_classes.update(player)
ctf_classes.set_cooldown(player:get_player_name())
if old_name == nil or old_name ~= new_name then if old_name == nil or old_name ~= new_name then
local old = old_name and ctf_classes.__classes[old_name] local old = old_name and ctf_classes.__classes[old_name]
for i=1, #registered_on_changed do for i=1, #registered_on_changed do
@ -150,10 +152,34 @@ function ctf_classes.can_change(player)
return true return true
end end
local flag_pos = get_flag_pos(player) if ctf_classes.get_cooldown(player:get_player_name()) then
if not flag_pos then return false, "You need to wait to change classes again!"
return false
end end
return sqdist(player:get_pos(), flag_pos) < 25 local flag_pos = get_flag_pos(player)
if flag_pos then
return sqdist(player:get_pos(), flag_pos) < 25,
"Move closer to the flag to change classes!"
end
return false, "Flag does not exist!"
end end
-- Cooldown time to prevent consumables abuse by changing to a class and back
local COOLDOWN_TIME = 30
local cooldown = {}
function ctf_classes.get_cooldown(name)
return cooldown[name]
end
function ctf_classes.set_cooldown(name)
cooldown[name] = true
minetest.after(COOLDOWN_TIME, function()
cooldown[name] = nil
end)
end
minetest.register_on_dieplayer(function(player)
cooldown[player:get_player_name()] = nil
end)

View file

@ -24,7 +24,12 @@ local function on_punch(pos, node, player, ...)
end end
local function show(_, _, player) local function show(_, _, player)
local can_change, reason = ctf_classes.can_change(player)
if not can_change then
minetest.chat_send_player(player:get_player_name(), reason)
else
ctf_classes.show_gui(player:get_player_name(), player) ctf_classes.show_gui(player:get_player_name(), player)
end
end end
ctf_flag.on_rightclick = show ctf_flag.on_rightclick = show

View file

@ -1,16 +1,11 @@
function ctf_classes.show_gui(name, player) function ctf_classes.show_gui(name, player)
player = player or minetest.get_player_by_name(name) player = player or minetest.get_player_by_name(name)
assert(player.get_player_name) assert(player.get_player_name)
if not ctf_classes.can_change(player) then
minetest.chat_send_player(name, "Move closer to your flag to change classes!")
return
end
local fs = { local fs = {
"size[", #ctf_classes.__classes_ordered * 3 , ",3.4]" "size[", #ctf_classes.__classes_ordered * 3 , ",3.4]"
} }
local x = 0 local x = 0
local y = 0 local y = 0
for _, class in pairs(ctf_classes.__classes_ordered) do for _, class in pairs(ctf_classes.__classes_ordered) do
@ -79,9 +74,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return false return false
end end
if not ctf_classes.can_change(player) then local can_change, reason = ctf_classes.can_change(player)
minetest.chat_send_player(player:get_player_name(), if not can_change then
"Move closer to the flag to change classes!") minetest.chat_send_player(player:get_player_name(), reason)
return
end end
for name in pairs(ctf_classes.__classes) do for name in pairs(ctf_classes.__classes) do

View file

@ -15,8 +15,9 @@ dofile(minetest.get_modpath("ctf_classes") .. "/classes.lua")
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
ctf_classes.update(player) ctf_classes.update(player)
if minetest.check_player_privs(player, { interact = true }) then if ctf_classes.can_change(player) and
ctf_classes.show_gui(player:get_player_name()) minetest.check_player_privs(player, { interact = true }) then
ctf_classes.show_gui(player:get_player_name(), player)
end end
end) end)
@ -27,8 +28,9 @@ minetest.register_chatcommand("class", {
return false, "You must be online to do this!" return false, "You must be online to do this!"
end end
if not ctf_classes.can_change(player) then local can_change, reason = ctf_classes.can_change(player)
return false, "Move closer to your flag to change classes!" if not can_change then
return false, reason
end end
local cname = params:trim() local cname = params:trim()