1
1
import {
2
2
ConnectPacket ,
3
3
ReplayPacket ,
4
- SubmissionPacket
4
+ SubmissionPacket ,
5
5
} from '@codelm/common/src/packets/client.packet' ;
6
6
import { Packet } from '@codelm/common/src/packets/packet' ;
7
7
import { SubmissionStatus } from '@codelm/common/src/packets/server.packet' ;
@@ -11,19 +11,19 @@ import {
11
11
isGradedProblem ,
12
12
isOpenEndedProblem ,
13
13
OpenEndedProblemModel ,
14
- ProblemType
14
+ ProblemType ,
15
15
} from '@codelm/common/src/models/problem.model' ;
16
16
import {
17
17
isFalse ,
18
18
isTestCaseSubmissionCorrect ,
19
19
SubmissionDao ,
20
- submissionResult
20
+ submissionResult ,
21
21
} from './daos/submission.dao' ;
22
22
import {
23
23
GradedSubmissionModel ,
24
24
isGradedSubmission ,
25
25
SubmissionModel ,
26
- TestCaseSubmissionModel
26
+ TestCaseSubmissionModel ,
27
27
} from '@codelm/common/src/models/submission.model' ;
28
28
import * as WebSocket from 'ws' ;
29
29
import { Express } from 'express' ;
@@ -114,7 +114,12 @@ export class SocketManager {
114
114
}
115
115
116
116
public kick ( id : string ) {
117
- this . sockets = this . sockets . filter ( socket => socket . _id !== id ) ;
117
+ const index = this . sockets . findIndex ( socket => socket . _id === id ) ;
118
+
119
+ if ( index !== - 1 ) {
120
+ this . sockets [ index ] . close ( ) ;
121
+ this . sockets . splice ( index , 1 ) ;
122
+ }
118
123
}
119
124
120
125
public kickTeams ( ) {
@@ -135,7 +140,7 @@ export class SocketManager {
135
140
this . sockets = this . sockets . filter ( socket => socket . ping ( ) ) ;
136
141
} , 15 * 1000 ) ;
137
142
138
- ( ( app as any ) as WithWebsocketMethod ) . ws ( '/' , webSocket => {
143
+ ( app as any as WithWebsocketMethod ) . ws ( '/' , webSocket => {
139
144
let socket : Socket ;
140
145
141
146
webSocket . onmessage = data => {
@@ -147,7 +152,7 @@ export class SocketManager {
147
152
this . onConnectPacket ( packet as ConnectPacket , webSocket ) . then (
148
153
_socket => {
149
154
socket = _socket ;
150
- }
155
+ } ,
151
156
) ;
152
157
} ) ;
153
158
@@ -162,7 +167,7 @@ export class SocketManager {
162
167
webSocket . onclose = ( ) => {
163
168
if ( socket ) {
164
169
this . sockets = this . sockets . filter (
165
- _socket => _socket . _id !== socket . _id
170
+ _socket => _socket . _id !== socket . _id ,
166
171
) ;
167
172
}
168
173
} ;
@@ -175,13 +180,13 @@ export class SocketManager {
175
180
176
181
onConnectPacket (
177
182
packet : ConnectPacket ,
178
- webSocket : WebSocket
183
+ webSocket : WebSocket ,
179
184
) : Promise < Socket > {
180
185
return new Promise < Socket > ( ( resolve , reject ) => {
181
186
jwt . verify ( packet . jwt , JWT_PRIVATE_KEY , ( err , decoded ) => {
182
187
if ( err ) {
183
188
webSocket . send (
184
- JSON . stringify ( { name : 'connectResponse' , success : false } )
189
+ JSON . stringify ( { name : 'connectResponse' , success : false } ) ,
185
190
) ;
186
191
webSocket . close ( ) ;
187
192
reject ( err ) ;
@@ -204,13 +209,13 @@ export class SocketManager {
204
209
problemTitle : problem . title ,
205
210
type : problem . type ,
206
211
language : problemSubmission . language ,
207
- code : problemSubmission . code
212
+ code : problemSubmission . code ,
208
213
} ;
209
214
210
215
if ( isGradedProblem ( problem ) ) {
211
216
serverProblemSubmission . testCases = problem . testCases . filter (
212
217
testCase =>
213
- isFalse ( problemSubmission . test . toString ( ) ) || ! testCase . hidden
218
+ isFalse ( problemSubmission . test . toString ( ) ) || ! testCase . hidden ,
214
219
) ;
215
220
} else if ( isOpenEndedProblem ( problem ) ) {
216
221
serverProblemSubmission . gameType = problem . gameType ;
@@ -219,7 +224,7 @@ export class SocketManager {
219
224
220
225
const { testCases, compilationError } = await this . runSubmission (
221
226
serverProblemSubmission ,
222
- socket
227
+ socket ,
223
228
) ;
224
229
225
230
let submission : SubmissionModel = {
@@ -230,7 +235,7 @@ export class SocketManager {
230
235
code : problemSubmission . code ,
231
236
testCases : testCases ?? [ ] ,
232
237
compilationError,
233
- test : problemSubmission . test
238
+ test : problemSubmission . test ,
234
239
} as GradedSubmissionModel ;
235
240
236
241
if ( problemSubmission . test ) {
@@ -251,14 +256,14 @@ export class SocketManager {
251
256
// TODO: Combine this with onSubmissionPacket
252
257
async onReplayPacket ( packet : ReplayPacket , socket : Socket ) {
253
258
const submission = await SubmissionDao . getSubmission (
254
- packet . replayRequest . _id
259
+ packet . replayRequest . _id ,
255
260
) ;
256
261
257
262
const serverProblemSubmission : ServerProblemSubmission = {
258
263
problemTitle : submission . problem . title ,
259
264
type : submission . problem . type ,
260
265
language : submission . language ,
261
- code : submission . code
266
+ code : submission . code ,
262
267
} ;
263
268
264
269
const problem = submission . problem as OpenEndedProblemModel ;
@@ -271,7 +276,7 @@ export class SocketManager {
271
276
272
277
private async runGradedSubmission (
273
278
serverProblemSubmission : ServerProblemSubmission ,
274
- connection : CodeRunnerConnection
279
+ connection : CodeRunnerConnection ,
275
280
) : Promise < { testCases : TestCaseSubmissionModel [ ] } > {
276
281
const testCases : TestCaseSubmissionModel [ ] = [ ] ;
277
282
@@ -289,7 +294,7 @@ export class SocketManager {
289
294
output : '' ,
290
295
correctOutput : testCase . output ,
291
296
inputDisplay : testCase . inputDisplay ,
292
- outputDisplay : testCase . outputDisplay
297
+ outputDisplay : testCase . outputDisplay ,
293
298
} ;
294
299
295
300
if ( e . name === 'dockerKilled' ) {
@@ -321,7 +326,7 @@ export class SocketManager {
321
326
private runOpenEndedSubmission (
322
327
serverProblemSubmission : ServerProblemSubmission ,
323
328
connection : CodeRunnerConnection ,
324
- socket : Socket
329
+ socket : Socket ,
325
330
) : Promise < { } > {
326
331
return new Promise < { } > ( resolve => {
327
332
let packetStream : Observable < CodeRunnerPacket > ;
@@ -333,7 +338,7 @@ export class SocketManager {
333
338
}
334
339
case GameType . Timesweeper : {
335
340
packetStream = connection . runGame (
336
- new Timesweeper ( serverProblemSubmission . problemExtras )
341
+ new Timesweeper ( serverProblemSubmission . problemExtras ) ,
337
342
) ;
338
343
break ;
339
344
}
@@ -348,21 +353,21 @@ export class SocketManager {
348
353
} ,
349
354
( ) => {
350
355
resolve ( { } ) ;
351
- }
356
+ } ,
352
357
) ;
353
358
} ) ;
354
359
}
355
360
356
361
private async runSubmission (
357
362
serverProblemSubmission : ServerProblemSubmission ,
358
- socket : Socket
363
+ socket : Socket ,
359
364
) : Promise < {
360
365
testCases ?: TestCaseSubmissionModel [ ] ;
361
366
compilationError ?: string ;
362
367
} > {
363
368
socket . send ( {
364
369
name : 'submissionExtras' ,
365
- extras : serverProblemSubmission . problemExtras
370
+ extras : serverProblemSubmission . problemExtras ,
366
371
} ) ;
367
372
368
373
let connection : CodeRunnerConnection ;
@@ -373,17 +378,17 @@ export class SocketManager {
373
378
return {
374
379
compilationError :
375
380
'The server is overloaded. Please send the following error to a judge: ' +
376
- JSON . stringify ( e )
381
+ JSON . stringify ( e ) ,
377
382
} ;
378
383
}
379
384
380
385
try {
381
386
socket . send ( {
382
387
name : 'submissionStatus' ,
383
- status : SubmissionStatus . Compiling
388
+ status : SubmissionStatus . Compiling ,
384
389
} ) ;
385
390
const compilationResult = await connection . compile (
386
- serverProblemSubmission
391
+ serverProblemSubmission ,
387
392
) ;
388
393
389
394
if ( ! compilationResult . success ) {
@@ -396,19 +401,19 @@ export class SocketManager {
396
401
if ( e . name === 'dockerKilled' ) {
397
402
return {
398
403
compilationError :
399
- 'Your code could not compile. Maybe the server is overloaded?'
404
+ 'Your code could not compile. Maybe the server is overloaded?' ,
400
405
} ;
401
406
} else if ( e . name === 'unexpectedData' ) {
402
407
return {
403
408
compilationError :
404
409
'An unexpected error occurred. Maybe the server is overloaded? Please send the following error to a judge: ' +
405
- e . data
410
+ e . data ,
406
411
} ;
407
412
} else {
408
413
return {
409
414
compilationError :
410
415
'An unknown occurred. Please send the following error to a judge: ' +
411
- JSON . stringify ( e )
416
+ JSON . stringify ( e ) ,
412
417
} ;
413
418
}
414
419
}
@@ -418,13 +423,13 @@ export class SocketManager {
418
423
if ( serverProblemSubmission . type === ProblemType . Graded ) {
419
424
return await this . runGradedSubmission (
420
425
serverProblemSubmission ,
421
- connection
426
+ connection ,
422
427
) ;
423
428
} else {
424
429
return await this . runOpenEndedSubmission (
425
430
serverProblemSubmission ,
426
431
connection ,
427
- socket
432
+ socket ,
428
433
) ;
429
434
}
430
435
}
0 commit comments