mirror of
https://github.com/tiennm99/260404.git
synced 2026-06-09 22:11:22 +00:00
main
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
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
MatchFoundand 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.
Description
Languages
Go
98.7%
Dockerfile
1.3%