#!/bin/bash # Shattered Void MMO - Shell Startup Wrapper # # This script provides a simple shell interface for starting the Shattered Void MMO # with various options and environment configurations. set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' MAGENTA='\033[0;35m' CYAN='\033[0;36m' WHITE='\033[1;37m' NC='\033[0m' # No Color # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" NODE_SCRIPT="$SCRIPT_DIR/start-game.js" LOG_DIR="$SCRIPT_DIR/logs" PID_FILE="$LOG_DIR/startup.pid" # Default environment DEFAULT_ENV="development" ENV="${NODE_ENV:-$DEFAULT_ENV}" # Function to print colored output print_color() { local color=$1 local message=$2 echo -e "${color}${message}${NC}" } # Function to print banner print_banner() { if [ "${DISABLE_BANNER}" != "true" ]; then print_color $CYAN "╔═══════════════════════════════════════════════════════════════╗" print_color $CYAN "║ ║" print_color $CYAN "║ ${WHITE}SHATTERED VOID MMO LAUNCHER${CYAN} ║" print_color $CYAN "║ ${WHITE}Post-Collapse Galaxy Strategy Game${CYAN} ║" print_color $CYAN "║ ║" print_color $CYAN "╚═══════════════════════════════════════════════════════════════╝" echo fi } # Function to show usage show_usage() { echo "Usage: $0 [OPTIONS]" echo echo "Options:" echo " -e, --env ENV Set environment (development|production|staging)" echo " -p, --port PORT Set backend port (default: 3000)" echo " -f, --frontend-port Set frontend port (default: 5173)" echo " --no-frontend Disable frontend server" echo " --no-health Disable health monitoring" echo " --no-database Disable database checks" echo " --no-redis Disable Redis" echo " --skip-preflight Skip pre-flight checks" echo " --verbose Enable verbose logging" echo " --debug Enable debug mode" echo " --no-colors Disable colored output" echo " --log-file FILE Log output to file" echo " -h, --help Show this help message" echo " -v, --version Show version information" echo echo "Environment Variables:" echo " NODE_ENV Environment mode (development|production|staging)" echo " PORT Backend server port" echo " FRONTEND_PORT Frontend server port" echo " DISABLE_FRONTEND Disable frontend (true|false)" echo " DISABLE_REDIS Disable Redis (true|false)" echo " DISABLE_DATABASE Disable database (true|false)" echo " SKIP_PREFLIGHT Skip pre-flight checks (true|false)" echo " VERBOSE_STARTUP Enable verbose startup (true|false)" echo echo "Examples:" echo " $0 Start in development mode" echo " $0 --env production Start in production mode" echo " $0 --no-frontend Start without frontend" echo " $0 --port 8080 Start backend on port 8080" echo " $0 --debug --verbose Start with debug and verbose logging" } # Function to show version show_version() { if [ -f "$SCRIPT_DIR/package.json" ]; then local version=$(grep '"version"' "$SCRIPT_DIR/package.json" | cut -d'"' -f4) print_color $GREEN "Shattered Void MMO v$version" else print_color $GREEN "Shattered Void MMO (version unknown)" fi echo "Node.js $(node --version)" echo "NPM $(npm --version)" echo "Platform: $(uname -s) $(uname -m)" } # Function to check prerequisites check_prerequisites() { print_color $BLUE "🔍 Checking prerequisites..." # Check Node.js if ! command -v node &> /dev/null; then print_color $RED "❌ Node.js is not installed" exit 1 fi # Check Node.js version local node_version=$(node --version | cut -d'v' -f2 | cut -d'.' -f1) if [ "$node_version" -lt 18 ]; then print_color $RED "❌ Node.js 18+ required, found version $(node --version)" exit 1 fi # Check NPM if ! command -v npm &> /dev/null; then print_color $RED "❌ NPM is not installed" exit 1 fi # Check if startup script exists if [ ! -f "$NODE_SCRIPT" ]; then print_color $RED "❌ Startup script not found: $NODE_SCRIPT" exit 1 fi # Check if package.json exists if [ ! -f "$SCRIPT_DIR/package.json" ]; then print_color $RED "❌ package.json not found" exit 1 fi # Check if node_modules exists if [ ! -d "$SCRIPT_DIR/node_modules" ]; then print_color $YELLOW "⚠️ node_modules not found, running npm install..." npm install fi print_color $GREEN "✅ Prerequisites check passed" } # Function to create log directory setup_logging() { if [ ! -d "$LOG_DIR" ]; then mkdir -p "$LOG_DIR" fi } # Function to check if game is already running check_running() { if [ -f "$PID_FILE" ]; then local pid=$(cat "$PID_FILE") if kill -0 "$pid" 2>/dev/null; then print_color $YELLOW "⚠️ Game appears to be already running (PID: $pid)" print_color $YELLOW " Use 'pkill -f start-game.js' to stop it first" exit 1 else # Remove stale PID file rm -f "$PID_FILE" fi fi } # Function to setup signal handlers setup_signals() { trap cleanup SIGINT SIGTERM } # Function to cleanup on exit cleanup() { print_color $YELLOW "\n🛑 Received shutdown signal, cleaning up..." if [ -f "$PID_FILE" ]; then local pid=$(cat "$PID_FILE") if kill -0 "$pid" 2>/dev/null; then print_color $BLUE " Stopping game process (PID: $pid)..." kill -TERM "$pid" 2>/dev/null || true # Wait for graceful shutdown local wait_count=0 while kill -0 "$pid" 2>/dev/null && [ $wait_count -lt 10 ]; do sleep 1 wait_count=$((wait_count + 1)) done # Force kill if still running if kill -0 "$pid" 2>/dev/null; then print_color $RED " Force stopping game process..." kill -KILL "$pid" 2>/dev/null || true fi fi rm -f "$PID_FILE" fi print_color $GREEN "✅ Cleanup completed" exit 0 } # Function to start the game start_game() { print_color $GREEN "🚀 Starting Shattered Void MMO..." print_color $BLUE " Environment: $ENV" print_color $BLUE " Node.js: $(node --version)" print_color $BLUE " Working Directory: $SCRIPT_DIR" echo # Export environment variables export NODE_ENV="$ENV" # Change to script directory cd "$SCRIPT_DIR" # Start the game and capture PID if [ -n "$LOG_FILE" ]; then print_color $BLUE "📝 Logging to: $LOG_FILE" node "$NODE_SCRIPT" > "$LOG_FILE" 2>&1 & else node "$NODE_SCRIPT" & fi local game_pid=$! echo "$game_pid" > "$PID_FILE" print_color $GREEN "✅ Game started with PID: $game_pid" # Wait for the process wait "$game_pid" local exit_code=$? # Cleanup PID file rm -f "$PID_FILE" if [ $exit_code -eq 0 ]; then print_color $GREEN "✅ Game exited successfully" else print_color $RED "❌ Game exited with error code: $exit_code" fi exit $exit_code } # Function to validate environment validate_environment() { case "$ENV" in development|production|staging|testing) ;; *) print_color $RED "❌ Invalid environment: $ENV" print_color $YELLOW " Valid environments: development, production, staging, testing" exit 1 ;; esac } # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -e|--env) ENV="$2" shift 2 ;; -p|--port) export PORT="$2" shift 2 ;; -f|--frontend-port) export FRONTEND_PORT="$2" shift 2 ;; --no-frontend) export ENABLE_FRONTEND="false" shift ;; --no-health) export ENABLE_HEALTH_MONITORING="false" shift ;; --no-database) export DISABLE_DATABASE="true" shift ;; --no-redis) export DISABLE_REDIS="true" shift ;; --skip-preflight) export SKIP_PREFLIGHT="true" shift ;; --verbose) export VERBOSE_STARTUP="true" export LOG_LEVEL="debug" shift ;; --debug) export NODE_ENV="development" export DEBUG="*" export VERBOSE_STARTUP="true" export LOG_LEVEL="debug" ENV="development" shift ;; --no-colors) export DISABLE_COLORS="true" shift ;; --log-file) LOG_FILE="$2" shift 2 ;; -h|--help) show_usage exit 0 ;; -v|--version) show_version exit 0 ;; *) print_color $RED "❌ Unknown option: $1" echo show_usage exit 1 ;; esac done # Main execution main() { # Show banner print_banner # Validate environment validate_environment # Set up logging setup_logging # Check if already running check_running # Check prerequisites check_prerequisites # Set up signal handlers setup_signals # Start the game start_game } # Run main function main "$@"