UPS display plugin that shows on master

This commit is contained in:
Danielv123 committed 2017-05-26 19:50:33 +02:00
1 parent 960cd79be3
commit 844bfebcd7
4 files changed
+80

No files matched your search

+18
View File
@@ -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",
}
+38
View File
@@ -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);