> 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/towers.md).

# Towers

Towers is a single-player game where players climb a tower of 8 layers. Each layer contains multiple buttons — some safe, some bombs. Players reveal one button per layer and can cash out any time after passing at least one layer.

***

### Difficulty Reference

| Difficulty | Buttons per layer | Bombs per layer | Safe probability |
| ---------- | :---------------: | :-------------: | :--------------: |
| `easy`     |         3         |        1        |   2/3 (\~66.7%)  |
| `medium`   |         4         |        2        |     2/4 (50%)    |
| `hard`     |         3         |        2        |   1/3 (\~33.3%)  |

**Button indices** are `0`-based. Valid range: `0` to `buttons - 1` (e.g. `0–2` for easy/hard, `0–3` for medium).

**Multiplier formula**: `((1 - 0.10) / probability) ** layer` (10% house edge)

***

### Events

#### Client → Server

**`towers:start`**

Starts a new game.

```json
{
    "amount": 10.5,
    "difficulty": "easy",
    "clientSeed": "user-provided-seed"
}
```

* `amount`: number, min `0.01`, max `50`
* `difficulty`: `"easy"` | `"medium"` | `"hard"`
* `clientSeed`: non-empty string (used for Provably Fair)

***

**`towers:reveal`**

Reveals a button on the current layer.

```json
{
    "button": 1
}
```

* `button`: integer index, `0` to `buttons - 1` for the current difficulty

***

**`towers:cashout`**

Cashes out at the current multiplier. Requires at least **one layer** to have been successfully revealed.

* **Payload**: *(none)*

***

**`towers:active`**

Fetches the currently active game state. Use this to restore the game on refresh.

* **Payload**: *(none)*

***

#### Server → Client

**`towers:start` (Response)**

**Success:**

```json
{
    "status": true,
    "nextMultiplier": 1.35,
    "pf": {
        "serverSeedCommitment": "<sha256-of-server-seed>",
        "clientSeed": "user-provided-seed",
        "nonce": 42
    }
}
```

**Failure:**

```json
{
    "status": false,
    "message": "Bets are currently disabled"
}
```

Possible failure messages: `"Bets are currently disabled"`, `"Invalid difficulty"`, `"clientSeed is required"`, `"Invalid bet amount"`, `"Max bet is $50"`, `"Invalid user"`, `"You have an active game"`, `"Insufficient balance"`, `"An error occurred"`.

***

**`towers:reveal` (Response)**

**Safe tile — game continues:**

```json
{
    "lost": false,
    "nextMultiplier": 2.12
}
```

**Bomb hit — game over:**

```json
{
    "lost": true,
    "pattern": [[0, 1, 1], [1, 0, 1], ...],
    "proof": {
        "serverSeed": "<plaintext-server-seed>",
        "serverSeedCommitment": "<hash>",
        "clientSeed": "user-provided-seed",
        "nonce": 42
    }
}
```

> `pattern` is the full 8-layer board revealed. `0` = bomb, `1` = safe. `proof` is present only if the server seed is still held in memory (normal flow). Always verify `serverSeed` by hashing it and comparing to `serverSeedCommitment`.

**All 8 layers cleared — auto cashout:**

```json
{
    "lost": false,
    "nextMultiplier": 0,
    "payout": 250.00,
    "proof": {
        "serverSeed": "<plaintext-server-seed>",
        "serverSeedCommitment": "<hash>",
        "clientSeed": "user-provided-seed",
        "nonce": 42
    }
}
```

**Error:**

```json
{
    "status": false,
    "message": "No active game"
}
```

***

**`towers:cashout` (Response)**

**Success:**

```json
{
    "status": true,
    "payout": 150.00,
    "proof": {
        "serverSeed": "<plaintext-server-seed>",
        "serverSeedCommitment": "<hash>",
        "clientSeed": "user-provided-seed",
        "nonce": 42
    }
}
```

> `proof` is present under normal conditions. Always verify by hashing `serverSeed` → compare to `serverSeedCommitment`.

**Failure:**

```json
{
    "status": false,
    "message": "You must reveal at least one layer"
}
```

Possible failure messages: `"No active game"`, `"You must reveal at least one layer"`, `"An error occurred"`.

***

**`towers:active` (Response)**

**Active game found:**

```json
{
    "status": true,
    "active": true,
    "game": {
        "difficulty": "easy",
        "layer": 3,
        "betAmount": 10.5,
        "currentMultiplier": 2.12,
        "nextMultiplier": 3.05,
        "pf": {
            "serverSeedCommitment": "<sha256-of-server-seed>",
            "clientSeed": "user-provided-seed",
            "nonce": 42
        }
    }
}
```

**No active game:**

```json
{
    "status": true,
    "active": false
}
```

***

### Disconnect Behaviour

* The game remains active upon disconnect. The frontend must fetch the active state using `towers:active` Upon reconnection.

***

### Provably Fair

The outcome of every layer is determined before the game starts using:

* **`serverSeed`** — random 32-byte hex, generated by the server. **Kept secret until game end.**
* **`serverSeedCommitment`** — `SHA-256(serverSeed)`. Sent to the client at game start so they can verify fairness after the game.
* **`clientSeed`** — provided by the player. Included in the outcome derivation, giving the player control.
* **`nonce`** — a per-user incrementing counter.

**Verification** (after receiving `serverSeed`):

1. Compute `SHA-256(serverSeed)` and confirm it matches the `serverSeedCommitment` you received at game start.
2. For each layer `l` (0-indexed, 0 = bottom), The button layout is determined by a Fisher-Yates shuffle seeded with `HMAC-SHA256(serverSeed, "${clientSeed}:${nonce}:i:layer_${l}")`.


---

# 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/towers.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.
