mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
researchsync: every server can now research
This commit is contained in:
1 parent
8935d03b4a
commit
d1a295ae4e
4 files changed
+51
-28
No files matched your search
@@ -450,6 +450,7 @@ function instanceManagement() {
|
|||||||
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
||||||
|
|
||||||
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) {
|
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) {
|
||||||
|
if (filename == null) return;
|
||||||
// get array of lines in file
|
// get array of lines in file
|
||||||
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
||||||
// if you found anything, reset the file
|
// if you found anything, reset the file
|
||||||
@@ -491,6 +492,7 @@ function instanceManagement() {
|
|||||||
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
||||||
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) {
|
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) {
|
||||||
// get array of lines in file
|
// get array of lines in file
|
||||||
|
// TODO async
|
||||||
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
||||||
// if you found anything, reset the file
|
// if you found anything, reset the file
|
||||||
if (stuff[0]) {
|
if (stuff[0]) {
|
||||||
|
|||||||
@@ -193,6 +193,16 @@ app.post("/api/getSlaveMeta", function (req, res) {
|
|||||||
res.send('{"INVALID REQUEST":1}');
|
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
|
// 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) {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
"nedb": "^1.8.0",
|
"nedb": "^1.8.0",
|
||||||
"needle": "^1.1.0",
|
"needle": "^1.1.0",
|
||||||
"node-factorio-api": "^0.1.28",
|
"node-factorio-api": "^0.1.28",
|
||||||
|
"node-lua": "^1.0.4",
|
||||||
"request": "^2.83.0",
|
"request": "^2.83.0",
|
||||||
"right-pad": "^1.0.1",
|
"right-pad": "^1.0.1",
|
||||||
"rmdir-sync": "^1.0.1",
|
"rmdir-sync": "^1.0.1",
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ class ResearchSync {
|
|||||||
|
|
||||||
this.research = {};
|
this.research = {};
|
||||||
this.lua = new nodelua.LuaState();
|
this.lua = new nodelua.LuaState();
|
||||||
this.isMaster = this.config.master == this.config.unique;
|
|
||||||
|
|
||||||
messageInterface("ResearchSync enabled");
|
messageInterface("ResearchSync enabled");
|
||||||
setInterval(function () {
|
setInterval(function () {
|
||||||
@@ -29,7 +28,43 @@ class ResearchSync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
doSync() {
|
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', {
|
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
|
||||||
instanceID: this.config.unique,
|
instanceID: this.config.unique,
|
||||||
password: this.config.clientPassword,
|
password: this.config.clientPassword,
|
||||||
@@ -37,33 +72,8 @@ class ResearchSync {
|
|||||||
}, function (err, resp) {
|
}, function (err, resp) {
|
||||||
// success?
|
// success?
|
||||||
});
|
});
|
||||||
} else {
|
}.bind(this));
|
||||||
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) {
|
diff(array1, array2) {
|
||||||
|
|||||||
Reference in new issue
Block a user