From b1f35c9b724feaf0367808122e2d11e56ea57611 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 10 Dec 2017 11:38:56 +0100 Subject: [PATCH] Fixed UPSdisplay --- sharedPlugins/UPSdisplay/config.js | 5 +-- sharedPlugins/UPSdisplay/index.js | 70 +++++++++++++++--------------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/sharedPlugins/UPSdisplay/config.js b/sharedPlugins/UPSdisplay/config.js index 8b2e6d16..436b204c 100644 --- a/sharedPlugins/UPSdisplay/config.js +++ b/sharedPlugins/UPSdisplay/config.js @@ -7,11 +7,10 @@ module.exports = { // Name of package. For display somewhere I guess. name: "UPSdisplay", - version: "1.0.0", + version: "2.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"], + binary: "nodePackage", 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", diff --git a/sharedPlugins/UPSdisplay/index.js b/sharedPlugins/UPSdisplay/index.js index d3902c4a..a226b38a 100644 --- a/sharedPlugins/UPSdisplay/index.js +++ b/sharedPlugins/UPSdisplay/index.js @@ -1,42 +1,40 @@ const needle = require("needle"); -console.log("/silent-command game.print('UPSdisplay enabled')"); -setInterval(function(){ - console.log("/silent-command game.write_file('UPSdisplay.txt', game.tick, true, 0)"); -},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); + +class UPSdisplay { + constructor(slaveConfig, msgInterface){ + this.config = slaveConfig; + this.messageInterface = msgInterface; + this.historicalTicks = []; + setInterval(() => { + this.messageInterface("/silent-command game.write_file('UPSdisplay.txt', game.tick, true, 0)"); + },1000); + setInterval(() => { + if(this.historicalTicks && this.historicalTicks[0]){ + let timePeriod = this.historicalTicks[this.historicalTicks.length-1].timestamp - this.historicalTicks[0].timestamp; + let ticksInPeriod = this.historicalTicks[this.historicalTicks.length-1].tick - this.historicalTicks[0].tick; + let UPS = Math.round(ticksInPeriod / (timePeriod/1000)); + // console.log("UPS: " + UPS); + try{ + needle.post(this.config.masterIP+':'+this.config.masterPort+'/api/editSlaveMeta', {instanceID: this.config.unique, password: this.config.clientPassword, meta: {UPS:UPS}}, function(err, resp) { + // success? + }); + } catch (err){ + console.log(err); + } + } + }, 10000); + this.messageInterface("/silent-command game.print('UPSdisplay enabled')"); } -}); -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); - try{ - needle.post(config.masterIP+':'+config.masterPort+'/api/editSlaveMeta', {instanceID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) { - // success? - }); - } catch (err){ - console.log(err); + scriptOutput(data){ + if (data !== null && !isNaN(data)){ + // console.log("Tick: " + data); + this.historicalTicks[this.historicalTicks.length] = {tick:Number(data), timestamp:Date.now()}; + if(this.historicalTicks.length > 30){ + this.historicalTicks.shift(); // delete last element in array (position 0); + } } } -}, 10000); +} +module.exports = UPSdisplay; \ No newline at end of file