From d717547d41d88ccf42df556c93e56076b956d6b6 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 20 Feb 2021 21:10:19 +0000 Subject: [PATCH] Add class choice metrics --- mods/ctf/ctf_metrics/init.lua | 43 +++++++++++++++++++++-------------- mods/ctf/ctf_metrics/mod.conf | 2 +- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/mods/ctf/ctf_metrics/init.lua b/mods/ctf/ctf_metrics/init.lua index 3eb26fa..6210ea3 100644 --- a/mods/ctf/ctf_metrics/init.lua +++ b/mods/ctf/ctf_metrics/init.lua @@ -2,23 +2,12 @@ if not minetest.create_metric then return end -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, - } +local function counter(...) + return minetest.create_metric("counter", ...) end -local function gauge(name, help) - return minetest.create_metric("gauge", name, help) +local function gauge(...) + return minetest.create_metric("gauge", ...) end @@ -54,15 +43,35 @@ minetest.register_on_placenode(function() end) +-- +-- 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 + 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 class_counts = {} 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) + local class = ctf_classes.get(player) + class_counts[class.name] = (class_counts[class.name] or 0) + 1 + end + + online_score:set(sum) match_time:set(ctf_match.get_match_duration() or 0) + + for _, class in ipairs(ctf_classes.__classes_ordered) do + class_gauges[class.name]:set(class_counts[class.name] or 0) + end end) diff --git a/mods/ctf/ctf_metrics/mod.conf b/mods/ctf/ctf_metrics/mod.conf index c333ae4..eac4cbd 100644 --- a/mods/ctf/ctf_metrics/mod.conf +++ b/mods/ctf/ctf_metrics/mod.conf @@ -1,2 +1,2 @@ name = ctf_metrics -depends = ctf, ctf_stats +depends = ctf, ctf_stats, ctf_classes