Braid trial: apply groups side by side (braid#306)
  • TypeScript 87.4%
  • CSS 10.7%
  • JavaScript 1.2%
  • HTML 0.4%
  • Dockerfile 0.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-11 13:30:09 +00:00
e2e apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
openspec Archive ux-and-visual-review-21-tasks-from-deskt-c51d6z 2026-11-11 13:30:09 +00:00
packages apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
scripts apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
.dockerignore apply: implement the change and its tests 2026-11-06 10:16:22 +00:00
.gitignore propose: the change, its specs, its tasks and its estimate 2026-11-06 07:01:25 +00:00
.npmrc propose: the change, its specs, its tasks and its estimate 2026-11-06 07:01:25 +00:00
.nvmrc propose: the change, its specs, its tasks and its estimate 2026-11-06 07:01:25 +00:00
AGENTS.md propose: the change, its specs, its tasks and its estimate 2026-11-06 07:01:25 +00:00
Dockerfile apply: implement the change and its tests 2026-11-06 10:16:22 +00:00
package-lock.json apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
package.json apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
playwright.config.ts apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
README.md apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
tsconfig.json apply: implement the change and its tests 2026-11-11 13:27:36 +00:00
vitest.config.ts apply: implement the change and its tests 2026-11-06 10:16:22 +00:00

Scratch

A multiplayer online trick-taking card game in the Oh Hell family. Bid how many tricks you think you'll win, play to win exactly that many, and collect the fewest scratches to take the game. Play against friends in real time over the web — create a room in one browser tab, share the 4-letter code, and join from as many as four more.

The game is a small npm-workspaces monorepo:

  • packages/shared — the rules engine (deck, bidding, play, scoring, winner) and the client/server protocol types. Pure TypeScript, no I/O.
  • packages/server — a Socket.IO server that owns rooms and authoritative game state, and serves the built client. One process, one port.
  • packages/client — a React + Vite + TypeScript single-page app.

Requirements

  • Node.js 26 (the repo pins 26 in .nvmrc; anything >= 20 works). If you use nvm, nvm use in the repo root picks the right version automatically.
  • npm (ships with Node).

Install

nvm use          # optional: select Node 26 from .nvmrc
npm install

This installs the root and all workspace dependencies.

Run it locally

npm start

npm start typechecks, builds the client, and starts the server. When it prints Scratch server listening on http://127.0.0.1:3000, open that URL in your browser.

For development with server auto-reload on change:

npm run dev

npm run dev builds the client once, then runs the server under tsx watch so server edits reload automatically. Re-run it after client edits to rebuild.

The server binds to 127.0.0.1 by default and reads the port from PORT (default 3000). To serve on a specific host/port:

PORT=8080 HOST=0.0.0.0 npm start

Play a full game with multiple browser tabs

Everything runs from the same origin, so each browser tab (or window, or machine on your network) is a separate player:

  1. Open http://127.0.0.1:3000 in a first tab. Enter a display name, pick an avatar (remembered on this device for next time), and choose Create game. Pick your house rules (schedule, dealer restriction, first lead) and create the room. You'll see a 4-letter room code.
  2. Open the same URL in a second tab (a private window works too). Enter a different display name and choose Join with code, then type the room code. (You can also use Browse open games to see and join rooms by name.)
  3. Back in the first tab, the room now shows both seats. The creator presses Start game.
  4. Play: bid when it's your turn, then tap a card to select it and tap it again (or press Enter) to play it. Cards you're not allowed to play are dimmed.
  5. When the final round ends, the end screen shows the winner. Everyone presses Accept rematch to play again with the same players and settings.

Playing with bots

The creator can fill empty seats with named bots from the waiting room. Each bot plays in its own style:

  • Cautious Carl — bids almost nothing and dumps low cards.
  • Bluffing Bea — bids and plays unpredictably.
  • Aggressive Archie — bids high and fights for every trick.
  • Master Marge — counts the cards and plays to hit her bid exactly; the bot to beat.

Seat a bot with its Seat button, remove it with Remove on its seat row, or take the whole table with Fill remaining seats. Bots are played automatically by the server and accept rematches on their own, so a room with one human and four bots plays itself to the end.

The server also understands a fill:bots intent (exposed on the window.__app test surface as fillBots(n)) that seats bots from the roster in order — used by the automated judge and handy for a quick solo game.

Seated bots act with a short randomized delay (roughly 0.8–2.5 s per move, scaled by the room's game speed: 1.6× on Relaxed, 0.25× on Fast) so a table of bots feels human-paced rather than instant; the delay is applied by the server, so every player sees the same pacing. The automated judge and smoke runs opt out of it by starting the server with SCRATCH_BOT_DELAY_MS=0, which makes bot moves apply instantly:

SCRATCH_BOT_DELAY_MS=0 npm start

Automated runs can also pin the deal with SCRATCH_SEED=<number>: the server then feeds a seeded PRNG into the deal and the bots' moves, so the same sequence of actions replays the same game. Unset, deals stay random:

SCRATCH_SEED=1337 npm start

Deploy with Docker

A multi-stage Dockerfile builds the client and runs it together with the server in a single small Node image:

docker build -t scratch .
docker run --rm -p 3000:3000 scratch

Then open http://127.0.0.1:3000. The container listens on port 3000 inside; map it to any host port with -p. Set HOST=0.0.0.0 if you need to reach it from outside the container's network:

docker run --rm -p 3000:3000 -e HOST=0.0.0.0 scratch

Install as a PWA

The app is installable: on iOS, use Safari's Share → Add to Home Screen; on Android/Chrome, use the address-bar install icon. It launches in standalone mode (no browser chrome) with its own icon, and a service worker caches the app shell so it opens instantly — and still loads when the network is down (a game itself needs a connection, since it is played in real time over WebSockets).

The raster icons and phone splash images in packages/client/public/icons/ are generated from icons/icon.svg by node scripts/gen-pwa-assets.mjs (it renders the SVG in headless Chromium, so npx playwright install chromium is needed once). Re-run it after changing the icon and commit the new PNGs.

How to play Scratch

The same rules are available in-app under Help (reachable from the landing screen, the lobby, and the table).

  • Goal. Win exactly the number of tricks you bid. Fewer scratches than everyone else wins the game.
  • Rounds. Down plays 10 rounds, dealing 10, 9, 8, …, 1 cards per player. Down-and-Up plays 19 rounds: 10 down to 1, then 2, 3, …, 10 (the 1-card round is never repeated).
  • Trump. After each round is dealt, one card is flipped face up. Its suit is trump for that round and stays visible to everyone.
  • Bidding. Bidding starts with the player left of the dealer and goes clockwise, with the dealer last. Each player bids from 0 up to their hand size — the number of tricks they expect to win. All bids are public.
  • Play. The first trick is led, then play goes clockwise. You must follow the suit led if you can; if you have none, play anything. The highest trump wins the trick, otherwise the highest card of the led suit. The winner leads next.
  • Scratches. At the end of a round, any player who did not win exactly their bid — over or under — takes one scratch.
  • Winner. After the final round, the player with the fewest scratches wins. Ties are broken by the most total tricks won; if still tied, the win is shared.
  • House rules.
    • Dealer restriction (default off): when on, the dealer may not bid the number that would make the total of all bids equal the number of tricks in the round.
    • First lead (default player left of dealer): who leads the first trick of each round. The alternative, highest bidder, makes the highest bidder lead, with ties broken toward the dealer's left.

Tests

npm test

Runs the whole suite with Vitest: rules-engine unit tests (bidding, play, scoring, winner, and a full-game replay harness across every house-rule combination), server integration tests (rooms, sessions, legality, disconnection, rematch) driven by real Socket.IO clients, and client unit tests (sound, hand sorting, lobby and table markup, and the PWA artifacts).

Browser smoke tests (Playwright, in e2e/) run against a locally built, seeded server:

npx playwright test

The root playwright.config.ts builds the client, starts the server on port 4173 with a fixed test seed (SCRATCH_SEED), and waits for it to answer before the tests run.

To typecheck without running tests:

npm run typecheck

Project layout

packages/
  shared/    Rules engine + protocol types (no I/O)
  server/    Socket.IO game server + static client hosting
  client/    React + Vite single-page app