researchsync: every server can now research

This commit is contained in:
theCatsPaw committed 2018-02-23 23:02:44 +01:00
1 parent 8935d03b4a
commit d1a295ae4e
4 files changed
+51 -28

No files matched your search

+2
View File
@@ -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]) {
+10
View File
@@ -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) {
+1
View File
@@ -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",
+38 -28
View File
@@ -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) {