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.
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.
| Request | Behaviour | Responses |
|---|---|---|
GET /api/games | All games and tombstones. Tombstones older than 90 days are deleted along the way. | 200 {games:[…], now} |
PUT /api/games/:idbody: 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/:id | Writes 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)
- Every change calls
save(). It setsgame.updated = Date.now(), writes localStorage, and queues the game id. - The queue waits 700 ms after the last change (a debounce), then sends one request per game at a time:
PUT, orDELETEif the game was deleted locally. Deleted ids also sit in an outbox in localStorage, so a delete made offline survives a reload. - 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
updatedis 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:
- Skip it if a write for that id is pending or in flight. Local intent wins until it has been sent.
- Tombstone: delete the local copy if
local.updated ≤ tombstone.updated. Otherwise the local game was edited after the delete, so re-upload it. - Game: if this phone deleted it (outbox), send the delete again. Otherwise take the server copy if it's newer, or queue an upload if the local copy is newer.
- Local games the server has never seen are uploaded.
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
- The passcode is a shared group secret, not per-user authentication. The server compares SHA-256 digests with
crypto.timingSafeEqual. - The passcode is only ever sent in the
x-group-codeheader, never in a URL. It's never logged, and it's stored only in the browser's localStorage. - The server stores only validated, known fields, never the raw request body.
- All names are HTML-escaped (
esc()) before rendering, including data that comes from the server. vercel.jsonsetsX-Content-Type-Options: nosniff,X-Frame-Options: DENYandReferrer-Policy: same-origin.- To revoke access, change
GROUP_PASSCODEin Vercel and redeploy. Every phone will ask for the new code.
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
vercel link(or import the GitHub repo in the Vercel dashboard). Use the framework preset Other, with no build command. The output ispublic/.- Add Upstash Redis (free plan) from the Vercel Marketplace and connect it to every environment. That sets
UPSTASH_REDIS_REST_URL/_TOKENorKV_REST_API_URL/_TOKEN, and either pair works. vercel env add GROUP_PASSCODEfor Production, Preview and Development.vercel --prod. Once the repo is connected, every push tomaindeploys automatically.- Optional: add a custom domain under Project → Settings → Domains.
The full walkthrough is in docs/DEPLOYMENT.md in the repository.
Costs and limits
- Vercel Hobby: a static page plus a few hundred function calls per game night is far below the free limits.
- Upstash free tier: polling every 5 s is about 720 reads per hour per open phone. A few phones for a 3-hour night comes to several thousand commands, well within the free monthly allowance for a weekly game. To use less, raise
POLL_MSinapp.js. - Size: a 20-player, 100-round game is about 45 KB, under the 64 KB request limit.