Skip to content

Commit

Permalink
Merge branch 'Callum027-rcon-spaces' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jammsen committed Apr 4, 2024
2 parents f45e3a3 + beab5c7 commit eac234b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ENV DEBIAN_FRONTEND=noninteractive \
RESTART_ENABLED=false \
RESTART_DEBUG_OVERRIDE=false \
RESTART_CRON_EXPRESSION="0 18 * * *" \
# RCON-Playerdection - NEEDS RCON ENABLED!
# RCON-Playerdetection - NEEDS RCON ENABLED!
RCON_PLAYER_DETECTION=true \
RCON_PLAYER_DETECTION_STARTUP_DELAY=60 \
RCON_PLAYER_DETECTION_CHECK_INTERVAL=15 \
Expand Down
2 changes: 1 addition & 1 deletion default.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BACKUP_RETENTION_AMOUNT_TO_KEEP=72
RESTART_ENABLED=false
RESTART_DEBUG_OVERRIDE=false
RESTART_CRON_EXPRESSION="0 18 * * *"
# RCON-Playerdection - NEEDS RCON ENABLED!
# RCON-Playerdetection - NEEDS RCON ENABLED!
RCON_PLAYER_DETECTION=true
RCON_PLAYER_DETECTION_STARTUP_DELAY=60
RCON_PLAYER_DETECTION_CHECK_INTERVAL=15
Expand Down
24 changes: 12 additions & 12 deletions includes/playerdetection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ player_detection_loop() {
while true; do
compare_players
sleep "$RCON_PLAYER_DETECTION_CHECK_INTERVAL"
done
done
}

