File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments