I built a GeoGuessr-style multiplayer game for a GTA 6 map on one Durable Object
Leonida Arena is a small multiplayer game on top of a fan-made map of GTA VI's state of Leonida. A lobby gets five places, everyone has 30 seconds per place to drop a pin, closer pins score more, and after each round a 3
Leonida Arena is a small multiplayer game on top of a fan-made map of GTA VI's state of Leonida. A lobby gets five places, everyone has 30 seconds per place to drop a pin, closer pins score more, and after each round a 3D camera flies to the real spot. Up to 8 players, no accounts, and browser and iPhone players share the same lobbies.
Try it: https://leonidamaps.app/arena (open a lobby, send the 4-letter code to a friend).
This post is about the backend, because it turned out smaller than I expected: one Worker and one Durable Object.
One Durable Object for everything
Every request under /api/arena/ goes to the same object:
if (new URL(request.url).pathname.startsWith(`${ARENA_PATH}/`))
return env.ARENA.get(env.ARENA.idFromName('global')).fetch(request)
That single object owns nickname reservations, all lobbies, every WebSocket and the daily ranking. At this scale (a fan project, a few lobbies at a time) one object is plenty, and it removes a whole class of problems: no cross-object coordination, no distributed locks, and a lobby list is just a SELECT.
State lives in the object's built-in SQLite: a names table, a lobbies table where each lobby is one JSON row, and player_scores for the daily ranking.
One alarm for many timers
A game is a small state machine: waiting → countdown → round → reveal → … → finished. Each phase has a deadline, and a Durable Object has only one alarm. Each lobby row stores its next deadline and the alarm is always set to the nearest one:
const next = this.sql.exec<{ next: number | null }>(
'SELECT MIN(deadline) AS next FROM lobbies WHERE deadline IS NOT NULL',
).one().next
if (next === null) await this.ctx.storage.deleteAlarm()
else await this.ctx.storage.setAlarm(next)
When the alarm fires, it advances every lobby whose deadline has passed and sets the alarm again. When nobody is playing, there is no alarm at all.
Hibernating WebSockets
Sockets are accepted with ctx.acceptWebSocket(server, [lobby.code]), the hibernation API. The lobby code is the socket's tag, so broadcasting to a lobby is ctx.getWebSockets(code), and the object can be evicted from memory between messages without dropping anyone. Between rounds, while people stare at the reveal, it costs next to nothing.
Messages are small and validated with zod on both ends, using one protocol file shared by the Worker, the web client and the Expo app: start, guess, ready, again, close, ping from clients and a full state snapshot from the server. Sending the whole lobby view each time is simpler than diffs, and a lobby of 8 is a few kilobytes.
Not leaking the answers
A guessing game is easy to cheat if the client knows the answer. The server keeps the five places for the game, sends place ids only up to the current round, and sends the answer point only in the reveal phase. Scoring happens on the server from the distance in km:
const accuracy = km <= FULL_SCORE_KM
? MAX_ROUND_POINTS
: Math.round(MAX_ROUND_POINTS * Math.exp(-(km - FULL_SCORE_KM) / SCORE_FALLOFF_KM))
Fair timers on slow phones
Two details made rounds feel fair:
- Ready gate. The 3-2-1 countdown only starts when every device reports its map loaded (or after 15 seconds, so one stuck phone can't block the lobby). Before this, people on slow connections lost the first seconds of round one.
-
Server time. Every state carries
serverNowanddeadline. The client turns that into a local deadline when the message arrives, so a phone with a wrong clock still sees the right countdown.
Accounts without accounts
Nicknames are reserved without sign-up: on the first claim the server creates a random 256-bit key, stores only its SHA-256 hash next to the name and hands the key back once. The browser keeps it in localStorage, so the same browser keeps the same name. A name nobody has used for 90 days frees up.
The map side
The game board runs on MapLibre GL with terrain tiles built from community elevation data, which is what makes the 3D flight to the answer possible. The main site map is Leaflet with its own vector basemap. Places come from GTADB (CC BY 4.0).
What I'd do differently
- A single global object is a scaling ceiling. If it ever matters, lobbies can move to one object per code with the lobby list kept elsewhere. Until then, simple wins.
- Write the phase machine as pure functions first. The tricky bugs were all around edges like "the host leaves during the reveal" or "only one player is left", and they're much easier to test without sockets.
If you play a round, I'd love feedback on the scoring curve and on how the 3D reveal runs on your phone: https://leonidamaps.app/arena
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.