Fix crash in ctf_stats on new worlds
This commit is contained in:
parent
872150c0ca
commit
d46e705329
1 changed files with 62 additions and 53 deletions
|
@ -3,12 +3,19 @@ ctf_stats = {}
|
||||||
local storage = minetest.get_mod_storage()
|
local storage = minetest.get_mod_storage()
|
||||||
local data_to_persist = { "matches", "players" }
|
local data_to_persist = { "matches", "players" }
|
||||||
|
|
||||||
function ctf_stats.load()
|
function ctf_stats.load_legacy()
|
||||||
local file = io.open(minetest.get_worldpath() .. "/ctf_stats.txt", "r")
|
local file = io.open(minetest.get_worldpath() .. "/ctf_stats.txt", "r")
|
||||||
if file then
|
if not file then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
local table = minetest.deserialize(file:read("*all"))
|
local table = minetest.deserialize(file:read("*all"))
|
||||||
file:close()
|
file:close()
|
||||||
if type(table) == "table" then
|
os.remove(minetest.get_worldpath() .. "/ctf_stats.txt")
|
||||||
|
if type(table) ~= "table" then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
ctf.log("ctf_stats", "Migrating stats...")
|
ctf.log("ctf_stats", "Migrating stats...")
|
||||||
ctf_stats.matches = table.matches
|
ctf_stats.matches = table.matches
|
||||||
ctf_stats.players = table.players
|
ctf_stats.players = table.players
|
||||||
|
@ -43,36 +50,38 @@ function ctf_stats.load()
|
||||||
}
|
}
|
||||||
|
|
||||||
ctf.needs_save = true
|
ctf.needs_save = true
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
os.remove(minetest.get_worldpath() .. "/ctf_stats.txt")
|
|
||||||
else
|
function ctf_stats.load()
|
||||||
|
if not ctf_stats.load_legacy() then
|
||||||
for _, key in pairs(data_to_persist) do
|
for _, key in pairs(data_to_persist) do
|
||||||
ctf_stats[key] = minetest.parse_json(storage:get_string(key))
|
ctf_stats[key] = minetest.parse_json(storage:get_string(key))
|
||||||
end
|
end
|
||||||
ctf.needs_save = true
|
ctf.needs_save = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Make sure all tables are present
|
||||||
|
ctf_stats.players = ctf_stats.players or {}
|
||||||
|
ctf_stats.matches = ctf_stats.matches or {
|
||||||
|
wins = {
|
||||||
|
blue = 0,
|
||||||
|
red = 0,
|
||||||
|
},
|
||||||
|
skipped = 0,
|
||||||
|
}
|
||||||
|
ctf_stats.current = ctf_stats.current or {
|
||||||
|
red = {},
|
||||||
|
blue = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- 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
|
||||||
ctf_stats.players[name] = nil
|
ctf_stats.players[name] = nil
|
||||||
ctf.needs_save = true
|
ctf.needs_save = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ctf_stats.matches = ctf_stats.matches or {
|
|
||||||
wins = {
|
|
||||||
blue = 0,
|
|
||||||
red = 0,
|
|
||||||
},
|
|
||||||
skipped = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
ctf_stats.current = ctf_stats.current or {
|
|
||||||
red = {},
|
|
||||||
blue = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctf_stats.players = ctf_stats.players or {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ctf.register_on_save(function()
|
ctf.register_on_save(function()
|
||||||
|
|
Loading…
Reference in a new issue