Skip to content

Commit

Permalink
Rework rankings, make redis backend use sorted sets
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWolfHT committed Feb 20, 2025
1 parent cee5b92 commit 89ead44
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 158 deletions.
12 changes: 1 addition & 11 deletions mods/ctf/ctf_modebase/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,7 @@ minetest.register_on_mods_loaded(function()
player:set_nametag_attributes({color = {a = 0, r = 255, g = 255, b = 255}, text = ""})
end)
elseif ctf_core.settings.server_mode == "play" then
minetest.chat_send_all(S("[CTF] Sorting rankings..."))
local function check()
if not ctf_rankings:rankings_sorted() then
return minetest.after(1, check)
end

minetest.chat_send_all(S("[CTF] Rank sorting done. Starting new match..."))
ctf_modebase.start_new_match()
end

check()
ctf_modebase.start_new_match()
end

for _, name in pairs(ctf_modebase.modelist) do
Expand Down
19 changes: 10 additions & 9 deletions mods/ctf/ctf_modebase/ranking_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ local function rank(name, mode_name, mode_data, pname)
return_str = string.format("%s%s: %s\n",
return_str,
minetest.colorize("#63d437", "Place"),
minetest.colorize("#ffea00", mode_data.rankings.top:get_place(pname))
minetest.colorize("#ffea00", mode_data.rankings:get_place(pname, "score"))
)

