Case study · 2026 · Personal
Code Battle
Two developers, the same problem set, one clock. The hard part isn't the editor — it's getting two browsers to agree on who won without trusting either of them.
- Stack
- Next.js 16 · React 19 · TypeScript · Supabase · Monaco
- Tables
- 8
- DB functions
- 8
- Indexes
- 12
- Source files
- 68
The problem
Practising for coding interviews is normally a solo activity: one person, one problem, a compiler that never disagrees with you. What that never rehearses is the part the interview actually tests — writing correct code while someone else races you to the same problem. Code Battle makes that a mode rather than an accident: a host picks the problems, shares a six-character room code, and both players solve the same set on their own devices against one countdown.
Decisions
- 01
Rating arithmetic lives in SQL, not the application
Elo and XP are computed inside finalize_match, a SECURITY DEFINER function, along with the per-match ledger rows. Putting that maths in the database costs testability — SQL is harder to unit test than application code — and in exchange the numbers a player is incentivised to inflate stop being an application concern. The settlement is also idempotent (it early-returns if the match is already finished) and takes a row lock on the match before writing, so two players finishing at the same moment cannot both settle it.
- 02
Transitions are compare-and-set, not a whitelist
A match moves waiting → matched → countdown → active → finished, and both clients follow it over Supabase Realtime rather than polling. transition_match only advances a match when the row is still in the status the caller expected, and allowed values are constrained by a CHECK on the column. Stamping starts_at at countdown time means both browsers read one shared deadline instead of each starting their own timer.
- 03
Realtime, with polling underneath it
Realtime alone was not enough: Supabase does not echo a client's own writes back to that client, which is exactly the case that matters when you are the host. The client subscribes to postgres_changes on the matches, players and submissions tables and also refetches on an interval, so a dropped or missed event degrades into a slow update rather than a frozen screen.
- 04
One final submission per player, per problem
A unique partial index — submissions_one_final_per_player on (match_id, player_id, problem_id) where is_final — makes double-scoring structurally impossible rather than something the code hopes won't happen. Every write path inserts with is_final true and no upsert, so a racing second insert is rejected by the database, and the action surfaces that error instead of silently retrying.
- 05
Room codes that survive people typing them
Codes are six characters drawn from an alphabet with the ambiguous glyphs removed — no I, O, 0 or 1 — because they get read aloud and retyped. The generator probes for an unused code before inserting, but the real guarantee is a unique constraint on the column; the probe just avoids a pointless failed insert.
Where it stands
- Pass and fail counts are computed in the browser and stored as reported. The server trusts those numbers when it awards XP, so re-executing submissions server-side is the next step and the weakest link in the current design.
- User code runs through new Function inside the page context. That is isolation by convention, not a real boundary — a Worker or iframe would be the honest version of it.
- Timeouts only apply to solutions that return a promise, so a synchronous infinite loop will hang the tab rather than being cut off.
- The state machine has no automated tests, and it is the highest-risk logic in the app.