@@ -58,21 +58,11 @@ async function processMatch(
5858 continue ;
5959 }
6060
61- const setPendingToFalse = async ( userId : string ) => {
62- await redisClient . hSet ( `match:${ userId } ` , 'pending' , 'false' ) ;
63- console . log ( `User ${ userId } is now in MATCHING state, pending set to false.` ) ;
64- } ;
65-
66- await Promise . all ( [
67- setPendingToFalse ( requestorUserId ) , // Set pending to false for requester
68- setPendingToFalse ( matchedUserId ) , // Set pending to false for matched user
69- ] ) ;
70-
7161 // To block cancellation
7262 sendNotif ( [ matchedSocketPort ] , MATCHING_EVENT . MATCHING ) ;
63+ await redisClient . hSet ( getPoolKey ( matchedUserId ) , 'pending' , 'false' ) ;
7364
7465 const matchedStreamId = getStreamId ( timestamp ) ;
75-
7666 logger . info ( `Found match: ${ JSON . stringify ( matched ) } ` ) ;
7767
7868 await Promise . all ( [
@@ -141,8 +131,9 @@ async function match() {
141131
142132 // To Block Cancellation
143133 sendNotif ( [ requestorSocketPort ] , MATCHING_EVENT . MATCHING ) ;
134+ await redisClient . hSet ( getPoolKey ( requestorUserId ) , 'pending' , 'false' ) ;
144135
145- const clause = [ `-@userId:(${ requestorUserId } )` ] ;
136+ const clause = [ `-@userId:(${ requestorUserId } ) @pending:(true) ` ] ;
146137
147138 if ( difficulty ) {
148139 clause . push ( `@difficulty:{${ difficulty } }` ) ;
@@ -176,7 +167,7 @@ async function match() {
176167 // Match on Topic
177168 const topicMatches = await redisClient . ft . search (
178169 POOL_INDEX ,
179- `@topic:{ ${ topic } } -@userId:( ${ requestorUserId } )` ,
170+ clause . filter ( ( v ) => ! v . startsWith ( '@difficulty' ) ) . join ( ' ' ) ,
180171 searchParams
181172 ) ;
182173 const topicMatchFound = await processMatch (
@@ -195,7 +186,7 @@ async function match() {
195186 // Match on Difficulty
196187 const difficultyMatches = await redisClient . ft . search (
197188 POOL_INDEX ,
198- `@difficulty: ${ difficulty } -@userId:( ${ requestorUserId } )` ,
189+ clause . filter ( ( v ) => ! v . startsWith ( '@topic' ) ) . join ( ' ' ) ,
199190 searchParams
200191 ) ;
201192 const hasDifficultyMatch = await processMatch (
@@ -209,14 +200,9 @@ async function match() {
209200
210201 if ( ! hasDifficultyMatch ) {
211202 // To allow cancellation
203+ await redisClient . hSet ( getPoolKey ( requestorUserId ) , 'pending' , 'true' ) ;
212204 sendNotif ( [ requestorSocketPort ] , MATCHING_EVENT . PENDING ) ;
213-
214- const setPendingToTrue = async ( userId : string ) => {
215- await redisClient . hSet ( `match:${ userId } ` , 'pending' , 'true' ) ;
216- console . log ( `User ${ userId } is now in PENDING state, pending set to true.` ) ;
217- } ;
218-
219- await setPendingToTrue ( requestorUserId ) ;
205+ logger . info ( `${ requestorUserId } is now in mode ${ MATCHING_EVENT . PENDING } ` ) ;
220206 }
221207 }
222208 }
0 commit comments