Use mod_storage and add converter for awards.txt

This commit is contained in:
rubenwardy 2018-04-04 18:49:59 +01:00
parent f7956d97e2
commit 6c79a2f73f

64
api.lua

@ -20,24 +20,76 @@ awards.registered_awards = {}
awards.on = {} awards.on = {}
awards.on_unlock = {} awards.on_unlock = {}
local storage = minetest.get_mod_storage()
-- Table Save Load Functions -- Table Save Load Functions
function awards.save() function awards.save()
local file = io.open(minetest.get_worldpath().."/awards.txt", "w") storage:set_string("player_data", minetest.write_json(awards.players))
if file then end
file:write(minetest.serialize(awards.players))
file:close() local function convert_data()
minetest.log("warning", "Importing awards data from previous version")
for name, data in pairs(awards.players) do
while name.name do
name = name.name
end
data.name = name
print("Converting data for " .. name)
-- Just rename counted
local counted = {
chats = "chat",
deaths = "death",
joins = "join",
}
for from, to in pairs(counted) do
data[to] = data[from]
data[from] = nil
end
-- Convert item db to new format
local counted_items = {
count = "dig",
place = "place",
craft = "craft",
}
for from, to in pairs(counted_items) do
local ret = {}
local count = 0
for modname, items in pairs(data[from]) do
for itemname, value in pairs(items) do
itemname = modname .. ":" .. itemname
local key = minetest.registered_aliases[itemname] or itemname
ret[key] = value
count = count + value
end
end
ret.__total = count
data[from] = nil
data[to] = ret
end
end end
end end
function awards.load() function awards.load()
local file = io.open(minetest.get_worldpath().."/awards.txt", "r") local old_save_path = minetest.get_worldpath().."/awards.txt"
local file = io.open(old_save_path, "r")
if file then if file then
local table = minetest.deserialize(file:read("*all")) local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then if type(table) == "table" then
awards.players = table awards.players = table
convert_data()
else
awards.players = {}
end end
file:close()
os.rename(old_save_path, minetest.get_worldpath().."/awards.bk.txt")
else
awards.players = minetest.parse_json(storage:get_string("player_data")) or {}
end end
awards.players = {}
end end
function awards.player(name) function awards.player(name)