diff --git a/client.js b/client.js index e8a6f506..6570e837 100644 --- a/client.js +++ b/client.js @@ -9,6 +9,7 @@ var ncp = require('ncp').ncp; var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); var _ = require('underscore'); +var deepmerge = require("deepmerge"); // internal libraries var objectOps = require("./lib/objectOps.js"); @@ -352,6 +353,14 @@ function instanceManagement() { } }); } + // these are our two config files. We need to send these in case plugin + // wants to contact master or know something. + let combinedConfig = deepmerge(instanceconfig,config,{clone:true}) + // send through script-output file, maybe more compat? + fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, JSON.stringify(combinedConfig)+"\r\n"); + // send directly through stdin + // plugins[i].stdin.write(JSON.stringify(combinedConfig)+"\n"); + console.log("Clusterio | Loaded plugin " + pluginDirectories[i]); plugins[i].stdout.on("data", (data) => { if(data.toString('utf8')[0] != "/") { diff --git a/sharedPlugins/UPSdisplay/config.js b/sharedPlugins/UPSdisplay/config.js new file mode 100644 index 00000000..8b2e6d16 --- /dev/null +++ b/sharedPlugins/UPSdisplay/config.js @@ -0,0 +1,18 @@ +/* + Example config file for example Clusterio plugin + Binary will be spawned using child_process.spawn and get input from stdin and text from stdout will be ran as + commands in the factorio server. +*/ + +module.exports = { + // Name of package. For display somewhere I guess. + name: "UPSdisplay", + 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: "node", + arguments: ["index.js"], + description: "Internal clusterio plugin to get the worlds UPS and display it on master", + // We'll send everything in this file to your stdin. Beware. + scriptOutputFileSubscription: "UPSdisplay.txt", +} \ No newline at end of file diff --git a/sharedPlugins/UPSdisplay/index.js b/sharedPlugins/UPSdisplay/index.js new file mode 100644 index 00000000..05dfab0f --- /dev/null +++ b/sharedPlugins/UPSdisplay/index.js @@ -0,0 +1,38 @@ +const needle = require("needle"); + +console.log("/silent-command game.print('UPSdisplay enabled')\n"); + +setInterval(function(){ + console.log("/silent-command game.write_file('UPSdisplay.txt', game.tick, true, 0)\n"); +},1000); +var config = {}; +process.stdin.setEncoding('utf8'); +let historicalTicks = []; +process.stdin.on('readable', () => { + var chunk = process.stdin.read(); + if (chunk !== null && !isNaN(chunk) && !chunk.includes("{")){ + // console.log("Tick: " + chunk); + historicalTicks[historicalTicks.length] = {tick:Number(chunk), timestamp:Date.now()}; + if(historicalTicks.length > 30){ + historicalTicks.shift(); // delete last element in array (position 0); + } + } else { + config = JSON.parse(chunk); + } +}); +process.stdin.on('end', () => { + process.stdout.write('end'); +}); + +// Post average UPS +setInterval(function(){ + if(historicalTicks && historicalTicks[0]){ + let timePeriod = historicalTicks[historicalTicks.length-1].timestamp - historicalTicks[0].timestamp; + let ticksInPeriod = historicalTicks[historicalTicks.length-1].tick - historicalTicks[0].tick; + let UPS = Math.round(ticksInPeriod / (timePeriod/1000)); + // console.log("UPS: " + UPS); + needle.post(config.masterIP+':'+config.masterPort+'/api/editSlaveMeta', {slaveID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) { + // success? + }); + } +}, 10000); \ No newline at end of file diff --git a/static/nodeDetails.js b/static/nodeDetails.js index ec4c47b4..6165fe87 100644 --- a/static/nodeDetails.js +++ b/static/nodeDetails.js @@ -56,6 +56,7 @@ function getParameterByName(name, url) { setInterval(function(){ getJSON("/api/slaves", function(err, slaveData){ if(err){ + // our request threw an error, most likely master is unavailable so we show yellow indicator document.querySelector("#onlineIndicator").style.backgroundColor = "yellow"; // compare with Math.floor(x/100000) allows us to check if they are within 10s of each other } else if(Math.floor(slaveData[getParameterByName("slaveID")].time/100000) == Math.floor(Date.now()/100000)){ @@ -124,6 +125,20 @@ function populateSlaveInfo(){ HTML += '
' document.querySelector("#hero").innerHTML = HTML; + + // slaveDetails + HTML = ""; + if(slave.meta) + for(let key in slave.meta){ + let t = slave.meta[key]; + if(typeof t == "string"){ + HTML += ""+key+": "+t+"
"; + } + } + + document.querySelector("#body > #details").innerHTML = HTML; + + // make production graph makeGraph(slave.unique, slave.unique) });