> For the complete documentation index, see [llms.txt](https://trident-cas.sabn.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trident-cas.sabn.xyz/games/roulette.md).

# Roulette

There are 15 total squares:

* 7 Gray (2x payout)
* 7 Gold (2x payout)
* 1 Purple (14x payout)

***

### Game States

The game continuously loops through three states:

* **`0` (Waiting)**: Time to place bets (20 seconds).
* **`1` (Spinning)**: The result is generated, UI animation spins (7 seconds).
* **`2` (After Spin)**: Payouts are distributed to winners (3 seconds).

***

### Events

#### Client -> Server

**`roulette:bet`**

Places a bet on a specific color.

* **Payload**:

  ```json
  {
      "color": "gold", // "gray", "gold", or "purple"
      "amount": 10.5
  }
  ```
* **Response**: `roulette:bet` (Confirmation)

**`roulette:status`**

Fetches the current global game state. Usually called when the client first opens the game or reconnects.

* **Payload**: None
* **Response**: `roulette:status` (Current state, timer, grouped bets, and history)

***

#### Server -> Client

**`roulette:bet` (Response)**

Confirms whether the bet was successfully placed.

* **Payload (Success)**:

  ```json
  {
      "status": true,
      "amount": 10.5
  }
  ```
* **Payload (Error)**:

  ```json
  {
      "status": false,
      "message": "Game has already started" // or "Not enough balance", etc.
  }
  ```

**`roulette:state` (Broadcast)**

Broadcasted by the server loop to update the countdown timer or state transitions.

* **Payload**:

  ```json
  {
      "state": 0, // 0 = waiting, 1 = spinning, 2 = after spin
      "timer": 15000 // Milliseconds remaining for this state
  }
  ```

**`roulette:bets` (Broadcast)**

Broadcasted to all clients whenever a new bet is successfully placed. It contains the aggregated bets grouped by color.

* **Payload**:

  ```json
  {
      "bets": {
          "gray": [
              {
                  "amount": 25.0,
                  "pfp": "url",
                  "steamid": "user-id",
                  "username": "Player1",
                  "userLevel": 5
              }
          ],
          "gold": [],
          "purple": []
      }
  }
  ```

**`roulette:status` (Response)**

Returns the complete current context of the game.

* **Payload**:

  ```json
  {
      "bets": { "gray": [], "gold": [], "purple": [] }, // Grouped bets
      "state": 0,
      "timer": 12000,
      "last100": [4, 12, 14, 2, ...], // History of the last 100 round results (indexes 0-14)
      "index": 0 // The current round's winning index (if state > 0)
  }
  ```

**`roulette:result` (Broadcast)**

Broadcasted when the state transitions from `Waiting` (0) to `Spinning` (1). It tells the UI what the outcome of the spin is so the animation can begin.

* **Payload**:

  ```json
  {
      "result": "gold",
      "index": 4, // 0 to 14
      "last100": [4, 12, 14, 2, ...] // History with the new index prepended
  }
  ```

**`roulette:proof` (Broadcast)**

Broadcasted alongside `roulette:result` at the start of the spin, providing the provably fair commitment hashes.

* **Payload**:

  ```json
  {
      "serverSeedCommitment": "hash", // SHA256 hash of the unrevealed server seed
      "publicSeed": "block-id", // EOS Head Block ID or backup random hex
      "nonce": 123 // Global Redis nonce for the round
  }
  ```

**`roulette:reveal` (Broadcast)**

Broadcasted when the round ends and transitions back to `Waiting` (0). It reveals the unhashed server seed used in the previous spin.

* **Payload**:

  ```json
  {
      "serverSeed": "original-secret-hex"
  }
  ```

***

### Game Logic Details

1. **Provably Fair**:
   * Uses `serverSeed`, `publicSeed`, and a globally incrementing Redis `nonce`.
   * The generated `random` result is derived via `deriveUint32` modulo 15, yielding an `index` between 0 and 14.
   * `index === 14` maps to purple (1/15 chance).
   * Even `index` (excluding 14) maps to gold (7/15 chance).
   * Odd `index` maps to gray (7/15 chance).
2. **Concurrency & Architecture**:
   * The entire game state and loop timers are stored globally in Redis.
   * Global locks (`Game.withLock`) ensure only one server instance transitions the game states or processes winners.
   * Bets are wrapped in user locks to prevent balance deduction race conditions.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trident-cas.sabn.xyz/games/roulette.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
