mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
researchSync: master
This commit is contained in:
1 parent
4d3924b339
commit
e64db6b236
3 files changed
+83
No files matched your search
@@ -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
|
||||
|
||||
@@ -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;
|
||||
Reference in new issue
Block a user