Online Multiplayer For Yet Another Multiplayer Game
Chunk - multiplayer and singleplayer strategic game where you clain territory and dominate the board
TL;DR
Adding online multiplayer to the game I've written in Yer Another Multiplayer Game post. Play online by sharing link to someone.
Try it here: chunk.x96.dev
Development
Intro
2-day small game project be like:

And very weird bugs:

Making Error Page
Everything starts with... Error page. Because, when working with networking, it needs to display proper error messages and allow user to know what's happening and why it happens.

Architecture Plan
Plan is simple and consists of several points:
- Host creating room
- Players joining game
Host Creating Room
When Player wants to create a room, /session/create endpoint is used. User sends basic session info: planned amount, planned board size. Response contains session id used to manipulate the remote state.
Once session (room) is created, host should poll server's /session/state and wait for all players to join. Once all players are here, game begins.
Players Joining Game
Once room is created, ULR can be shared in order to allow other players to join. When there is "free space" in room, player makes request to /session/join and receives player id to use. Next step is also waiting for others.
When there is no room in a room, player receives error and falls back to main menu.
Post-Join
Once everyone have joined, game begins and players can start performing moves. Current player is determined by remote state and on each move, state is being synced to remote server with /session/update and other players can poll updates.
Server Remote State
Server needs to store minimal ampount of data to share it between players
package session
type BoardState struct {
// Size of game board
BoardSize int
// Matrix of cells
// Contains ID of player-owner or -1
Cells [][]int
// Pseudo-Matrix of edges
// Contains bool indicators if edge is open
Edges [][]bool
}
type Session struct {
// Max amount of players in session
Players int
// Current amount of players in session
CurrentPlayers int
// Server's board state
Board *BoardState
}
...
Yet another half of an hour to debug indexes, now to find adjacent edges for each node (edge intersection).

All this just to implement seamless state synchronization between clients.
Currently server stores state of edges and cells, but not nodes. That's wh, when you hit RESTART, a nice glitch will appear resulting in all nodes stay black. Resetting entire field and coloring only checked back is not a good idea because it may cause flickering.
Finally, I can fing adjacent edges for each node and perform clean repaint of game field.
Btw, sometimes forgontten debug logging looks creepy

...
After debugging lots of UI bugs (mostly related to state synchronization), it works.

Multiplayer works by polling backend for updates. In fact it makes a request every second to fetch new state. I hope my go server and 1MBps network channel will survive.
Final Notes
This game looks super simple, but when it comes to online functionality, there are some pitfalls:
- Synchronization of game fields - requires to sync states of each cell and edge and restore states of nodes if one of players hits restart.
- Coordinates. Translating to edges, translating from edges, to nodes, from nodes, to cells, from cells. Small hell.
- Making UI adaptive - it works on mobile and desktop, seamlessly.
- Making ffmpeg convert webm to gif with compression and saving this as a script on a wiki page.
- Handling restart - everything clears. Everyone notices. Everything should work as expected.
- Not mess with css class and
.selectedattribure and not forget any of them. - Handling errors with nice UI.
- Synchronize game field state (size, current player) and lock movement when it's not your turn.
- Sharing session code - simply replacing current URL for copy-paste.
And many other small issues...
Still, you can play game here: chunk.x96.dev. Play online by sharing link to someone.