# Poker 51 table service — deploy guide

The online Poker 51 tables (`/area-51/poker-51/table/`) need a small server that deals the cards. It is a Cloudflare Worker, `cg-table51`, with one Durable Object per table. It runs separately from the website.

- The site keeps deploying exactly as it does now; uploading the site never touches this Worker.
- The Worker only answers `https://cerebralgraphix.com/api/table/*`.
- Until the Worker is deployed, the table page says “Tables are not switched on yet” and charges nothing. It checks for the service's own JSON reply, because the site answers unknown paths with the homepage.

The site's `functions/` folder is not used here. The live site is uploaded through the dashboard, and that upload does not run Pages Functions.

## One-time setup (about five minutes)

You need the Cloudflare account that holds the `cerebralgraphix.com` zone. Wrangler 4 is already installed on this PC (`wrangler --version` should show 4.x).

1. Open a terminal in this folder: `<unzipped site>\services\table51`.
2. Sign in, once: `wrangler login`. A browser window asks you to approve.
3. Deploy: `wrangler deploy`. This:
   - creates the Worker `cg-table51`;
   - creates the Durable Object class `Table51Room` through migration `v1` (SQLite storage, which the Workers Free plan supports);
   - adds the route `cerebralgraphix.com/api/table/*`.
4. Check it: open `https://cerebralgraphix.com/api/table/health`. You should see:

   ```json
   {"ok":true,"service":"cg-table51","version":"table51-v1","rules":"poker51-table-v1","game":"poker51"}
   ```

   If you see the homepage instead, the route is not attached yet: check it under Workers & Pages → cg-table51 → Settings → Domains & Routes.
5. Open `https://cerebralgraphix.com/area-51/poker-51/table/`, open a table, and sit down.

Before you rely on it, check your plan's current Workers and Durable Objects limits in the dashboard. A table uses:

- one request per action;
- one WebSocket per open page;
- an alarm per timed decision.

## Updating

Run `wrangler deploy` again from this folder after replacing the files. Tables in progress keep their state. Keep the `[[migrations]]` block as it is; a new tag is needed only if the class is renamed or deleted.

## Watching and stopping

- **Live log:** `wrangler tail cg-table51`.
- **Pause tables:** remove the route in the dashboard. The page goes back to “not switched on yet”, and stored tables stay intact for when you re-add it.
- **Removing the Worker for good:** `wrangler delete` deletes every table and every unclaimed cash-out. Players' buy-ins are already out of their browser banks and would not come back. Leave the Worker running, or pause it by removing the route.

## What is stored

For each table:

- seat names, as typed;
- SHA-256 hashes of the seat tokens;
- stacks, held jbits and abit scores;
- the last 40 hands, including every card and the deck commitments;
- cash-out records.

Nothing else is stored: no IP addresses, no accounts, and nothing from the player's browser beyond what they type and do at the table. A table closes after two idle hours and pays out every stack. It keeps its cash-out records for 30 days, then deletes itself.

## Fallback: a separate host

If the route cannot be attached to `cerebralgraphix.com`, serve the Worker on its own subdomain:

1. In `wrangler.toml`, replace the `routes` block with
   `routes = [{ pattern = "tables.cerebralgraphix.com", custom_domain = true }]`, then run `wrangler deploy`.
2. In `area-51/poker-51/table/index.html`, change
   `<meta name="cg-table-api" content="/api/table">` to
   `<meta name="cg-table-api" content="https://tables.cerebralgraphix.com/api/table">`, and upload the site.

The Worker answers cross-origin requests only from `https://cerebralgraphix.com` and `https://www.cerebralgraphix.com`. To change that list, set a variable, `ALLOWED_ORIGINS = "https://a.example,https://b.example"`, under `[vars]` in `wrangler.toml`.

## Trying it on this PC first

From this folder:

```sh
wrangler dev --assets ../.. --persist-to ../../../table51-dev-state --ip 127.0.0.1 --port 8787
```

Then open `http://127.0.0.1:8787/area-51/poker-51/table/`. Nothing is deployed, and the tables live only in the `--persist-to` folder. Delete that folder and any `.wrangler` folder here before packaging the site.

## Tests (no packages needed)

From the site root:

```sh
node services/table51/test/room.test.mjs
node tests/v3_3/verify-poker51-multi-v1.mjs
```
