I ship a turn-based artillery game — one player shoots, the other waits, then they swap. Turn-based sounds like the easy version of multiplayer. It is not; it just moves the difficulty from frame timing to state.
Here is the model I ended up with after shipping to Android and iOS, with a small player base on mobile networks that drop constantly. It is not clever. It is boring on purpose.
1. Sync inputs, not state
The temptation is to send the world state after every turn: tank positions, HP, terrain deltas. That works until two clients disagree by a rounding error and then diverge forever.
Instead the server keeps an append-only event log: shot(angle, power, itemUsed), surrender, turnTimeout. Each client replays the log locally and computes the outcome itself.
Two benefits:
- Payload is tiny (a shot is a few dozen bytes, not a terrain diff).
- A reconnecting client can be caught up by replaying events, not by rebuilding the world.
2. Determinism is the contract, so remove everything that isn't
The event log only works if every device computes the same result from the same event. That means:
-
Fixed timestep physics. Never integrate with
deltafrom the frame clock — a 120Hz phone and a throttled one must step the same number of times. -
Seeded RNG. Wind, spread, and any crit roll come from a seed stored in the match, indexed by turn number. No
Random()anywhere in gameplay code. -
No time-based effects. A "3 second fuse" is 180 fixed steps, not
now + 3s.
I enforce this with a single deterministic lint rule and a scripted match replay in CI: same log in, same log out.
3. Terrain is content plus deltas
Destructible terrain is where determinism breaks quietly, because terrain edits accumulate. I keep the base map as static content (a heightmap per map id) and store destruction as a small list of crater events. Crater radius and falloff are constants, so the resulting heightmap is identical everywhere.
This also makes replays cheap — a match is a map id, a seed, and a list of events.
4. Reconnect: ask for what you missed
On reconnect the client sends its last applied sequence number. The server replies with the events after it. Because the client can recompute anything from the log, a full world snapshot is only needed when the gap is huge (I use 200 events, which never happens in a 5-minute match).
No "resync state" code path means no second source of truth to keep in sync.
5. Shots must be idempotent
Mobile clients time out and retry. Without protection, a retry fires the same shot twice — which players absolutely notice.
Every action carries a client-generated eventId. The server rejects a duplicate eventId and returns the original result. The client then treats "already applied" as success. This one rule removed the entire class of double-fire bugs.
6. What I deliberately gave up
- No rollback netcode. Turn-based does not need it; only one player acts at a time.
- No host migration. The server is the authority from turn zero, so there is no host to lose.
- No real-time interpolation. The shell animation is cosmetic; the result is already decided by the log.
7. Test it without humans
The most useful thing I built is not the netcode, it is the harness: a bot that plays full matches offline through the same event path as a real client, on a simulated 400ms / 5% packet loss link. It found three determinism leaks that no amount of manual testing found.
If you are building something turn-based, start with the log. The netcode gets simpler every time you remove a source of truth.
I build and ship apps, games, and client MVPs from Seoul as a one-person studio: https://cpcai-studio.netlify.app/en/
Top comments (0)