return true, return_str
Expand Down Expand Up @@ -228,7 +228,7 @@ minetest.register_chatcommand("reset_rankings", {

if pname then
if minetest.check_player_privs(name, {ctf_admin = true}) then
mode_data.rankings:set(pname, {}, true)
mode_data.rankings:del(pname)

minetest.log("action", string.format(
"[ctf_admin] %s reset rankings for player '%s' in mode %s", name, pname, mode_name
Expand All @@ -255,7 +255,7 @@ minetest.register_chatcommand("reset_rankings", {
S("team chest or userlimit skip. This is irreversable. If you're") .." "..
S("sure, re-type /reset_rankings within 30 seconds to reset.")
end
mode_data.rankings:set(name, {}, true)
mode_data.rankings:del(name)
allow_reset[key] = nil

minetest.log("action", string.format(
Expand All @@ -277,14 +277,15 @@ minetest.register_chatcommand("top50", {

local top50 = {}

for i, pname in ipairs(mode_data.rankings.top:get_top(50)) do
local t = table.copy(mode_data.rankings:get(pname) or {})
t.pname = pname
for i, info in ipairs(mode_data.rankings:get_top(50, "score")) do
local t = mode_data.rankings:get(info[1]) or {}
t.pname = info[1]
t.number = i
table.insert(top50, t)
end

local own_pos = mode_data.rankings.top:get_place(name)
if own_pos > 50 then
local own_pos = mode_data.rankings:get_place(name, "score")
if own_pos and own_pos > 50 then
local t = table.copy(mode_data.rankings:get(name) or {})
t.pname = name
t.number = own_pos
Expand Down Expand Up @@ -415,7 +416,7 @@ minetest.register_chatcommand("transfer_rankings", {
end

for _, mode in pairs(ctf_modebase.modes) do
mode.rankings:set(src, {}, true)
mode.rankings:del(src)
end

minetest.log("action", string.format(
Expand Down
2 changes: 2 additions & 0 deletions mods/ctf/ctf_modebase/summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ function ctf_modebase.summary.show_gui_sorted(name, rankings, special_rankings,
end

ctf_gui.old_show_formspec(name, "ctf_modebase:summary", formspec)

minetest.log("action", "[ctf_modebase.summary] Showed gui to "..dump(name))
end

ctf_core.register_chatcommand_alias("summary", "s", {
Expand Down
22 changes: 12 additions & 10 deletions mods/ctf/ctf_modes/ctf_mode_classes/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
local rankings = ctf_rankings:init()
local RANKLIST = {
_sort = "score",
"score",
"flag_captures", "flag_attempts",
"kills", "kill_assists", "bounty_kills",
"deaths",
"hp_healed",
"reward_given_to_enemy"
}

local rankings = ctf_rankings:init(RANKLIST)
local recent_rankings = ctf_modebase.recent_rankings(rankings)
local features = ctf_modebase.features(rankings, recent_rankings)

Expand Down Expand Up @@ -78,15 +88,7 @@ ctf_modebase.register_mode("classes", {
},
rankings = rankings,
recent_rankings = recent_rankings,
summary_ranks = {
_sort = "score",
"score",
"flag_captures", "flag_attempts",
"kills", "kill_assists", "bounty_kills",
"deaths",
"hp_healed",
"reward_given_to_enemy"
},
summary_ranks = RANKLIST,
build_timer = 90,
is_bound_item = function(_, name)
if name:match("ctf_mode_classes:") or name:match("ctf_melee:") or name == "ctf_healing:bandage" then
Expand Down
22 changes: 12 additions & 10 deletions mods/ctf/ctf_modes/ctf_mode_classic/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
local rankings = ctf_rankings:init()
local RANKLIST = {
_sort = "score",
"score",
"flag_captures", "flag_attempts",
"kills", "kill_assists", "bounty_kills",
"deaths",
"hp_healed",
"reward_given_to_enemy"
}

local rankings = ctf_rankings:init(RANKLIST)
local recent_rankings = ctf_modebase.recent_rankings(rankings)
local features = ctf_modebase.features(rankings, recent_rankings)

Expand Down Expand Up @@ -36,15 +46,7 @@ ctf_modebase.register_mode("classic", {
team_chest_items = {"default:cobble 99", "default:wood 99", "default:torch 30", "ctf_teams:door_steel 2"},
rankings = rankings,
recent_rankings = recent_rankings,
summary_ranks = {
_sort = "score",
"score",
"flag_captures", "flag_attempts",
"kills", "kill_assists", "bounty_kills",
"deaths",
"hp_healed",
"reward_given_to_enemy"
},
summary_ranks = RANKLIST,

stuff_provider = function()
return {"default:sword_stone", "default:pick_stone", "default:torch 15", "default:stick 5"}
Expand Down
22 changes: 12 additions & 10 deletions mods/ctf/ctf_modes/ctf_mode_nade_fight/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
local rankings = ctf_rankings:init()
local RANKLIST = {
_sort = "score",
"score",
"flag_captures", "flag_attempts",
"kills", "kill_assists", "bounty_kills",
"deaths",
"hp_healed",
"reward_given_to_enemy"
}

local rankings = ctf_rankings:init(RANKLIST)
local recent_rankings = ctf_modebase.recent_rankings(rankings)
local features = ctf_modebase.features(rankings, recent_rankings)

Expand Down Expand Up @@ -63,15 +73,7 @@ ctf_modebase.register_mode("nade_fight", {
},
rankings = rankings,
recent_rankings = recent_rankings,
summary_ranks = {
_sort = "score",
"score",
"flag_captures", "flag_attempts",
"kills", "kill_assists", "bounty_kills",
"deaths",
"hp_healed",
"reward_given_to_enemy"
},
summary_ranks = RANKLIST,
build_timer = 60 * 2,

is_bound_item = function(_, name)
Expand Down
26 changes: 24 additions & 2 deletions mods/ctf/ctf_rankings/default.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
return function(prefix, top, sorting_finished)
local top = ctf_core.include_files("top.lua"):new()

return function(prefix, sorting_finished)

local modstorage = assert(minetest.get_mod_storage(), "Can only init rankings at runtime!")

Expand Down Expand Up @@ -57,7 +59,6 @@ function()
"action",
"Sorted rankings by score '"..prefix:sub(1, -2).."'. Took "..((minetest.get_us_time()-timer) / 1e6)
)
sorting_finished()
end)

return {
Expand All @@ -69,6 +70,27 @@ return {

op_all = op_all,

get_top = function(self, rend, sortby, rstart)
local t = self.top:get_top(rend)

if sortby ~= "score" then
core.log("error", "Modstorage rankings only support sorting by score")
end

local out = {}
for i=(rstart or 1), #t do
out[i] = {t[i]}
end

return out
end,
get_place = function(self, pname, sortby)
if sortby ~= "score" then
core.log("error", "Modstorage rankings only support sorting by score")
end

return self.top:get_place(pname)
end,
get = function(self, pname)
pname = PlayerName(pname)

Expand Down
22 changes: 22 additions & 0 deletions mods/ctf/ctf_rankings/dummy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
return function()
return {
backend = "dummy",

prefix = "",

get_place = function()
return false
end,
get_top = function()
return {}
end,
get = function()
end,
set = function()
end,
add = function()
end,
del = function()
end,
}
end
36 changes: 3 additions & 33 deletions mods/ctf/ctf_rankings/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local backend = minetest.settings:get("ctf_rankings_backend") or "default"

local rankings
local top = ctf_core.include_files("top.lua")

if backend == "redis" then
local env = minetest.request_insecure_environment()
Expand All @@ -17,34 +16,17 @@ else
end

ctf_rankings = {
sorted = {},

rankings_sorted = function(self)
return false -- Returning false, since not all rankings are sorted yet. See below for full func
end,

init = function(self)
init = function(self, ranklist)
local modname = minetest.get_current_modname()

if self then
self.sorted[modname] = false
else
if not self then
minetest.log(
"error",
"[ctf_rankings] The mode "..modname.." is calling the init() function wrong. Use ':' instead of '.'"
)
end

return rankings(modname .. '|', top:new(), function()
if self then
self.sorted[modname] = true
else
minetest.log(
"error",
"[ctf_rankings] The mode "..modname.." is calling the init() function wrong. Use ':' instead of '.'"
)
end
end)
return rankings(modname .. '|', ranklist)
end,

registered_on_rank_reset = {},
Expand Down Expand Up @@ -95,16 +77,4 @@ function ctf_rankings.register_on_rank_reset(func)
table.insert(ctf_rankings.registered_on_rank_reset, func)
end

minetest.register_on_mods_loaded(function()
ctf_rankings.rankings_sorted = function(self)
for _, sorted in pairs(self.sorted) do
if not sorted then
return false
end
end

return true
end
end)

ctf_core.include_files("leagues.lua", "ranking_reset.lua")
2 changes: 1 addition & 1 deletion mods/ctf/ctf_rankings/leagues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ minetest.register_on_joinplayer(function(player)
-- Maybe up to wood league?

-- for mode, def in pairs(ctf_modebase.modes) do
-- local place = def.rankings.top:get_place(pname)
-- local place = def.rankings:get_place(pname, "score")

-- for _, league in ipairs(ctf_rankings.leagues_list) do
-- if place <= ctf_rankings.leagues[league] then
Expand Down
4 changes: 2 additions & 2 deletions mods/ctf/ctf_rankings/ranking_reset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ctf_rankings.do_reset = mods:get_int("_do_reset") == 1
local PLAYER_RANKING_PREFIX = "rank:"

local function do_reset()
assert(false, "Ranking resets don't currently work with the new rankings format (op_all not supported)")
local finish_count = 0
local function finish()
finish_count = finish_count - 1
Expand All @@ -37,7 +38,6 @@ local function do_reset()
end

for mode, def in pairs(ctf_modebase.modes) do
local top = def.rankings.top
local time = minetest.get_us_time()

finish_count = finish_count + 1
Expand All @@ -46,7 +46,7 @@ local function do_reset()
local rank = minetest.parse_json(value)

if rank then
rank.place = top:get_place(pname)
rank.place = def.rankings:get_place(pname, "score")

RunCallbacks(ctf_rankings.registered_on_rank_reset, pname, table.copy(rank), mode)

Expand Down
Loading

0 comments on commit 89ead44

Please sign in to comment.