Case study · 01
Magic Pack Simulator
A pack-opening game where the browser and the server run the same rules.

- 1rules function, run in the browser and on the server
- 23game actions, every one a pure state change
- 10,000packs simulated per set to test the odds
- 0runtime dependencies in the API
The problem
A rebuild of an older React/Vite app of mine as a static Astro site: open Magic: The Gathering Play Boosters in the browser, with real card data for every Play Booster set since Murders at Karlov Manor.
It grew into a full game: daily missions, sealed and drafts against bots, a collection and deck builder, and accounts with friends, trades and leaderboards. Once players can trade and rank, the save can’t just live in localStorage, where anyone can edit it. Guests still had to be able to play with no account and no connection.
Architecture
Every change to a save is an action handled by one pure function, apply(state, action, env): no DOM, no storage. For guests the browser runs it and keeps the save locally. For signed-in players the browser posts the action to the API, which runs the same file with server randomness and returns the new state. The UI only ever calls store.dispatch(action), so it doesn’t know which one it’s talking to.
Frontend
- Opener overlay: tear the pack, then stack, flip and summary. Taps land a card instantly, holding fast-forwards, and an auto-reveal mode holds longer on new cards, foils and rares.
- Foil and tilt done in CSS: a small script sets four custom properties from the pointer and the stylesheet draws the holo.
- A raw WebGL backdrop, tinted per set, that pauses when it’s off-screen.
- A service worker caches pages, scripts and the card data for sealed packs, so a guest can play offline; the manifest makes it installable.
- Everything respects reduced motion: a still backdrop frame, no sparks and instant transitions.
Backend
- Node’s built-in node:http and node:sqlite, bundled by esbuild into one file. No runtime dependencies and nothing native to compile.
- Accounts are link codes, not email and password. The code is shown once and only its SHA-256 is stored.
- Fairness rules: a guest save brought into a new account is clamped, imported cards can be sold but not traded, and leaderboards only read stats that server-run actions update.
- Rate limits are fixed windows in a database table, so they survive restarts.
- “Free pack ready” Web Push implemented from scratch on node:crypto (VAPID and RFC 8291 encryption), tested against the RFC’s own example. Subscriptions must point at a known push service, so the endpoint can’t be used for SSRF.
Data & infrastructure
- A build script turns Scryfall’s bulk card file into ~1.2 MB of JSON for 20 sets, plus set symbols. Pack structure per set (bonus sheets, special slots, wildcard splits, Collector recipes) is config.
- The build simulates each set’s expected pack value, which sets the in-game pack price.
- A scheduled GitHub Action refreshes the data every month and opens a pull request with a count check to review. Nothing deploys automatically.
- Two Coolify apps: the static site on nginx, and the API with a persistent volume and a backup script.
Interesting problems
01
Saves people can’t fake
Trades and leaderboards need an authoritative server, but forking the rules into a second codebase would drift. The API imports apply() straight from the site’s source and bundles it, so there is one implementation, tested once, and the server simply supplies the randomness.
02
Unreliable source data
Scryfall’s “in boosters” flag is missing on sets from 2026 and includes showcase prints on others. Pack pools are derived from print attributes instead (frame, promo status, one print per name), and a --check mode prints the derived counts next to Scryfall’s, which match wherever the flag is sane.
03
Getting the odds right
Pack odds come from the publisher’s own collecting articles where they exist. A unit test opens 10,000 seeded Play Boosters per set and checks every rate stays in bounds, so a failure reproduces exactly.
Outcome
Live at packs.hexproof.co.uk: guests play instantly and offline, and signed-in players get friends, trades, weekly leaderboards and push reminders, all on the same rules.
Covered by Playwright smoke tests on a desktop and a phone viewport, the odds test, and a smoke test for the API.