# Multiplayer Asteroids

A multiplayer party game based on the classic Asteroids arcade game. Players join using their mobile phones by scanning a QR code, and the game is displayed on a shared host screen. Perfect for parties, events, and gatherings.

## Features

- **Multiplayer** - Up to 20 players can join simultaneously
- **QR Code Join** - Players scan a QR code with their phones to instantly join the game
- **Auto-Launch** - Game automatically starts every 5 minutes
- **Game Modes**:
  - **Free-for-All (FFA)** - Every player for themselves, destroy asteroids and other ships
  - **Capture the Flag (CTF)** - Team-based mode with flag capturing mechanics
- **Mobile Touch Controls** - Optimized touch interface for phone controllers
- **Real-time WebSocket Communication** - Low-latency gameplay via WebSocket
- **Host Display** - Full-screen game arena with HUD, scores, and lobby management

## Prerequisites

- Node.js 16+

## Installation

```bash
npm install
```

## Running the Server

```bash
npm start
```

Or directly:

```bash
node server.js
```

The server will start on port 3000 by default.

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `PORT`   | `3000`  | The port the server listens on |

## How to Use

1. **Open the host screen** - Navigate to `http://localhost:3000` (or your server's address) in a browser on the display computer. This will show the game arena and a QR code.

2. **Players join** - Players scan the QR code with their mobile phones. This opens the controller interface where they enter their name and join the game.

3. **Game starts** - The game auto-launches every 5 minutes, or the host can click "Force Start" to begin immediately.

4. **Game modes**:
   - **Free-for-All**: Destroy asteroids and other players to earn points. Last player standing wins.
   - **Capture the Flag**: Players are split into Red and Blue teams. Grab the enemy flag and bring it to your base to score. First team to 3 captures wins, or the team with the most captures when the 5-minute timer expires.

5. **Controls** (on mobile):
   - Rotate Left / Rotate Right buttons for steering
   - Thrust button for acceleration
   - Fire button to shoot

## Deployment

### Nginx Subdomain Configuration

To deploy behind Nginx on a subdomain:

```nginx
server {
    listen 80;
    server_name game.yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

### Running with PM2 (Production)

For production use, run the server with PM2 for process management and auto-restart:

```bash
pm2 start server.js --name asteroids
```

Other useful PM2 commands:

```bash
pm2 stop asteroids
pm2 restart asteroids
pm2 logs asteroids
```

### Deployment Notes

- Set your domain's DNS A record to point to your server's IP address
- Update `server_name` in the Nginx config to match your chosen subdomain
- For HTTPS, add SSL certificates (e.g., via Let's Encrypt/Certbot)
- The WebSocket connection will automatically use `wss://` when served over HTTPS
- Make sure port 3000 (or your configured PORT) is not blocked by a firewall
