Rework inventory_sync (#411)

Complete rework of the inventory synchronization to reduce the number of
times the inventory has to be downloaded and the possibilities for item
loss/duplication.

The basic concept is to acquire exclusive access to the player data
before downloading the data.  This prevents the player from logging in
to two servers at the same time and getting the synced inventory twice.
The acquisition will in most cases execute faster than the player can
provide input to the game on login, and if the player data on the master
isn't what was previously uploaded then the player inventory is deleted
and the player is turned into a spectator while the updated inventory is
being transferred into the game.  This model allows for the downloading
of the synced inventory to be skipped in the case the player joins the
same server they left from.

The synced data is expanded to include more than just the character and
inventory to properly handle sync of dead players.  If the player leaves
dead on one server this state will be saved and when the player joins
another server they continue off as dead.  The ghost, god, and spectator
controllers are supported, while the cutscene and editor controllers are
not.  For players in a cutscene when joining the acquisition will be
attempted immediately, but the scenario waits until the player is out of
the cutscene before acting upon the response to the acquisition.  For
players in the editor mode the scenario toggles them out of the mode
before syncing data.

Since uploads to the master can fail handling of this is also added.
The scenario will retry uploading the player data at exponentially
increasing intervals until it gets the message back that the upload
succeeded.  Since it's possible for servers to go down and stay down for
a long time the exclusive access held by these servers are released after
a timeout.  This means that a player could be playing and or have
uploaded newer data by the time the instance comes back online and tries
to upload the inventory, in this case the master will report the upload
succeeded but discard the data.  Instances may also upload old data if
they load back to an earlier save, which will also be discarded by the
master.

Other small improvements made by this rework:
- Add a retry button to the failed download dialog
- Update the existing progress dialog instead of destroying and
  recreating it every time.
- Automatically stuff the items from the temporary inventory into the
  synced inventory if they fit.
- Move player back to the position they started the download from if
  they were turned into a spectator for the download.
- Ensure the player has a character if the stored data is wiped.
- Add error checking of the RCON commands sent by the instance.
This commit is contained in:
Hornwitser authored and GitHub committed 2021-09-07 15:49:53 +02:00
1 parent f9a1e28074
commit 90dacd98b2
21 files changed
+1136 -434

No files matched your search

@@ -1,5 +1,8 @@
-- Show inventory download progressbar
function dialog_failed_download(player)
local ensure_character = require("modules/inventory_sync/ensure_character")
local dialog_failed_download = {}
function dialog_failed_download.create(player, acquire_response)
if player.gui.screen.dialog_failed_download then
player.gui.screen.dialog_failed_download.destroy()
end
@@ -15,10 +18,15 @@ function dialog_failed_download(player)
}
titlebar.drag_target = frame
local title_caption = "Inventory sync download failed"
if acquire_response.status == "busy" then
title_caption = "Inventory is in use on " .. acquire_response.message
end
local title = titlebar.add {
type = "label",
style = "frame_title",
caption = "Inventory sync download failed",
caption = title_caption,
}
title.drag_target = frame
@@ -39,22 +47,101 @@ function dialog_failed_download(player)
}
content.style.width = 400
local reason
if acquire_response.status == "timeout" then
reason = "Timed out fetching inventory from the master server."
elseif acquire_response.status == "busy" then
reason =
"Your synced inventory is in in use on " .. acquire_response.message .. ". " ..
"If you are logged in there you will have to log out there before you can sync the inventory here. " ..
"If you just logged out from there it might take some time before the inventory is available."
elseif acquire_response.status == "error" then
reason = "Error fetching inventory from the master server: " .. acquire_response.message .. "."
else
reason = "Unknown status " .. acquire_response.status .. " fetching inventory from the master server."
end
local p1 = content.add {
type = "label",
caption = "The inventory download couldn't complete. You can choose to keep playing with an empty inventory until the connection is reestablished and the inventory download completes. At that point your temporary inventory will be put in a chest.",
caption = reason
}
p1.style.single_line = false
p1.style.bottom_margin = 8
local abort = content.add {
local p2 = content.add {
type = "label",
caption =
"You may choose to retry fetching the inventory or play with a temporary inventory which will be merged " ..
"with your synced inventory the next time you join this server."
,
}
p2.style.single_line = false
p2.style.top_margin = 8
local dialog_row = frame.add {
type = "flow",
}
dialog_row.style.top_margin = 8
dialog_row.drag_target = frame
local abort = dialog_row.add {
name = "inventory_sync_failed_download_abort",
type = "button",
caption = "Continue with new inventory",
tooltip = "Create a new empty temporary inventory. Items you pick up will be placed in a chest when the sync completes.",
style = "back_button",
caption = "Use temporary inventory",
tooltip = "Items you obtain here will be merged with your synced inventory the next time you join this server.",
}
local dialog_filler = dialog_row.add {
type = "empty-widget",
style = "draggable_space_header",
}
dialog_filler.style.horizontally_stretchable = "on"
dialog_filler.style.right_margin = 4
dialog_filler.style.height = 28
dialog_filler.drag_target = frame
local retry = dialog_row.add {
name = "inventory_sync_failed_download_retry",
type = "button",
style = "forward_button",
caption = "Retry",
}
frame.force_auto_center()
player.opened = frame
end
dialog_failed_download.events = {
[defines.events.on_gui_click] = function(event)
if not event.element.valid then
return
end
if event.element.name == "inventory_sync_failed_download_abort" then
-- Give the player a charatcer and let them play with that
local player = game.get_player(event.player_index)
ensure_character(player)
player.gui.screen.dialog_failed_download.destroy()
global.inventory_sync.players[player.name].dirty = true
global.inventory_sync.players[player.name].sync = false
elseif event.element.name == "inventory_sync_failed_download_retry" then
-- Retry acquiring the player from the master
local player = game.get_player(event.player_index)
inventory_sync.acquire(player)
player.gui.screen.dialog_failed_download.destroy()
end
end,
[defines.events.on_player_left_game] = function(event)
local player = game.get_player(event.player_index)
if player.gui.screen.dialog_failed_download then
player.gui.screen.dialog_failed_download.destroy()
end
end,
}
return dialog_failed_download
@@ -1,25 +0,0 @@
local ensure_character = require("modules/inventory_sync/ensure_character")
-- on_gui_click
local handle_gui_events = function(event)
if not event.element.valid then
return
end
local player = game.get_player(event.player_index)
if event.element.name == "inventory_sync_failed_download_abort" then
-- Give the player a new inventory and mark it as dirty
-- Show dirty status and an explanation in a GUI
-- Remove GUI element
player.gui.screen.dialog_failed_download.destroy()
-- Handle inventory dirtying
if not global.inventory_sync.players[player.name].dirty_inventory then
global.inventory_sync.players[player.name].dirty_inventory = true
-- Restore player character
ensure_character(player)
end
end
end
return handle_gui_events
@@ -1,12 +1,7 @@
-- Show inventory download progressbar
function open_dialog(player, progress, total)
if player.gui.screen.inventory_sync_progress then
player.gui.screen.inventory_sync_progress.destroy()
end
if progress == total then
return -- Hide dialog on completed download
end
local progress_dialog = {}
function progress_dialog.create(player, progress, total)
local frame = player.gui.screen.add {
name = "inventory_sync_progress",
type = "frame",
@@ -34,15 +29,6 @@ function open_dialog(player, progress, total)
filler.style.height = 24
filler.drag_target = frame
titlebar.add {
name = "inventory_sync_progress_close_button",
type = "sprite-button",
sprite = "utility/close_white",
hovered_sprite = "utility/close_black",
clicked_sprite = "utility/close_black",
style = "frame_action_button",
}
local content = frame.add {
name = "inventory_sync_progress_dialog_content",
type = "frame",
@@ -53,7 +39,7 @@ function open_dialog(player, progress, total)
local p1 = content.add {
type = "label",
caption = "Downloading inventory chunk"..progress.."/"..total,
caption = "Downloading inventory chunk " .. progress .. " / " .. total,
}
p1.style.single_line = false
p1.style.bottom_margin = 8
@@ -68,4 +54,45 @@ function open_dialog(player, progress, total)
player.opened = frame
end
return open_dialog
function progress_dialog.remove(player)
if player.gui.screen.inventory_sync_progress then
player.gui.screen.inventory_sync_progress.destroy()
end
end
function progress_dialog.update(player, progress, total)
-- Be defensive here in case an old GUI is present
local frame = player.gui.screen.inventory_sync_progress
if not frame then
return false
end
local content = frame.inventory_sync_progress_dialog_content
if not content then
return false
end
local p1 = content.children[1]
if not p1 or p1.type ~= "label" then
return false
end
local progressbar = content.children[2]
if not progressbar or progressbar.type ~= "progressbar" then
return false
end
p1.caption = "Downloading inventory chunk " .. progress .. " / " .. total
progressbar.value = progress / total
return true
end
function progress_dialog.display(player, progress, total)
if not progress_dialog.update(player, progress, total) then
progress_dialog.remove(player)
progress_dialog.create(player, progress, total)
end
end
return progress_dialog
@@ -1,9 +0,0 @@
local function welcome_new_player(player_name)
local player = game.get_player(player_name)
if player == nil then return end
player.print("Welcome to the server!")
player.print("Your inventory will be automatically synchronized between servers in this cluster.")
player.print("To improve sync performance, please avoid storing blueprints in your inventory.")
end
return welcome_new_player