Skip to content

Commit

Permalink
Add an 'all' option to /r (#1186)
Browse files Browse the repository at this point in the history
* Adding 'all' option to the ranking command

* Initialising prank variable

* Fixing erros; testing for final errors

* Ignore please

* Another one I missed

* Adding newlines and tabs

* I am oblivious

* Removing an extra line I added

* Rewriting code

* Perhaps a fix for the shadowing upvalue `rank` error

* Update ranking_commands.lua

* Update ranking_commands.lua

---------

Co-authored-by: LoneWolfHT <[email protected]>
  • Loading branch information
src4026 and LoneWolfHT authored Aug 19, 2023
1 parent 2762a9f commit fd7f3c2
Showing 1 changed file with 65 additions and 45 deletions.
110 changes: 65 additions & 45 deletions mods/ctf/ctf_modebase/ranking_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ local function get_gamemode(param)

if mode_param then
local mode = ctf_modebase.modes[mode_param]
if not mode then
if mode_param == "all" then
return "all", nil, opt_param
elseif not mode then
return false, "No such game mode: " .. mode_param
end

Expand All @@ -18,54 +20,72 @@ local function get_gamemode(param)
end
end

ctf_core.register_chatcommand_alias("rank", "r", {
description = "Get the rank of yourself or a player",
params = "[mode:technical modename] <playername>",
func = function(name, param)
local mode_name, mode_data, pname = get_gamemode(param)
if not mode_name then
return false, mode_data
end

if not pname then
pname = name
end
local prank = mode_data.rankings:get(pname) -- [p]layer [rank]
local function rank(name, mode_name, mode_data, pname)
if not mode_name then
return false, mode_data
end

if not prank then
return false, string.format("Player %s has no rankings in mode %s!", pname, mode_name)
end
if not pname then
pname = name
end
local prank = mode_data.rankings:get(pname) -- [p]layer [rank]

local return_str = string.format(
"Rankings for player %s in mode %s:\n\t", minetest.colorize("#ffea00", pname), mode_name
)
if not prank then
return false, string.format("Player %s has no rankings in mode %s!", pname, mode_name)
end

for _, rank in ipairs(mode_data.summary_ranks) do
return_str = string.format("%s%s: %s,\n\t",
return_str,
minetest.colorize("#63d437", HumanReadable(rank)),
minetest.colorize("#ffea00", math.round(prank[rank] or 0))
)
end
local return_str = string.format(
"\tRankings for player %s in mode %s:\n\t", minetest.colorize("#ffea00", pname), mode_name
)

for _, pair in pairs({{"kills", "deaths"}, {"score", "kills"}}) do
return_str = string.format("%s%s: %s,\n\t",
return_str,
minetest.colorize("#63d437", HumanReadable(pair[1].."/"..pair[2])),
minetest.colorize("#ffea00", 0.1 * math.round(10 * (
(prank[pair[1]] or 0 ) /
math.max(prank[pair[2]] or 0, 1)
)))
)
end
for _, irank in ipairs(mode_data.summary_ranks) do
return_str = string.format("%s%s: %s,\n\t",
return_str,
minetest.colorize("#63d437", HumanReadable(irank)),
minetest.colorize("#ffea00", math.round(prank[irank] or 0))
)
end

return_str = string.format("%s%s: %s",
for _, pair in pairs({{"kills", "deaths"}, {"score", "kills"}}) do
return_str = string.format("%s%s: %s,\n\t",
return_str,
minetest.colorize("#63d437", "Place"),
minetest.colorize("#ffea00", mode_data.rankings.top:get_place(pname))
minetest.colorize("#63d437", HumanReadable(pair[1].."/"..pair[2])),
minetest.colorize("#ffea00", 0.1 * math.round(10 * (
(prank[pair[1]] or 0 ) /
math.max(prank[pair[2]] or 0, 1)
)))
)
end

return_str = string.format("%s%s: %s",
return_str,
minetest.colorize("#63d437", "Place"),
minetest.colorize("#ffea00", mode_data.rankings.top:get_place(pname))
)

return true, return_str
end

return true, return_str
ctf_core.register_chatcommand_alias("rank", "r", {
description = "Get the rank of yourself or a player",
params = "[ all | mode:technical modename] <playername>",
func = function(name, param)
local mode_name, mode_data, pname = get_gamemode(param)
if mode_name == "all" then
local return_str = string.format(
"Rankings for player %s in mode %s:\n\t",
minetest.colorize("#ffea00", pname or name),
mode_name
)

for _, mode in ipairs(ctf_modebase.modelist) do
mode_data = ctf_modebase.modes[mode]
return_str = return_str .. select(2, rank(name, mode, mode_data, pname))
end
return true, return_str
else
return rank(name, mode_name, mode_data, pname)
end
end
})

Expand Down Expand Up @@ -105,8 +125,8 @@ minetest.register_chatcommand("donate", {
return false, "You should donate at least 5 score!"
end

if score > 400 then
return false, "You can donate no more than 400 score!"
if score > 100 then
return false, "You can donate no more than 100 score!"
end

if pname == name then
Expand All @@ -131,8 +151,8 @@ minetest.register_chatcommand("donate", {
return false, "You can donate only half of your match score!"
end

if donate_timer[name] and donate_timer[name] + 300 > os.time() then
return false, "You can donate only once every 5 minutes!"
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)
Expand Down

0 comments on commit fd7f3c2

Please sign in to comment.