# Function to compare current and previous player lists
compare_players() {
local old_players=("${current_players[@]}")
readarray -t current_players < <(rcon showplayers | tail -n +2)
readarray -t current_players < <(rcon showplayers 2> /dev/null | tail -n +2)

for player_info in "${current_players[@]}"; do
# Extract player name, UID, and Steam ID from player info
# This part sets the Internal Field Separator (IFS) variable to ','.
# In Bash, the IFS variable determines how Bash recognizes word boundaries.
# By default, it includes space, tab, and newline characters.
# This part sets the Internal Field Separator (IFS) variable to ','.
# In Bash, the IFS variable determines how Bash recognizes word boundaries.
# By default, it includes space, tab, and newline characters.
# By setting it to ',', we're telling Bash to split input lines at commas.
# https://tldp.org/LDP/abs/html/internalvariables.html#IFSREF
IFS=',' read -r -a player_data <<< "$player_info"
Expand Down Expand Up @@ -82,39 +82,39 @@ compare_players() {

# Function to announce a player join
announce_join() {
time=$(date '+%H:%M:%S')
time=$(date '+[%H:%M:%S]')
message="Player $1 has joined the server."
echo "${time}: $message"
if [[ -n $WEBHOOK_ENABLED ]] && [[ $WEBHOOK_ENABLED == "true" ]]; then
send_info_notification "$message"
fi
if [[ -n $RCON_ENABLED ]] && [[ $RCON_ENABLED == "true" ]]; then
broadcast_player_join "${1// /\-}"
broadcast_player_join "${1}"
fi
}

# Function to announce a player join
announce_name_change() {
time=$(date '+%H:%M:%S')
time=$(date '+[%H:%M:%S]')
message="Player $1 has changed their name to $2."
echo "${time}: $message"
if [[ -n $WEBHOOK_ENABLED ]] && [[ $WEBHOOK_ENABLED == "true" ]]; then
send_info_notification "$message"
fi
if [[ -n $RCON_ENABLED ]] && [[ $RCON_ENABLED == "true" ]]; then
broadcast_player_name_change "${1// /\-}" "${2// /\-}"
broadcast_player_name_change "${1}" "${2}"
fi
}

# Function to announce a player leave
announce_leave() {
time=$(date '+%H:%M:%S')
time=$(date '+[%H:%M:%S]')
message="Player $1 has left the server."
echo "${time}: $message"
if [[ -n $WEBHOOK_ENABLED ]] && [[ $WEBHOOK_ENABLED == "true" ]]; then
send_info_notification "$message"
fi
if [[ -n $RCON_ENABLED ]] && [[ $RCON_ENABLED == "true" ]]; then
broadcast_player_leave "${1// /\-}"
broadcast_player_leave "${1}"
fi
}
}
49 changes: 23 additions & 26 deletions includes/rcon.sh
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
# shellcheck disable=SC2148

function get_time() {
date '+[%H:%M:%S]'
}

function save_and_shutdown_server() {
rconcli 'broadcast Server-shutdown-was-requested-init-saving'
rconcli 'save'
rconcli 'broadcast Done-saving-server-shuts-down-now'
rconcli broadcast "$(get_time) Server shutdown requested. Saving..."
rconcli save
rconcli broadcast "$(get_time) Saving done. Server shutting down..."
}

function broadcast_automatic_restart() {
time=$(date '+%H:%M:%S')

for ((counter=1; counter<=15; counter++)); do
rconcli "broadcast ${time}-AUTOMATIC-RESTART-IN-$counter-MINUTES"
rconcli "broadcast $(get_time)-AUTOMATIC-RESTART-IN-$counter-MINUTES"
sleep 1
done
rconcli 'broadcast Saving-world-before-restart...'
rconcli 'save'
rconcli 'broadcast Saving-done'
rconcli 'broadcast Creating-backup'
rconcli broadcast "$(get_time) Saving world before restart..."
rconcli save
rconcli broadcast "$(get_time) Saving done"
rconcli broadcast "$(get_time) Creating backup..."
rcon "Shutdown 10"
}

function broadcast_backup_start() {
time=$(date '+%H:%M:%S')

rconcli "broadcast ${time}-Saving-in-5-seconds"
rconcli broadcast "$(get_time) Saving in 5 seconds..."
sleep 5
rconcli 'broadcast Saving-world...'
rconcli 'save'
rconcli 'broadcast Saving-done'
rconcli 'broadcast Creating-backup'
rconcli broadcast "$(get_time) Saving world..."
rconcli save
rconcli broadcast "$(get_time) Saving done"
rconcli broadcast "$(get_time) Creating backup..."
}

function broadcast_backup_success() {
rconcli 'broadcast Backup-done'
rconcli broadcast "$(get_time) Backup done"
}

function broadcast_backup_failed() {
rconcli 'broadcast Backup-failed'
rconcli broadcast "$(get_time) Backup failed"
}

function broadcast_player_join() {
time=$(date '+%H:%M:%S')
rconcli "broadcast ${time}-$1-joined-the-server"
rconcli broadcast "$(get_time) $1 joined the server"
}

function broadcast_player_name_change() {
time=$(date '+%H:%M:%S')
rconcli "broadcast ${time}-$1-renamed-to-$2"
rconcli broadcast "$(get_time) $1 renamed to $2"
}

function broadcast_player_leave() {
time=$(date '+%H:%M:%S')
rconcli "broadcast ${time}-$1-left-the-server"
rconcli broadcast "$(get_time) $1 left the server"
}

function check_is_server_empty() {
Expand All @@ -61,4 +58,4 @@ function check_is_server_empty() {
else
return 1 # Server not empty
fi
}
}
19 changes: 11 additions & 8 deletions scripts/backupmanager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ LOCAL_GAME_SAVE_PATH=${GAME_SAVE_PATH} # Directory where the game save files are
LOCAL_BACKUP_RETENTION_POLICY=${BACKUP_RETENTION_POLICY} # Number of backup files to keep
LOCAL_BACKUP_RETENTION_AMOUNT_TO_KEEP=${BACKUP_RETENTION_AMOUNT_TO_KEEP} # Number of backup files to keep

function get_time() {
date '+[%H:%M:%S]'
}

function print_usage() {
script_name=$(basename "$0")
echo "Usage:"
Expand Down Expand Up @@ -128,26 +132,25 @@ function create_backup() {
check_required_directories

date=$(date +%Y%m%d_%H%M%S)
time=$(date +%H-%M-%S)
backup_file_name="saved-${date}.tar.gz"

mkdir -p "${LOCAL_BACKUP_PATH}"

rconcli "broadcast ${time}-Saving-in-5-seconds"
rconcli broadcast "$(get_time) Saving in 5 seconds..."
sleep 5
rconcli 'broadcast Saving-world...'
rconcli 'save'
rconcli 'broadcast Saving-done'
rconcli broadcast "$(get_time) Saving world..."
rconcli save
rconcli broadcast "$(get_time) Saving done"
sleep 15
rconcli 'broadcast Creating-backup'
rconcli broadcast "$(get_time) Creating backup..."

if ! tar cfz "${LOCAL_BACKUP_PATH}/${backup_file_name}" -C "${LOCAL_GAME_PATH}/" "Saved" ; then
broadcast_backup_failed
ee ">>> Backup failed"
else
broadcast_backup_success
es ">>> Backup '${backup_file_name}' created successfully"
fi
fi

if [[ -n ${LOCAL_BACKUP_RETENTION_POLICY} ]] && [[ ${LOCAL_BACKUP_RETENTION_POLICY} == "true" ]] && [[ ${LOCAL_BACKUP_RETENTION_AMOUNT_TO_KEEP} =~ ^[0-9]+$ ]]; then
ls -1t "${LOCAL_BACKUP_PATH}"/saved-*.tar.gz | tail -n +"$(($LOCAL_BACKUP_RETENTION_AMOUNT_TO_KEEP + 1))" | xargs -d '\n' rm -f --
Expand Down Expand Up @@ -208,7 +211,7 @@ function clean_backups() {
ei "> No files in the backup directory ${LOCAL_BACKUP_PATH}. Exiting..."
exit 0
fi

files=$(ls -1t "${LOCAL_BACKUP_PATH}"/saved-*.tar.gz)
files_to_delete=$(echo "${files}" | tail -n +"$(($num_files_to_keep + 1))")
num_files=$(echo -n "${files}" | grep -c '^')
Expand Down
12 changes: 10 additions & 2 deletions scripts/rconcli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ source /includes/colors.sh
# Arguments: <command>
# Example: run_rcon_cli "showplayers"
run_rcon_cli() {
local cmd=$*
if [[ -z ${RCON_ENABLED+x} ]] || [[ "$RCON_ENABLED" != "true" ]]; then
ew ">>> RCON is not enabled. Aborting RCON command ..."
exit
fi
output=$(rcon -c "$RCON_CONFIG_FILE" "${cmd}")
local command=$1
shift
if [ $# -ge 1 ]; then
# In the command value, replace ASCII space characters with
# unicode non-breaking space characters.
full_command="$command $(echo "$@" | tr ' ' '\240')"
else
full_command=$command
fi
output=$(rcon -c "$RCON_CONFIG_FILE" "$full_command" | tr -d '\0')
ei_nn "> RCON: "; e "${output}"
}

Expand Down
19 changes: 11 additions & 8 deletions scripts/restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ source /includes/colors.sh
source /includes/server.sh
source /includes/webhook.sh

function get_time() {
date '+[%H:%M:%S]'
}

function schedule_restart() {
ew ">>> Automatic restart was triggered..."
PLAYER_DETECTION_PID=$(<PLAYER_DETECTION.PID)
Expand All @@ -22,23 +26,22 @@ function schedule_restart() {
send_restart_now_notification
fi
break
else
else
ew ">>> Server has still players"
fi
time=$(date '+%H:%M:%S')
rconcli "broadcast ${time}-AUTOMATIC-RESTART-IN-$counter-MINUTES"
rconcli broadcast "$(get_time) AUTOMATIC RESTART IN $counter MINUTES"
fi
if [[ -n $RESTART_DEBUG_OVERRIDE ]] && [[ $RESTART_DEBUG_OVERRIDE == "true" ]]; then
sleep 1
else
sleep 60
fi
fi
done

if [[ -n $RCON_ENABLED ]] && [[ $RCON_ENABLED == "true" ]]; then
rconcli 'broadcast Saving-world-before-restart...'
rconcli 'save'
rconcli 'broadcast Saving-done'
rconcli broadcast "$(get_time) Saving world before restart..."
rconcli save
rconcli broadcast "$(get_time) Saving done"
sleep 15
kill -SIGTERM "${PLAYER_DETECTION_PID}"
rcon "Shutdown 10"
Expand All @@ -58,4 +61,4 @@ function schedule_restart() {
fi
}

schedule_restart
schedule_restart

0 comments on commit eac234b

Please sign in to comment.