1
- import { MatchState } from "@/contexts/websocketcontext" ;
1
+ import { MatchInfo , MatchState } from "@/contexts/websocketcontext" ;
2
2
import { useEffect , useState } from "react" ;
3
3
import useWebSocket , { Options , ReadyState } from "react-use-websocket" ;
4
4
@@ -21,6 +21,13 @@ export type MatchFoundResponse = {
21
21
matchId : number ,
22
22
partnerId : number ,
23
23
partnerName : string ,
24
+ } | {
25
+ type : "match_found" ,
26
+ matchId : string ,
27
+ user : string ,
28
+ matchedUser : string ,
29
+ topic : string | string [ ] ,
30
+ difficulty : string
24
31
}
25
32
26
33
export type MatchTimeoutResponse = {
@@ -33,7 +40,7 @@ export type MatchRejectedResponse = {
33
40
message : string ,
34
41
}
35
42
36
- type MatchResponse = MatchFoundResponse | MatchTimeoutResponse ;
43
+ type MatchResponse = MatchFoundResponse | MatchTimeoutResponse | MatchRejectedResponse ;
37
44
38
45
export default function useMatching ( ) : MatchState {
39
46
const [ isSocket , setIsSocket ] = useState < boolean > ( false ) ;
@@ -55,17 +62,21 @@ export default function useMatching(): MatchState {
55
62
56
63
if ( responseJson . type == "match_found" ) {
57
64
setIsSocket ( false ) ;
65
+
66
+ const info : MatchInfo = parseInfoFromResponse ( responseJson ) ;
58
67
setSte ( {
59
68
state : "found" ,
60
- info : {
61
- matchId : responseJson . matchId . toString ( ) ,
62
- partnerId : responseJson . partnerId . toString ( ) ,
63
- partnerName : responseJson . partnerName ,
64
- } ,
69
+ info : info ,
65
70
ok : cancel
66
71
} )
67
72
return ;
68
73
}
74
+
75
+ if ( responseJson . type == "match_rejected" ) {
76
+ console . log ( "match rejected: " + responseJson . message ) ;
77
+ cancel ( ) ;
78
+ return ;
79
+ }
69
80
}
70
81
}
71
82
@@ -113,4 +124,21 @@ export default function useMatching(): MatchState {
113
124
}
114
125
115
126
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
+ }
0 commit comments