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 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")
|
||||
if file then
|
||||
if not file then
|
||||
return false
|
||||
end
|
||||
|
||||
local table = minetest.deserialize(file:read("*all"))
|
||||
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_stats.matches = table.matches
|
||||
ctf_stats.players = table.players
|
||||
|
@ -43,36 +50,38 @@ function ctf_stats.load()
|
|||
}
|
||||
|
||||
ctf.needs_save = true
|
||||
end
|
||||
os.remove(minetest.get_worldpath() .. "/ctf_stats.txt")
|
||||
else
|
||||
return true
|
||||
end
|
||||
|
||||
function ctf_stats.load()
|
||||
if not ctf_stats.load_legacy() then
|
||||
for _, key in pairs(data_to_persist) do
|
||||
ctf_stats[key] = minetest.parse_json(storage:get_string(key))
|
||||
end
|
||||
ctf.needs_save = true
|
||||
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
|
||||
if not player_stats.score or player_stats.score <= 0 then
|
||||
ctf_stats.players[name] = nil
|
||||
ctf.needs_save = true
|
||||
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
|
||||
|
||||
ctf.register_on_save(function()
|
||||
|
|
Loading…
Reference in a new issue