researchSync: master

This commit is contained in:
theCatsPaw committed 2018-02-21 20:54:02 +01:00
1 parent 4d3924b339
commit e64db6b236
3 files changed
+83

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
module.exports = {
// Name of package. For display somewhere I guess.
name: "researchSync",
version: "1.0.0",
// Binary entrypoint for plugin. Don't let it crash. Stdout is sent to game as server chat (run /c commands from here for interface)
// Make sure its cross platform somehow.
binary: "nodePackage",
arguments: ["index.js"],
description: "syncs research across servers",
// We'll send everything in this file to your stdin. Beware.
scriptOutputFileSubscription: "researchSync.txt",
}
@@ -0,0 +1,8 @@
/silent-command
for _, tech in pairs(game.forces["player"].technologies) do
local dict = {};
dict[tech.name] = tech.researched;
game.write_file("researchSync.txt",serpent.line(dict));
end
+61
View File
@@ -0,0 +1,61 @@
var fs = require('fs');
const nodelua = require('node-lua');
const needle = require("needle");
class ResearchSync {
constructor(slaveConfig, messageInterface){
this.functions = this.loadFunctions();
this.config = slaveConfig;
this.messageInterface = messageInterface;
this.research = {};
this.lua = new nodelua.LuaState();
this.isMaster = this.config.master == this.config.unique;
messageInterface("ResearchSync enabled");
if (this.isMaster) {
setInterval(function () {
this.pollResearch.call(this);
}.bind(this), 3000);
}
}
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?
});
}
loadFunctions() {
return {
dumpResearch: this.loadFunc("dumpResearch.lua")
};
}
loadFunc(path) {
return fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8').replace(/\r\n/g,' ');
}
scriptOutput(data){
try {
this.lua.DoString("a = " + data);
this.lua.GetGlobal("a");
let currentResearch = this.lua.ToValue(-1);
this.research = Object.assign(this.research, currentResearch);
this.lua.Pop();
} catch (e) {
console.log(e);
}
}
}
module.exports = ResearchSync;