From 220d5e2a3cc03186b1ff38aa9e654b4eeb0ed795 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 3 Dec 2015 23:46:52 +0000 Subject: [PATCH] Add end of match score board --- mods/ctf_stats/gui.lua | 49 +++++++++++++++++++++++++++++++++++++++++ mods/ctf_stats/init.lua | 19 ++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 mods/ctf_stats/gui.lua diff --git a/mods/ctf_stats/gui.lua b/mods/ctf_stats/gui.lua new file mode 100644 index 0000000..0883722 --- /dev/null +++ b/mods/ctf_stats/gui.lua @@ -0,0 +1,49 @@ +function ctf_stats.get_formspec_match_summary(stats) + local players = {} + for name, pstat in pairs(stats.red) do + pstat.name = name + pstat.color = ctf.flag_colors.red + table.insert(players, pstat) + end + for name, pstat in pairs(stats.blue) do + pstat.name = name + pstat.color = ctf.flag_colors.blue + table.insert(players, pstat) + end + local ret = ctf_stats.get_formspec("Match Summary", players, stats) + 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 + pstat.score = pstat.captures + 0.2 * pstat.attempts + 7 * pstat.kills / (pstat.deaths + 1) + end + table.sort(players, function(one, two) + return (one.score > two.score) + end) + + 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 .. "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" + + for i, pstat in pairs(players) do + local color = pstat.color or "#ffffff" + ret = ret .. + "," .. string.gsub(color, "0x", "#") .. + "," .. pstat.name .. + "," .. pstat.kills .. + "," .. pstat.deaths .. + "," .. math.floor(pstat.kills / (pstat.deaths + 1)*10)/10 .. + "," .. pstat.captures .. + "," .. pstat.attempts .. + "," .. pstat.score + end + + ret = ret .. ";-1]" + ret = ret .. "button_exit[0.5,6;3,1;close;Close]" + return ret +end diff --git a/mods/ctf_stats/init.lua b/mods/ctf_stats/init.lua index 898ad35..8d72a95 100644 --- a/mods/ctf_stats/init.lua +++ b/mods/ctf_stats/init.lua @@ -66,7 +66,8 @@ ctf.register_on_join_team(function(name, tname) ctf_stats.current[tname][name] = { kills = 0, deaths = 0, - attempts = 0 + attempts = 0, + captures = 0 } end) @@ -74,12 +75,24 @@ ctf_match.register_on_skip_map(function() 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 + end +end) + ctf_match.register_on_winner(function(winner) ctf_stats.matches[winner .. "_wins"] = ctf_stats.matches[winner .. "_wins"] + 1 end) ctf_match.register_on_new_match(function() - -- TODO: create and show match report + local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current) + local players = minetest.get_connected_players() + for _, player in pairs(players) do + minetest.show_formspec(player:get_player_name(), "ctf_stats:eom", fs) + end ctf_stats.current = { red = {}, @@ -113,3 +126,5 @@ minetest.register_on_dieplayer(function(player) end) ctf_stats.load() + +dofile(minetest.get_modpath("ctf_stats").."/gui.lua")