@@ -10,47 +10,68 @@ export class MatchWorkerService {
10
10
// Poll for matches at a regular interval
11
11
async pollForMatches ( ) {
12
12
setInterval ( async ( ) => {
13
- const users = await this . redisService . getUsersFromPool ( ) ;
13
+ const users = await this . redisService . getAllUsersFromPool ( ) ;
14
14
const currentTime = Date . now ( ) ;
15
15
const timeout = 300000 ; // 5 minutes to remove any zombie users
16
16
console . log ( 'Polling' , users ) ;
17
17
// Filter out users who have timed out
18
- const activeUsers = users . filter ( user => currentTime - user . timestamp < timeout ) ;
18
+ const activeUsers = users . filter (
19
+ ( user ) => currentTime - user . timestamp < timeout ,
20
+ ) ;
19
21
20
22
if ( activeUsers . length < users . length ) {
21
- const timedOutUsers = users . filter ( user => currentTime - user . timestamp >= timeout ) ;
22
- await this . notifyGatewayTimeout ( timedOutUsers . map ( user => user . userId ) ) ;
23
- await this . redisService . removeUsersFromPool ( timedOutUsers . map ( user => user . userId ) ) ;
23
+ const timedOutUsers = users . filter (
24
+ ( user ) => currentTime - user . timestamp >= timeout ,
25
+ ) ;
26
+ await this . notifyGatewayTimeout (
27
+ timedOutUsers . map ( ( user ) => user . userId ) ,
28
+ ) ;
29
+ await this . redisService . removeUsersFromPool (
30
+ timedOutUsers . map ( ( user ) => user . userId ) ,
31
+ ) ;
24
32
}
25
33
26
34
if ( activeUsers . length >= 2 ) {
27
35
const matches = this . rankUsers ( activeUsers ) ;
28
36
const bestMatch = matches [ 0 ] ;
29
37
30
38
// Notify the gateway via Redis Pub/Sub
31
- await this . notifyGateway ( [ bestMatch . user1 . userId , bestMatch . user2 . userId ] ) ;
39
+ await this . notifyGateway ( [
40
+ bestMatch . user1 . userId ,
41
+ bestMatch . user2 . userId ,
42
+ ] ) ;
32
43
33
44
// Remove the matched users from the Redis pool
34
- await this . redisService . removeUsersFromPool ( [ bestMatch . user1 . userId , bestMatch . user2 . userId ] ) ;
45
+ await this . redisService . removeUsersFromPool ( [
46
+ bestMatch . user1 . userId ,
47
+ bestMatch . user2 . userId ,
48
+ ] ) ;
35
49
}
36
50
} , 5000 ) ;
37
51
}
38
52
39
53
// Ranking logic for matches
40
- rankUsers ( users : MatchRequestDto [ ] ) : { user1 : MatchRequestDto , user2 : MatchRequestDto , score : number } [ ] {
54
+ rankUsers (
55
+ users : MatchRequestDto [ ] ,
56
+ ) : { user1 : MatchRequestDto ; user2 : MatchRequestDto ; score : number } [ ] {
41
57
const matches = [ ] ;
42
58
for ( let i = 0 ; i < users . length ; i ++ ) {
43
59
for ( let j = i + 1 ; j < users . length ; j ++ ) {
44
60
const score = this . calculateScore ( users [ i ] , users [ j ] ) ;
45
61
matches . push ( { user1 : users [ i ] , user2 : users [ j ] , score } ) ;
46
62
}
47
63
}
48
- return matches . sort ( ( a , b ) => b . score - a . score )
64
+ return matches . sort ( ( a , b ) => b . score - a . score ) ;
49
65
}
50
66
51
- private calculateScore ( user1 : MatchRequestDto , user2 : MatchRequestDto ) : number {
67
+ private calculateScore (
68
+ user1 : MatchRequestDto ,
69
+ user2 : MatchRequestDto ,
70
+ ) : number {
52
71
let score = 0 ;
53
- const matchingTopics = user1 . selectedTopic . filter ( topic => user2 . selectedTopic . includes ( topic ) ) ;
72
+ const matchingTopics = user1 . selectedTopic . filter ( ( topic ) =>
73
+ user2 . selectedTopic . includes ( topic ) ,
74
+ ) ;
54
75
score += matchingTopics . length * 3 ;
55
76
if ( user1 . selectedDifficulty === user2 . selectedDifficulty ) {
56
77
score += 2 ;
@@ -67,4 +88,3 @@ export class MatchWorkerService {
67
88
await this . redisService . publishTimedOutUsers ( userIds ) ;
68
89
}
69
90
}
70
-
0 commit comments