Skip to content

Commit d1908ef

Browse files
committed
Modify PerformMatching algorithm and NotifyUser function
1 parent fea8537 commit d1908ef

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

apps/matching-service/handlers/websocket.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ func waitForResult(ws *websocket.Conn, ctx, timeoutCtx, matchCtx context.Context
167167
return
168168
}
169169
log.Println("Match found for user: " + username)
170-
// Notify the users about the match
171-
notifyMatches(result.User, result.MatchedUser, result)
170+
// Notify the user about the match
171+
notifyMatches(result.User, result)
172172

173173
// NOTE: user and other user are already cleaned up in a separate matching algorithm process
174174
// so no clean up is required here.
@@ -187,22 +187,15 @@ func sendTimeoutResponse(ws *websocket.Conn) {
187187
}
188188
}
189189

190-
func notifyMatches(username, matchedUsername string, result models.MatchFound) {
190+
// Notify matches
191+
func notifyMatches(username string, result models.MatchFound) {
191192
mu.Lock()
192193
defer mu.Unlock()
193194

194-
// Send message to the first user
195+
// Send message to matched user
195196
if userConn, userExists := activeConnections[username]; userExists {
196197
if err := userConn.WriteJSON(result); err != nil {
197198
log.Printf("Error sending message to user %s: %v\n", username, err)
198199
}
199200
}
200-
201-
// Send message to the matched user
202-
if matchedUserConn, matchedUserExists := activeConnections[matchedUsername]; matchedUserExists {
203-
result.User, result.MatchedUser = result.MatchedUser, result.User // Swap User and MatchedUser values
204-
if err := matchedUserConn.WriteJSON(result); err != nil {
205-
log.Printf("Error sending message to user %s: %v\n", username, err)
206-
}
207-
}
208201
}

apps/matching-service/processes/match.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func PerformMatching(matchRequest models.MatchRequest, ctx context.Context, matc
7070
log.Println("Unable to randomly generate matchID")
7171
}
7272

73-
// Signal that a match has been found
73+
// Signal that a match has been found for user
7474
matchFoundChannels[username] <- models.MatchFound{
7575
Type: "match_found",
7676
MatchID: matchId,
@@ -80,6 +80,16 @@ func PerformMatching(matchRequest models.MatchRequest, ctx context.Context, matc
8080
Difficulty: matchedDifficulty,
8181
}
8282

83+
// Signal that a match has been found for matchedUser
84+
matchFoundChannels[matchedUsername] <- models.MatchFound{
85+
Type: "match_found",
86+
MatchID: matchId,
87+
User: matchedUsername,
88+
MatchedUser: username,
89+
Topic: matchedTopic,
90+
Difficulty: matchedDifficulty,
91+
}
92+
8393
} else {
8494
// log.Printf("No match found for user: %s", username)
8595

0 commit comments

Comments
 (0)