# Firebase Replacement Guide

This project has been modified to use a custom HTTP server instead of Firebase for leaderboard functionality.

## What Was Replaced

Firebase services that were removed:
- **Firebase Realtime Database** - Used for leaderboards and player scores
- **Firebase Auth** - Authentication service (was included but not heavily used)
- **Firebase Analytics** - Analytics service (was disabled)

## Custom Server Setup

### Prerequisites

You need Node.js installed on your system. Download from: https://nodejs.org/

### Installation

1. **Navigate to server directory:**
   ```bash
   cd server
   ```

2. **Install server dependencies:**
   ```bash
   npm install
   ```

3. **Start the server:**
   ```bash
   npm start
   ```

   Or for development with auto-restart:
   ```bash
   npm run dev
   ```

4. **Verify server is running:**
   ```bash
   npm test
   # or manually:
   curl http://localhost:3000/api/health
   ```

### Server API Endpoints

The server provides these endpoints:

- `GET /api/leaderboard` - Get all leaderboard data
- `POST /api/leaderboard` - Upload/update leaderboard data  
- `GET /api/player/:level/:name` - Get specific player's high score
- `GET /api/health` - Health check

### Configuration

By default, the client connects to `http://localhost:3000/api`. To change this:

1. Edit `src/core/database_http.cpp`
2. Modify the `serverUrl` variable:
   ```cpp
   std::string Database::serverUrl = "http://your-server:3000/api";
   ```

## How It Works

### Client-Side Changes

1. **Removed Firebase dependencies** from CMakeLists.txt
2. **Replaced `database.cpp`** with `database_http.cpp` 
3. **HTTP client implementation:**
   - **Windows**: Uses WinINet (built into Windows)
   - **Linux/Mac**: Uses libcurl 
   - **Fallback**: Local file storage only

### Server-Side Implementation

- **Simple Node.js/Express server**
- **File-based storage** (leaderboard.json)
- **Automatic fallback** to local files if server unavailable
- **Background HTTP requests** (non-blocking game performance)

### Data Flow

1. **Game tries HTTP server first**
2. **If server unavailable** → fallback to local JSON file
3. **Server saves data** to `leaderboard.json` file
4. **Automatic sync** when both client and server are available

## Benefits of Custom Server

✅ **No external dependencies** - No Firebase SDK linking issues  
✅ **Full control** - Host anywhere, modify as needed  
✅ **Simplified deployment** - Single Node.js file  
✅ **Local development** - Works offline with file fallback  
✅ **Cross-platform** - Uses platform-native HTTP clients  
✅ **Lightweight** - Minimal resource usage  

## Production Deployment Options

### Option 1: Local Network Server
Run on a local computer accessible to game clients:
```bash
node server.js
```

### Option 2: Cloud Hosting
Deploy to cloud services like:
- **Heroku**: `git push heroku main`
- **DigitalOcean Apps**: Upload and deploy
- **AWS/Azure**: Use container services
- **Vercel/Netlify**: For serverless deployment

### Option 3: Docker Container
```dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY server.js .
EXPOSE 3000
CMD ["npm", "start"]
```

### Option 4: Database Backend (Optional)
For larger scale, replace file storage with:
- **SQLite** - Simple embedded database
- **PostgreSQL** - Full featured database
- **MongoDB** - Document database

## Troubleshooting

### Build Issues
If you get linking errors:
- **Windows**: Make sure `wininet.lib` is available
- **Linux**: Install libcurl: `sudo apt-get install libcurl4-openssl-dev`
- **Mac**: Install curl: `brew install curl`

### Connection Issues
- Check server is running: `curl http://localhost:3000/api/health`
- Verify client URL in `database_http.cpp`
- Check firewall settings for port 3000

### Game Fallback
If HTTP server is unavailable, the game automatically:
1. Uses local `levels/leaderboard.json` file
2. Saves scores locally only  
3. Syncs with server when it becomes available

## Migration Notes

The new system maintains **100% compatibility** with existing leaderboard data format. Existing `levels/leaderboard.json` files work without modification.

No changes needed to game logic - the `Database` class interface remains identical. 