Fix data converter

I used a old factions version and created two teams with land claims and ranks. It converted fine. Make sure you pick the same parcel size as the old save file because if they are not they don't show up in the same spot or not at all.
This commit is contained in:
Coder12a 2018-10-28 22:03:55 -05:00
parent c069828a28
commit bc54df4925
1 changed files with 86 additions and 55 deletions

View File

@ -127,8 +127,7 @@ end
function factions.start_diplomacy(name,faction) function factions.start_diplomacy(name,faction)
for i in pairs(factions.factions) do for i in pairs(factions.factions) do
if i ~= name and not (faction.neutral[i] or faction.allies[i] or faction.enemies[i]) then if i ~= name and not (faction.neutral[i] or faction.allies[i] or faction.enemies[i]) then
faction:new_enemy(factions.factions[i].name) faction:new_enemy(i)
factions.factions[i]:new_enemy(faction.name)
end end
end end
end end
@ -841,55 +840,70 @@ function factions.load()
local current_version = misc_mod_data.data.factions_version local current_version = misc_mod_data.data.factions_version
misc_mod_data.load() misc_mod_data.load()
local old_version = misc_mod_data.data.factions_version local old_version = misc_mod_data.data.factions_version
factions.factions = minetest.deserialize(raw_data) local tabledata = minetest.deserialize(raw_data)
if current_version ~= old_version or factions.is_old_file(factions.factions) then file:close()
if factions.convert(filename) then if tabledata then
minetest.after(5, factions.factions = tabledata
function() if current_version ~= old_version or factions.is_old_file(tabledata) then
minetest.chat_send_all("Factions successfully converted.") if factions.convert(filename) then
end) minetest.after(5,
factions.save() function()
minetest.chat_send_all("Factions successfully converted.")
end)
end
end end
for facname, faction in pairs(factions.factions) do
minetest.log("action", facname..","..faction.name)
for player, rank in pairs(faction.players) do
minetest.log("action", player..","..rank)
factions.players[player] = facname
end
for parcelpos, val in pairs(faction.land) do
factions.parcels[parcelpos] = facname
end
setmetatable(faction, factions.Faction)
if not faction.maxpower or faction.maxpower <= 0. then
faction.maxpower = faction.power
if faction.power < 0. then
faction.maxpower = 0.
end
end
if not faction.attacked_parcels then
faction.attacked_parcels = {}
end
if not faction.usedpower then
faction.usedpower = faction:count_land() * factions_config.power_per_parcel
end
if #faction.name > factions_config.faction_name_max_length then
faction:disband()
end
if not faction.last_logon then
faction.last_logon = os.time()
end
if faction.no_parcel ~= -1 then
faction.no_parcel = os.time()
end
if faction:count_land() > 0 then
faction.no_parcel = -1
end
if faction.onlineplayers and faction.offlineplayers then
for i, _ in pairs(faction.onlineplayers) do
faction.offlineplayers[i] = _
end
else
faction.offlineplayers = {}
end
faction.onlineplayers = {}
end
misc_mod_data.data.factions_version = current_version
misc_mod_data.save()
factions.save()
else
minetest.after(5,
function()
minetest.chat_send_all("Failed to deserialize saved file.")
end)
end end
for facname, faction in pairs(factions.factions) do
minetest.log("action", facname..","..faction.name)
for player, rank in pairs(faction.players) do
minetest.log("action", player..","..rank)
factions.players[player] = facname
end
for parcelpos, val in pairs(faction.land) do
factions.parcels[parcelpos] = facname
end
setmetatable(faction, factions.Faction)
if not faction.maxpower or faction.maxpower <= 0. then
faction.maxpower = faction.power
if faction.power < 0. then
faction.maxpower = 0.
end
end
if not faction.attacked_parcels then
faction.attacked_parcels = {}
end
if not faction.usedpower then
faction.usedpower = faction:count_land() * factions_config.power_per_parcel
end
if #faction.name > factions_config.faction_name_max_length then
faction:disband()
end
if not faction.last_logon then
faction.last_logon = os.time()
end
if faction.no_parcel ~= -1 then
faction.no_parcel = os.time()
end
for i, _ in pairs(faction.onlineplayers) do
faction.offlineplayers[i] = _
end
faction.onlineplayers = {}
end
file:close()
misc_mod_data.data.factions_version = current_version
misc_mod_data.save()
end end
factions_ip.load() factions_ip.load()
end end
@ -931,23 +945,40 @@ function factions.convert(filename)
return false return false
end end
local raw_data = file:read("*a") local raw_data = file:read("*a")
file:close()
local data = minetest.deserialize(raw_data) local data = minetest.deserialize(raw_data)
for facname,faction in pairs(data) do for facname,faction in pairs(data) do
local newfac = factions.new_faction(facname) local newfac = factions.new_faction(facname,true)
for oi, ol in pairs(faction) do for oi, ol in pairs(faction) do
if newfac[oi] then if newfac[oi] then
newfac[oi] = ol newfac[oi] = ol
end end
end end
for player, unused in pairs(newfac.players) do if faction.players then
factions.players[player] = newfac.name newfac.players = faction.players
end end
for land, unused in pairs(newfac.land) do if faction.land then
factions.parcels[land] = newfac.name newfac.land = faction.land
end
if faction.ranks then
newfac.ranks = faction.ranks
end end
factions.start_diplomacy(facname,newfac) factions.start_diplomacy(facname,newfac)
newfac:check_power() newfac:check_power()
end end
-- Create runtime data.
for facname,faction in pairs(factions.factions) do
if faction.players then
for player, unused in pairs(faction.players) do
factions.players[player] = faction.name
end
end
if faction.land then
for l, unused in pairs(faction.land) do
factions.parcels[l] = facname
end
end
end
return true return true
end end
@ -1001,7 +1032,7 @@ function(player)
if faction:has_permission(name, "diplomacy") then if faction:has_permission(name, "diplomacy") then
for _ in pairs(faction.request_inbox) do minetest.chat_send_player(name,"You have diplomatic requests in the inbox.") break end for _ in pairs(faction.request_inbox) do minetest.chat_send_player(name,"You have diplomatic requests in the inbox.") break end
end end
if faction.message_of_the_day ~= "" or faction.message_of_the_day ~= " " then if faction.message_of_the_day and (faction.message_of_the_day ~= "" or faction.message_of_the_day ~= " ") then
minetest.chat_send_player(name,faction.message_of_the_day) minetest.chat_send_player(name,faction.message_of_the_day)
end end
end end