@@ -69,7 +69,7 @@ function matchUsers(channel, msg, userId, difficulty, category) {
69
69
70
70
if ( waitingUsers [ criteriaKey ] . length >= 2 ) {
71
71
const matchedUsers = waitingUsers [ criteriaKey ] . splice ( 0 , 2 ) ;
72
- removeMatchedUsersFromOtherLists ( matchedUsers , categoryKey ) ;
72
+ removeMatchedUsersFromOtherLists ( matchedUsers , criteriaKey ) ;
73
73
console . log ( "waitingusers after strict matching: " , waitingUsers )
74
74
notifyMatch ( channel , matchedUsers , category ) ;
75
75
return true ;
@@ -81,7 +81,7 @@ function matchUsers(channel, msg, userId, difficulty, category) {
81
81
console . log ( `Fallback: User ${ userId } added to ${ categoryKey } . Waiting list: ${ waitingUsers [ categoryKey ] . length } ` ) ;
82
82
if ( waitingUsers [ categoryKey ] . length >= 2 ) {
83
83
const matchedUsers = waitingUsers [ categoryKey ] . splice ( 0 , 2 ) ;
84
- removeMatchedUsersFromOtherLists ( matchedUsers , criteriaKey ) ;
84
+ removeMatchedUsersFromOtherLists ( matchedUsers , categoryKey ) ;
85
85
console . log ( "waitingusers after lenient matching: " , waitingUsers )
86
86
notifyMatch ( channel , matchedUsers , category ) ;
87
87
return true ;
@@ -99,6 +99,7 @@ function removeMatchedUsersFromOtherLists(matchedUsers, keyToSkip) {
99
99
) ;
100
100
}
101
101
}
102
+ console . log ( "waiting users after removing: " , waitingUsers ) ;
102
103
}
103
104
104
105
async function notifyMatch ( channel , matchedUsers , category ) {
@@ -191,19 +192,21 @@ async function acknowledgeMessage(channel, msg) {
191
192
// });
192
193
// }
193
194
194
- async function rejectMessage ( channel , msg , userId ) {
195
+ async function rejectMessage ( channel , msg , user ) {
195
196
return new Promise ( ( resolve , reject ) => {
196
197
try {
197
198
const userData = JSON . parse ( msg . content . toString ( ) ) ;
198
199
channel . reject ( msg , false ) ; // Reject without requeuing
199
- console . log ( `Rejected message for user: ${ userId } ` ) ;
200
- if ( timeoutMap [ userId ] ) {
201
- clearTimeout ( timeoutMap [ userId ] ) ;
202
- delete timeoutMap [ userId ] ;
200
+ console . log ( `Rejected message for user: ${ user . userId } ` ) ;
201
+ if ( timeoutMap [ user . userId ] ) {
202
+ clearTimeout ( timeoutMap [ user . userId ] ) ;
203
+ delete timeoutMap [ user . userId ] ;
203
204
}
205
+ removeMatchedUsersFromOtherLists ( [ user ] , 'all' )
206
+
204
207
resolve ( ) ;
205
208
} catch ( error ) {
206
- console . error ( `Failed to reject message for user ${ userId } :` , error ) ;
209
+ console . error ( `Failed to reject message for user ${ user . userId } :` , error ) ;
207
210
reject ( error ) ;
208
211
}
209
212
} ) ;
@@ -275,7 +278,8 @@ async function consumeQueue() {
275
278
if ( ! matched ) {
276
279
console . log ( `No match for ${ userId } , waiting for rejection timeout.` ) ;
277
280
const timeoutId = setTimeout ( async ( ) => {
278
- await rejectMessage ( channel , msg , userId ) ;
281
+ const categorykey = "any." + category
282
+ await rejectMessage ( channel , msg , userData ) ;
279
283
} , 10000 ) ; // 10 seconds delay
280
284
281
285
timeoutMap [ userId ] = timeoutId ;
@@ -286,8 +290,8 @@ async function consumeQueue() {
286
290
287
291
console . log ( "Listening to matchmaking queues" ) ;
288
292
289
- await consumeCancelQueue ( ) ;
290
- console . log ( "Listening to Cancel Queue" ) ;
293
+ // await consumeCancelQueue();
294
+ // console.log("Listening to Cancel Queue");
291
295
} catch ( error ) {
292
296
console . error ( 'Error consuming RabbitMQ queue:' , error ) ;
293
297
}
@@ -320,72 +324,72 @@ async function consumeDLQ() {
320
324
}
321
325
}
322
326
323
- async function consumeCancelQueue ( ) {
324
- try {
325
- const connection = await amqp . connect ( process . env . RABBITMQ_URL ) ;
326
- const channel = await connection . createChannel ( ) ;
327
+ // async function consumeCancelQueue() {
328
+ // try {
329
+ // const connection = await amqp.connect(process.env.RABBITMQ_URL);
330
+ // const channel = await connection.createChannel();
327
331
328
- // Subscribe to the cancel queue
329
- await channel . consume ( 'cancel_queue' , async ( msg ) => {
330
- if ( msg !== null ) {
331
- const { userId } = JSON . parse ( msg . content . toString ( ) ) ;
332
- console . log ( `Received cancel request for user: ${ userId } ` ) ;
332
+ // // Subscribe to the cancel queue
333
+ // await channel.consume('cancel_queue', async (msg) => {
334
+ // if (msg !== null) {
335
+ // const { userId } = JSON.parse(msg.content.toString());
336
+ // console.log(`Received cancel request for user: ${userId}`);
333
337
334
- // Process the cancel request
335
- await cancelMatching ( channel , msg , userId ) ;
336
- }
337
- } , { noAck : false } ) ; // Ensure manual acknowledgment
338
+ // // Process the cancel request
339
+ // await cancelMatching(channel, msg, userId);
340
+ // }
341
+ // }, { noAck: false }); // Ensure manual acknowledgment
338
342
339
- console . log ( "Listening for cancel requests" ) ;
340
- } catch ( error ) {
341
- console . error ( 'Error consuming cancel queue:' , error ) ;
342
- }
343
- }
343
+ // console.log("Listening for cancel requests");
344
+ // } catch (error) {
345
+ // console.error('Error consuming cancel queue:', error);
346
+ // }
347
+ // }
344
348
345
- async function cancelMatching ( cancelChannel , cancelMsg , userId ) {
346
- try {
347
- let foundOriginalMsg = false ;
348
-
349
- // Loop through waitingUsers to find the original message for the user
350
- Object . keys ( waitingUsers ) . forEach ( criteriaKey => {
351
- const userIndex = waitingUsers [ criteriaKey ] . findIndex ( user => user . userId === userId ) ;
352
- // const userIndexCat = waitingUsers[categoryKey].findIndex(user => user.userId === userId);
353
- if ( userIndex !== - 1 ) {
354
- const { msg, channel } = waitingUsers [ criteriaKey ] [ userIndex ] ; // Get original msg and its channel
355
-
356
- // Acknowledge the original matchmaking message from the queue (e.g., easy.python)
357
- if ( msg && channel ) {
358
- console . log ( `Acknowledging original message for user ${ userId } in queue ${ criteriaKey } ` ) ;
359
- channel . ack ( msg ) ; // Use the same channel that consumed the message to acknowledge it
360
- foundOriginalMsg = true ;
361
- }
349
+ // async function cancelMatching(cancelChannel, cancelMsg, userId) {
350
+ // try {
351
+ // let foundOriginalMsg = false;
352
+
353
+ // // Loop through waitingUsers to find the original message for the user
354
+ // Object.keys(waitingUsers).forEach(criteriaKey => {
355
+ // const userIndex = waitingUsers[criteriaKey].findIndex(user => user.userId === userId);
356
+ // // const userIndexCat = waitingUsers[categoryKey].findIndex(user => user.userId === userId);
357
+ // if (userIndex !== -1) {
358
+ // const { msg, channel } = waitingUsers[criteriaKey][userIndex]; // Get original msg and its channel
359
+
360
+ // // Acknowledge the original matchmaking message from the queue (e.g., easy.python)
361
+ // if (msg && channel) {
362
+ // console.log(`Acknowledging original message for user ${userId} in queue ${criteriaKey}`);
363
+ // channel.ack(msg); // Use the same channel that consumed the message to acknowledge it
364
+ // foundOriginalMsg = true;
365
+ // }
362
366
363
- // Remove the user from the waiting list
364
- waitingUsers [ criteriaKey ] . splice ( userIndex , 1 ) ;
365
- // waitingUsers[categoryKey].splice(userIndex, 1);
366
- console . log ( `User ${ userId } removed from waiting list for ${ criteriaKey } ` ) ;
367
- }
368
- } ) ;
367
+ // // Remove the user from the waiting list
368
+ // waitingUsers[criteriaKey].splice(userIndex, 1);
369
+ // // waitingUsers[categoryKey].splice(userIndex, 1);
370
+ // console.log(`User ${userId} removed from waiting list for ${criteriaKey}`);
371
+ // }
372
+ // });
369
373
370
- // If original message not found, log a warning
371
- if ( ! foundOriginalMsg ) {
372
- console . warn ( `Original message for user ${ userId } not found in matchmaking queues.` ) ;
373
- }
374
+ // // If original message not found, log a warning
375
+ // if (!foundOriginalMsg) {
376
+ // console.warn(`Original message for user ${userId} not found in matchmaking queues.`);
377
+ // }
374
378
375
- // Clear any timeouts for the user
376
- if ( timeoutMap [ userId ] ) {
377
- clearTimeout ( timeoutMap [ userId ] ) ;
378
- delete timeoutMap [ userId ] ;
379
- }
379
+ // // Clear any timeouts for the user
380
+ // if (timeoutMap[userId]) {
381
+ // clearTimeout(timeoutMap[userId]);
382
+ // delete timeoutMap[userId];
383
+ // }
380
384
381
- // Acknowledge the cancel message from the cancel queue
382
- cancelChannel . ack ( cancelMsg ) ;
383
- console . log ( `Cancel processed for user ${ userId } ` ) ;
385
+ // // Acknowledge the cancel message from the cancel queue
386
+ // cancelChannel.ack(cancelMsg);
387
+ // console.log(`Cancel processed for user ${userId}`);
384
388
385
- } catch ( error ) {
386
- console . error ( `Failed to process cancel for user ${ userId } :` , error ) ;
387
- }
388
- }
389
+ // } catch (error) {
390
+ // console.error(`Failed to process cancel for user ${userId}:`, error);
391
+ // }
392
+ // }
389
393
390
394
391
395
module . exports = { consumeQueue, consumeDLQ } ;
0 commit comments