# 1v1 FPS Multiplayer Shooter

A browser-based first-person shooter multiplayer game where players compete in 1v1 matches with different weapons and maps.

## Features

- ✅ Real-time 1v1 matchmaking
- ✅ Browser-based (Babylon.js)
- ✅ P2P gameplay network
- ✅ Multiple weapons (Pistol, Rifle, Shotgun)
- ✅ Arena maps
- ✅ Global leaderboard/highscores
- ✅ Player statistics

## Tech Stack

- **Frontend**: Babylon.js, TypeScript, Webpack
- **Backend**: Node.js, Express, Colyseus
- **Database**: PostgreSQL
- **Networking**: WebRTC (P2P) + Colyseus (Matchmaking)

## Quick Start

### Prerequisites
- Node.js 18+
- PostgreSQL 12+
- npm

### Installation

1. **Clone repository**
```bash
cd 1v1
```

2. **Setup Database**
```bash
createdb fps_1v1
psql fps_1v1 < backend/src/db/schema.sql
```

3. **Install Backend Dependencies**
```bash
cd backend
npm install
cp .env.example .env
# Edit .env with your database URL
```

4. **Install Frontend Dependencies**
```bash
cd ../frontend
npm install
```

### Running

**Terminal 1 - Backend Server**
```bash
cd backend
npm run dev
# Runs on ws://localhost:3000
```

**Terminal 2 - Frontend Dev Server**
```bash
cd frontend
npm start
# Opens on http://localhost:8080
```

Open multiple browser tabs to test 1v1 matches!

## Game Controls

- **WASD**: Move
- **Mouse**: Look around
- **Left Click**: Shoot
- **1/2/3**: Switch weapons
- **ESC**: Open menu

## Project Structure

```
1v1/
├── backend/
│   ├── src/
│   │   ├── server.ts           # Main server entry
│   │   ├── rooms/
│   │   │   ├── MatchmakingRoom.ts
│   │   │   └── GameRoom.ts
│   │   ├── api/
│   │   │   └── scores.ts       # REST API for leaderboard
│   │   └── db/
│   │       └── schema.sql      # Database schema
│   └── package.json
├── frontend/
│   ├── src/
│   │   ├── index.ts            # Entry point
│   │   ├── game.ts             # Game engine
│   │   ├── player.ts           # Player controller
│   │   ├── network.ts          # Networking
│   │   ├── weapons.ts          # Weapon system
│   │   └── ui.ts               # UI components
│   ├── public/
│   │   └── index.html          # HTML template
│   └── package.json
└── README.md
```

## API Endpoints

### GET `/api/leaderboard`
Get top 100 players by wins

**Response:**
```json
[
  {
    "rank": 1,
    "username": "Player1",
    "total_wins": 42,
    "total_losses": 5,
    "total_kills": 328,
    "total_deaths": 45,
    "win_rate": 89.36
  }
]
```

### GET `/api/player/:username/stats`
Get specific player statistics

### POST `/api/match-end`
Record match result (called by game server)

## Development

### Building for Production

**Backend:**
```bash
cd backend
npm run build
npm start  # Runs compiled JS
```

**Frontend:**
```bash
cd frontend
npm run build
# Output in frontend/dist/
```

### Type Checking
```bash
npm run type-check
```

## Next Steps / TODO

- [ ] Enhanced weapon balancing
- [ ] More maps (5+ arenas)
- [ ] Cosmetics/skins system
- [ ] Team modes (2v2)
- [ ] Spectator mode
- [ ] Anti-cheat system
- [ ] Mobile responsive
- [ ] Sound effects & music
- [ ] Character customization
- [ ] In-game chat

## Troubleshooting

**"Failed to connect to server"**
- Check backend is running: `ws://localhost:3000`
- Check CORS settings

**Database connection error**
- Verify PostgreSQL is running
- Check DATABASE_URL in .env

**No opponent found**
- Need at least 2 clients connected
- Check matchmaking room in server logs

## License

MIT
