Remove extra whitespace in inventory_sync plugin

This commit is contained in:
Hornwitser committed 2021-08-13 06:22:45 +02:00
1 parent 4a29c13acb
commit cbfc0adf3d
7 files changed
+13 -13

No files matched your search

+1 -1
View File
@@ -45,6 +45,6 @@ This plugin does event based synchronization of inventories. The data flow is as
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. 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.
Communication between the server and instance goes over stdout or rcon, depending on the size of the data. 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. Communication between the instance and the master server goes over websockets.
Overall, we are able to achieve a latency between 3 and infinite ticks from server join, depending on the size of the inventory. The major limiter is rcon transfer speeds with larger inventories, especially if they contain blueprints. Overall, we are able to achieve a latency between 3 and infinite ticks from server join, depending on the size of the inventory. The major limiter is rcon transfer speeds with larger inventories, especially if they contain blueprints.
@@ -1,5 +1,5 @@
local inventories = { local inventories = {
defines.inventory.character_main, defines.inventory.character_main,
defines.inventory.character_guns, defines.inventory.character_guns,
defines.inventory.character_ammo, defines.inventory.character_ammo,
defines.inventory.character_armor, defines.inventory.character_armor,
@@ -77,7 +77,7 @@ function download_inventory(player_name, data, number, total)
end end
-- Load crafting queue -- Load crafting queue
load_crafting_queue(serialized_player.crafting_queue, player) load_crafting_queue(serialized_player.crafting_queue, player)
-- Misc -- Misc
player.cheat_mode = serialized_player.cheat_mode player.cheat_mode = serialized_player.cheat_mode
@@ -15,7 +15,7 @@ local handle_gui_events = function(event)
-- Handle inventory dirtying -- Handle inventory dirtying
if not global.inventory_sync.players[player.name].dirty_inventory then if not global.inventory_sync.players[player.name].dirty_inventory then
global.inventory_sync.players[player.name].dirty_inventory = true global.inventory_sync.players[player.name].dirty_inventory = true
-- Restore player character -- Restore player character
ensure_character(player) ensure_character(player)
end end
@@ -2,7 +2,7 @@
When a player leaves the game, serialize their inventory and upload it to the master. When a player leaves the game, serialize their inventory and upload it to the master.
When a player joins the game, wait until they have a character, then send a request to When a player joins the game, wait until they have a character, then send a request to
the master server for the players inventory. the master server for the players inventory.
When the master sends an inventory, parse it and give it to the player. When the master sends an inventory, parse it and give it to the player.
@@ -105,7 +105,7 @@ end
-- Upload inventory when a player leaves the game. Triggers on restart after crash if player was online during crash. -- Upload inventory when a player leaves the game. Triggers on restart after crash if player was online during crash.
inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) inventory_sync.events[defines.events.on_pre_player_left_game] = function(event)
-- for some reason, on_pre_player_left_game gets called before on_server_startup so global isn't ready yet -- for some reason, on_pre_player_left_game gets called before on_server_startup so global isn't ready yet
if not global.inventory_sync then if not global.inventory_sync then
log("ERROR: Global inventory sync not defined") log("ERROR: Global inventory sync not defined")
return return
end end
@@ -156,7 +156,7 @@ function inventory_sync.initiate_inventory_download(player)
global.inventory_sync.players[player.name].sync_start_tick = game.tick global.inventory_sync.players[player.name].sync_start_tick = game.tick
end end
function inventory_sync.check_inventory_download_failed() function inventory_sync.check_inventory_download_failed()
-- Used for letting the master report the inventory download failing, like when the master is offline or an -- Used for letting the master report the inventory download failing, like when the master is offline or an
-- error occurs somewhere in the server code. The player will be returned to a playable state. -- error occurs somewhere in the server code. The player will be returned to a playable state.
for name, data in pairs(global.inventory_sync.players) do for name, data in pairs(global.inventory_sync.players) do
if data.sync_start_tick ~= 0 and data.sync_start_tick <= game.tick - 600 then if data.sync_start_tick ~= 0 and data.sync_start_tick <= game.tick - 600 then
@@ -20,7 +20,7 @@ function save_crafts(event)
while player.crafting_queue_size > 0 do while player.crafting_queue_size > 0 do
local old_queue = player.crafting_queue local old_queue = player.crafting_queue
local queueItem = player.crafting_queue[1] local queueItem = player.crafting_queue[1]
-- Cancel craft -- Cancel craft
player.cancel_crafting { player.cancel_crafting {
index = 1, index = 1,
@@ -29,10 +29,10 @@ function save_crafts(event)
local rightmost_right_index = 0 -- 0 indexed since it is subtractive in a 1 indexed language local rightmost_right_index = 0 -- 0 indexed since it is subtractive in a 1 indexed language
local new_queue = player.crafting_queue local new_queue = player.crafting_queue
while while
new_queue ~= nil and new_queue ~= nil and
new_queue[#new_queue - rightmost_right_index] ~= nil and new_queue[#new_queue - rightmost_right_index] ~= nil and
new_queue[#new_queue - rightmost_right_index].count >= old_queue[#old_queue - rightmost_right_index].count new_queue[#new_queue - rightmost_right_index].count >= old_queue[#old_queue - rightmost_right_index].count
do do
rightmost_right_index = rightmost_right_index + 1 rightmost_right_index = rightmost_right_index + 1
end end
@@ -48,7 +48,7 @@ function save_crafts(event)
added = oldItem.count - newItem.count added = oldItem.count - newItem.count
if oldItem.recipe ~= newItem.recipe then if oldItem.recipe ~= newItem.recipe then
log("ERROR: Old item "..oldItem.recipe.." is not equal "..newItem.recipe) log("ERROR: Old item "..oldItem.recipe.." is not equal "..newItem.recipe)
end end
end end
-- If the last item we added was of the same type, merge them in the queue -- If the last item we added was of the same type, merge them in the queue
@@ -63,7 +63,7 @@ function save_crafts(event)
end end
-- game.print("Saved craft "..oldItem.recipe) -- game.print("Saved craft "..oldItem.recipe)
end end
-- Find amount of items added and remove from inventory -- Find amount of items added and remove from inventory
local new_items = inventory.get_contents() local new_items = inventory.get_contents()
local difference = {} local difference = {}
@@ -73,7 +73,7 @@ function upload_inventory(playerIndex)
end end
clusterio_api.send_json("inventory_sync_upload", serialized_player) clusterio_api.send_json("inventory_sync_upload", serialized_player)
-- Clear saved crafting queue -- Clear saved crafting queue
global.inventory_sync.saved_crafting_queue[player.name] = nil global.inventory_sync.saved_crafting_queue[player.name] = nil
end end