diff --git a/sharedPlugins/researchSync/config.js b/sharedPlugins/researchSync/config.js new file mode 100644 index 00000000..63fa14d9 --- /dev/null +++ b/sharedPlugins/researchSync/config.js @@ -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", +} \ No newline at end of file diff --git a/sharedPlugins/researchSync/dumpResearch.lua b/sharedPlugins/researchSync/dumpResearch.lua new file mode 100644 index 00000000..621b8e8f --- /dev/null +++ b/sharedPlugins/researchSync/dumpResearch.lua @@ -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 + diff --git a/sharedPlugins/researchSync/index.js b/sharedPlugins/researchSync/index.js new file mode 100644 index 00000000..6b40ad07 --- /dev/null +++ b/sharedPlugins/researchSync/index.js @@ -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; \ No newline at end of file