tiennm99 4c56a23e58 feat: add Go game server with matchmaking, lobby, and WebSocket support
Implement a real-time game server using protobuf over WebSocket:
- Protobuf schema with 14 message types in oneof Envelope
- Room-based lobby with join/leave/chat broadcast
- ELO-based matchmaking with expanding range (±100 to ±500)
- Turn-based game session lifecycle management
- Multi-stage Docker build (Alpine)
- Unit tests for matchmaking and game session logic
2026-04-04 21:25:03 +07:00
2026-04-04 20:53:32 +07:00
2026-04-04 20:53:32 +07:00

Go Game Server

A real-time game server with matchmaking, lobby chat, and WebSocket communication using Protocol Buffers.

Architecture

cmd/server/main.go              Entry point, HTTP server, graceful shutdown
internal/
  player/player.go              Player model, WebSocket send helpers
  lobby/lobby.go                Room-based lobby with join/leave/chat broadcast
  matchmaking/matchmaker.go     ELO-based queue with expanding range
  game/session.go               Turn-based game session lifecycle
  ws/handler.go                 WebSocket upgrade, protobuf message routing
proto/game.proto                Protobuf schema (all message types)
pkg/pb/game.pb.go               Generated Go protobuf code

Protocol

All messages use binary protobuf over WebSocket. The Envelope message wraps every payload via a oneof:

Direction Message Purpose
C→S JoinLobbyRequest Join a lobby room
C→S LeaveLobbyRequest Leave current room
C→S ChatMessage Send chat to room
C→S QueueRequest Enter matchmaking with ELO
C→S CancelQueueRequest Leave matchmaking queue
C→S GameAction Send a game action (your turn)
S→C LobbyState Full room state on join
S→C PlayerJoined / PlayerLeft Room membership changes
S→C ChatBroadcast Chat from another player
S→C MatchFound Matched with opponent(s)
S→C GameState Current game state + whose turn
S→C GameOver Final scores and winner
S→C Error Error response

Matchmaking

  • Players enter the queue with an ELO rating
  • Every 2 seconds, the engine tries to pair players
  • Match range starts at ±100 ELO and expands by 50 per tick (max ±500)
  • On match, both players receive MatchFound and a game session starts

Run

# Local
go run ./cmd/server

# Custom port
ADDR=:9090 go run ./cmd/server

# Docker
docker build -t game-server .
docker run -p 8080:8080 game-server

Generate Protobuf

Requires buf and protoc-gen-go:

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
buf generate

Health Check

GET /health → 200 "ok"

WebSocket Endpoint

ws://localhost:8080/ws

Connect and send/receive binary protobuf Envelope messages.

S
Description
Just a foo I created with Claude Code today
Readme Apache-2.0 135 KiB
Languages
Go 98.7%
Dockerfile 1.3%