From 25c9f09d6e24b35713c21cb2a73d377e1327539b Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 19 Feb 2021 22:25:16 +0000 Subject: [PATCH] Update ctf_metrics --- mods/ctf/ctf_metrics/init.lua | 93 ++++++++++++++++++++++------------- mods/ctf/ctf_metrics/mod.conf | 1 - 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/mods/ctf/ctf_metrics/init.lua b/mods/ctf/ctf_metrics/init.lua index 36ebc02..f7c27be 100644 --- a/mods/ctf/ctf_metrics/init.lua +++ b/mods/ctf/ctf_metrics/init.lua @@ -1,44 +1,69 @@ -if not minetest.global_exists("prometheus") then +if not minetest.create_metric then + error("No metrics!") return end -local kill_counter = 0 +local storage = minetest.get_mod_storage() +local function counter(name, help) + local metric = minetest.create_metric("counter", name, help) + metric:increment(tonumber(storage:get(name) or 0)) + return { + get = function() + return metric:get() + end, + + increment = function(_, value) + metric:increment(value) + storage:set_string(name, tostring(metric:get())) + end, + } +end +local function gauge(name, help) + return minetest.create_metric("gauge", name, help) +end + + +-- +-- Kills +-- +local kill_counter = counter("ctf_kills", "Total kills") ctf.register_on_killedplayer(function(victim, killer, type) - kill_counter = kill_counter + 1 + kill_counter:increment() end) -local function step() - prometheus.post("minetest_kills", kill_counter) - kill_counter = 0 +-- +-- Damage +-- +local punch_counter = counter("ctf_punches", "Total punches") +local damage_counter = counter("ctf_damage_given", "Total damage given") +ctf.register_on_attack(function(_, _, _, _, _, damage) + punch_counter:increment() + damage_counter:increment(damage) +end) + +-- +-- Digs / places +-- +local dig_counter = counter("ctf_digs", "Total digs") +local place_counter = counter("ctf_places", "Total digs") +minetest.register_on_dignode(function() + dig_counter:increment() +end) +minetest.register_on_placenode(function() + place_counter:increment() +end) + + +local online_score = gauge("ctf_online_score", "Total score of online players") +local match_time = gauge("ctf_match_play_time", "Current time in match") +minetest.register_globalstep(function() local sum = 0 - local avg = 0 - local bins = { r050=0, r200=0, r5000=0, rest=0 } - if #minetest.get_connected_players() > 0 then - for _, player in pairs(minetest.get_connected_players()) do - local total, _ = ctf_stats.player(player:get_player_name()) - sum = sum + total.score - - if total.score > 174000 then - bins.r050 = bins.r050 + 1 - elseif total.score > 10000 then - bins.r200 = bins.r200 + 1 - elseif total.score > 1000 then - bins.r5000 = bins.r5000 + 1 - else - bins.rest = bins.rest + 1 - end - end - avg = sum / #minetest.get_connected_players() + for _, player in pairs(minetest.get_connected_players()) do + local total, _ = ctf_stats.player(player:get_player_name()) + sum = sum + total.score end + online_score:set(sum) - for key, value in pairs(bins) do - prometheus.post("minetest_ctf_score_bins{rank=\"" .. key .. "\"}", value) - end - - prometheus.post("minetest_ctf_score_total", sum) - prometheus.post("minetest_ctf_score_avg", avg) - - minetest.after(15, step) -end -minetest.after(15, step) + match_time:set(ctf_match.get_match_duration() or 0) +end) diff --git a/mods/ctf/ctf_metrics/mod.conf b/mods/ctf/ctf_metrics/mod.conf index 2a0f1f2..c333ae4 100644 --- a/mods/ctf/ctf_metrics/mod.conf +++ b/mods/ctf/ctf_metrics/mod.conf @@ -1,3 +1,2 @@ name = ctf_metrics depends = ctf, ctf_stats -optional_depends = prometheus