Skip to content

Commit 94f005d

Browse files
committed
Revert "Add confirmMatch event"
This reverts commit 8e2f51e.
1 parent 8e2f51e commit 94f005d

File tree

4 files changed

+40
-147
lines changed

4 files changed

+40
-147
lines changed

backend/gateway-service/package-lock.json

Lines changed: 1 addition & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/gateway-service/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
"ioredis": "^5.4.1",
3535
"passport-jwt": "^4.0.1",
3636
"reflect-metadata": "^0.1.13",
37-
"rxjs": "^7.8.1",
38-
"uuid": "^10.0.0"
37+
"rxjs": "^7.8.1"
3938
},
4039
"devDependencies": {
4140
"@nestjs/cli": "^10.0.0",
@@ -45,7 +44,6 @@
4544
"@types/jest": "^29.5.2",
4645
"@types/node": "^20.3.1",
4746
"@types/supertest": "^2.0.12",
48-
"@types/uuid": "^10.0.0",
4947
"@typescript-eslint/eslint-plugin": "^6.0.0",
5048
"@typescript-eslint/parser": "^6.0.0",
5149
"eslint": "^8.42.0",

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

Lines changed: 38 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import {
1818
MATCH_CONFIRMED,
1919
MATCH_TIMEOUT,
2020
MATCH_REQUESTED,
21+
MATCH_ERROR,
2122
EXCEPTION,
2223
} from './match.event';
23-
import { CANCEL_MATCH, CONFIRM_MATCH, FIND_MATCH } from './match.message';
24-
import { v4 as uuidv4 } from 'uuid';
24+
import { CANCEL_MATCH, FIND_MATCH } from './match.message';
2525

2626
@WebSocketGateway({
2727
namespace: '/match',
@@ -35,15 +35,6 @@ import { v4 as uuidv4 } from 'uuid';
3535
export class MatchGateway implements OnGatewayInit {
3636
@WebSocketServer() server: Server;
3737
private userSockets: Map<string, string> = new Map();
38-
private matchConfirmations: Map<
39-
string,
40-
{
41-
user1: string;
42-
user2: string;
43-
user1Confirmed: boolean;
44-
user2Confirmed: boolean;
45-
}
46-
> = new Map();
4738

4839
constructor(
4940
@Inject('MATCHING_SERVICE') private matchingClient: ClientProxy,
@@ -54,9 +45,7 @@ export class MatchGateway implements OnGatewayInit {
5445
afterInit() {
5546
// Subscribe to Redis Pub/Sub for match notifications
5647
this.redisService.subscribeToMatchEvents((matchedUsers) => {
57-
const matchId = this.generateMatchId(); // Generate matchId
58-
this.notifyUsersMatchFound(matchId, matchedUsers); // Notify users of match with matchId
59-
this.trackMatchConfirmation(matchId, matchedUsers); // Track match confirmation
48+
this.notifyUsersWithMatch(matchedUsers);
6049
});
6150

6251
this.redisService.subscribeToTimeoutEvents((timedOutUsers) => {
@@ -74,7 +63,7 @@ export class MatchGateway implements OnGatewayInit {
7463
!payload.selectedTopic ||
7564
!payload.selectedDifficulty
7665
) {
77-
client.emit(EXCEPTION, 'Invalid payload for match request.');
66+
client.emit(EXCEPTION, 'Invalid match request payload.');
7867
return;
7968
}
8069

@@ -106,7 +95,10 @@ export class MatchGateway implements OnGatewayInit {
10695
@MessageBody() payload: { userId: string },
10796
) {
10897
if (!payload.userId) {
109-
client.emit(EXCEPTION, 'Invalid userId.');
98+
client.emit(
99+
EXCEPTION,
100+
'Invalid userId. Please check your payload and try again.',
101+
);
110102
return;
111103
}
112104

@@ -133,45 +125,36 @@ export class MatchGateway implements OnGatewayInit {
133125
}
134126
}
135127

136-
@SubscribeMessage(CONFIRM_MATCH)
137-
async handleConfirmMatch(
138-
@ConnectedSocket() client: Socket,
139-
@MessageBody() payload: { userId: string; matchId: string },
140-
) {
141-
if (!payload.userId || !payload.matchId) {
142-
client.emit(EXCEPTION, 'Invalid message payload for match confirmation.');
143-
return;
144-
}
145-
146-
const { userId, matchId } = payload;
147-
if (!this.validateUserId(client, userId)) {
148-
return;
149-
}
150-
151-
// Get the confirmation state of the users
152-
const confirmationState = this.matchConfirmations.get(matchId);
153-
if (!confirmationState) {
154-
client.emit(EXCEPTION, 'Invalid Match Id.');
155-
return;
156-
}
157-
158-
// Checks which user is confirming the match
159-
if (confirmationState.user1 === payload.userId) {
160-
confirmationState.user1Confirmed = true;
161-
} else if (confirmationState.user2 === payload.userId) {
162-
confirmationState.user2Confirmed = true;
163-
} else {
164-
client.emit(EXCEPTION, 'Invalid userId for this match.');
165-
return;
128+
// Notify both matched users via WebSocket
129+
notifyUsersWithMatch(matchedUsers: string[]) {
130+
const [user1, user2] = matchedUsers;
131+
const user1SocketId = this.getUserSocketId(user1);
132+
const user2SocketId = this.getUserSocketId(user2);
133+
if (user1SocketId && user2SocketId) {
134+
this.server.to(user1SocketId).emit(MATCH_FOUND, {
135+
message: `You have been matched with user ${user2}`,
136+
matchedUserId: user2,
137+
});
138+
this.server.to(user2SocketId).emit(MATCH_FOUND, {
139+
message: `You have been matched with user ${user1}`,
140+
matchedUserId: user1,
141+
});
166142
}
143+
}
167144

168-
// Check if both users have confirmed
169-
if (confirmationState.user1Confirmed && confirmationState.user2Confirmed) {
170-
this.notifyUsersMatchConfirmed(payload.matchId, confirmationState);
171-
}
145+
notifyUsersWithTimeout(timedOutUsers: string[]) {
146+
timedOutUsers.forEach((user) => {
147+
const socketId = this.getUserSocketId(user);
148+
if (socketId) {
149+
this.server.to(socketId).emit(MATCH_TIMEOUT, {
150+
message: `You have been timed out.`,
151+
timedOutUserId: user,
152+
});
153+
}
154+
});
172155
}
173156

174-
async handleConnect(@ConnectedSocket() client: Socket) {
157+
async handleConnection(@ConnectedSocket() client: Socket) {
175158
const id = client.handshake.query.userId as string;
176159

177160
if (!id) {
@@ -200,8 +183,10 @@ export class MatchGateway implements OnGatewayInit {
200183
return;
201184
}
202185

203-
this.userSockets.set(id as string, client.id);
204-
console.log(`User ${id} connected with socket ID ${client.id}`);
186+
if (id) {
187+
this.userSockets.set(id as string, client.id);
188+
console.log(`User ${id} connected with socket ID ${client.id}`);
189+
}
205190
} catch (error) {
206191
this.emitExceptionAndDisconnect(client, error.message);
207192
return;
@@ -245,73 +230,6 @@ export class MatchGateway implements OnGatewayInit {
245230
}
246231
}
247232

248-
trackMatchConfirmation(matchId: string, matchedUsers: string[]) {
249-
const confirmationState = {
250-
user1: matchedUsers[0],
251-
user2: matchedUsers[1],
252-
user1Confirmed: false,
253-
user2Confirmed: false,
254-
};
255-
256-
this.matchConfirmations.set(matchId, confirmationState);
257-
}
258-
259-
notifyUsersMatchFound(matchId: string, matchedUsers: string[]) {
260-
const [user1, user2] = matchedUsers;
261-
const user1SocketId = this.getUserSocketId(user1);
262-
const user2SocketId = this.getUserSocketId(user2);
263-
if (user1SocketId && user2SocketId) {
264-
this.server.to(user1SocketId).emit(MATCH_FOUND, {
265-
message: `You have been matched with user ${user2}`,
266-
matchId,
267-
});
268-
this.server.to(user2SocketId).emit(MATCH_FOUND, {
269-
message: `You have been matched with user ${user1}`,
270-
matchId,
271-
});
272-
}
273-
}
274-
275-
notifyUsersWithTimeout(timedOutUsers: string[]) {
276-
timedOutUsers.forEach((user) => {
277-
const socketId = this.getUserSocketId(user);
278-
if (socketId) {
279-
this.server.to(socketId).emit(MATCH_TIMEOUT, {
280-
message: `You have been timed out.`,
281-
timedOutUserId: user,
282-
});
283-
}
284-
});
285-
}
286-
287-
notifyUsersMatchConfirmed(matchId: string, confirmationState: any) {
288-
const user1SocketId = this.getUserSocketId(confirmationState.user1);
289-
const user2SocketId = this.getUserSocketId(confirmationState.user2);
290-
291-
const sessionId = this.generateSessionId(); // TODO - To be substituted with collab-service method in next MS
292-
293-
if (user1SocketId && user2SocketId) {
294-
this.server.to(user1SocketId).emit(MATCH_CONFIRMED, {
295-
message: 'Both users have confirmed the match.',
296-
sessionId,
297-
});
298-
this.server.to(user2SocketId).emit(MATCH_CONFIRMED, {
299-
message: 'Both users have confirmed the match.',
300-
sessionId,
301-
});
302-
}
303-
304-
this.matchConfirmations.delete(matchId);
305-
}
306-
307-
private generateMatchId(): string {
308-
return uuidv4();
309-
}
310-
311-
private generateSessionId(): string {
312-
return uuidv4();
313-
}
314-
315233
private getUserSocketId(userId: string): string | undefined {
316234
return this.userSockets.get(userId);
317235
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export const FIND_MATCH = 'findMatch';
22
export const CANCEL_MATCH = 'cancelMatch';
3-
export const CONFIRM_MATCH = 'confirmMatch';

0 commit comments

Comments
 (0)