mineforge_html/current_players.sh

49 lines
1 KiB
Bash
Raw Permalink Normal View History

2025-01-22 23:20:24 +00:00
#!/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