Improve match duration metric; move to ctf_match (#539)

This commit is contained in:
ANAND 2020-04-21 20:19:57 +05:30 committed by GitHub
parent ff483a2092
commit a417e857ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 11 deletions

View file

@ -15,7 +15,12 @@ ctf.register_on_init(function()
minetest.settings:set_bool("enable_pvp", true) minetest.settings:set_bool("enable_pvp", true)
end) 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.register_on_build_time_end(function()
ctf_match.match_start_time = os.time()
minetest.sound_play({name="ctf_match_attack"}, { gain = 1.0 }) minetest.sound_play({name="ctf_match_attack"}, { gain = 1.0 })
end) end)

View file

@ -94,3 +94,9 @@ ctf_flag.register_on_capture(function(attname, flag)
ctf_match.check_for_winner() ctf_match.check_for_winner()
end) 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

View file

@ -53,7 +53,7 @@ end
local function summary_func(name) local function summary_func(name)
local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, 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]" fs = fs .. "button[6,7.5;4,1;b_prev;<< Previous match]"

View file

@ -58,10 +58,13 @@ function ctf_stats.get_formspec_match_summary(stats, winner_team, winner_player,
blue.score = blue.score + pstat.score blue.score = blue.score + pstat.score
end end
local match_length = string.format("%02d:%02d:%02d", local match_length = "-"
math.floor(time / 3600), -- hours if time then
math.floor((time % 3600) / 60), -- minutes match_length = string.format("%02d:%02d:%02d",
math.floor(time % 60)) -- seconds 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) 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 >>]" fs = fs .. "button[6,7.5;4,1;b_curr;Current match >>]"
elseif fields.b_curr then elseif fields.b_curr then
fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, 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]" fs = fs .. "button[6,7.5;4,1;b_prev;<< Previous match]"
else else
return return

View file

@ -79,8 +79,6 @@ function ctf_stats.load()
blue = {} blue = {}
} }
ctf_stats.start = os.time()
-- Strip players which have no score -- Strip players which have no score
for name, player_stats in pairs(ctf_stats.players) do for name, player_stats in pairs(ctf_stats.players) do
if not player_stats.score or player_stats.score <= 0 then 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 -- Show match summary
local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, 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 for _, player in pairs(minetest.get_connected_players()) do
minetest.show_formspec(player:get_player_name(), "ctf_stats:eom", fs) 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 -- Show match summary
local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, 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 for _, player in pairs(minetest.get_connected_players()) do
minetest.show_formspec(player:get_player_name(), "ctf_stats:eom", fs) 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_team = "-"
ctf_stats.winner_player = "-" ctf_stats.winner_player = "-"
ctf_stats.start = os.time()
_needs_save = true _needs_save = true
end) 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 -- ctf_map can't be added as a dependency, as that'd result
-- in cyclic dependencies between ctf_map and ctf_stats -- in cyclic dependencies between ctf_map and ctf_stats
minetest.after(0, function() minetest.after(0, function()