@@ -63,7 +63,7 @@ const groomingMode = {
63
63
] ,
64
64
} ;
65
65
66
- const rooms = [ ] ;
66
+ let rooms = [ ] ;
67
67
const groomings = { } ;
68
68
69
69
const handleErrors = ( errorFunctionName , roomID , socket ) => {
@@ -72,25 +72,30 @@ const handleErrors = (errorFunctionName, roomID, socket) => {
72
72
return null ;
73
73
}
74
74
if ( rooms && ! rooms . some ( room => room . roomID === roomID ) ) {
75
- console . log ( "Room is deleted, error shown to the user." , errorFunctionName , roomID , rooms ) ;
76
- return socket . emit ( "encounteredError" , {
75
+ console . log ( "Room is deleted, info shown to the user." , errorFunctionName , roomID ) ;
76
+ socket . emit ( "encounteredError" , {
77
77
id : 1 ,
78
78
message :
79
- "Room is not exist. Rooms are available for only 6 hours after they are created. You can continue by creating new one ." ,
79
+ "Room is expired, if you think this is an error, please contact Armağan Dalkıran ." ,
80
80
} ) ;
81
+ return {
82
+ isSuccess : false ,
83
+ message : "Room is expired, if you think this is an error, please contact Armağan Dalkıran."
84
+ } ;
81
85
}
82
86
83
- console . log ( "User lost connection but popup shown." , errorFunctionName , roomID , rooms ) ;
87
+ console . log ( "Something unexpected happen!" ) ;
84
88
85
- return socket . emit ( "encounteredError" , {
86
- id : 2 ,
87
- message : "Your connection is lost. Connect again" ,
88
- } ) ;
89
+ return {
90
+ id : 3 ,
91
+ isSuccess : false ,
92
+ message : "Your connection is lost. Connect again"
93
+ }
89
94
} ;
90
95
91
96
const generateNewRoom = ( nickName , groomingType ) => {
92
97
const currentTime = new Date ( ) . getTime ( ) ;
93
- const expireTime = currentTime + 6 * 60 * 60 * 1000 ;
98
+ const expireTime = currentTime + 12 * 60 * 60 * 1000 ; // 12 hours
94
99
const roomID = uuid . v4 ( ) ;
95
100
96
101
const user = userJoin ( nickName , roomID ) ;
@@ -131,7 +136,7 @@ const generateNewRoom = (nickName, groomingType) => {
131
136
} ;
132
137
} ;
133
138
134
- const handleJoinRoom = ( nickName , roomID ) => {
139
+ const handleJoinRoom = ( nickName , roomID , isAdmin ) => {
135
140
const user = userJoin ( nickName , roomID ) ;
136
141
if ( ! user ) {
137
142
return handleErrors ( "handleJoinRoom" , roomID ) ;
@@ -140,9 +145,13 @@ const handleJoinRoom = (nickName, roomID) => {
140
145
user . isAdmin = false ;
141
146
user . connected = true ;
142
147
148
+ if ( isAdmin ) {
149
+ user . isAdmin = isAdmin
150
+ }
151
+
143
152
groomings [ roomID ] = {
144
153
...groomings [ roomID ] ,
145
- totalParticipants : user . userID + 1 ,
154
+ totalParticipants : Object . keys ( groomings [ roomID ] . participants ) . length + 1 ,
146
155
} ;
147
156
148
157
const { credentials, ...userWithoutCredentials } = user ;
@@ -191,11 +200,11 @@ const removeUserFromOngoingGrooming = (roomID, userID) => {
191
200
}
192
201
193
202
delete groomings [ roomID ] . participants [ userID ] ;
194
- groomings [ roomID ] . totalParticipants -- ;
203
+ groomings [ roomID ] . totalParticipants = groomings [ roomID ] . totalParticipants - 1 ;
195
204
} ;
196
205
197
206
const updateParticipantsVote = ( data , credentials , roomID , socket ) => {
198
- const user = getCurrentUser ( credentials ) ;
207
+ const user = getCurrentUser ( credentials , socket ) ;
199
208
if ( ! user ) {
200
209
return handleErrors ( "updateParticipantsVote" , roomID , socket ) ;
201
210
}
@@ -322,7 +331,7 @@ const calculateScore = (mode, participants, roomID) => {
322
331
} ;
323
332
324
333
const getResults = ( credentials , roomID , socket ) => {
325
- const user = getCurrentUser ( credentials ) ;
334
+ const user = getCurrentUser ( credentials , socket ) ;
326
335
if ( ! user ) {
327
336
return handleErrors ( "getResults" , roomID , socket ) ;
328
337
}
@@ -334,7 +343,7 @@ const getResults = (credentials, roomID, socket) => {
334
343
335
344
336
345
const setIssues = ( data , credentials , roomID , socket ) => {
337
- const user = getCurrentUser ( credentials ) ;
346
+ const user = getCurrentUser ( credentials , socket ) ;
338
347
if ( ! user ) {
339
348
return handleErrors ( "setIssues" , roomID , socket ) ;
340
349
}
@@ -345,7 +354,7 @@ const setIssues = (data, credentials, roomID, socket) => {
345
354
} ;
346
355
347
356
const updateTimer = ( data , credentials , roomID , socket ) => {
348
- const user = getCurrentUser ( credentials ) ;
357
+ const user = getCurrentUser ( credentials , socket ) ;
349
358
if ( ! user ) {
350
359
return handleErrors ( "updateTimer" , roomID , socket ) ;
351
360
}
@@ -356,7 +365,7 @@ const updateTimer = (data, credentials, roomID, socket) => {
356
365
} ;
357
366
358
367
const updateAvatar = ( data , credentials , roomID , socket ) => {
359
- const user = getCurrentUser ( credentials ) ;
368
+ const user = getCurrentUser ( credentials , socket ) ;
360
369
if ( ! user ) {
361
370
return handleErrors ( "updateAvatar" , roomID , socket ) ;
362
371
}
@@ -374,7 +383,7 @@ const updateAvatar = (data, credentials, roomID, socket) => {
374
383
} ;
375
384
376
385
const resetVotes = ( credentials , roomID , socket ) => {
377
- const user = getCurrentUser ( credentials ) ;
386
+ const user = getCurrentUser ( credentials , socket ) ;
378
387
if ( ! user ) {
379
388
return handleErrors ( "resetVotes" , roomID , socket ) ;
380
389
}
@@ -396,6 +405,12 @@ const getRooms = () => {
396
405
return rooms ;
397
406
} ;
398
407
408
+ const logRooms = ( ) => {
409
+ setInterval ( ( ) => {
410
+ console . log ( rooms ) ;
411
+ } , 10000 ) ;
412
+ }
413
+
399
414
const checkRoomExistance = ( roomId ) => {
400
415
return rooms . some ( ( room ) => room . roomID === roomId ) ;
401
416
} ;
@@ -431,22 +446,25 @@ function findClosestFibonacci(number) {
431
446
432
447
const cleanRoomsAndUsers = ( ) => {
433
448
setInterval ( ( ) => {
434
- const currentTime = new Date ( ) . getTime ( ) ;
435
- rooms . forEach ( ( room ) => {
436
- if ( room . expiredAt < currentTime ) {
437
- const indexToRemove = rooms . findIndex (
438
- ( indexToRemoveRoom ) => indexToRemoveRoom . roomID === room . roomID
439
- ) ;
440
- rooms . splice ( indexToRemove , 1 ) ;
441
- delete groomings [ room . roomID ] ;
442
- clearUser ( room . roomID ) ;
443
- }
444
- } ) ;
445
- } , 60000 * 60 * 3 ) ; // work every 3 hours
449
+ const currentTime = Date . now ( ) ;
450
+
451
+ // Get expired room IDs before modifying arrays
452
+ const expiredRoomIDs = rooms . filter ( room => room . expiredAt < currentTime ) . map ( room => room . roomID ) ;
453
+
454
+ // Remove expired rooms safely
455
+ rooms = rooms . filter ( room => room . expiredAt >= currentTime ) ;
456
+
457
+ // Remove expired rooms from `groomings`
458
+ expiredRoomIDs . forEach ( roomID => delete groomings [ roomID ] ) ;
459
+
460
+ // Remove users in expired rooms
461
+ expiredRoomIDs . forEach ( clearUser ) ;
462
+ console . log ( "Rooms and users cleaned!" ) ;
463
+ } , 1000 * 60 * 60 * 12 ) ; // work every 12 hours
446
464
} ;
447
465
448
466
const updateNickName = ( credentials , newNickName , roomID , socket ) => {
449
- const user = getCurrentUser ( credentials ) ;
467
+ const user = getCurrentUser ( credentials , socket ) ;
450
468
if ( ! user ) {
451
469
return handleErrors ( "updateNickName" , roomID , socket ) ;
452
470
}
@@ -465,6 +483,7 @@ module.exports = {
465
483
checkRoomExistance,
466
484
generateNewRoom,
467
485
getRooms,
486
+ logRooms,
468
487
handleJoinRoom,
469
488
getGrooming,
470
489
leaveUserFromGrooming,
0 commit comments