Skip to content

Commit fd9879c

Browse files
authored
Merge pull request #87 from CS3219-AY2425S1/ms4-evan/patch-matching-disconnect
Handle edge case when user disconnect
2 parents d5c9f91 + 5f1bdac commit fd9879c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

backend/gateway-service/src/modules/match/match.controller.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export class MatchGateway implements OnGatewayInit {
232232
}
233233

234234
// Notify both users when they both accept the match
235-
notifyUsersMatchConfirmed(matchId: string, users: string[]) {
235+
private notifyUsersMatchConfirmed(matchId: string, users: string[]) {
236236
const sessionId = this.generateSessionId();
237237
users.forEach((user) => {
238238
const socketId = this.getUserSocketId(user);
@@ -266,7 +266,7 @@ export class MatchGateway implements OnGatewayInit {
266266
});
267267
}
268268

269-
notifyUsersWithTimeout(timedOutUsers: string[]) {
269+
private notifyUsersWithTimeout(timedOutUsers: string[]) {
270270
timedOutUsers.forEach((user) => {
271271
const socketId = this.getUserSocketId(user);
272272
if (socketId) {
@@ -344,6 +344,22 @@ export class MatchGateway implements OnGatewayInit {
344344
console.log(`No match cancelled: ${result.message}`);
345345
}
346346

347+
// Remove user from matchParticipants and matchConfirmations
348+
let matchIdToRemove: string | null = null;
349+
350+
this.matchParticipants.forEach((participants, matchId) => {
351+
if (participants.has(userId)) {
352+
this.notifyOtherUserMatchDeclined(matchId, userId);
353+
matchIdToRemove = matchId;
354+
}
355+
});
356+
357+
if (matchIdToRemove) {
358+
this.matchParticipants.delete(matchIdToRemove);
359+
this.matchConfirmations.delete(matchIdToRemove);
360+
}
361+
362+
// Remove user from userSockets
347363
this.userSockets.delete(userId);
348364
console.log(`User ${userId} disconnected and removed from userSockets.`);
349365
} catch (error) {

0 commit comments

Comments
 (0)