Skip to content

Commit

Permalink
Fix summary not stored before restart
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
ClobberXD authored and rubenwardy committed Nov 14, 2018
1 parent b28f5b9 commit 66a8a73
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
18 changes: 0 additions & 18 deletions mods/ctf_stats/gui.lua
Original file line number Diff line number Diff line change
@@ -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
local tablecolumns = {
"tablecolumns[color;",
Expand Down Expand Up @@ -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[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
end

Expand Down Expand Up @@ -375,14 +368,3 @@ minetest.register_chatcommand("transfer_rankings", {
return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'."
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
})
45 changes: 36 additions & 9 deletions mods/ctf_stats/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ ctf.register_on_join_team(function(name, tname)
}
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_player = "-"

Expand All @@ -159,19 +154,41 @@ ctf_flag.register_on_capture(function(name, flag)
winner_player = name
end)

local prev_match_summary = storage:get_string("prev_match_summary")
ctf_match.register_on_winner(function(winner)
ctf.needs_save = true
ctf_stats.matches.wins[winner] = ctf_stats.matches.wins[winner] + 1
winner_team = winner

-- 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()
local fs = ctf_stats.get_formspec_match_summary(ctf_stats.current, winner_team, winner_player, os.time()-ctf_stats.start)
local players = minetest.get_connected_players()
for _, player in pairs(players) do
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 = {
red = {},
blue = {}
Expand Down Expand Up @@ -291,6 +308,16 @@ minetest.register_on_dieplayer(function(player)
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()

dofile(minetest.get_modpath("ctf_stats").."/gui.lua")

0 comments on commit 66a8a73

Please sign in to comment.