# Poker 51 table service — deploy guide

> **Site v4.2:** `wrangler.toml` now deploys `src/worker-v2.js` (Riemann and Steve). If v1 is already live, see `UPDATE_TO_V2.md`. The health check then shows `"version":"table51-v2"`.

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.

No terminal? `DASHBOARD_DEPLOY.md` in this folder deploys the same Worker from the Cloudflare dashboard, using the single-file build `dist/cg-table51-worker.js`.

## 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 routes `cerebralgraphix.com/api/table/*` and `www.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”. Table timers keep running while the route is off: idle tables close, and 30 days after closing they delete themselves along with any unclaimed cash-outs. Re-add the route within 30 days so players can collect.
- **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. Every table, including one nobody sat at, closes after two idle hours and pays out every stack. A closed table with cash-out records keeps them for 30 days, then deletes itself; one with none is deleted at once. A table holds at most 400 cash-out records; after that it refuses new seats and players open a new table.

## Known limit: buy-ins are not verified

The service cannot see a browser's jbit bank, so `/sit` accepts any buy-in from 1 to 100 jbits. A scripted seat can bring unbacked chips to a table, and one person can hold several seats. Jbits have no monetary value. A Cloudflare rate-limiting rule on `POST /api/table/new` and `POST /api/table/*/sit` limits how often this can be done (see `DASHBOARD_DEPLOY.md`, step 7). Closing it for good needs a bank kept on the server, which is a design decision, not a patch.

## 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 services/table51/test/golive.test.mjs
node services/table51/test/bundle.test.mjs
node tests/v3_3/verify-poker51-multi-v1.mjs
```
