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)
ctf_classes.update(player)
ctf_classes.set_cooldown(player:get_player_name())
if old_name == nil or old_name ~= new_name then
local old = old_name and ctf_classes.__classes[old_name]
for i=1, #registered_on_changed do
@ -150,10 +152,34 @@ function ctf_classes.can_change(player)
return true
end
local flag_pos = get_flag_pos(player)
if not flag_pos then
return false
if ctf_classes.get_cooldown(player:get_player_name()) then
return false, "You need to wait to change classes again!"
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
-- 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
local function show(_, _, player)
ctf_classes.show_gui(player:get_player_name(), 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)
end
end
ctf_flag.on_rightclick = show

View file

@ -1,16 +1,11 @@
function ctf_classes.show_gui(name, player)
player = player or minetest.get_player_by_name(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 = {
"size[", #ctf_classes.__classes_ordered * 3 , ",3.4]"
}
local x = 0
local y = 0
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
end
if not ctf_classes.can_change(player) then
minetest.chat_send_player(player:get_player_name(),
"Move closer to the flag to change classes!")
local can_change, reason = ctf_classes.can_change(player)
if not can_change then
minetest.chat_send_player(player:get_player_name(), reason)
return
end
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)
ctf_classes.update(player)
if minetest.check_player_privs(player, { interact = true }) then
ctf_classes.show_gui(player:get_player_name())
if ctf_classes.can_change(player) and
minetest.check_player_privs(player, { interact = true }) then
ctf_classes.show_gui(player:get_player_name(), player)
end
end)
@ -27,8 +28,9 @@ minetest.register_chatcommand("class", {
return false, "You must be online to do this!"
end
if not ctf_classes.can_change(player) then
return false, "Move closer to your flag to change classes!"
local can_change, reason = ctf_classes.can_change(player)
if not can_change then
return false, reason
end
local cname = params:trim()