Fixed UPSdisplay

This commit is contained in:
Danielv123 committed 2017-12-10 11:38:56 +01:00
1 parent 44b68c92d5
commit b1f35c9b72
2 files changed
+36 -39

No files matched your search

+2 -3
View File
@@ -7,11 +7,10 @@
module.exports = { module.exports = {
// Name of package. For display somewhere I guess. // Name of package. For display somewhere I guess.
name: "UPSdisplay", 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) // 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. // Make sure its cross platform somehow.
binary: "node", binary: "nodePackage",
arguments: ["index.js"],
description: "Internal clusterio plugin to get the worlds UPS and display it on master", 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. // We'll send everything in this file to your stdin. Beware.
scriptOutputFileSubscription: "UPSdisplay.txt", scriptOutputFileSubscription: "UPSdisplay.txt",
+34 -36
View File
@@ -1,42 +1,40 @@
const needle = require("needle"); 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)"); class UPSdisplay {
},1000); constructor(slaveConfig, msgInterface){
var config = {}; this.config = slaveConfig;
process.stdin.setEncoding('utf8'); this.messageInterface = msgInterface;
let historicalTicks = []; this.historicalTicks = [];
process.stdin.on('readable', () => { setInterval(() => {
var chunk = process.stdin.read(); this.messageInterface("/silent-command game.write_file('UPSdisplay.txt', game.tick, true, 0)");
if (chunk !== null && !isNaN(chunk) && !chunk.includes("{")){ },1000);
// console.log("Tick: " + chunk); setInterval(() => {
historicalTicks[historicalTicks.length] = {tick:Number(chunk), timestamp:Date.now()}; if(this.historicalTicks && this.historicalTicks[0]){
if(historicalTicks.length > 30){ let timePeriod = this.historicalTicks[this.historicalTicks.length-1].timestamp - this.historicalTicks[0].timestamp;
historicalTicks.shift(); // delete last element in array (position 0); let ticksInPeriod = this.historicalTicks[this.historicalTicks.length-1].tick - this.historicalTicks[0].tick;
} let UPS = Math.round(ticksInPeriod / (timePeriod/1000));
} else { // console.log("UPS: " + UPS);
config = JSON.parse(chunk); 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')");
} }
}); scriptOutput(data){
process.stdin.on('end', () => { if (data !== null && !isNaN(data)){
process.stdout.write('end'); // console.log("Tick: " + data);
}); this.historicalTicks[this.historicalTicks.length] = {tick:Number(data), timestamp:Date.now()};
if(this.historicalTicks.length > 30){
// Post average UPS this.historicalTicks.shift(); // delete last element in array (position 0);
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);
} }
} }
}, 10000); }
module.exports = UPSdisplay;