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 = {
// 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",
+34 -36
View File
@@ -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;