researchSync: client

This commit is contained in:
theCatsPaw committed 2018-02-21 22:26:33 +01:00
1 parent e64db6b236
commit 8935d03b4a
4 files changed
+77 -18

No files matched your search

+2 -1
View File
@@ -40,4 +40,5 @@ package-lock.json
sharedPlugins/* sharedPlugins/*
!sharedPlugins/inventoryImports/ !sharedPlugins/inventoryImports/
!sharedPlugins/UPSdisplay/ !sharedPlugins/UPSdisplay/
!sharedPlugins/remoteMap/ !sharedPlugins/remoteMap/
!sharedPlugins/researchSync/
+12 -1
View File
@@ -176,12 +176,23 @@ app.post("/api/editSlaveMeta", function(req,res) {
slaves[req.body.instanceID].meta = {}; slaves[req.body.instanceID].meta = {};
} }
slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true}); 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 { } else {
res.send("ERROR: Invalid instanceID or password") 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 // mod management
// should handle uploading and checking if mods are uploaded // should handle uploading and checking if mods are uploaded
app.post("/api/checkMod", function(req,res) { app.post("/api/checkMod", function(req,res) {
+5 -1
View File
@@ -1,8 +1,12 @@
/silent-command /silent-command
local lines = "";
for _, tech in pairs(game.forces["player"].technologies) do for _, tech in pairs(game.forces["player"].technologies) do
local dict = {}; local dict = {};
dict[tech.name] = tech.researched; dict[tech.name] = tech.researched;
game.write_file("researchSync.txt",serpent.line(dict)); lines = lines .. serpent.line(dict) .. "\n";
end end
game.write_file("researchSync.txt",lines)
+58 -15
View File
@@ -3,8 +3,8 @@ const nodelua = require('node-lua');
const needle = require("needle"); const needle = require("needle");
class ResearchSync { class ResearchSync {
constructor(slaveConfig, messageInterface){ constructor(slaveConfig, messageInterface){
this.functions = this.loadFunctions(); this.functions = this.loadFunctions();
this.config = slaveConfig; this.config = slaveConfig;
@@ -15,26 +15,70 @@ class ResearchSync {
this.isMaster = this.config.master == this.config.unique; this.isMaster = this.config.master == this.config.unique;
messageInterface("ResearchSync enabled"); messageInterface("ResearchSync enabled");
if (this.isMaster) { setInterval(function () {
setInterval(function () { this.pollResearch.call(this);
this.pollResearch.call(this); setTimeout(this.doSync.bind(this), 2)
}.bind(this), 3000); }.bind(this), 2000);
}
} }
pollResearch() { pollResearch() {
this.messageInterface("Polling Research\n") this.messageInterface("Polling Research\n")
this.messageInterface(this.functions.dumpResearch); 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() { loadFunctions() {
return { return {
@@ -53,7 +97,6 @@ class ResearchSync {
this.research = Object.assign(this.research, currentResearch); this.research = Object.assign(this.research, currentResearch);
this.lua.Pop(); this.lua.Pop();
} catch (e) { } catch (e) {
console.log(e);
} }
} }
} }