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

+16 -21
View File
@@ -19,31 +19,26 @@ Substitute clusteriomaster with clusterioslave or clusterioctl if this a dedicat
## Method of operation
This plugin does event based synchronization of inventories. The data flow is as follows:
This plugin does event based synchronization of inventories.
The greatly simplified data flow is as follows:
1. Player joins server, gets put in spectator mode
1. Player joins server.
2. Scenario script asks master for exclusive access to the player.
3. Master grants exclusive access for the player to the instance.
4. Scenario script checks the access response and then acts according to the situation:
2. Lua script asks instance for inventory
1. If the player has no inventory on the master then the current inventory becomes the synced inventory and the process is done.
2. If the player inventory was previously uploaded and is the same as the one stored on the master then the current inventory becomes the synced inventory and the process is done.
3. If the player inventory was previously uploaded but is not the same then the player inventory is deleted and the player is turned into a spectator.
4. Otherwise the player inventory is kept.
3. Instance asks master for inventory
5. Scenario script asks for the player inventory from the master.
6. Master sends it to the scenario in a stream of chunks.
7. Scenario displays and updates a progress bar as each chunk is received.
8. Once all chunks have loaded the player's synced inventory is recreated from the data and the player can start playing.
4. Master does not find inventory, and responds to instance with undefined
5. Instance sets player to survival
6. Player keeps playing, and eventually leaves. Inventory is serialized and sent to instance through stdout/file
7. Instance sends inventory to master
8. Player joins a different server. Lua sends a request to instance, puts player into spectator mode
9. Instance sends request to master
10. Master responds with json data
11. Instance calls command in scenario script using rcon
12. Once the entire command is transfered, the scenario script clears the old inventory, applies the new inventory from the scenario script and sets the player to survival mode.
When the player leaves the inventory is uploaded if it's a synced inventory and the exclusive access the scenario script holds is released.
Should an error occur during this process the player is given the option to use a temporary inventory instead, which will be merged back into the synced inventory the next time the sync succeeds on that instance.
Communication between the server and instance goes over stdout or rcon, depending on the size of the data.
Communication between the instance and the master server goes over websockets.