Overview
Game rooms are isolated multiplayer sessions where players compete in trivia games. Each room has a unique ID, configurable settings, and supports multiple concurrent players.Room Architecture
Hub and Room System
Showdown Trivia uses a Hub-and-Spoke architecture:- Hub - Central registry managing all active rooms
- Rooms - Individual game sessions with their own player lists
- Clients - WebSocket connections representing individual players
The Hub maintains a thread-safe map of all active rooms using
sync.RWMutex for concurrent access.Implementation: internal/web/ws/hub.go:27-32Room Structure
Each room contains:internal/web/ws/rooms.go:12-20
Creating a Room
Room Creation Flow
Players can create a new game room with custom settings:
Handler code:
internal/web/handlers/game.go:44-91, internal/web/handlers/game.go:113-142
Configuration Options
Room creators can customize:Question Category
Question Category
Select from available trivia categories.Validation: Must be between 0-32Parameter:
category (integer)Form validation: internal/web/form/game.go:29-31Timer Duration
Timer Duration
How long players have to answer each question.Range: 2-20 seconds
Parameter:
Parameter:
timer (integer)Form validation: internal/web/form/game.go:35-37Number of Questions
Number of Questions
Total questions in the game.Range: 1-50 questions
Parameter:
amount (integer)Form validation: internal/web/form/game.go:32-34Room ID Generation
When a room is created:internal/web/ws/ws_upgrader.go:18
Questions Fetched
The system fetches trivia questions when creating a room:- Questions retrieved from external trivia API
- Number of questions based on
amountparameter - Category filter applied if specified
- Questions stored in room’s Game instance
internal/web/handlers/game.go:135-139
Joining a Room
Join Flow
Players join existing rooms through:
Join handlers:
internal/web/handlers/game.go:102-112, internal/web/handlers/game.go:143-153
Join Validation
Room lookup:internal/web/ws/hub.go:34-40
Active Games List
Players can view all joinable rooms: Endpoint:GET /activegames
The list shows:
- Room ID
- Room owner’s username
- Current player list
- Number of players
Only rooms where the game hasn’t started appear in the active games list.Filtering logic:
internal/web/ws/hub.go:51-53internal/web/ws/hub.go:46-61
Room Lifecycle
Room States
- Created - Room initialized, waiting for players
- Populated - Players joining, game not started
- Active - Game in progress (not listed in active games)
- Completed - Game ended, room about to be removed
- Removed - Room deleted from hub
Starting the Game
Only the room owner can start the game:- Players converted to Game Player instances
- Game state set to
GameStarted = true - Room removed from active games list
- First question sent to all players
internal/web/ws/client.go:78-84, internal/core/game/game.go:25-35
Room Cleanup
Rooms are automatically removed when:- All players disconnect - Last player leaving triggers cleanup
- Game ends - After winner announcement, room deleted
- Owner disconnects before start - Room becomes empty and removed
internal/web/ws/rooms.go:92-110, internal/web/ws/rooms.go:71
Room cleanup uses mutex locks to prevent race conditions during concurrent player disconnections.
Player Management
Adding Players
When a player joins:- Client added to room’s client map
- WebSocket connection counter incremented (metrics)
- Updated player list broadcast to everyone
internal/web/ws/rooms.go:81-91
Removing Players
When a player disconnects:- WebSocket connection closed
- Client removed from room’s client map
- Connection counter decremented
- Room deleted if no players remain
internal/web/ws/rooms.go:92-111
Player List Display
The room maintains a live list of connected players:internal/web/ws/rooms.go:112-118
Concurrent Room Management
The Hub supports multiple simultaneous rooms:Thread-safe operations
Thread-safe operations
All room operations use mutex locks:
addRoom()- Lock during additionremoveRoom()- Lock during deletionListRooms()- Read lock for listinggetRoom()- Read lock for lookup
internal/web/ws/hub.go:27-32Isolated game state
Isolated game state
Each room maintains independent:
- Player lists
- Game progress
- Question sets
- Score tracking
Scalable architecture
Scalable architecture
The system can handle:
- Unlimited concurrent rooms (memory-limited)
- Multiple players per room
- Independent game timers
- Parallel message broadcasting
Room Ownership
The room creator has special privileges: ✅ Can start the game when ready✅ Listed as owner in active games
❌ Cannot change settings after creation
❌ Cannot remove players Ownership stored as username string:
internal/web/ws/rooms.go:18
Game Configuration Entity
Settings passed to the room:internal/web/entity/gameConfig.go:3-10
Metrics and Monitoring
Room operations are tracked for observability:- Active WebSocket connections - Incremented on join, decremented on leave
- Room count - Implicit from hub’s room map
- Player counts - Per-room tracking
internal/web/ws/rooms.go:85, internal/web/ws/rooms.go:101
Prometheus metrics are exposed at
/metrics endpoint for monitoring system health and game activity.