Export realtime game statistics to disk in JSON and other formats.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB

const fs = require('fs');
var lynx = require('lynx');
var metrics = new lynx('localhost', 8125);
const filePath = '../../../script-output/stats.json';
processFileMetrics();
fs.watch(filePath, function(eventName, filename) {
if (filename){
setTimeout(
() => processFileMetrics(),
0
);
}
});
function processFileMetrics() {
try {
const file = fs.readFileSync(filePath);
console.log(JSON.parse(file));
/* data = flattenObject({ 'factorio': JSON.parse(file) });
for (const key in data) {
metrics.gauge(key, data[key]);
}
*/
} catch (e) {}
}
function processData() {
}
// https://stackoverflow.com/a/53739792/4623317
function flattenObject(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object' && ob[i] !== null) {
var flatObject = flattenObject(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
}