Update ctf_metrics

This commit is contained in:
rubenwardy 2021-02-19 22:25:16 +00:00
parent a86bbdbe82
commit 25c9f09d6e
2 changed files with 59 additions and 35 deletions

View file

@ -1,44 +1,69 @@
if not minetest.global_exists("prometheus") then if not minetest.create_metric then
error("No metrics!")
return return
end 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) ctf.register_on_killedplayer(function(victim, killer, type)
kill_counter = kill_counter + 1 kill_counter:increment()
end) 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 sum = 0
local avg = 0 for _, player in pairs(minetest.get_connected_players()) do
local bins = { r050=0, r200=0, r5000=0, rest=0 } local total, _ = ctf_stats.player(player:get_player_name())
if #minetest.get_connected_players() > 0 then sum = sum + total.score
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()
end end
online_score:set(sum)
for key, value in pairs(bins) do match_time:set(ctf_match.get_match_duration() or 0)
prometheus.post("minetest_ctf_score_bins{rank=\"" .. key .. "\"}", value) end)
end
prometheus.post("minetest_ctf_score_total", sum)
prometheus.post("minetest_ctf_score_avg", avg)
minetest.after(15, step)
end
minetest.after(15, step)

View file

@ -1,3 +1,2 @@
name = ctf_metrics name = ctf_metrics
depends = ctf, ctf_stats depends = ctf, ctf_stats
optional_depends = prometheus