Skip to content

Commit bcaee89

Browse files
committed
Set up socket connection for each page
1 parent 8002061 commit bcaee89

File tree

12 files changed

+505
-295
lines changed

12 files changed

+505
-295
lines changed

backend/matching-service/config/rabbitmq.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import amqplib, { Connection } from "amqplib";
22
import dotenv from "dotenv";
33
import { matchUsers } from "../src/utils/mq_utils";
4-
import { MatchItem } from "../src/handlers/matchHandler";
4+
import { MatchRequestItem } from "../src/handlers/matchHandler";
55

66
dotenv.config();
77

@@ -26,7 +26,9 @@ export const connectRabbitMq = async () => {
2626
}
2727
};
2828

29-
export const sendRabbitMq = async (data: MatchItem): Promise<boolean> => {
29+
export const sendRabbitMq = async (
30+
data: MatchRequestItem
31+
): Promise<boolean> => {
3032
try {
3133
const senderChannel = await mrConnection.createChannel();
3234
senderChannel.sendToQueue(queue, Buffer.from(JSON.stringify(data)));
Lines changed: 26 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import { io } from "../../server";
21
import { v4 as uuidv4 } from "uuid";
3-
import {
4-
MATCH_FOUND,
5-
MATCH_IN_PROGRESS,
6-
MATCH_SUCCESSFUL,
7-
MATCH_ENDED,
8-
MATCH_REQUEST_ERROR,
9-
MATCH_NOT_ACCEPTED,
10-
} from "../utils/constants";
11-
import { Socket } from "socket.io";
122
import { sendRabbitMq } from "../../config/rabbitmq";
3+
import { sendMatchFound } from "./websocketHandler";
134

145
interface MatchUser {
156
id: string;
@@ -31,7 +22,7 @@ export interface MatchRequest {
3122
timeout: number;
3223
}
3324

34-
export interface MatchItem {
25+
export interface MatchRequestItem {
3526
user: MatchUser;
3627
complexities: string[];
3728
categories: string[];
@@ -41,23 +32,12 @@ export interface MatchItem {
4132
}
4233

4334
const matches = new Map<string, Match>();
44-
const userSockets = new Map<string, Socket>();
4535

46-
export const createMatchItem = async (
47-
socket: Socket,
48-
matchRequest: MatchRequest,
49-
isRematch?: boolean
36+
export const sendMatchRequest = async (
37+
matchRequest: MatchRequest
5038
): Promise<boolean> => {
5139
const { user, complexities, categories, languages, timeout } = matchRequest;
52-
53-
if (!isRematch && userSockets.has(user.id)) {
54-
socket.emit(MATCH_IN_PROGRESS);
55-
return false;
56-
}
57-
58-
userSockets.set(user.id, socket);
59-
60-
const matchQueueItem: MatchItem = {
40+
const matchItem: MatchRequestItem = {
6141
user: user,
6242
complexities: complexities,
6343
categories: categories,
@@ -66,98 +46,43 @@ export const createMatchItem = async (
6646
ttlInSecs: timeout,
6747
};
6848

69-
const result = await sendRabbitMq(matchQueueItem);
70-
if (!result) {
71-
socket.emit(MATCH_REQUEST_ERROR);
72-
}
73-
return result;
49+
const sent = await sendRabbitMq(matchItem);
50+
return sent;
7451
};
7552

76-
export const createMatch = (matchItem1: MatchItem, matchItem2: MatchItem) => {
77-
const uid1 = matchItem1.user.id;
78-
const uid2 = matchItem2.user.id;
79-
53+
export const createMatch = (
54+
requestItem1: MatchRequestItem,
55+
requestItem2: MatchRequestItem
56+
) => {
8057
const matchId = uuidv4();
8158
matches.set(matchId, {
82-
uid1: uid1,
83-
uid2: uid2,
59+
uid1: requestItem1.user.id,
60+
uid2: requestItem2.user.id,
8461
accepted: false,
8562
});
8663

87-
userSockets.get(uid1)?.join(matchId);
88-
userSockets.get(uid2)?.join(matchId);
89-
io.to(matchId).emit(MATCH_FOUND, {
90-
matchId: matchId,
91-
user1: matchItem1.user,
92-
user2: matchItem2.user,
93-
});
64+
sendMatchFound(matchId, requestItem1, requestItem2);
9465
};
9566

96-
export const handleMatchAcceptance = (matchId: string) => {
67+
export const handleMatchAccept = (matchId: string): boolean => {
9768
const match = matches.get(matchId);
9869
if (!match) {
99-
return;
100-
}
101-
102-
if (match.accepted) {
103-
io.to(matchId).emit(MATCH_SUCCESSFUL);
104-
} else {
105-
matches.set(matchId, { ...match, accepted: true });
106-
}
107-
};
108-
109-
const handleMatchDecline = (socket: Socket, matchId: string) => {
110-
if (matches.delete(matchId)) {
111-
socket.to(matchId).emit(MATCH_NOT_ACCEPTED);
70+
return false;
11271
}
113-
};
11472

115-
export const handleRematch = async (
116-
socket: Socket,
117-
matchId: string,
118-
rematchRequest: MatchRequest
119-
): Promise<boolean> => {
120-
handleMatchDecline(socket, matchId);
121-
return await createMatchItem(socket, rematchRequest, true);
73+
const partnerAccepted = match.accepted;
74+
matches.set(matchId, { ...match, accepted: true });
75+
return partnerAccepted;
12276
};
12377

124-
export const handleMatchStopRequest = (
125-
socket: Socket,
126-
uid: string | undefined,
127-
matchId: string | null,
128-
matchPending: boolean,
129-
isMutual: boolean
130-
) => {
131-
if (matchId) {
132-
if (matchPending) {
133-
handleMatchDecline(socket, matchId);
134-
return;
135-
}
136-
137-
const match = matches.get(matchId);
138-
if (match) {
139-
userSockets.delete(match.uid1);
140-
userSockets.delete(match.uid2);
141-
matches.delete(matchId);
142-
}
78+
export const handleMatchDelete = (matchId: string): boolean =>
79+
matches.delete(matchId);
14380

144-
if (!isMutual) {
145-
socket.to(matchId).emit(MATCH_ENDED);
81+
export const getMatchIdByUid = (uid: string): string | null => {
82+
for (const [matchId, match] of matches) {
83+
if (match.uid1 === uid || match.uid2 === uid) {
84+
return matchId;
14685
}
147-
} else if (uid) {
148-
userSockets.delete(uid);
14986
}
150-
};
151-
152-
export const handleUserDisconnect = (disconnectedSocket: Socket) => {
153-
for (const [uid, socket] of userSockets) {
154-
if (socket.id === disconnectedSocket.id) {
155-
userSockets.delete(uid);
156-
break;
157-
}
158-
}
159-
};
160-
161-
export const hasUserDisconnected = (uid: string) => {
162-
return !userSockets.has(uid);
87+
return null;
16388
};

0 commit comments

Comments
 (0)