From d1a295ae4ed28779d54e4bae1d041cc306b02140 Mon Sep 17 00:00:00 2001 From: theCatsPaw Date: Fri, 23 Feb 2018 23:02:44 +0100 Subject: [PATCH] researchsync: every server can now research --- client.js | 2 + master.js | 10 +++++ package.json | 1 + sharedPlugins/researchSync/index.js | 66 +++++++++++++++++------------ 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/client.js b/client.js index db823519..7e915c99 100644 --- a/client.js +++ b/client.js @@ -450,6 +450,7 @@ function instanceManagement() { global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true; fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) { + if (filename == null) return; // get array of lines in file let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n"); // if you found anything, reset the file @@ -491,6 +492,7 @@ function instanceManagement() { global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true; fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) { // get array of lines in file + // TODO async let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n"); // if you found anything, reset the file if (stuff[0]) { diff --git a/master.js b/master.js index 0d907c21..83a4278f 100644 --- a/master.js +++ b/master.js @@ -193,6 +193,16 @@ app.post("/api/getSlaveMeta", function (req, res) { res.send('{"INVALID REQUEST":1}'); } }); + +app.post("/api/getSlavesMeta", function (req, res) { + if(req.body.password){ + let metas = {}; + res.send(JSON.stringify(Object.keys(slaves).map(key => metas[key] = slaves[key].meta))); + } else { + res.status(400); + res.send('{"INVALID REQUEST":1}'); + } +}); // mod management // should handle uploading and checking if mods are uploaded app.post("/api/checkMod", function(req,res) { diff --git a/package.json b/package.json index 19273a35..c1b78137 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "nedb": "^1.8.0", "needle": "^1.1.0", "node-factorio-api": "^0.1.28", + "node-lua": "^1.0.4", "request": "^2.83.0", "right-pad": "^1.0.1", "rmdir-sync": "^1.0.1", diff --git a/sharedPlugins/researchSync/index.js b/sharedPlugins/researchSync/index.js index cc1d28f2..bb47f43e 100644 --- a/sharedPlugins/researchSync/index.js +++ b/sharedPlugins/researchSync/index.js @@ -12,7 +12,6 @@ class ResearchSync { this.research = {}; this.lua = new nodelua.LuaState(); - this.isMaster = this.config.master == this.config.unique; messageInterface("ResearchSync enabled"); setInterval(function () { @@ -29,7 +28,43 @@ class ResearchSync { } doSync() { - if (this.isMaster) { + + + + needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/getSlavesMeta', { + password: this.config.clientPassword, + }, function (err, resp, body) { + if (err) { + throw err; + } + + if (resp.statusCode != 200){ + console.log("got error when calling getSlaveMeta", resp.statusCode, resp.body); + return; + } + var allmeta = JSON.parse(resp.body); + + let needResearch = {}; + + Object.entries(allmeta).map((instance) => { + if (instance[1] == null || instance[1].research == null) return; + Object.entries(instance[1].research).map((research) => { + if (research[1] == 1) { + needResearch[research[0]] = 1; + } + }); + }) + + + var difference = this.diff(this.research, needResearch); + + Object.keys(difference).forEach((key) => { + if (difference[key] == 1) { + this.messageInterface("/c game.forces['player'].technologies['" + key + "'].researched=true"); + } + }) + + console.log("difference from other servers", difference); needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', { instanceID: this.config.unique, password: this.config.clientPassword, @@ -37,33 +72,8 @@ class ResearchSync { }, function (err, resp) { // success? }); - } else { - console.log(this.config.masterIP, this.config.masterPort, this.config.master) - needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/getSlaveMeta', { - instanceID: this.config.master, - password: this.config.clientPassword, - }, function (err, resp, body) { - if (err) { - throw err; - } + }.bind(this)); - if (resp.statusCode != 200){ - console.log("got error when calling getSlaveMeta", resp.statusCode, resp.body); - return; - } - var mastermeta = JSON.parse(resp.body); - - var difference = this.diff(this.research, mastermeta.research); - - Object.keys(difference).forEach((key) => { - this.messageInterface("/c game.forces['player'].technologies['"+key+"'].researched="+ (difference[key] == 1? "true": "false")); - - }) - - console.log("difference from master", difference); - // success? - }.bind(this)); - } } diff(array1, array2) {