Skip to content

Commit 6e2914d

Browse files
fix: improve game cleanup process and enhance username validation in matchmaking
1 parent 0708859 commit 6e2914d

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

backend/game/game.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ func (g *Game) BroadcastGameOver() {
225225

226226
// Emit Kafka event
227227
analytics.EmitGameEnd(g.ID, winnerStr, duration)
228+
229+
// Clean up game after a short delay to allow clients to receive game over message
230+
go func() {
231+
time.Sleep(5 * time.Second)
232+
GameManagerInstance.RemoveGame(g.ID)
233+
log.Printf("Game %s cleaned up from active games", g.ID)
234+
}()
228235
}
229236

230237
func (g *Game) TriggerBotMove() {

backend/handlers/ws_handler.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,26 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
5959
username = "Anonymous"
6060
}
6161

62+
// Check if username is already in matchmaking queue
6263
if game.GlobalMatchmaker.IsPlayerInQueue(username) {
63-
conn.WriteJSON(game.Message{Type: game.MsgError, Payload: "Username already taken"})
64+
conn.WriteJSON(game.Message{Type: game.MsgError, Payload: "Username already in matchmaking queue"})
6465
continue
6566
}
6667

68+
// Check if current player is already in an active game
69+
if currentPlayer != nil {
70+
playerGame := game.GameManagerInstance.GetGameByPlayerID(currentPlayer.ID)
71+
if playerGame != nil && playerGame.State == "active" {
72+
conn.WriteJSON(game.Message{Type: game.MsgError, Payload: "You are already in an active game"})
73+
continue
74+
}
75+
}
76+
77+
// Check if username exists in an active game
6778
existingPlayer, existingGame := game.GameManagerInstance.GetPlayerByUsername(username)
6879
if existingPlayer != nil && existingGame != nil {
6980
if !existingPlayer.IsConnected {
81+
// Player disconnected, allow reconnection
7082
log.Printf("User %s reconnecting to game %s", username, existingGame.ID)
7183
reconnectedPlayer, success := existingGame.HandleReconnect(existingPlayer.ID, conn)
7284
if success {
@@ -76,7 +88,8 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
7688
}
7789
continue
7890
} else {
79-
conn.WriteJSON(game.Message{Type: game.MsgError, Payload: "Username already taken"})
91+
// Player is connected in another session
92+
conn.WriteJSON(game.Message{Type: game.MsgError, Payload: "Username already in use"})
8093
continue
8194
}
8295
}

frontend/components/join-game-form.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,18 @@ export function JoinGameForm({
117117
type="submit"
118118
className="w-full"
119119
disabled={
120-
!username.trim() || !isConnected || isWaiting || !!validationError || hasActiveGame
120+
!username.trim() ||
121+
!isConnected ||
122+
isWaiting ||
123+
!!validationError ||
124+
hasActiveGame
121125
}
122126
>
123-
{isWaiting ? 'Searching...' : hasActiveGame ? 'Already in Game' : 'Find Match'}
127+
{isWaiting
128+
? 'Searching...'
129+
: hasActiveGame
130+
? 'Already in Game'
131+
: 'Find Match'}
124132
</Button>
125133
</form>
126134
</CardContent>

0 commit comments

Comments
 (0)