# Clubhouse Sessions — Blackjack

A fully playable blackjack game built with vanilla PHP, HTML, CSS, JavaScript, and SQLite.

---

## Requirements

- PHP 8.0 or higher (with PDO and pdo_sqlite enabled)
- A web server (Apache, Nginx) **or** PHP's built-in dev server
- Write access to the project directory (for the SQLite database)

---

## Project Structure

```
clubhouse/
├── index.html          ← Main game page
├── db.php              ← Database connection & schema init
├── schema.sql          ← SQL schema reference (auto-applied by db.php)
├── css/
│   └── styles.css      ← All game styles
├── js/
│   └── game.js         ← Blackjack engine (vanilla JS)
├── api/
│   ├── player.php      ← GET/POST player session
│   └── hand.php        ← POST hand result → update stats & bankroll
└── data/               ← Created automatically on first run
    └── clubhouse.sqlite← SQLite database (auto-created)
```

---

## Setup Options

### Option A — PHP Built-in Dev Server (quickest)

```bash
cd /path/to/clubhouse
php -S localhost:8000
```

Then open: http://localhost:8000

### Option B — Apache / WAMP / XAMPP

1. Copy the `clubhouse/` folder into your web root:
   - XAMPP: `C:/xampp/htdocs/clubhouse/`
   - WAMP:  `C:/wamp64/www/clubhouse/`
   - Linux: `/var/www/html/clubhouse/`

2. Open: `http://localhost/clubhouse/`

### Option C — Nginx + PHP-FPM

```nginx
server {
    listen 80;
    server_name clubhouse.local;
    root /var/www/clubhouse;
    index index.html;

    location /api/ {
        try_files $uri $uri/ =404;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
```

---

## First Run

The `data/` directory and `clubhouse.sqlite` database are **created automatically** on the first API request. No manual SQL setup is needed.

To inspect the database manually:

```bash
sqlite3 data/clubhouse.sqlite
.tables
SELECT * FROM players;
SELECT * FROM stats;
SELECT * FROM hands LIMIT 10;
```

---

## How It Works

| Layer | Tech | Responsibility |
|-------|------|----------------|
| UI & Game Engine | Vanilla JS (`js/game.js`) | Card logic, deck management, UI rendering, phase control |
| Styles | CSS (`css/styles.css`) | Pokémon battle layout, responsive design |
| API — Player | PHP (`api/player.php`) | Create/load player session via `session_id` stored in localStorage |
| API — Hand | PHP (`api/hand.php`) | Resolve hand outcomes, calculate side bets, update bankroll & stats |
| Database | SQLite (`db.php`) | Persist player, stats, and hand history across sessions |

### Session persistence
- On first load, a random `session_id` is generated and stored in `localStorage`
- Every subsequent visit reloads the same player, bankroll, and stats
- Clearing `localStorage` starts a new session with a fresh $1,000 bankroll

---

## Game Rules

- **6-deck shoe**, reshuffled when fewer than 52 cards remain
- **Dealer hits soft 17**
- **Blackjack pays 3:2**
- **Double down** on any two cards
- **Split** up to 4 hands; splits use the same bet as the original
- **Insurance** offered when dealer shows an Ace (pays 2:1)
- **Minimum bet**: $25

### Side Bets

| Bet | Win Condition | Payout |
|-----|--------------|--------|
| Lucky 7s | Any 7 in first two cards | 3:1 |
| Lucky 7s | Two suited 7s | 50:1 |
| Perfect Pairs | Mixed pair (different colour) | 5:1 |
| Perfect Pairs | Coloured pair (same colour, different suit) | 15:1 |
| Perfect Pairs | Perfect pair (same suit) | 30:1 |
| 21+3 | Flush (player 2 + dealer up-card) | 5:1 |
| 21+3 | Straight | 10:1 |
| 21+3 | Three of a kind | 30:1 |
| 21+3 | Straight flush | 100:1 |

---

## Resetting a Player

To reset a player's bankroll and stats manually:

```sql
-- In sqlite3 shell
UPDATE players SET bankroll=1000 WHERE session_id='<their_session_id>';
UPDATE stats SET wins=0,losses=0,pushes=0,blackjacks=0,profit=0,current_streak=0,streak_type='',longest_win=0,longest_loss=0 WHERE player_id=(SELECT id FROM players WHERE session_id='<their_session_id>');
```

Or clear `localStorage` in the browser (DevTools → Application → Local Storage → delete `cs_session_id`).

---

## Troubleshooting

**"Could not connect to server"**
- Make sure PHP is running and the API files are being served through PHP (not opened directly as files)
- Check that `pdo_sqlite` is enabled: `php -m | grep sqlite`

**Database permission error**
- The web server needs write permission on the project directory
- `chmod 755 /path/to/clubhouse && chmod -R 755 /path/to/clubhouse/data`

**Blank avatar images**
- Avatars are loaded from `api.dicebear.com` — requires internet access
- If offline, avatars fall back to a blank box (game still works fine)
