Skip to content

Commit 4aef9d3

Browse files
committed
Add mc-players.js plugin
1 parent 8f7cf57 commit 4aef9d3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { readFileSync } = require('fs');
2+
const { log } = require('../node-common')(['log']);
3+
const { updateMetrics } = require('../modules/metrics');
4+
5+
const JOINED_TAG = 'joined the game';
6+
const LEFT_TAG = 'left the game';
7+
8+
/**
9+
* Log metrics for logging Minecraft players online.
10+
*
11+
* @param {object} args - Plugin args.
12+
*/
13+
module.exports = async (args = {}) => {
14+
const { LOG_PATH = '/home/pi/hom-mc-server.log' } = args;
15+
try {
16+
const mcPlayers = [];
17+
18+
// Load log file
19+
const lines = readFileSync(LOG_PATH, 'utf-8')
20+
.split('\n')
21+
.map((p) => p.trim())
22+
.filter((p) => p.includes(JOINED_TAG) || p.includes(LEFT_TAG))
23+
.map((p) => p.split(': ')[1]);
24+
25+
// Get all names mentioned
26+
const names = lines.reduce((acc, p) => {
27+
const [name] = p.split(' ');
28+
return acc.includes(name) ? acc : [...acc, name];
29+
}, []);
30+
log.debug(`Found MC players: ${names.join(', ')}`);
31+
32+
// Determine who's still there
33+
names.forEach((p) => {
34+
const joinCount = lines.filter((l) => l.includes(p) && l.includes(JOINED_TAG)).length;
35+
const leftCount = lines.filter((l) => l.includes(p) && l.includes(LEFT_TAG)).length;
36+
37+
if (joinCount > leftCount) mcPlayers.push(p);
38+
});
39+
40+
updateMetrics({ mcPlayers });
41+
} catch (e) {
42+
log.error(e);
43+
}
44+
};

0 commit comments

Comments
 (0)