summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2018-04-04 18:49:59 +0100
committerrubenwardy <rw@rubenwardy.com>2018-04-04 19:01:33 +0100
commit6c79a2f73fdf5f2a7e79da0f0291e61f7239f2ee (patch)
treeb109ea92e26b32c8989dcf37950727588440417d
parentf7956d97e2e3071718cff9562b86d332b48e6570 (diff)
Use mod_storage and add converter for awards.txt
-rw-r--r--api.lua64
1 files changed, 58 insertions, 6 deletions
diff --git a/api.lua b/api.lua
index d7d11e3..21f196b 100644
--- a/api.lua
+++ b/api.lua
@@ -20,24 +20,76 @@ awards.registered_awards = {}
awards.on = {}
awards.on_unlock = {}
+local storage = minetest.get_mod_storage()
+
-- Table Save Load Functions
function awards.save()
- local file = io.open(minetest.get_worldpath().."/awards.txt", "w")
- if file then
- file:write(minetest.serialize(awards.players))
- file:close()
+ storage:set_string("player_data", minetest.write_json(awards.players))
+end
+
+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
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
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
awards.players = table
+ convert_data()
+ else
+ awards.players = {}
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
- awards.players = {}
end
function awards.player(name)