Changed inventory storage to use Map

This commit is contained in:
danielv authored and Daniel committed 2021-07-12 22:05:54 +02:00
1 parent 98f801e244
commit 37052e1f68
3 files changed
+15 -24

No files matched your search

+4 -8
View File
@@ -12,16 +12,11 @@ const libLuaTools = require("@clusterio/lib/lua_tools");
* @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, ""))];
return string.match(new RegExp(`.{1,${chunkSize}}`, "g"));
}
class InstancePlugin extends libPlugin.BaseInstancePlugin {
async init() {
this.inventoryQueue = [];
this.instance.server.on("ipc-inventory_sync_upload", content => this.handleUpload(content)
.catch(err => this.logger.error(`Error handling ipc-inventory_sync_upload:\n${err.stack}`)));
this.instance.server.on("ipc-inventory_sync_download", content => this.handleDownload(content)
@@ -44,14 +39,15 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
player_name: player.player_name,
});
if (response.new_player) {
await this.sendRcon(`/sc inventory_sync.welcome_new_player('${response.player_name}')`)
await this.sendRcon(`/sc inventory_sync.welcome_new_player('${response.player_name}')`);
} else if (response.inventory) {
const chunkSize = this.instance.config.get("inventory_sync.rcon_chunk_size");
const chunks = chunkify(chunkSize, JSON.stringify(response.inventory));
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.download_inventory('${response.player_name}', '${libLuaTools.escapeString(chunks[i])}', ${i + 1}, ${chunks.length})`);
await this.sendRcon(`/sc inventory_sync.download_inventory('${response.player_name}',` +
`'${libLuaTools.escapeString(chunks[i])}', ${i + 1}, ${chunks.length})`, true);
}
}
}