diff --git a/.gitignore b/.gitignore index 24ee0f36..d663d5d3 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ package-lock.json sharedPlugins/* !sharedPlugins/inventoryImports/ !sharedPlugins/UPSdisplay/ -!sharedPlugins/remoteMap/ \ No newline at end of file +!sharedPlugins/remoteMap/ +!sharedPlugins/researchSync/ diff --git a/master.js b/master.js index 695d3f9c..0d907c21 100644 --- a/master.js +++ b/master.js @@ -176,12 +176,23 @@ app.post("/api/editSlaveMeta", function(req,res) { slaves[req.body.instanceID].meta = {}; } slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true}); - console.log("Updating slave: " + slaves[req.body.instanceID].mac + " : " + slaves[req.body.instanceID].serverPort+" at " + slaves[req.body.instanceID].publicIP); + console.log("Updating slave: " + slaves[req.body.instanceID].mac + " : " + slaves[req.body.instanceID].serverPort+" at " + slaves[req.body.instanceID].publicIP, req.body.meta); } else { res.send("ERROR: Invalid instanceID or password") } } }); + +app.post("/api/getSlaveMeta", function (req, res) { + console.log("body", req.body); + if(req.body && req.body.instanceID && req.body.password){ + console.log("returning meta for ", req.body.instanceID); + res.send(JSON.stringify(slaves[req.body.instanceID].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/sharedPlugins/researchSync/dumpResearch.lua b/sharedPlugins/researchSync/dumpResearch.lua index 621b8e8f..fd4bc2c1 100644 --- a/sharedPlugins/researchSync/dumpResearch.lua +++ b/sharedPlugins/researchSync/dumpResearch.lua @@ -1,8 +1,12 @@ /silent-command +local lines = ""; + for _, tech in pairs(game.forces["player"].technologies) do local dict = {}; dict[tech.name] = tech.researched; - game.write_file("researchSync.txt",serpent.line(dict)); + lines = lines .. serpent.line(dict) .. "\n"; end +game.write_file("researchSync.txt",lines) + diff --git a/sharedPlugins/researchSync/index.js b/sharedPlugins/researchSync/index.js index 6b40ad07..cc1d28f2 100644 --- a/sharedPlugins/researchSync/index.js +++ b/sharedPlugins/researchSync/index.js @@ -3,8 +3,8 @@ const nodelua = require('node-lua'); const needle = require("needle"); - class ResearchSync { + constructor(slaveConfig, messageInterface){ this.functions = this.loadFunctions(); this.config = slaveConfig; @@ -15,26 +15,70 @@ class ResearchSync { this.isMaster = this.config.master == this.config.unique; messageInterface("ResearchSync enabled"); - if (this.isMaster) { - setInterval(function () { - this.pollResearch.call(this); - }.bind(this), 3000); - } + setInterval(function () { + this.pollResearch.call(this); + setTimeout(this.doSync.bind(this), 2) + }.bind(this), 2000); + } pollResearch() { this.messageInterface("Polling Research\n") this.messageInterface(this.functions.dumpResearch); - console.log(this.config.master, this.config.unique, this.isMaster); - needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', { - instanceID: this.config.unique, - password: this.config.clientPassword, - meta: {research: this.research} - }, function (err, resp) { - // success? - }); + } + doSync() { + if (this.isMaster) { + needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', { + instanceID: this.config.unique, + password: this.config.clientPassword, + meta: {research: this.research} + }, 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; + } + + 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) { + var diff = {}; + + + + Object.keys(array2).forEach((element) => { + if (array2[element] != array1[element]){ + diff[element] = array2[element]; + } + }); + + return diff; + } loadFunctions() { return { @@ -53,7 +97,6 @@ class ResearchSync { this.research = Object.assign(this.research, currentResearch); this.lua.Pop(); } catch (e) { - console.log(e); } } }