Save edited boxes at restart. Make the shutdown of the db a function that is called at the end of the boxes shutdown procedure.

This commit is contained in:
Ekdohibs 2017-01-27 15:41:02 +01:00
parent 5fce0f413c
commit cf168c7ab1
2 changed files with 33 additions and 16 deletions

View File

@ -443,11 +443,6 @@ minetest.register_on_punchnode(function(pos, node, puncher, pointed_thing)
end)
-- TODO: the boxes players are editing are probably one of the
-- only things which should survive a restart (because we want players to
-- be able to edit their boxes over some time, and release it when they are
-- ready). This also implies saving the box allocation tree.
local players_editing_boxes = {}
-- FIXME: get that from db each time, but how?
local min_free_id = 0
@ -684,10 +679,31 @@ minetest.register_chatcommand("edite", {
end,
})
function boxes.save_edit(player, id)
local name = player:get_player_name()
local box = players_editing_boxes[name]
if not box then return end
if id == nil then
id = box.box_id
end
db.box_set_data(id, boxes.save(box.minp, box.maxp))
end
function boxes.stop_edit(player)
local name = player:get_player_name()
local box = players_editing_boxes[name]
if not box then return end
boxes.cleanup(box.minp, box.maxp)
boxes.vfree(box.minp)
players_editing_boxes[name] = nil
end
minetest.register_chatcommand("save", {
params = "[<id>]",
description = "Save the box you are currently editing.",
description = "Save the box you are currently editing. If id is supplied, a copy is instead saved to the box numbered id.",
privs = {server = true},
func = function(name, param)
if not players_editing_boxes[name] then
@ -695,8 +711,7 @@ minetest.register_chatcommand("save", {
return
end
local box = players_editing_boxes[name]
local box_id = box.box_id
local box_id = nil
if param ~= "" then
local id = tonumber(param)
if not id or id ~= math.floor(id) or id < 0 then
@ -706,7 +721,7 @@ minetest.register_chatcommand("save", {
box_id = id
end
db.box_set_data(box_id, boxes.save(box.minp, box.maxp))
boxes.save_edit(minetest.get_player_by_name(name), box_id)
end,
})
@ -720,17 +735,18 @@ minetest.register_chatcommand("stopedit", {
return
end
local box = players_editing_boxes[name]
boxes.cleanup(box.minp, box.maxp)
boxes.vfree(box.minp)
players_editing_boxes[name] = nil
players.return_to_lobby(minetest.get_player_by_name(name))
local player = minetest.get_player_by_name(name)
boxes.save_edit(player)
boxes.stop_edit(player)
players.return_to_lobby(player)
end,
})
local function on_leaveplayer(player)
print("leave", player:get_player_name())
boxes.close_box(player)
boxes.save_edit(player)
boxes.stop_edit(player)
end
minetest.register_on_leaveplayer(on_leaveplayer)
@ -738,4 +754,5 @@ minetest.register_on_shutdown(function()
for _, player in ipairs(minetest.get_connected_players()) do
on_leaveplayer(player)
end
db.shutdown()
end)

View File

@ -169,6 +169,6 @@ function db.box_set_meta(box_id, meta)
end
end
minetest.register_on_shutdown(function()
function db.shutdown()
itb_db:close()
end)
end