Files
factorio-clusterio/plugins/research_sync/instance.js
T
Hornwitser 957983d8a5 Add logging system based on winston
Add winston based logger for master, slave, ctl, lib, and web_ui code.
Logging done from slaves and instances are sent to the master server and
tagged with the instance and/or slave it originated from.  This allows
for example error messages to propagate back to the source that iniated
the action.  The master server also stores the log to allow it to be
queried at a later point.  For slaves a copy of the slave's log is also
stored locally.
2020-11-27 17:51:07 +01:00

83 lines
2.4 KiB
JavaScript

"use strict";
const libPlugin = require("@clusterio/lib/plugin");
const libLuaTools = require("@clusterio/lib/lua_tools");
class InstancePlugin extends libPlugin.BaseInstancePlugin {
unexpectedError(err) {
this.logger.error(`Unexpected error:\n${err.stack}`);
}
async init() {
if (!this.instance.config.get("factorio.enable_save_patching")) {
throw new Error("research_sync plugin requires save patching.");
}
this.instance.server.on("ipc-research_sync:contribution", (tech) => {
this.researchContribution(tech).catch(err => this.unexpectedError(err));
});
this.instance.server.on("ipc-research_sync:finished", (tech) => {
this.researchFinished(tech).catch(err => this.unexpectedError(err));
});
}
async researchContribution(tech) {
this.info.messages.contribution.send(this.instance, tech);
}
async progressEventHandler(message) {
let techsJson = libLuaTools.escapeString(JSON.stringify(message.data.technologies));
await this.sendRcon(`/sc research_sync.update_progress("${techsJson}")`, true);
}
async researchFinished(tech) {
this.info.messages.finished.send(this.instance, tech);
}
async finishedEventHandler(message) {
let { name, level, researched } = message.data;
await this.sendRcon(
`/sc research_sync.research_technology("${libLuaTools.escapeString(name)}", ${level})`, true
);
}
async onStart() {
let dumpJson = await this.sendRcon("/sc research_sync.dump_technologies()");
let techsToSend = [];
let instanceTechs = new Map();
for (let tech of JSON.parse(dumpJson)) {
techsToSend.push([
tech.name,
tech.level,
tech.progress || null,
tech.researched,
]);
instanceTechs.set(tech.name, tech);
}
let response = await this.info.messages.syncTechnologies.send(this.instance, { technologies: techsToSend });
let techsToSync = [];
for (let masterTech of response.technologies) {
let [name, level, progress, researched] = masterTech;
let instanceTech = instanceTechs.get(name);
if (
!instanceTech
|| instanceTech.level !== level
|| (instanceTech.progress || null) !== progress
|| instanceTech.researched !== researched
) {
techsToSync.push(masterTech);
}
}
if (techsToSync.length) {
let syncJson = libLuaTools.escapeString(JSON.stringify(techsToSync));
await this.sendRcon(`/sc research_sync.sync_technologies("${syncJson}")`, true);
}
}
}
module.exports = {
InstancePlugin,
};