From f952112be0d81d9bb526817b8b7f38b149f633c6 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 3 Dec 2015 23:57:28 +0000 Subject: [PATCH] Add league tables --- mods/ctf_stats/gui.lua | 31 ++++++++++++++++++++++++++----- mods/ctf_stats/init.lua | 17 +++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/mods/ctf_stats/gui.lua b/mods/ctf_stats/gui.lua index 0883722..f614abf 100644 --- a/mods/ctf_stats/gui.lua +++ b/mods/ctf_stats/gui.lua @@ -10,14 +10,17 @@ function ctf_stats.get_formspec_match_summary(stats) pstat.color = ctf.flag_colors.blue table.insert(players, pstat) end - local ret = ctf_stats.get_formspec("Match Summary", players, stats) + local ret = ctf_stats.get_formspec("Match Summary", players) ret = ret .. "label[3.5,6.2;Tip: type /rankings for league tables]" return ret end -function ctf_stats.get_formspec(title, players, stats) - for _, pstat in pairs(players) do +function ctf_stats.get_formspec(title, players) + for i, pstat in pairs(players) do pstat.score = pstat.captures + 0.2 * pstat.attempts + 7 * pstat.kills / (pstat.deaths + 1) + if i > 40 then + break + end end table.sort(players, function(one, two) return (one.score > two.score) @@ -25,15 +28,16 @@ function ctf_stats.get_formspec(title, players, stats) local ret = "size[9,6.5]" ret = ret .. "vertlabel[0,0;" .. title .. "]" - ret = ret .. "tablecolumns[color;text;text;text;text;text;text;text]" + ret = ret .. "tablecolumns[color;text;text;text;text;text;text;text;text]" ret = ret .. "tableoptions[highlight=#00000000]" ret = ret .. "table[0.5,0;8.25,6;scores;" - ret = ret .. "#ffffff,username,kills,deaths,K/D ratio,wins,attempts,score" + ret = ret .. "#ffffff,,username,kills,deaths,K/D ratio,captures,attempts,score" for i, pstat in pairs(players) do local color = pstat.color or "#ffffff" ret = ret .. "," .. string.gsub(color, "0x", "#") .. + "," .. i .. "," .. pstat.name .. "," .. pstat.kills .. "," .. pstat.deaths .. @@ -41,9 +45,26 @@ function ctf_stats.get_formspec(title, players, stats) "," .. pstat.captures .. "," .. pstat.attempts .. "," .. pstat.score + if i > 40 then + break + end end ret = ret .. ";-1]" ret = ret .. "button_exit[0.5,6;3,1;close;Close]" return ret end + + +minetest.register_chatcommand("rankings", { + func = function(name) + local players = {} + for name, pstat in pairs(ctf_stats.players) do + pstat.name = name + pstat.color = nil + table.insert(players, pstat) + end + local fs = ctf_stats.get_formspec("Player Rankings", players) + minetest.show_formspec(name, "a", fs) + end +}) diff --git a/mods/ctf_stats/init.lua b/mods/ctf_stats/init.lua index 8d72a95..5c01268 100644 --- a/mods/ctf_stats/init.lua +++ b/mods/ctf_stats/init.lua @@ -47,10 +47,12 @@ function ctf_stats.player(name) local player = ctf_stats.players[name] if not player then player = { + name = name, red_wins = 0, blue_wins = 0, kills = 0, deaths = 0, + captures = 0, attempts = 0 } ctf_stats.players[name] = player @@ -72,18 +74,21 @@ ctf.register_on_join_team(function(name, tname) end) ctf_match.register_on_skip_map(function() + ctf.needs_save = true ctf_stats.matches.skipped = ctf_stats.matches.skipped + 1 end) ctf_flag.register_on_capture(function(name, flag) - local tname = ctf.player(name).team - if ctf_stats.current[tname] and ctf_stats.current[tname][name] then - ctf_stats.current[tname][name].captures = - ctf_stats.current[tname][name].captures + 1 + local main, match = ctf_stats.player(name) + if main and match then + main.captures = main.captures + 1 + match.captures = match.captures + 1 + ctf.needs_save = true end end) ctf_match.register_on_winner(function(winner) + ctf.needs_save = true ctf_stats.matches[winner .. "_wins"] = ctf_stats.matches[winner .. "_wins"] + 1 end) @@ -98,6 +103,7 @@ ctf_match.register_on_new_match(function() red = {}, blue = {} } + ctf.needs_save = true end) ctf_flag.register_on_pick_up(function(name, flag) @@ -105,6 +111,7 @@ ctf_flag.register_on_pick_up(function(name, flag) if main and match then main.attempts = main.attempts + 1 match.attempts = match.attempts + 1 + ctf.needs_save = true end end) @@ -113,6 +120,7 @@ ctf_flag.register_on_precapture(function(name, flag) local main, match = ctf_stats.player(name) if main then main[tplayer.team .. "_wins"] = main[tplayer.team .. "_wins"] + 1 + ctf.needs_save = true end return true end) @@ -122,6 +130,7 @@ minetest.register_on_dieplayer(function(player) if main and match then main.deaths = main.deaths + 1 match.deaths = match.deaths + 1 + ctf.needs_save = true end end)