From 9402575a3a6bbd013cb18d55a51dd7f448ed2b88 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Wed, 22 Jan 2025 23:20:24 +0000 Subject: [PATCH] add current players script --- current_players.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 current_players.sh diff --git a/current_players.sh b/current_players.sh new file mode 100644 index 0000000..8333afb --- /dev/null +++ b/current_players.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +logfile=/opt/luanti/log/debug.log +player_re="([a-zA-Z0-9\s]+ (digs|places|leaves)|[a-zA-Z0-9]+ \[[0-9\.]+\] joins) " +left="" +old_data="old" +plays_re="(digs|places|joins)" +leaves_re="leaves" + +declare -A players + +while true +do + data=$(tail -n500 "$logfile" | grep -o -E "$player_re") + if [ "$data" != "$old_data" ]; then + data=$(echo $data | sed -e 's/\[[0-9\.]*\]//g') + flip=0 + for i in $data; do + if [ $flip = 0 ]; then + player="$i" + flip=1 + else + status="$i" + play=$(echo "$status" | grep -o -E "$plays_re") + left=$(echo "$status" | grep -o -E "$leaves_re") + if [ ! "$play" = "" ]; then + players[$player]=P + elif [ ! "$left" = "" ]; then + players[$player]=L + fi + flip=0 + fi + + done + #old_data=$data + fi + output="{\"players\":[" + c="" + for key in "${!players[@]}"; do + output="$output$c{\"player\":\"$key\",\"status\":\"${players[$key]}\"}" + if [ "$c" = "" ]; then + c="," + fi + done + output="$output],\"updated\":\"$(date)\"}" + echo $output | jq > players.json + sleep 15 +done