Add the donate command to donate your match score to your teammate

master
savilli 2022-06-25 21:50:13 +00:00
parent 8a896e5b77
commit c6a2603f19
2 changed files with 80 additions and 3 deletions

View File

@ -58,6 +58,77 @@ ctf_core.register_chatcommand_alias("rank", "r", {
end
})
local donate_timer = {}
ctf_api.register_on_match_end(function()
donate_timer = {}
end)
minetest.register_chatcommand("donate", {
description = "Donate your match score to your teammate\nCan be used only once in 10 minutes",
params = "<playername> <score>",
func = function(name, param)
local current_mode = ctf_modebase:get_current_mode()
if not current_mode or not ctf_modebase.match_started then
return false, "The match hasn't started yet!"
end
local pname, score = string.match(param, "^(.*) (.*)$")
if not pname then
return false, "You should provide the player name!"
end
score = tonumber(score)
if not score then
return false, "You should provide score amount!"
end
if score < 1 then
return false, "You should donate at least 1 score!"
end
if pname == name then
return false, 'You cannot donate to yourself!'
end
local team = ctf_teams.get(pname)
if not team then
return false, string.format("Player %s is not online!", pname)
end
if ctf_teams.get(pname) ~= ctf_teams.get(name) then
return false, string.format("Player %s is not on your team!", pname)
end
local cur_score = math.min(
current_mode.recent_rankings.get(name).score or 0,
(current_mode.rankings:get(name) or {}).score or 0
)
if score > cur_score / 2 then
return false, "You can donate only half of your match score!"
end
if donate_timer[name] and donate_timer[name] + 600 > os.time() then
return false, "You can donate only once in 10 minutes!"
end
current_mode.recent_rankings.add(pname, {score=score}, true)
current_mode.recent_rankings.add(name, {score=-score}, true)
donate_timer[name] = os.time()
minetest.chat_send_all(minetest.colorize("#00EEFF",
string.format("%s donated %s score to %s for their hard work", name, score, pname)
))
minetest.log("action", string.format(
"Player '%s' donated %s score to player '%s'", name, score, pname
))
return true
end
})
local allow_reset = {}
minetest.register_chatcommand("reset_rankings", {
description = minetest.colorize("red", "Resets rankings of you or another player to nothing"),
@ -75,7 +146,7 @@ minetest.register_chatcommand("reset_rankings", {
minetest.log("action", string.format(
"[ctf_admin] %s reset rankings for player '%s' in mode %s", name, pname, mode_name
))
return true, "Rankings reset for player " .. pname
return true, string.format("Rankings reset for player '%s' in mode %s", pname, mode_name)
else
return false, "The ctf_admin priv is required to reset the rankings of other players!"
end
@ -96,12 +167,14 @@ minetest.register_chatcommand("reset_rankings", {
mode_data.rankings:set(name, {}, true)
allow_reset[key] = nil
minetest.log("action", string.format(
"Player '%s' reset their rankings in mode %s", name, mode_name
))
return true, "Your rankings have been reset"
end
end
})
minetest.register_chatcommand("top50", {
description = "Show the top 50 players",
params = "[mode:technical modename]",
@ -146,7 +219,7 @@ minetest.register_chatcommand("make_pro", {
end
if not pname then
return false, "You should provide the player name"
return false, "You should provide the player name!"
end
local old_ranks = mode_data.rankings:get(pname)

View File

@ -32,6 +32,10 @@ return {
rankings:add(player, amounts)
end,
get = function(player)
player = PlayerName(player)
return rankings_players[player] or {}
end,
set_team = function(player, team)
player = PlayerName(player)
local tcolor = ctf_teams.team[team].color