1
- import { io } from "../../server" ;
2
1
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" ;
12
2
import { sendRabbitMq } from "../../config/rabbitmq" ;
3
+ import { sendMatchFound } from "./websocketHandler" ;
13
4
14
5
interface MatchUser {
15
6
id : string ;
@@ -31,7 +22,7 @@ export interface MatchRequest {
31
22
timeout : number ;
32
23
}
33
24
34
- export interface MatchItem {
25
+ export interface MatchRequestItem {
35
26
user : MatchUser ;
36
27
complexities : string [ ] ;
37
28
categories : string [ ] ;
@@ -41,23 +32,12 @@ export interface MatchItem {
41
32
}
42
33
43
34
const matches = new Map < string , Match > ( ) ;
44
- const userSockets = new Map < string , Socket > ( ) ;
45
35
46
- export const createMatchItem = async (
47
- socket : Socket ,
48
- matchRequest : MatchRequest ,
49
- isRematch ?: boolean
36
+ export const sendMatchRequest = async (
37
+ matchRequest : MatchRequest
50
38
) : Promise < boolean > => {
51
39
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 = {
61
41
user : user ,
62
42
complexities : complexities ,
63
43
categories : categories ,
@@ -66,98 +46,43 @@ export const createMatchItem = async (
66
46
ttlInSecs : timeout ,
67
47
} ;
68
48
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 ;
74
51
} ;
75
52
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
+ ) => {
80
57
const matchId = uuidv4 ( ) ;
81
58
matches . set ( matchId , {
82
- uid1 : uid1 ,
83
- uid2 : uid2 ,
59
+ uid1 : requestItem1 . user . id ,
60
+ uid2 : requestItem2 . user . id ,
84
61
accepted : false ,
85
62
} ) ;
86
63
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 ) ;
94
65
} ;
95
66
96
- export const handleMatchAcceptance = ( matchId : string ) => {
67
+ export const handleMatchAccept = ( matchId : string ) : boolean => {
97
68
const match = matches . get ( matchId ) ;
98
69
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 ;
112
71
}
113
- } ;
114
72
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 ;
122
76
} ;
123
77
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 ) ;
143
80
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 ;
146
85
}
147
- } else if ( uid ) {
148
- userSockets . delete ( uid ) ;
149
86
}
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 ;
163
88
} ;
0 commit comments