mail/persistence.lua

44 lines
954 B
Lua
Raw Normal View History

2018-12-05 11:06:25 +01:00
function mail.load()
2019-01-12 22:20:40 +00:00
local file = io.open(minetest.get_worldpath().."/mail.db", "r")
2018-12-05 11:06:25 +01:00
if file then
local data = file:read("*a")
mail.messages = minetest.deserialize(data)
file:close()
end
end
2018-12-05 11:09:21 +01:00
function save_json()
2019-01-12 22:20:40 +00:00
local file = io.open(minetest.get_worldpath().."/mail.json", "w")
if file and file:write(minetest.write_json(mail.messages)) and file:close() then
return true
else
minetest.log("error","[mail] Json-Save failed - messages may be lost!")
return false
end
2018-12-05 11:09:21 +01:00
end
2018-12-05 11:06:25 +01:00
function mail.save()
2018-12-05 11:09:21 +01:00
save_json()
2018-12-05 11:06:25 +01:00
local file = io.open(minetest.get_worldpath().."/mail.db","w")
if file and file:write(minetest.serialize(mail.messages)) and file:close() then
return true
else
minetest.log("error","[mail] Save failed - messages may be lost!")
return false
end
2018-12-12 13:28:40 +01:00
end
-- save periodically
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
2019-01-12 22:20:40 +00:00
if timer < 30 then
return
end
2018-12-12 13:28:40 +01:00
2019-01-12 22:20:40 +00:00
timer = 0
2018-12-12 13:28:40 +01:00
mail.save()
end)