Made inventories download in chunks to prevent blocking rcon

This commit is contained in:
Daniel committed 2021-07-12 22:05:54 +02:00
1 parent 6bb2c50e5c
commit c1d0404f23
4 files changed
+90 -62

No files matched your search

+20 -52
View File
@@ -8,36 +8,14 @@ const libLuaTools = require("@clusterio/lib/lua_tools");
class InstancePlugin extends libPlugin.BaseInstancePlugin {
async init() {
this.inventoryQueue = [];
// setInterval(() => {
// console.log("Connected")
// this.info.messages.upload.send(this.instance, {
// instance_id: this.instance.id,
// player: "danielv",
// inventory: "stuff",
// })
// },5000)
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).catch(err => this.logger.error(
`Error handling ipc-inventory_sync_download:\n${err.stack}`
))
);
}
onMasterConnectionEvent(event) {
console.log("Connectionevent", event);
if (event === "connect") {
}
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)
.catch(err => this.logger.error(`Error handling ipc-inventory_sync_download:\n${err.stack}`)));
}
async handleUpload(player) {
console.log("Uploading", player);
this.logger.verbose(`Uploading ${player.name} (${JSON.stringify(player).length / 1000}kB)`);
this.info.messages.upload.send(this.instance, {
instance_id: this.instance.id,
instance_name: this.instance.name,
@@ -47,39 +25,29 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
}
async handleDownload(player) {
console.log("Downloading", player.player_name);
this.logger.verbose(`Downloading ${player.player_name}`);
let response = await this.info.messages.download.send(this.instance, {
player_name: player.player_name,
});
if (response.inventory) {
console.log("Sending command");
await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}')`);
}
}
async chatEventHandler(message) {
// TODO check if cross server chat is enabled
let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`;
await this.sendRcon(`/sc game.print('${libLuaTools.escapeString(content)}')`, true);
}
sendChat(message) {
this.info.messages.chat.send(this.instance, {
instance_name: this.instance.name,
content: message,
});
}
async onOutput(output) {
if (output.type === "action" && output.action === "INVENTORY") {
if (this.slave.connector.connected) {
this.sendChat(output.message);
} else {
this.inventoryQueue.push(output.message);
const chunkSize = this.instance.config.get("inventory_sync.rcon_chunk_size");
const chunks = chunkify(chunkSize, 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.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}', ${i + 1}, ${chunks.length})`);
}
}
}
}
function chunkify(chunkSize, string) {
if (string.length <= chunkSize) {
return [string]
} else {
let chunk = string.substr(0, chunkSize)
return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))]
}
}
module.exports = {
InstancePlugin,