mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 07:24:53 +09:00
Changed usage of globals, worked around edge cases
This commit is contained in:
1 parent
f9ef2ebbe1
commit
98f801e244
11 files changed
+124
-62
No files matched your search
@@ -5,6 +5,7 @@ When a player leaves the game, serialize their inventory and upload it to the ma
|
||||
When a player joins the game, send a request to the master server for the players inventory.
|
||||
|
||||
When the master sends an inventory, parse it and give it to the player.
|
||||
If the master responds that its a new player, show a welcome message.
|
||||
|
||||
]]
|
||||
|
||||
@@ -12,42 +13,62 @@ local clusterio_api = require("modules/clusterio/api")
|
||||
local add_commands = require("modules/inventory_sync/commands")
|
||||
local upload_inventory = require("modules/inventory_sync/upload_inventory")
|
||||
local download_inventory = require("modules/inventory_sync/download_inventory")
|
||||
local welcome_new_player = require("modules/inventory_sync/welcome_new_player")
|
||||
|
||||
inventory_sync = {}
|
||||
inventory_sync.welcome_new_player = welcome_new_player
|
||||
-- This function is called by instance.js as /sc inventory_sync.download_inventory("danielv", Escaped JSON string, package_number, total_packages_count) with data from the master
|
||||
inventory_sync.download_inventory = download_inventory
|
||||
-- This function is called internally when a player leaves the game for their inventory to upload to the master
|
||||
inventory_sync.upload_inventory = upload_inventory
|
||||
|
||||
inventory_sync.add_commands = add_commands
|
||||
inventory_sync.events = {}
|
||||
inventory_sync.events[clusterio_api.events.on_server_startup] = function(event)
|
||||
-- set up global table
|
||||
if not global.inventory_sync then
|
||||
global.inventory_sync = {
|
||||
download_start_tick = {},
|
||||
saved_crafting_queue = {},
|
||||
download_cache = {},
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
-- Cleanup
|
||||
inventory_sync.events[defines.events.on_player_removed] = function(event)
|
||||
local player = game.get_player(event.player_index)
|
||||
|
||||
-- Remove inventory download performance counter
|
||||
global["inv_sync_download_start_tick "..player.name] = nil
|
||||
global.inventory_sync.download_start_tick[player.name] = nil
|
||||
-- Remove stored crafting queue
|
||||
global["saved_crafting_queue_"..player.name] = nil
|
||||
global.inventory_sync.saved_crafting_queue[player.name] = nil
|
||||
end
|
||||
|
||||
-- Download inventory from master
|
||||
inventory_sync.events[defines.events.on_player_joined_game] = function(event)
|
||||
local playerIndex = event.player_index
|
||||
inventory_sync.initiateInventoryDownload(playerIndex)
|
||||
inventory_sync.initiate_inventory_download(playerIndex)
|
||||
end
|
||||
|
||||
-- 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)
|
||||
local playerIndex = event.player_index
|
||||
inventory_sync.uploadInventory(playerIndex)
|
||||
global.download_cache[playerIndex] = "" -- Clear cache to avoid leaking memory to people quitting during download
|
||||
-- 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
|
||||
log("ERROR: Global inventory sync not defined")
|
||||
return
|
||||
end
|
||||
|
||||
local player_index = event.player_index
|
||||
inventory_sync.upload_inventory(player_index)
|
||||
global.inventory_sync.download_cache[player_index] = "" -- Clear cache to avoid leaking memory to people quitting during download
|
||||
end
|
||||
|
||||
function inventory_sync.initiateInventoryDownload(playerIndex)
|
||||
function inventory_sync.initiate_inventory_download(playerIndex)
|
||||
local player = game.get_player(playerIndex)
|
||||
player.print("Initiating inventory download...")
|
||||
global["inv_sync_download_start_tick "..player.name] = game.tick
|
||||
if global.download_cache == nil then
|
||||
global.download_cache = {}
|
||||
end
|
||||
global.download_cache[playerIndex] = ""
|
||||
global.inventory_sync.download_start_tick[player.name] = game.tick
|
||||
global.inventory_sync.download_cache[playerIndex] = ""
|
||||
clusterio_api.send_json("inventory_sync_download", {
|
||||
player_name = player.name
|
||||
})
|
||||
@@ -60,9 +81,4 @@ function inventory_sync.initiateInventoryDownload(playerIndex)
|
||||
if character ~= nil then character.destroy() end
|
||||
end
|
||||
|
||||
-- This function is called by instance.js as /sc inventory_sync.downloadInventory("danielv", Escaped JSON string, package_number, total_packages_count) with data from the master
|
||||
inventory_sync.downloadInventory = download_inventory
|
||||
-- This function is called internally when a player leaves the game for their inventory to upload to the master
|
||||
inventory_sync.uploadInventory = upload_inventory
|
||||
|
||||
return inventory_sync
|
||||
Reference in new issue
Block a user