From 220db5b1c501b2748599cca9e85c51ab7df53cdc Mon Sep 17 00:00:00 2001 From: luatan Date: Sun, 11 Feb 2024 12:30:16 +0100 Subject: [PATCH] add helper function get_player_count --- scripts/auto_reboot.sh | 6 ++++-- scripts/helper_functions.sh | 11 +++++++++++ scripts/update.sh | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/scripts/auto_reboot.sh b/scripts/auto_reboot.sh index 376af7d0a..313df8b4c 100644 --- a/scripts/auto_reboot.sh +++ b/scripts/auto_reboot.sh @@ -1,10 +1,12 @@ #!/bin/bash +# shellcheck source=/dev/null +source "/home/steam/server/helper_functions.sh" if [ "${RCON_ENABLED,,}" = true ]; then if [ "${AUTO_REBOOT_EVEN_IF_PLAYERS_ONLINE,,}" != true ]; then - players_count=$(rcon-cli -c /home/steam/server/rcon.yaml showplayers) + players_count=$(get_player_count) - if [ "$(echo -n "$players_count" | wc -l)" -gt 0 ]; then + if [ "$players_count" -gt 0 ]; then echo "There are ${players_count} players online. Skipping auto reboot." exit 1 fi diff --git a/scripts/helper_functions.sh b/scripts/helper_functions.sh index 2386a77a6..273b3e4e2 100644 --- a/scripts/helper_functions.sh +++ b/scripts/helper_functions.sh @@ -65,3 +65,14 @@ isExecutable() { fi return "$return_val" } + +# Checks how many players are currently connected +# Returns 0 if RCON is not enabled +# Returns the player count if rcon is enabled +get_player_count() { + if [ "${RCON_ENABLED,,}" != true ]; then + return 0 + fi + PLAYERLIST=$(rcon-cli -c /home/steam/server/rcon.yaml "ShowPlayers") + return "$(echo -n "$PLAYERLIST" | wc -l)" +} \ No newline at end of file diff --git a/scripts/update.sh b/scripts/update.sh index e8965ecb5..8eb6ba302 100644 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -1,4 +1,6 @@ #!/bin/bash +# shellcheck source=/dev/null +source "/home/steam/server/helper_functions.sh" if [ "${UPDATE_ON_BOOT}" = false ]; then echo "Update on Boot needs to be enabled for auto updating" @@ -42,8 +44,7 @@ if [ "$CURRENTBUILD" != "$TARGETBUILD" ]; then echo "New Build was found. Updating the server from $CURRENTBUILD to $TARGETBUILD." rm /palworld/steamapps/appmanifest_2394010.acf - PLAYERLIST=$(rcon-cli -c /home/steam/server/rcon.yaml "ShowPlayers") - if [ "$(echo -n "$PLAYERLIST" | wc -l)" -gt 0 ]; then + if [ "$(get_player_count)" -gt 0 ]; then if [ -n "${DISCORD_WEBHOOK_URL}" ]; then /home/steam/server/discord.sh "Server will update in ${AUTO_UPDATE_WARN_MINUTES} minutes" "info" & fi