mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
UPS display plugin that shows on master
This commit is contained in:
1 parent
960cd79be3
commit
844bfebcd7
4 files changed
+80
No files matched your search
@@ -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] != "/") {
|
||||
|
||||
@@ -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",
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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 += '<div id="' + slave.unique + '" class="productionGraph" style="width: calc(100% - 300px);"></div>'
|
||||
|
||||
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 += "<p>"+key+": "+t+"</p>";
|
||||
}
|
||||
}
|
||||
|
||||
document.querySelector("#body > #details").innerHTML = HTML;
|
||||
|
||||
// make production graph
|
||||
makeGraph(slave.unique, slave.unique)
|
||||
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user