Case study · Gaming / Real-time Applications
Kadi
The Kenyan card game, live in the browser: private tables for 2–4 players and bots, run by an authoritative game server on a tested rules engine.
01Overview
Kadi is a popular Kenyan card game with many house rules. This project brings it to the browser: 2–4 players at a private table, playing as guests with no sign-up, against friends, bots or both. It is live at kadi-ke.fly.dev.
02The problem
Card games look simple, but their rules are full of edge cases: several cards played at once, question cards, wild cards, the moment a player must declare "Kadi", and rules that block certain finishing cards. If those rules are scattered through UI and network code, the game becomes buggy and impossible to change. Multiplayer adds a second problem: players must not be able to cheat by seeing other hands or sending invalid moves.
03Target users
People who want to play Kadi with friends online: 2–4 players at a private table, invited with a code or a WhatsApp link, joining as guests without creating an account. Bots fill seats when friends aren't around.
04The solution
Build the rules first, as a pure and fully tested engine, then put an authoritative server in front of it. Clients only send intentions ("play these cards"); the server validates them against the engine and sends each player a view containing only their own hand. Live games run in memory, and every change is saved, so a server restart doesn't lose a game.
05My role
Solo project. I wrote the rules specification and the architecture, and built the engine, the game server and the UI with an AI coding assistant (Claude), in small pull requests checked by CI. A written rule keeps all game logic out of the UI.
06Technical architecture
A TypeScript monorepo. packages/game-engine is a deterministic rules engine with no UI or network code; it applies actions, produces per-player views and drives the bots. packages/shared holds the typed socket event contract and the parsers every payload passes through. apps/server owns rooms, turn timers, a bot worker pool and persistence. apps/web is the Next.js UI, and its server attaches the game server on the same port, so one process on one Fly.io machine serves both. During a game the room lives in memory; after every change the full room, including the hidden deck, is written to SQLite on a Fly volume and reloaded on boot. Voice chat runs browser to browser over WebRTC, with the server only passing set-up messages. Pushes to main deploy automatically through GitHub Actions.
Client
Browser UI (Built)
Next.js and React. Renders views only
Voice chat (In progress)
WebRTC, browser to browser
intents ↓ · per-player views ↑ (Socket.IO)Server
Authoritative game server (Built)
Rooms, turns, timers, reconnection
Bot worker pool (Built)
Bot search off the main thread
Accounts (In progress)
Sign-in, profiles, leaderboard
applyAction(state, action, rng)Core & persistence
Rules engine (Built)
Pure, deterministic TypeScript. No UI or network
SQLite (Built)
Every room change saved; reloaded on restart
- Built
- In progress
07Technology choices
- TypeScript
- One language and one set of types across the rules engine, the shared event contract, the game server and the UI.
- Socket.IO
- Persistent two-way connections so every player sees each move as it happens.
- SQLite
- Live games are held in memory on one machine, so SQLite on a disk volume is enough to recover them after a restart. Postgres is deferred until the game needs to scale out.
- Fly.io
- A long-running process for WebSockets, with the web app and game server sharing one port.
- WebRTC
- Voice goes directly between browsers, so no audio passes through the game server.
- Vitest
- Fast tests for the engine, including hundreds of seeded full-game simulations.
- Playwright
- Real multi-browser games with 2, 3 and 4 players, including one on a phone-sized screen.
08Implementation decisions
Rules engine first
The engine was built and tested before any multiplayer code. Every rule can be verified on its own, and seeded simulations play 450 full games to catch rule interactions a hand-written test would miss. The same seed always replays to the same final state.
The specification is the source of truth
Rules live in a written spec with numbered decisions (D-001 to D-035). A rule changes in the spec first, then in code and tests, which keeps house-rule debates out of the codebase.
Authoritative server, intent-only clients
The browser never decides what is legal. It sends an intent, and the server validates it and responds with a per-player view. Deck order and the random seed never leave the server, and bots decide from the same limited views, so they can't see hidden cards either.
Rule changes never alter a running game
Each game stores the rule set it started with. New rules and modes, such as Brutal, are added as named options, so a fix can ship without changing the rules of games already in progress.
One machine, memory first, SQLite for recovery
Live game state needs a long-running process, not serverless functions. Keeping it on one machine made SQLite enough, and the choice is easy to revisit if the game outgrows a single server.
AI builds the UI, never the rules
The UI was first planned for an AI UI builder, then built in the repository with an AI coding assistant instead. Either way, the UI only renders the server's view: even whether the Play button is enabled is decided by the engine.
09Challenges encountered
The rules kept surprising me. In a two-player game, a player could declare Kadi with a King and win on the King's extra turn before the opponent could respond, so the spec now requires a declaration to survive another player's turn. Penalty rules changed four times in four days as play-testing exposed forced pickups that should have been passable. Load was the other challenge: a local load test showed the hardest bots' search blocking the server for up to 2.5 seconds at 40 tables, so bot thinking moved to a pool of worker threads, falling back to a simpler move if a worker is slow. After that, 600 simulated players across 200 tables ran without errors. A flaky four-player browser test also turned out to be a real bug: speech bubbles on edge seats widened the page on phones and pushed the suit picker off screen.
10Trade-offs & lessons learned
Writing the rules down before writing code was the most valuable step. Most of the difficulty in a card game is in how rules interact, and settling each ambiguity as a numbered decision gave the code exactly one answer to implement. Keeping the engine pure, with no UI or network code, is what made it testable: randomised tests can play whole games and check rule interactions that hand-written tests would miss. The trade-off was pace. Nothing was playable while the engine was being built, and variants like penalty stacking had to wait so the core game could be correct first.
11Current status
Live and playable at kadi-ke.fly.dev, deployed automatically from main. 357 unit and integration tests and 4 end-to-end browser tests run in CI. Player accounts, profiles, streaks and a leaderboard are built but not switched on yet, because SMS sign-in is still in test mode. Voice chat needs a relay server to work reliably on mobile data.
Built · 8
- Rules engine implementing the full written spec, with seeded randomness
- Authoritative Socket.IO server: every move validated, each player sees only their own hand
- Guest play, private tables with 6-letter codes and WhatsApp invites
- 2–4 players, with bots to fill empty seats
- Idempotent, versioned actions
- Reconnection with a 30-second grace period, and game recovery after a server restart
- Classic and Brutal game modes, with optional penalty stacking
- Turn timer, rematch, mobile layout and 8 card themes
In progress · 2
- Voice chat between players (WebRTC); reliability on mobile data still being worked on
- Player accounts, profiles, streaks and a leaderboard
Milestones
M0 · FoundationBuilt
Engine consolidated; tests, type checks and lint run from the root
M1 · Rule decisionsBuilt
Rules spec v2.0; no open rule questions
M2 · Engine completionBuilt
Rules v2, seeded randomness and per-player views
M3 · Shared contractsBuilt
Typed socket events and payload parsers
M4 · DatabaseBuilt
SQLite on a Fly volume
M5 · RoomsBuilt
Guest sessions, room codes, readiness
M6 · Game loopBuilt
Idempotent, versioned actions over Socket.IO
M7 · ResilienceBuilt
Reconnection, 30-second grace, restart recovery
M8 · Multiplayer E2EBuilt
2, 3 and 4 players tested end to end in CI
M9 · Launch readinessBuilt
Limits, logs, crash reports, link previews, home-screen app, privacy and terms
M10 · Player accountsIn progress
Sign-in, profiles, streaks, leaderboard; waiting on SMS keys