capturetheflag/mods/ctf/ctf_metrics/init.lua

78 lines
1.8 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
2021-02-20 21:10:19 +00:00
local function counter(...)
return minetest.create_metric("counter", ...)
2021-02-19 22:25:16 +00:00
end
2021-02-20 21:10:19 +00:00
local function gauge(...)
return minetest.create_metric("gauge", ...)
2021-02-19 22:25:16 +00:00
end
--
-- Kills
--
local kill_counter = counter("ctf_kills", "Total kills")
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-20 21:10:19 +00:00
--
-- Gauges
--
local class_gauges = {}
for _, class in ipairs(ctf_classes.__classes_ordered) do
class_gauges[class.name] = gauge("ctf_class_players", "Player count for class", {
class = class.name
})
end
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")
2021-02-20 21:10:19 +00:00
2021-02-19 22:25:16 +00:00
minetest.register_globalstep(function()
local sum = 0
2021-02-20 21:10:19 +00:00
local class_counts = {}
2021-02-19 22:25:16 +00:00
for _, player in pairs(minetest.get_connected_players()) do
local total, _ = ctf_stats.player(player:get_player_name())
sum = sum + total.score
2021-02-20 21:10:19 +00:00
local class = ctf_classes.get(player)
class_counts[class.name] = (class_counts[class.name] or 0) + 1
2021-02-19 22:25:16 +00:00
end
2021-02-20 21:10:19 +00:00
online_score:set(sum)
2021-02-19 22:25:16 +00:00
match_time:set(ctf_match.get_match_duration() or 0)
2021-02-20 21:10:19 +00:00
for _, class in ipairs(ctf_classes.__classes_ordered) do
class_gauges[class.name]:set(class_counts[class.name] or 0)
end
2021-02-19 22:25:16 +00:00
end)