Overview
The scoring system tracks player performance throughout the game, awards points for correct answers, and determines winners when all questions are completed.Score Tracking
Player Score Structure
Each player in a game has a score object:internal/core/game/domain.go:21-24
Initialization
When a game starts, all connected players are converted to Player instances:- Username - From WebSocket session
- Score - Set to 0
internal/web/ws/client.go:79-82, internal/core/game/domain.go:26-30
Answer Submission
How Answers Work
Players submit answers while the question timer is active:
Answer handling:
internal/web/ws/client.go:85-93
Answer Structure
Each answer contains:internal/core/game/domain.go:33-36
Answer Collection
During each question, answers are collected in a map:- Timer expiration
- All players to submit (implementation waits for timer)
internal/core/game/game.go:61-67
Players can only submit one answer per question. The last submission before timer expiration is used.
Scoring Logic
When Scoring Occurs
Scoring happens at the end of each question when the timer expires:internal/core/game/game.go:69-70
Score Calculation
TheendOfQuestion method evaluates all submitted answers:
Scoring implementation:
internal/core/game/game.go:37-44
Point System
Correct Answer
Correct Answer
Points Awarded: +1A player’s score increments by one for each correct answer.
Incorrect Answer
Incorrect Answer
Points Awarded: 0No penalty for wrong answers. Score remains unchanged.
No Answer
No Answer
Points Awarded: 0If a player doesn’t submit before timer expires, treated as incorrect.
internal/core/game/game.go:46-53
Thread-Safe Scoring
The Game struct usessync.RWMutex to prevent race conditions during concurrent score updates:
internal/core/game/game.go:14-23
Live Score Display
Scores are broadcast to players in real-time:Score Updates with Questions
Each question message includes current scores for all players:- Their own current score
- All other players’ scores
- Question progress (e.g., “Question 3 of 10”)
internal/web/ws/rooms.go:46
Real-Time Broadcast
Score updates sent via WebSocket to all room participants:internal/web/ws/rooms.go:22-28
All players see score updates simultaneously after each question, maintaining competitive transparency.
Winner Determination
End of Game
When the last question’s timer expires:internal/core/game/game.go:72-74
Winner Calculation
TheDisplayWinner method creates a final scoreboard:
Winner logic:
internal/core/game/game.go:86-95
Winners Map
The final scores are represented as:internal/core/game/domain.go:49
Handling Ties
The current implementation doesn’t have special tie-breaking logic. Multiple players can have the same high score, making them all winners.
Game End Message
The final message sent to all players:game_endPayload: Winners map with all player scores Message sending:
internal/core/game/game.go:92, internal/web/ws/rooms.go:64-74
Score Persistence
For persistent leaderboards or player statistics, you would need to:- Save final scores to database before room removal
- Create a statistics service to aggregate historical data
- Add endpoints to retrieve player history
Question Progress Tracking
Along with scores, the game tracks question progress:Progress Updates
- CurrentQues increments after each question
- Sent to players with each new question
- Used to display “Question X of Y”
internal/core/game/game.go:31
Game State
The game maintains state throughout scoring:internal/core/game/game.go:14-23
Scoring Flow Summary
Implementation Notes
Channel-Based Communication
Scoring uses Go channels for thread-safe message passing:- AnswerCh - Receives player answers
- Message - Sends score updates to WebSocket layer
internal/core/game/game.go:18-19
Blocking Game Loop
The game runs in a goroutine with a blocking select statement:internal/core/game/game.go:63-83
The scoring system is designed for fairness: all players have the same time to answer, and scoring happens simultaneously for everyone after each question timer.