From f9ef2ebbe172616cd37299e428b94747b5136040 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 15 Jun 2021 23:33:58 +0200 Subject: [PATCH] Microoptimizations after code review --- packages/web_ui/src/util/websocket.jsx | 6 ----- plugins/inventory_sync/instance.js | 25 ++++++++++++------- plugins/inventory_sync/master.js | 6 ++--- .../module/download_inventory.lua | 7 +++--- .../inventory_sync/module/inventory_sync.lua | 4 +-- .../module/load_crafting_queue.lua | 3 --- .../inventory_sync/module/restore_crafts.lua | 2 +- plugins/inventory_sync/module/save_crafts.lua | 2 +- .../module/upload_inventory.lua | 2 +- 9 files changed, 27 insertions(+), 30 deletions(-) diff --git a/packages/web_ui/src/util/websocket.jsx b/packages/web_ui/src/util/websocket.jsx index b08b2fc6..6b3b983f 100644 --- a/packages/web_ui/src/util/websocket.jsx +++ b/packages/web_ui/src/util/websocket.jsx @@ -273,12 +273,6 @@ export class Control extends libLink.Link { handler(info); } } - if (info.slave_id !== undefined) { - let slaveHandlers = this.slaveLogHandlers.get(info.slave_id); - for (let handler of slaveHandlers || []) { - handler(info); - } - } } async onInstanceLog(id, handler) { diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index 8f400f03..79c96da3 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -5,6 +5,20 @@ const libPlugin = require("@clusterio/lib/plugin"); const libLuaTools = require("@clusterio/lib/lua_tools"); +/** + * Splits string into array of strings with max of a certain length + * @param {Number} chunkSize - Max length of each chunk + * @param {String} string - String to split into chunks + * @returns {String[]} Chunks + */ +function chunkify(chunkSize, string) { + if (string.length <= chunkSize) { + return [string]; + } + let chunk = string.substr(0, chunkSize); + return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))]; +} + class InstancePlugin extends libPlugin.BaseInstancePlugin { async init() { this.inventoryQueue = []; @@ -35,19 +49,12 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { this.logger.verbose(`Sending inventory for ${player.player_name} in ${chunks.length} chunks`); for (let i = 0; i < chunks.length; i++) { // this.logger.verbose(`Sending chunk ${i+1} of ${chunks.length}`) - await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}', ${i + 1}, ${chunks.length})`); + await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', ' + ${libLuaTools.escapeString(response.inventory)}', ${i + 1}, ${chunks.length})`); } } } } -function chunkify(chunkSize, string) { - if (string.length <= chunkSize) { - return [string]; - } - let chunk = string.substr(0, chunkSize); - return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))]; - -} module.exports = { InstancePlugin, diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index d2ffd5aa..bfd9e8f1 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -1,5 +1,5 @@ "use strict"; -const fs = require("fs-extra"); +const fs = require("fs").promises; const path = require("path"); const libPlugin = require("@clusterio/lib/plugin"); @@ -40,14 +40,14 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { } async uploadRequestHandler(message) { - console.log("Saving inventory for", message.data.player_name); + this.logger.verbose(`Saving inventory for ${message.data.player_name}`); this.inventories[message.data.player_name] = message.data.inventory; return { success: true }; } async downloadRequestHandler(message) { - console.log("Downloading inventory for", message.data.player_name); + this.logger.verbose(`Downloading inventory for ${message.data.player_name}`); return { player_name: message.data.player_name, inventory: this.inventories[message.data.player_name], diff --git a/plugins/inventory_sync/module/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua index e8c511b3..7dba1625 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -4,7 +4,7 @@ local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue" local inventories = require("modules/inventory_sync/define_player_inventories") function download_inventory(playerName, data, number, total) - local player = game.players[playerName] + local player = game.get_player(playerName) if player == nil then return end if number then if global.download_cache[playerName] == nil then @@ -16,7 +16,7 @@ function download_inventory(playerName, data, number, total) if number ~= total then -- Show progress in console - -- game.players[playerName].print("Downloaded "..(number + 1).."/"..total.." parts") + -- player.print("Downloaded "..(number + 1).."/"..total.." parts") -- Show progress in GUI progress_dialog(player, number+1, total) @@ -31,7 +31,6 @@ function download_inventory(playerName, data, number, total) -- Load downloaded inventory local serialized_player = game.json_to_table(global.download_cache[playerName]) global.download_cache[playerName] = nil -- remove data to lower mapsize - local player = game.players[playerName] -- Load inventories for _, inv in pairs(inventories) do local inventory = player.get_inventory(inv) @@ -87,7 +86,7 @@ function download_inventory(playerName, data, number, total) player.disable_flashlight() end - local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] + local startTick = global["inv_sync_download_start_tick "..player.name] local log_line = "Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks" player.print(log_line) log("[inventory_sync] "..log_line) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 97683ce7..0c5e65f3 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -19,7 +19,7 @@ inventory_sync.add_commands = add_commands inventory_sync.events = {} -- Cleanup inventory_sync.events[defines.events.on_player_removed] = function(event) - local player = game.players[event.player_index] + local player = game.get_player(event.player_index) -- Remove inventory download performance counter global["inv_sync_download_start_tick "..player.name] = nil @@ -41,7 +41,7 @@ inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) end function inventory_sync.initiateInventoryDownload(playerIndex) - local player = game.players[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 diff --git a/plugins/inventory_sync/module/load_crafting_queue.lua b/plugins/inventory_sync/module/load_crafting_queue.lua index f44d060b..91f43626 100644 --- a/plugins/inventory_sync/module/load_crafting_queue.lua +++ b/plugins/inventory_sync/module/load_crafting_queue.lua @@ -12,9 +12,6 @@ function load_crafting_queue(crafting_queue, player) -- Load crafting queue for _, queueItem in pairs(crafting_queue.crafting_queue) do - local recipe = game.recipe_prototypes[queueItem.recipe] - -- game.print("Crafting "..queueItem.recipe) - -- Start crafting (consume items) player.begin_crafting { count = queueItem.count, diff --git a/plugins/inventory_sync/module/restore_crafts.lua b/plugins/inventory_sync/module/restore_crafts.lua index 5b7650a9..09abb7f7 100644 --- a/plugins/inventory_sync/module/restore_crafts.lua +++ b/plugins/inventory_sync/module/restore_crafts.lua @@ -2,7 +2,7 @@ local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue" function restore_crafts(event) if event.player_index then - local player = game.players[event.player_index] + local player = game.get_player(event.player_index) local crafting_queue = global["saved_crafting_queue_"..player.name] if crafting_queue == nil then diff --git a/plugins/inventory_sync/module/save_crafts.lua b/plugins/inventory_sync/module/save_crafts.lua index 4e2cc1f2..87612616 100644 --- a/plugins/inventory_sync/module/save_crafts.lua +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -1,6 +1,6 @@ function save_crafts(event) if event.player_index then - local player = game.players[event.player_index] + local player = game.get_player(event.player_index) local crafting_queue = {} -- Avoid overwriting old save diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua index c3e6cc50..14c6bdae 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -4,7 +4,7 @@ local inventories = require("modules/inventory_sync/define_player_inventories") local save_crafts = require("modules/inventory_sync/save_crafts") function upload_inventory(playerIndex) - local player = game.players[playerIndex] + local player = game.get_player(playerIndex) local serialized_player = { name = player.name,