capturetheflag/mods/ctf/ctf_metrics/init.lua

69 lines
1.6 KiB
Lua
Raw Normal View History

2021-02-19 22:25:16 +00:00
if not minetest.create_metric then
2017-11-07 01:20:34 +00:00
return
end
2021-02-19 22:25:16 +00:00
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")
2017-11-07 01:20:34 +00:00
ctf.register_on_killedplayer(function(victim, killer, type)
2021-02-19 22:25:16 +00:00
kill_counter:increment()
2017-11-07 01:20:34 +00:00
end)
2021-02-19 22:25:16 +00:00
--
-- 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)
2017-11-07 01:20:34 +00:00
2021-02-19 22:25:16 +00:00
--
-- 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)
2019-01-11 16:29:36 +00:00
2017-11-07 01:20:34 +00:00
2021-02-19 22:25:16 +00:00
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
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)
match_time:set(ctf_match.get_match_duration() or 0)
end)