Get a notification when someone joins your ARK server

I am running a small community for some passionate ARK: Survival Evolved Players and found out that there's a node running gamedig and get gameserver data. That's perfect for looking for my folks.

Setup as follows:

The first node is a HTTP endpoint triggered by a LaMetric app I did - you can replace that with any trigger you'd like, but in my case it's just convenient to recycle that signal.

The switch after the ASE is just sorting out if the server is on- or offline.

Extracting players collects the names:

var names = msg.data.players.map(function(player) {
    return player['name'];
});

msg.payload = names;

return msg;

Now the player count is just the length of the array that's fed into a template to merrily give out the message, getting headers set and a HTTP reply of 200 to make the web service happy. I know you can handle error cases here, but that's all done on the lametric side by simply not doing anything :slight_smile:

The real magic happens in 'Compare to old players' which gets the names list from the "Extract players" node:

var old_players = flow.get("players");
if (!old_players) old_players = new Array();

var new_players = msg.payload;

new_players = new_players.filter(function (el) {
  return el != undefined;
});


if (!new_players.includes(undefined)) {
    pdiff = arr_diff(old_players, new_players).join();

    if (pdiff.length > 0) {
        if (old_players.length < new_players.length) {
            msg.payload = pdiff + " joined the server."
        } else {
            msg.payload = pdiff + " left the server."
        }
        flow.set("players", new_players);
        
    }
    
} 
    
return msg;



function arr_diff(a1, a2) {
    var a = [], diff = [];
    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }
    
    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }
    
    for (var k in a) {
        diff.push(k);
    }
    
    return diff;
}

here I came up with a JS solution to diff two arrays and figure if someone came online or left. The part that kept me busy for some time was actually the if (!new_players.includes(undefined)) line as the gamedig stuff also accounts for players still logging in and so do not have a name yet.

The switches in the end are just a crude way to sort out the guys I want to be notified about hooking into my home-assistant alerting that's already set up towards the lametric clock.

The result on the clock:

... plus a ' joined the server' and ' left the server' note with sounds on occasion.

Hope this helps someone who struggles with the query-game-server node

2 Likes