← Back to the scorekeeper

Technical documentation

Architecture, data model, API, sync algorithm, security, local development and deployment.

Architecture

The app is a static page with no framework and no build step. Two small Vercel Functions sit behind it, and a single Upstash Redis hash stores the games. Each phone keeps a full copy in localStorage, so the app opens instantly and keeps working offline. The server is only the meeting point where phones exchange changes.

Phones talk to Vercel Functions over HTTPS with the x-group-code header; the functions read and write one Redis hash. Each phone index.html · app.js localStorage (full copy) write queue (700 ms) Vercel Static CDN: public/ GET /api/games PUT · DELETE /api/games/:id Upstash Redis judgment:games load poll 5 s push HGETALL HSET
Every API call carries the x-group-code header. Scores, totals and ranks are never stored. They're always worked out from bids and results on the phone.

Project layout

public/                  static site (served as-is)
  index.html             app shell
  app.css, app.js        UI + sync layer (ES module)
  lib/game.js            pure rules: scoring, ranks, trump/dealer rotation, CSV
  lib/validate.js        game/tombstone validation (shared by server + Import)
  guide.html, docs.html  user guide, this page
  manifest.webmanifest, sw.js, icon*.png|svg   Add-to-Home-Screen + offline shell
api/
  _lib.js                Redis client, passcode check, JSON helper
  games.js               GET  /api/games
  games/[id].js          PUT / DELETE /api/games/:id
dev/server.js            zero-dependency local server (in-memory store)
test/                    node:test suites (rules, validation, API)
docs/                    Markdown copies of the guides
reference/index.html     the original Claude-artifact version

Data model

game = {
  id,                 // /^[a-z0-9]{6,32}$/i
  name,               // ≤ 80 chars
  created, updated,   // ms timestamps; `updated` decides which copy wins
  mode,               // "exp" (10 + 11×bid) | "simple" (10 + bid)
  target,             // planned rounds, 0–100 (0 = not set)
  trump0,             // 0..3 index into ♠ ♦ ♣ ♥
  dealer0,            // seat index of the first dealer
  players: [{ id, name }],                 // 2–20, seating order, name ≤ 40
  rounds:  [{ t, cards, bids: {pid: n}, made: {pid: bool} }]   // ≤ 100
}                     // t = trump index 0–3, cards = null | 1–52, bids 0–52

tombstone = { id, deleted: true, updated }

Derived values (all in public/lib/game.js): the dealer for round i is (dealer0 + i) mod n. Bidding starts at the next seat. Round i's trump is (trump0 + i) mod 4. Cards per round go 1, 2, 3… up to ⌊52 / n⌋, and a manual change carries forward. Ranks are dense, so equal totals share a rank.

Storage: one Redis hash, judgment:games. Each field is a game id and each value is the JSON string of the game or tombstone. Local storage keys: judgment-score-v1 (games plus the current game), judgment-code (passcode), judgment-outbox (deletes not yet confirmed), judgment-local-only.

API

Every request needs the header x-group-code: <GROUP_PASSCODE>. Every response is JSON with Cache-Control: no-store.

RequestBehaviourResponses
GET /api/gamesAll games and tombstones. Tombstones older than 90 days are deleted along the way.200 {games:[…], now}
PUT /api/games/:id
body: game
Validates the game and stores only known fields. If the stored copy (game or tombstone) has a newer updated, it isn't overwritten.200 {ok:true} · 409 {game: existing} · 400 {error:"invalid"}
DELETE /api/games/:idWrites the tombstone {id, deleted:true, updated: now}.200 {ok:true}

Shared errors: 401 {error:"bad_code"}, 500 {error:"server_not_configured", detail} when the passcode or Redis env vars are missing, and 502 {error:"storage_error"} when Redis fails.

# list
curl -H "x-group-code: $CODE" https://www.judgementscore.com/api/games
# upsert
curl -X PUT -H "x-group-code: $CODE" -H "content-type: application/json" \
     --data @game.json https://www.judgementscore.com/api/games/mx29knewvnry6
# delete
curl -X DELETE -H "x-group-code: $CODE" https://www.judgementscore.com/api/games/mx29knewvnry6

Validation rules

Body ≤ 64 KB and the URL id must equal body.id. Players: 2–20, with unique ids (/^[a-z0-9_-]{1,32}$/i) and names of 1–40 characters. Rounds: at most 100. t 0–3, cards null or 1–52, bids are integers 0–52 keyed by known player ids, and made values are booleans. The same module validates games on Import in the browser.

Sync algorithm

Push (local change → server)

  1. Every change calls save(). It sets game.updated = Date.now(), writes localStorage, and queues the game id.
  2. The queue waits 700 ms after the last change (a debounce), then sends one request per game at a time: PUT, or DELETE if the game was deleted locally. Deleted ids also sit in an outbox in localStorage, so a delete made offline survives a reload.
  3. 409: the server copy is newer, so the phone adopts it. 401: the phone forgets the passcode and shows the gate. Network error: the change stays local, and the next pull re-queues it because the local updated is newer than the server's.

Pull (server → local)

On load, then every 5 s while the page is visible, and immediately on visibilitychange and online. For each server record:

The app re-renders only if something changed. It doesn't re-render while a setup-form field has focus or a dialog is open, and the bid sheet stays open, because render() only replaces #app and the sheet lives in #layer.

Conflict model: last writer wins, for the whole game, decided by the updated timestamp. That's enough for one scorekeeper per game at a family table. The trade-off is that two simultaneous editors can overwrite each other's last few taps, and phone clock skew affects ordering.

Security

Local development

npm install
npm test                                  # rules, validation and API tests (node:test)
npm run dev                               # http://localhost:3000, in-memory store, passcode "test"
GROUP_PASSCODE=hukum2026 PORT=4000 npm run dev
vercel dev                                # production-like, needs `vercel env pull .env.local`

dev/server.js serves public/ and calls the same handler functions as Vercel. They use web-standard Request → Response exports named GET, PUT and DELETE. MEMORY_STORE=1 is ignored when running on Vercel.

Deploy

  1. vercel link (or import the GitHub repo in the Vercel dashboard). Use the framework preset Other, with no build command. The output is public/.
  2. Add Upstash Redis (free plan) from the Vercel Marketplace and connect it to every environment. That sets UPSTASH_REDIS_REST_URL/_TOKEN or KV_REST_API_URL/_TOKEN, and either pair works.
  3. vercel env add GROUP_PASSCODE for Production, Preview and Development.
  4. vercel --prod. Once the repo is connected, every push to main deploys automatically.
  5. Optional: add a custom domain under Project → Settings → Domains.

The full walkthrough is in docs/DEPLOYMENT.md in the repository.

Costs and limits