Microoptimizations after code review

This commit is contained in:
Daniel committed 2021-07-12 22:05:54 +02:00
1 parent a939e11641
commit f9ef2ebbe1
9 files changed
+27 -30

No files matched your search

-6
View File
@@ -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) {
+16 -9
View File
@@ -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,
+3 -3
View File
@@ -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],
@@ -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)
@@ -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
@@ -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,
@@ -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
@@ -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
@@ -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,