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,76 +3,85 @@ 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
|
||||||
local table = minetest.deserialize(file:read("*all"))
|
return false
|
||||||
file:close()
|
end
|
||||||
if type(table) == "table" then
|
|
||||||
ctf.log("ctf_stats", "Migrating stats...")
|
|
||||||
ctf_stats.matches = table.matches
|
|
||||||
ctf_stats.players = table.players
|
|
||||||
|
|
||||||
for name, player_stats in pairs(ctf_stats.players) do
|
local table = minetest.deserialize(file:read("*all"))
|
||||||
if not player_stats.score or player_stats.score < 0 then
|
file:close()
|
||||||
player_stats.score = 0
|
os.remove(minetest.get_worldpath() .. "/ctf_stats.txt")
|
||||||
end
|
if type(table) ~= "table" then
|
||||||
if player_stats.score > 300 then
|
return false
|
||||||
player_stats.score = (player_stats.score - 300) / 30 + 300
|
end
|
||||||
end
|
|
||||||
if player_stats.score > 800 then
|
|
||||||
player_stats.score = 800
|
|
||||||
end
|
|
||||||
|
|
||||||
player_stats.wins = player_stats.wins or {}
|
ctf.log("ctf_stats", "Migrating stats...")
|
||||||
if player_stats.blue_wins then
|
ctf_stats.matches = table.matches
|
||||||
player_stats.wins.blue = player_stats.blue_wins
|
ctf_stats.players = table.players
|
||||||
player_stats.blue_wins = nil
|
|
||||||
end
|
|
||||||
if player_stats.red_wins then
|
|
||||||
player_stats.wins.red = player_stats.red_wins
|
|
||||||
player_stats.red_wins = nil
|
|
||||||
end
|
|
||||||
player_stats.wins.blue = player_stats.wins.blue or 0
|
|
||||||
player_stats.wins.red = player_stats.wins.red or 0
|
|
||||||
end
|
|
||||||
|
|
||||||
ctf_stats.matches.wins = ctf_stats.matches.wins or {
|
for name, player_stats in pairs(ctf_stats.players) do
|
||||||
red = ctf_stats.matches.red_wins or 0,
|
if not player_stats.score or player_stats.score < 0 then
|
||||||
blue = ctf_stats.matches.blue_wins or 0,
|
player_stats.score = 0
|
||||||
}
|
|
||||||
|
|
||||||
ctf.needs_save = true
|
|
||||||
end
|
end
|
||||||
os.remove(minetest.get_worldpath() .. "/ctf_stats.txt")
|
if player_stats.score > 300 then
|
||||||
else
|
player_stats.score = (player_stats.score - 300) / 30 + 300
|
||||||
|
end
|
||||||
|
if player_stats.score > 800 then
|
||||||
|
player_stats.score = 800
|
||||||
|
end
|
||||||
|
|
||||||
|
player_stats.wins = player_stats.wins or {}
|
||||||
|
if player_stats.blue_wins then
|
||||||
|
player_stats.wins.blue = player_stats.blue_wins
|
||||||
|
player_stats.blue_wins = nil
|
||||||
|
end
|
||||||
|
if player_stats.red_wins then
|
||||||
|
player_stats.wins.red = player_stats.red_wins
|
||||||
|
player_stats.red_wins = nil
|
||||||
|
end
|
||||||
|
player_stats.wins.blue = player_stats.wins.blue or 0
|
||||||
|
player_stats.wins.red = player_stats.wins.red or 0
|
||||||
|
end
|
||||||
|
|
||||||
|
ctf_stats.matches.wins = ctf_stats.matches.wins or {
|
||||||
|
red = ctf_stats.matches.red_wins or 0,
|
||||||
|
blue = ctf_stats.matches.blue_wins or 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctf.needs_save = true
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
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