Pen Warrior
Pen Tauk from my school desk to the browser. Shared TypeScript physics and server-authoritative rooms on Cloudflare.
Overview
At middle school, we called it Pen Tauk: put two pens on a desk, take turns flicking, and try to knock the other one off. Most of those friends live in different countries now. I built Pen Warrior so we could share a desk again, through a room code in the browser.
- The challenge
- Two players on different devices need to agree on where each pen lands. A phone’s frame rate or a delayed message should not change the outcome of a shot.
- The approach
- I shared one deterministic TypeScript simulation between the browser and server. Each room has a Cloudflare Durable Object that validates moves and owns the match state; the clients replay the accepted inputs.
- The result
- V1 supports computer matches and room-code 1v1 on a phone or desktop, with no sign-up or installation.
Project highlights
- 01
Five pens with different strength, reach and accuracy, plus desk runes that grant a one-turn ability.
- 02
Room codes bring friends to the same desk; quick chat keeps the conversation in the match. A computer opponent makes it possible to play alone too.
- 03
Built solo over two weeks after hours, with Claude Opus/Sonnet and Codex Astra/Sol as pair programmers.
Tech stack & tools
One physics simulation, two runtimes
The shared TypeScript core runs at a fixed 120 steps per second and handles pen collisions, spin, friction and falls. Rendering interpolates between simulation frames, so display refresh rate does not set the physics speed. After a turn, a hash of the settled state lets the browser check its result against the server’s.
What happens when you flick
The browser sends the grab point and pull vector over a WebSocket. The room validates the input and turn, runs the simulation, then sends the accepted move and result hash back to the players. Each browser replays that move locally. The server decides whether the shot counts; the client draws the motion.
A Durable Object for each desk
One Cloudflare Durable Object owns a room’s seats, turn order, deadlines and match state. Keeping those decisions together gives both players one authority for the match. The computer opponent also runs inside the room and submits moves through the same validation and simulation rules.
Preact for screens, PixiJS for the arena
Preact handles the home screen, room flow, pen selection and match UI. PixiJS renders the desk, pens, aiming guides and effects. Pointer input supports pulling and releasing on touchscreens or with a mouse; keyboard controls provide another way to aim and flick.
Deployment and storage
Vite builds the client for Cloudflare Pages. Pages forwards API and WebSocket traffic to a Worker, which routes players into their room objects. D1 stores player identities, while room state and events live in Durable Object storage. V1 runs on Cloudflare’s free plan, with hosting at $0 for this release.
Checking that both sides agree
Vitest covers simulation and room rules, including tests in the Workers runtime. Golden trajectory tests compare recorded outcomes and state hashes across Node and browser engines. Playwright exercises browser journeys, including two-player rooms and computer matches, so the checks cover the path from input to a resolved turn.