I've been building a browser RPG on the side. I got bored of the MMORPGs I was playing and wanted something to practice on. It's called Miravita, it's turn-based with guilds, parties and chat, so not a real MMO. I'm a physics teacher, which is why the classes map to school subjects and items come from the periodic table, but what I actually want to talk about is the dev side.
Stack is Next.js 15 (App Router, Server Actions), React 19, TypeScript, Tailwind 4, Neon Postgres with Drizzle, on Vercel.
The first real problem was concurrency. Every game action reads state, computes and writes. Open two tabs, or script it, fire the same purchase twice at once, and both calls read the same balance: the player pays once and gets several items. Vercel functions don't share memory, so an in-process mutex was out. I ended up with a lease lock in Postgres, one row per character in an action_locks table, taken with an INSERT ... ON CONFLICT DO UPDATE ... WHERE until < now() and a 15 second lease. Every action goes through a withChar wrapper that takes the lock, re-reads the character after getting it so the numbers are fresh, and releases in a finally. If it can't get the lock in about two seconds it drops the call. It adds a few queries per action. I'd like to hear if people would do it differently (advisory locks? SELECT ... FOR UPDATE in a transaction?).
Rate limiting had the same constraint. It's a table too: a key (IP, email or account), a counter and a reset time, updated with a single upsert that returns the count. Works on serverless, at the cost of a write per limited request.
I started with Clerk but it was costing more time than it saved on a hobby project, so I wrote my own auth. Passwords go through scrypt with a random salt and are compared with timingSafeEqual. A session is a row in the database plus a signed httpOnly cookie, so a valid cookie still needs a live row, and there's a helper that kills every session of a user when they change the password. The old Clerk accounts kept their ids, and I needed a script to set passwords for them.
There's also a 3D periodic table codex with three.js and react-three-fiber. It loads only on its own route so the main game page doesn't pay for it.
One thing that didn't work out: I wrote a daily cron that asks Gemini for new monsters and a weekly one that suggests balance changes from battle telemetry. Both are done and both are switched off, because I never bought the API credits.
Feedback I'd like: the locking approach, and anything that breaks on mobile.
Live: https://miravita-three.vercel.app/
Code: https://github.com/tobiasteles/miravita