Fix summary not stored before restart

- Match summary is now shown at the end of the going match (using `registered_on_winner` and `register_on_skip_map` callbacks), instead of at the start of the next match.
- Therefore, `prev_match_summary` is now preserved even after restart.
- Also fixes the "Can't initialize mod storage twice" error which occurred due to mod storage also being initialized in `gui.lua` to set/get summary from mod storage. Now, the code for storing and retrieving `prev_match_summary` has been moved to init.lua itself.
This commit is contained in:
ClobberXD 2018-11-14 18:18:05 +05:30 committed by rubenwardy
parent b28f5b94bb
commit 66a8a73c68
2 changed files with 37 additions and 28 deletions

View file

@ -1,6 +1,3 @@
local storage = minetest.get_mod_storage()
local prev_match_summary = storage:get_string("prev_match_summary")
-- Formspec element that governs table columns and their attributes -- Formspec element that governs table columns and their attributes
local tablecolumns = { local tablecolumns = {
"tablecolumns[color;", "tablecolumns[color;",
@ -86,10 +83,6 @@ function ctf_stats.get_formspec_match_summary(stats, winner_team, winner_player,
ret = ret .. "label[12,0.5;" .. render_team_stats(red, blue, "score", true) .. "]" ret = ret .. "label[12,0.5;" .. render_team_stats(red, blue, "score", true) .. "]"
ret = ret .. "label[2,7.75;Tip: type /rankings for league tables]" ret = ret .. "label[2,7.75;Tip: type /rankings for league tables]"
-- Set prev_match_summary and write to mod_storage
prev_match_summary = ret
storage:set_string("prev_match_summary", ret)
return ret return ret
end end
@ -375,14 +368,3 @@ minetest.register_chatcommand("transfer_rankings", {
return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'." return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'."
end end
}) })
minetest.register_chatcommand("summary", {
description = "Display the scoreboard of the previous match.",
func = function (name, param)
if not prev_match_summary then
return false, "Couldn't find the requested data."
end
minetest.show_formspec(name, "ctf_stats:prev_match_summary", prev_match_summary)
end
})

View file

@ -139,11 +139,6 @@ ctf.register_on_join_team(function(name, tname)
} }
end) end)
ctf_match.register_on_skip_map(function()
ctf.needs_save = true
ctf_stats.matches.skipped = ctf_stats.matches.skipped + 1
end)
local winner_team = "-" local winner_team = "-"
local winner_player = "-" local winner_player = "-"
@ -159,19 +154,41 @@ ctf_flag.register_on_capture(function(name, flag)
winner_player = name winner_player = name
end) end)
local prev_match_summary = storage:get_string("prev_match_summary")
ctf_match.register_on_winner(function(winner) ctf_match.register_on_winner(function(winner)
ctf.needs_save = true ctf.needs_save = true
ctf_stats.matches.wins[winner] = ctf_stats.matches.wins[winner] + 1 ctf_stats.matches.wins[winner] = ctf_stats.matches.wins[winner] + 1
winner_team = winner winner_team = winner
end)
ctf_match.register_on_new_match(function() -- Show match summary
local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, winner_team, winner_player, os.time()-ctf_stats.start) local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current,
local players = minetest.get_connected_players() winner_team, winner_player, os.time()-ctf_stats.start)
for _, player in pairs(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)
end end
-- Set prev_match_summary and write to mod_storage
prev_match_summary = fs
storage:set_string("prev_match_summary", fs)
end)
ctf_match.register_on_skip_map(function()
ctf.needs_save = true
ctf_stats.matches.skipped = ctf_stats.matches.skipped + 1
-- Show match summary
local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current,
winner_team, winner_player, os.time()-ctf_stats.start)
for _, player in pairs(minetest.get_connected_players()) do
minetest.show_formspec(player:get_player_name(), "ctf_stats:eom", fs)
end
-- Set prev_match_summary and write to mod_storage
prev_match_summary = fs
storage:set_string("prev_match_summary", fs)
end)
ctf_match.register_on_new_match(function()
ctf_stats.current = { ctf_stats.current = {
red = {}, red = {},
blue = {} blue = {}
@ -291,6 +308,16 @@ minetest.register_on_dieplayer(function(player)
end end
end) end)
minetest.register_chatcommand("summary", {
func = function (name, param)
if not prev_match_summary then
return false, "Couldn't find the requested data."
end
minetest.show_formspec(name, "ctf_stats:prev_match_summary", prev_match_summary)
end
})
ctf_stats.load() ctf_stats.load()
dofile(minetest.get_modpath("ctf_stats").."/gui.lua") dofile(minetest.get_modpath("ctf_stats").."/gui.lua")