mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 15:24:52 +09:00
Changed inventory storage to use Map
This commit is contained in:
1 parent
98f801e244
commit
37052e1f68
3 files changed
+15
-24
No files matched your search
@@ -59,9 +59,6 @@ module.exports = {
|
||||
"player_name": { type: "string" },
|
||||
"inventory": { type: "object" },
|
||||
},
|
||||
responseProperties: {
|
||||
"success": { type: "boolean" },
|
||||
},
|
||||
}),
|
||||
download: new libLink.Request({
|
||||
type: "inventory_sync:download",
|
||||
@@ -80,7 +77,6 @@ module.exports = {
|
||||
type: "inventory_sync:databaseStats",
|
||||
links: ["control-master"],
|
||||
permission: "inventory_sync.inventory.view",
|
||||
requestProperties: {},
|
||||
responseProperties: {
|
||||
"database_size": { type: "integer" },
|
||||
"database_entries": { type: "integer" },
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ async function loadDatabase(config, logger) {
|
||||
logger.verbose(`Loading ${itemsPath}`);
|
||||
try {
|
||||
let content = await fs.readFile(itemsPath);
|
||||
return JSON.parse(content);
|
||||
return new Map(JSON.parse(content));
|
||||
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
logger.verbose("Creating new inventory database");
|
||||
return {};
|
||||
return new Map();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
@@ -24,7 +24,7 @@ async function saveDatabase(masterConfig, inventories, logger) {
|
||||
if (inventories) {
|
||||
let file = path.resolve(masterConfig.get("master.database_directory"), "inventories.json");
|
||||
logger.verbose(`writing ${file}`);
|
||||
let content = JSON.stringify(inventories);
|
||||
let content = JSON.stringify(Array.from(inventories));
|
||||
await fs.outputFile(file, content);
|
||||
}
|
||||
}
|
||||
@@ -41,17 +41,16 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
|
||||
|
||||
async uploadRequestHandler(message) {
|
||||
this.logger.verbose(`Saving inventory for ${message.data.player_name}`);
|
||||
this.inventories[message.data.player_name] = message.data.inventory;
|
||||
|
||||
return { success: true };
|
||||
this.inventories.set(message.data.player_name, message.data.inventory);
|
||||
}
|
||||
|
||||
async downloadRequestHandler(message) {
|
||||
this.logger.verbose(`Downloading inventory for ${message.data.player_name}`);
|
||||
let inventory = this.inventories.get(message.data.player_name);
|
||||
return {
|
||||
player_name: message.data.player_name,
|
||||
inventory: this.inventories[message.data.player_name] || {},
|
||||
new_player: !this.inventories[message.data.player_name],
|
||||
inventory: inventory || {},
|
||||
new_player: !inventory,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -61,18 +60,18 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
|
||||
}
|
||||
|
||||
async databaseStatsRequestHandler(message) {
|
||||
let inventories = Object.keys(this.inventories)
|
||||
let inventories = Array.from(this.inventories.keys())
|
||||
.map(name => ({
|
||||
name,
|
||||
length: JSON.stringify(this.inventories[name]).length,
|
||||
length: JSON.stringify(this.inventories.get(name)).length,
|
||||
}))
|
||||
.sort((a, b) => a.stringified.length - b.stringified.length);
|
||||
return {
|
||||
database_size: inventories.map(x => x.length).reduce((a, b) => b - a, 0),
|
||||
database_entries: inventories.length,
|
||||
largest_entry: {
|
||||
name: inventories[0].name,
|
||||
size: inventories[0].length,
|
||||
name: inventories[0]?.name || "-",
|
||||
size: inventories[0]?.length || 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user