From a417e857ef8630cbe518d3cb3e88cbf1c14956fe Mon Sep 17 00:00:00 2001 From: ANAND Date: Tue, 21 Apr 2020 20:19:57 +0530 Subject: [PATCH] Improve match duration metric; move to ctf_match (#539) --- mods/ctf/ctf_match/init.lua | 5 +++++ mods/ctf/ctf_match/matches.lua | 6 ++++++ mods/ctf/ctf_stats/chat.lua | 2 +- mods/ctf/ctf_stats/gui.lua | 13 ++++++++----- mods/ctf/ctf_stats/init.lua | 12 +++++++----- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/mods/ctf/ctf_match/init.lua b/mods/ctf/ctf_match/init.lua index 2dde42f..e2f8dab 100644 --- a/mods/ctf/ctf_match/init.lua +++ b/mods/ctf/ctf_match/init.lua @@ -15,7 +15,12 @@ ctf.register_on_init(function() minetest.settings:set_bool("enable_pvp", true) end) +ctf_match.register_on_build_time_start(function() + ctf_match.match_start_time = nil +end) + ctf_match.register_on_build_time_end(function() + ctf_match.match_start_time = os.time() minetest.sound_play({name="ctf_match_attack"}, { gain = 1.0 }) end) diff --git a/mods/ctf/ctf_match/matches.lua b/mods/ctf/ctf_match/matches.lua index b81fcf1..c649927 100644 --- a/mods/ctf/ctf_match/matches.lua +++ b/mods/ctf/ctf_match/matches.lua @@ -94,3 +94,9 @@ ctf_flag.register_on_capture(function(attname, flag) ctf_match.check_for_winner() end) + +ctf_match.match_start_time = nil +function ctf_match.get_match_duration() + return ctf_match.match_start_time and + (os.time() - ctf_match.match_start_time) +end diff --git a/mods/ctf/ctf_stats/chat.lua b/mods/ctf/ctf_stats/chat.lua index 98c5602..28142d6 100644 --- a/mods/ctf/ctf_stats/chat.lua +++ b/mods/ctf/ctf_stats/chat.lua @@ -53,7 +53,7 @@ end local function summary_func(name) local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, - ctf_stats.winner_team, ctf_stats.winner_player, os.time() - ctf_stats.start) + ctf_stats.winner_team, ctf_stats.winner_player, ctf_match.get_match_duration()) fs = fs .. "button[6,7.5;4,1;b_prev;<< Previous match]" diff --git a/mods/ctf/ctf_stats/gui.lua b/mods/ctf/ctf_stats/gui.lua index 8e3b7bd..fd26da5 100644 --- a/mods/ctf/ctf_stats/gui.lua +++ b/mods/ctf/ctf_stats/gui.lua @@ -58,10 +58,13 @@ function ctf_stats.get_formspec_match_summary(stats, winner_team, winner_player, blue.score = blue.score + pstat.score end - local match_length = string.format("%02d:%02d:%02d", - math.floor(time / 3600), -- hours - math.floor((time % 3600) / 60), -- minutes - math.floor(time % 60)) -- seconds + local match_length = "-" + if time then + match_length = string.format("%02d:%02d:%02d", + math.floor(time / 3600), -- hours + math.floor((time % 3600) / 60), -- minutes + math.floor(time % 60)) -- seconds + end local ret = ctf_stats.get_formspec("Match Summary", players, 1) @@ -242,7 +245,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) fs = fs .. "button[6,7.5;4,1;b_curr;Current match >>]" elseif fields.b_curr then fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, - ctf_stats.winner_team, ctf_stats.winner_player, os.time() - ctf_stats.start) + ctf_stats.winner_team, ctf_stats.winner_player, ctf_match.get_match_duration()) fs = fs .. "button[6,7.5;4,1;b_prev;<< Previous match]" else return diff --git a/mods/ctf/ctf_stats/init.lua b/mods/ctf/ctf_stats/init.lua index 1e2e5ac..480787d 100644 --- a/mods/ctf/ctf_stats/init.lua +++ b/mods/ctf/ctf_stats/init.lua @@ -79,8 +79,6 @@ function ctf_stats.load() blue = {} } - ctf_stats.start = os.time() - -- Strip players which have no score for name, player_stats in pairs(ctf_stats.players) do if not player_stats.score or player_stats.score <= 0 then @@ -241,7 +239,7 @@ ctf_match.register_on_winner(function(winner) -- Show match summary local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, - ctf_stats.winner_team, ctf_stats.winner_player, os.time() - ctf_stats.start) + ctf_stats.winner_team, ctf_stats.winner_player, ctf_match.get_match_duration()) for _, player in pairs(minetest.get_connected_players()) do minetest.show_formspec(player:get_player_name(), "ctf_stats:eom", fs) @@ -258,7 +256,7 @@ ctf_match.register_on_skip_match(function() -- Show match summary local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, - ctf_stats.winner_team, ctf_stats.winner_player, os.time() - ctf_stats.start) + ctf_stats.winner_team, ctf_stats.winner_player, ctf_match.get_match_duration()) for _, player in pairs(minetest.get_connected_players()) do minetest.show_formspec(player:get_player_name(), "ctf_stats:eom", fs) @@ -276,10 +274,14 @@ ctf_match.register_on_new_match(function() } ctf_stats.winner_team = "-" ctf_stats.winner_player = "-" - ctf_stats.start = os.time() _needs_save = true end) +ctf_stats.start = nil +ctf_match.register_on_build_time_end(function() + ctf_stats.start = os.time() +end) + -- ctf_map can't be added as a dependency, as that'd result -- in cyclic dependencies between ctf_map and ctf_stats minetest.after(0, function()