diff --git a/Factorio/factcheck.sh b/Factorio/factcheck.sh new file mode 100644 index 0000000..efd2b6c --- /dev/null +++ b/Factorio/factcheck.sh @@ -0,0 +1,172 @@ +#!/bin/bash +game=factorio + +# Color and style codes +RED='\033[1;31m' +GREEN='\033[1;32m' +YELLOW='\033[1;33m' +BLUE='\033[1;34m' +CYAN='\033[1;36m' +BOLD='\033[1m' +UNDERLINE='\033[4m' +RESET='\033[0m' + +timestamp() { + date +"%Y-%m-%d %H:%M:%S" +} + +section() { + echo -e "${BOLD}${CYAN}\n==============================" + echo -e "$1" + echo -e "==============================${RESET}" +} + +status() { + # $1 = message, $2 = color + echo -e "${2}${BOLD}[$(timestamp)]${RESET} $1" +} +################################ UPDATE FUNCTION ################################## +update(){ + section "โฌ†๏ธ Checking for Factorio Updates" + # URL for the Factorio API + FACTORIO_API="https://factorio.com/api/latest-releases" + + # Use curl to get the JSON response from the Factorio API + FACTORIO_RESPONSE=$(curl -s "$FACTORIO_API") + + # Use jq to parse the JSON and extract the stable headless version + FACTORIO_VERSION=$(echo "$FACTORIO_RESPONSE" | jq -r '.stable.headless') + + # Remove periods from the Factorio version number + FACTORIO_VERSION_NO_PERIODS=$(echo "$FACTORIO_VERSION" | tr -d '.') + + # Path to the file storing the current Factorio version + VERSION_FILE="/home/$game/factorio_version.txt" + + # Check if version.txt exists and if the latest Factorio version is newer + if [ -f "$VERSION_FILE" ]; then + CURRENT_VERSION=$(cat "$VERSION_FILE") + + if [ "$FACTORIO_VERSION_NO_PERIODS" -gt "$CURRENT_VERSION" ]; then + status "Newer Factorio version available. Updating $VERSION_FILE and downloading..." "$YELLOW" + + # Update Factorio + cd /home/$game || exit 1 + #NOTIFY + DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1299159784817299478/hE_NNiiMQ0maHa5UNXNsOHpRVugezXBdU1jgOXyZAqhEAAUPps0p_3M-5SBYnUt8a31e" #Personal server spam + DISCORD_USERNAME="Factorio" + DISCORD_AVATAR_URL="https://i.imgur.com/olnuoqL.png" + discord_message="# Factorio update available\nRestarting server in one minute" + curl -H "Content-Type: application/json" -X POST -d "{\"username\": \"$DISCORD_USERNAME\", \"content\": \"$discord_message\", \"avatar_url\": \"$DISCORD_AVATAR_URL\"}" "$DISCORD_WEBHOOK_URL" + screen -S private -X stuff 'Server update available. Reboot in 1 minute\n' + sleep 60 + + /usr/bin/screen -dmS update /home/factorio/new_screen_start.sh --update + status "Waiting 1min for server to boot before checking that they are running." "$BLUE" + sleep 60 + + else + status "Factorio is already up-to-date. No need to download an update." "$GREEN" + fi + else + # Create version.txt if it doesn't exist + echo "$FACTORIO_VERSION_NO_PERIODS" > "$VERSION_FILE" + status "Factorio version file created." "$GREEN" + fi +} + + + + + +#run the update check +update + + + + + + +CONFIG_FILE="/home/$game/servers-config.ini" + +section "๐Ÿ–ฅ๏ธ Checking Factorio Server Screens" + +# Parse enabled server names from config robustly (no brackets, no comments, no config header) +mapfile -t sessions < <( + awk '/^\s*\[.*\]\s*$/ && $0 !~ /^\s*;/ { gsub(/^\s*\[|\]\s*$/,""); if (tolower($0)!="factorio/servers-config.ini") print $0 }' "$CONFIG_FILE" +) + +################################ LOG CHECK FOR CONNECTION REFUSAL ################################## +LOG_BASE="/home/factorio/servers" +error_found=false + +for server in "${sessions[@]}"; do + LOG_FILE="$LOG_BASE/${server}/factorio-current.log" + if [ -f "$LOG_FILE" ]; then + # Check for both strings on the same line + if grep -q "Refusing connection" "$LOG_FILE" && grep -q "UserVerificationMissing" "$LOG_FILE"; then + error_found=true + status "Detected 'Refusing connection' with 'UserVerificationMissing' in $LOG_FILE. Will restart servers." "$RED" + break + fi + fi +done + +if [ "$error_found" = true ]; then + section "๐Ÿšจ UserVerificationMissing Detected" + #/usr/bin/bash /home/factorio/new_screen_start.sh + exit 0 +fi + +############################### CHECKING SCREENS ################## + +if [ "${#sessions[@]}" -eq 0 ]; then + status "No enabled servers found in $CONFIG_FILE. Exiting." "$RED" + exit 1 +fi + +status "Checking sessions: ${sessions[*]}" "$CYAN" + +section "๐Ÿงน Cleaning up Dead Screens" +screen -wipe > /dev/null 2>&1 +status "Dead screens cleaned up (if any)." "$BLUE" + +should_start_script=false + +check_screen_session() { + local session_name="$1" + if [ -n "$session_name" ]; then + if screen -ls | grep -qE "\\.${session_name}[[:space:]]"; then + status "๐ŸŸข Screen session '$session_name' is already active or detached." "$GREEN" + else + status "๐Ÿ”ด Screen session '$session_name' is not active." "$RED" + should_start_script=true + fi + fi +} + +for session in "${sessions[@]}"; do + check_screen_session "$session" +done + +# If any session is not active, run the screenstart.sh script +if [ "$should_start_script" = true ]; then + section "๐Ÿ”„ Restarting Missing Servers" + status "At least one specified screen session is not active. Starting the script." "$YELLOW" + /usr/bin/bash /home/factorio/new_screen_start.sh + summary_emoji="๐ŸŸก" + summary_msg="Some servers were not running and have been (re)started." +else + status "All specified screen sessions are active. No action needed." "$GREEN" + summary_emoji="โœ…" + summary_msg="All servers are online and all screens are active." +fi + +section "๐Ÿงน Active Screen Sessions" +screen -ls + +section "${summary_emoji} Summary" +status "$summary_msg" "$CYAN" +section "๐Ÿ Check complete" + +exit 0