Skip to content

Commit 6247ae1

Browse files
committed
Add handling of imporper match_found.
Add handling of match_rejected
1 parent bd4d597 commit 6247ae1

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

apps/frontend/src/app/services/use-matching.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MatchState } from "@/contexts/websocketcontext";
1+
import { MatchInfo, MatchState } from "@/contexts/websocketcontext";
22
import { useEffect, useState } from "react";
33
import useWebSocket, { Options, ReadyState } from "react-use-websocket";
44

@@ -21,6 +21,13 @@ export type MatchFoundResponse = {
2121
matchId: number,
2222
partnerId: number,
2323
partnerName: string,
24+
} | {
25+
type: "match_found",
26+
matchId: string,
27+
user: string,
28+
matchedUser: string,
29+
topic: string | string[],
30+
difficulty: string
2431
}
2532

2633
export type MatchTimeoutResponse = {
@@ -33,7 +40,7 @@ export type MatchRejectedResponse = {
3340
message: string,
3441
}
3542

36-
type MatchResponse = MatchFoundResponse | MatchTimeoutResponse;
43+
type MatchResponse = MatchFoundResponse | MatchTimeoutResponse | MatchRejectedResponse;
3744

3845
export default function useMatching(): MatchState {
3946
const [isSocket, setIsSocket] = useState<boolean>(false);
@@ -55,17 +62,21 @@ export default function useMatching(): MatchState {
5562

5663
if (responseJson.type == "match_found") {
5764
setIsSocket(false);
65+
66+
const info: MatchInfo = parseInfoFromResponse(responseJson);
5867
setSte({
5968
state: "found",
60-
info: {
61-
matchId: responseJson.matchId.toString(),
62-
partnerId: responseJson.partnerId.toString(),
63-
partnerName: responseJson.partnerName,
64-
},
69+
info: info,
6570
ok: cancel
6671
})
6772
return;
6873
}
74+
75+
if (responseJson.type == "match_rejected") {
76+
console.log("match rejected: " + responseJson.message);
77+
cancel();
78+
return;
79+
}
6980
}
7081
}
7182

@@ -113,4 +124,21 @@ export default function useMatching(): MatchState {
113124
}
114125

115126
return isSocket ? matchState : ste;
116-
}
127+
}
128+
129+
function parseInfoFromResponse(responseJson: MatchFoundResponse): MatchInfo {
130+
// test whether old or new
131+
if ("partnerId" in responseJson) {
132+
return {
133+
matchId: responseJson.matchId?.toString() ?? "unknown",
134+
partnerId: responseJson.partnerId?.toString() ?? "unknown",
135+
partnerName: responseJson.partnerName ?? "unknown",
136+
};
137+
} else {
138+
return {
139+
matchId: responseJson.matchId?.toString() ?? "unknown",
140+
partnerId: "unknown",
141+
partnerName: responseJson.matchedUser ?? "unknown",
142+
};
143+
}
144+
}

apps/frontend/src/contexts/websocketcontext.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ export type SocketState = {
1212
cancel(): void;
1313
timeout(): void;
1414
};
15-
15+
export type MatchInfo = {
16+
matchId: string;
17+
partnerId: string;
18+
partnerName: string;
19+
}
1620
export type MatchState = SocketState | {
1721
state: "found";
18-
info: {
19-
matchId: string;
20-
partnerId: string;
21-
partnerName: string;
22-
};
22+
info: MatchInfo;
2323
ok(): void;
2424
} | {
2525
state: "timeout";

0 commit comments

Comments
 (0)