teeworlds 2.5.4 → 2.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/{index.ts → index.d.ts} +6 -6
- package/index.js +25 -24
- package/index.js.map +1 -0
- package/lib/MsgPacker.d.ts +11 -0
- package/lib/MsgPacker.js +47 -55
- package/lib/MsgPacker.js.map +1 -0
- package/lib/MsgUnpacker.d.ts +16 -0
- package/lib/MsgUnpacker.js +53 -53
- package/lib/MsgUnpacker.js.map +1 -0
- package/lib/UUIDManager.d.ts +27 -0
- package/lib/UUIDManager.js +45 -50
- package/lib/UUIDManager.js.map +1 -0
- package/lib/client.d.ts +135 -0
- package/lib/client.js +762 -830
- package/lib/client.js.map +1 -0
- package/lib/components/game.d.ts +32 -0
- package/lib/components/game.js +104 -106
- package/lib/components/game.js.map +1 -0
- package/lib/components/movement.d.ts +34 -0
- package/lib/components/movement.js +75 -82
- package/lib/components/movement.js.map +1 -0
- package/lib/components/rcon.d.ts +33 -0
- package/lib/components/rcon.js +92 -122
- package/lib/components/rcon.js.map +1 -0
- package/lib/components/snapshot.d.ts +59 -0
- package/lib/components/snapshot.js +144 -234
- package/lib/components/snapshot.js.map +1 -0
- package/lib/enums_types/protocol.d.ts +120 -0
- package/lib/enums_types/protocol.js +14 -13
- package/lib/enums_types/protocol.js.map +1 -0
- package/lib/huffman.d.ts +24 -0
- package/lib/huffman.js +187 -192
- package/lib/huffman.js.map +1 -0
- package/lib/snapshot.d.ts +49 -0
- package/lib/snapshot.js +599 -610
- package/lib/snapshot.js.map +1 -0
- package/package.json +21 -24
- package/docs/documentation.md +0 -368
- package/docs/examples/chat_bot.js +0 -25
- package/docs/examples/kill_on_freeze.js +0 -20
- package/lib/MsgPacker.ts +0 -45
- package/lib/MsgUnpacker.ts +0 -58
- package/lib/UUIDManager.ts +0 -45
- package/lib/client.ts +0 -926
- package/lib/components/game.ts +0 -133
- package/lib/components/movement.ts +0 -88
- package/lib/components/rcon.ts +0 -114
- package/lib/components/snapshot.ts +0 -187
- package/lib/enums_types/protocol.ts +0 -137
- package/lib/enums_types/types.d.ts +0 -236
- package/lib/huffman.ts +0 -226
- package/lib/snapshot.ts +0 -632
- package/tsconfig.json +0 -69
package/lib/client.js
CHANGED
|
@@ -1,830 +1,762 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
if (this.queueChunkEx.length > 0) {
|
|
246
|
-
|
|
247
|
-
this.queueChunkEx
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
if
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
//
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
if (
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
_this.SendControlMsg(0);
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
});
|
|
767
|
-
return [2 /*return*/];
|
|
768
|
-
});
|
|
769
|
-
});
|
|
770
|
-
};
|
|
771
|
-
/** Sending the input. (automatically done unless options.lightweight is on) */
|
|
772
|
-
Client.prototype.sendInput = function (input) {
|
|
773
|
-
if (input === void 0) { input = this.movement.input; }
|
|
774
|
-
if (this.State != protocol_1.States.STATE_ONLINE)
|
|
775
|
-
return;
|
|
776
|
-
var inputMsg = new MsgPacker_1.MsgPacker(16, true, 0);
|
|
777
|
-
inputMsg.AddInt(this.AckGameTick);
|
|
778
|
-
inputMsg.AddInt(this.PredGameTick);
|
|
779
|
-
inputMsg.AddInt(40);
|
|
780
|
-
inputMsg.AddInt(input.m_Direction);
|
|
781
|
-
inputMsg.AddInt(input.m_TargetX);
|
|
782
|
-
inputMsg.AddInt(input.m_TargetY);
|
|
783
|
-
inputMsg.AddInt(input.m_Jump);
|
|
784
|
-
inputMsg.AddInt(input.m_Fire);
|
|
785
|
-
inputMsg.AddInt(input.m_Hook);
|
|
786
|
-
inputMsg.AddInt(input.m_PlayerFlags);
|
|
787
|
-
inputMsg.AddInt(input.m_WantedWeapon);
|
|
788
|
-
inputMsg.AddInt(input.m_NextWeapon);
|
|
789
|
-
inputMsg.AddInt(input.m_PrevWeapon);
|
|
790
|
-
this.SendMsgEx(inputMsg);
|
|
791
|
-
};
|
|
792
|
-
Object.defineProperty(Client.prototype, "input", {
|
|
793
|
-
/** returns the movement object of the client */
|
|
794
|
-
get: function () {
|
|
795
|
-
return this.movement.input;
|
|
796
|
-
},
|
|
797
|
-
enumerable: false,
|
|
798
|
-
configurable: true
|
|
799
|
-
});
|
|
800
|
-
/** Disconnect the client. */
|
|
801
|
-
Client.prototype.Disconnect = function () {
|
|
802
|
-
var _this = this;
|
|
803
|
-
return new Promise(function (resolve) {
|
|
804
|
-
_this.SendControlMsg(4).then(function () {
|
|
805
|
-
resolve(true);
|
|
806
|
-
if (_this.socket)
|
|
807
|
-
_this.socket.close();
|
|
808
|
-
_this.socket = undefined;
|
|
809
|
-
_this.State = protocol_1.States.STATE_OFFLINE;
|
|
810
|
-
});
|
|
811
|
-
});
|
|
812
|
-
};
|
|
813
|
-
Object.defineProperty(Client.prototype, "VoteOptionList", {
|
|
814
|
-
/** Get all available vote options (for example for map voting) */
|
|
815
|
-
get: function () {
|
|
816
|
-
return this.VoteList;
|
|
817
|
-
},
|
|
818
|
-
enumerable: false,
|
|
819
|
-
configurable: true
|
|
820
|
-
});
|
|
821
|
-
Object.defineProperty(Client.prototype, "rawSnapUnpacker", {
|
|
822
|
-
get: function () {
|
|
823
|
-
return this.SnapUnpacker;
|
|
824
|
-
},
|
|
825
|
-
enumerable: false,
|
|
826
|
-
configurable: true
|
|
827
|
-
});
|
|
828
|
-
return Client;
|
|
829
|
-
}(stream_1.EventEmitter));
|
|
830
|
-
exports.Client = Client;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Client = void 0;
|
|
16
|
+
const crypto_1 = require("crypto");
|
|
17
|
+
const dns_1 = require("dns");
|
|
18
|
+
const dgram_1 = __importDefault(require("dgram"));
|
|
19
|
+
const stream_1 = require("stream");
|
|
20
|
+
const MsgUnpacker_1 = require("./MsgUnpacker");
|
|
21
|
+
const package_json_1 = require("../package.json");
|
|
22
|
+
const movement_1 = __importDefault(require("./components/movement"));
|
|
23
|
+
const MsgPacker_1 = require("./MsgPacker");
|
|
24
|
+
const snapshot_1 = require("./snapshot");
|
|
25
|
+
const huffman_1 = require("./huffman");
|
|
26
|
+
const game_1 = require("./components/game");
|
|
27
|
+
const snapshot_2 = require("./components/snapshot");
|
|
28
|
+
const UUIDManager_1 = require("./UUIDManager");
|
|
29
|
+
const protocol_1 = require("./enums_types/protocol");
|
|
30
|
+
const rcon_1 = require("./components/rcon");
|
|
31
|
+
const huff = new huffman_1.Huffman();
|
|
32
|
+
var messageTypes = [
|
|
33
|
+
["none, starts at 1", "SV_MOTD", "SV_BROADCAST", "SV_CHAT", "SV_KILL_MSG", "SV_SOUND_GLOBAL", "SV_TUNE_PARAMS", "SV_EXTRA_PROJECTILE", "SV_READY_TO_ENTER", "SV_WEAPON_PICKUP", "SV_EMOTICON", "SV_VOTE_CLEAR_OPTIONS", "SV_VOTE_OPTION_LIST_ADD", "SV_VOTE_OPTION_ADD", "SV_VOTE_OPTION_REMOVE", "SV_VOTE_SET", "SV_VOTE_STATUS", "CL_SAY", "CL_SET_TEAM", "CL_SET_SPECTATOR_MODE", "CL_START_INFO", "CL_CHANGE_INFO", "CL_KILL", "CL_EMOTICON", "CL_VOTE", "CL_CALL_VOTE", "CL_IS_DDNET", "SV_DDRACE_TIME", "SV_RECORD", "UNUSED", "SV_TEAMS_STATE", "CL_SHOW_OTHERS_LEGACY"],
|
|
34
|
+
["none, starts at 1", "INFO", "MAP_CHANGE", "MAP_DATA", "CON_READY", "SNAP", "SNAP_EMPTY", "SNAP_SINGLE", "INPUT_TIMING", "RCON_AUTH_STATUS", "RCON_LINE", "READY", "ENTER_GAME", "INPUT", "RCON_CMD", "RCON_AUTH", "REQUEST_MAP_DATA", "PING", "PING_REPLY", "RCON_CMD_ADD", "RCON_CMD_REMOVE"]
|
|
35
|
+
];
|
|
36
|
+
class Client extends stream_1.EventEmitter {
|
|
37
|
+
on(event, listener) {
|
|
38
|
+
return super.on(event, listener);
|
|
39
|
+
}
|
|
40
|
+
emit(event, ...args) {
|
|
41
|
+
return super.emit(event, ...args);
|
|
42
|
+
}
|
|
43
|
+
constructor(ip, port, nickname, options) {
|
|
44
|
+
super();
|
|
45
|
+
this.host = ip;
|
|
46
|
+
this.port = port;
|
|
47
|
+
this.name = nickname;
|
|
48
|
+
this.AckGameTick = 0;
|
|
49
|
+
this.PredGameTick = 0;
|
|
50
|
+
this.currentSnapshotGameTick = 0;
|
|
51
|
+
this.SnapshotParts = 0;
|
|
52
|
+
this.rcon = new rcon_1.Rcon(this);
|
|
53
|
+
this.SnapUnpacker = new snapshot_1.Snapshot(this);
|
|
54
|
+
// this.eSnapHolder = [];
|
|
55
|
+
this.requestResend = false;
|
|
56
|
+
this.VoteList = [];
|
|
57
|
+
if (options)
|
|
58
|
+
this.options = options;
|
|
59
|
+
this.snaps = [];
|
|
60
|
+
this.sentChunkQueue = [];
|
|
61
|
+
this.queueChunkEx = [];
|
|
62
|
+
this.State = protocol_1.States.STATE_OFFLINE; // 0 = offline; 1 = STATE_CONNECTING = 1, STATE_LOADING = 2, STATE_ONLINE = 3
|
|
63
|
+
this.ack = 0; // ack of messages the client has received
|
|
64
|
+
this.clientAck = 0; // ack of messages the client has sent
|
|
65
|
+
this.lastCheckedChunkAck = 0; // this.ack gets reset to this when flushing - used for resetting tick on e.g. map change
|
|
66
|
+
this.receivedSnaps = 0; /* wait for 2 snaps before seeing self as connected */
|
|
67
|
+
this.socket = dgram_1.default.createSocket("udp4");
|
|
68
|
+
this.socket.bind();
|
|
69
|
+
this.TKEN = Buffer.from([255, 255, 255, 255]);
|
|
70
|
+
this.time = new Date().getTime() + 2000; // time (used for keepalives, start to send keepalives after 2 seconds)
|
|
71
|
+
this.lastSendTime = new Date().getTime();
|
|
72
|
+
this.lastRecvTime = new Date().getTime();
|
|
73
|
+
this.lastSentMessages = [];
|
|
74
|
+
this.movement = new movement_1.default();
|
|
75
|
+
this.game = new game_1.Game(this);
|
|
76
|
+
this.SnapshotUnpacker = new snapshot_2.SnapshotWrapper(this);
|
|
77
|
+
this.UUIDManager = new UUIDManager_1.UUIDManager();
|
|
78
|
+
this.UUIDManager.RegisterName("what-is@ddnet.tw", 65536 /* NETMSG.System.NETMSG_WHATIS */);
|
|
79
|
+
this.UUIDManager.RegisterName("it-is@ddnet.tw", 65537 /* NETMSG.System.NETMSG_ITIS */);
|
|
80
|
+
this.UUIDManager.RegisterName("i-dont-know@ddnet.tw", 65538 /* NETMSG.System.NETMSG_IDONTKNOW */);
|
|
81
|
+
this.UUIDManager.RegisterName("rcon-type@ddnet.tw", 65539 /* NETMSG.System.NETMSG_RCONTYPE */);
|
|
82
|
+
this.UUIDManager.RegisterName("map-details@ddnet.tw", 65540 /* NETMSG.System.NETMSG_MAP_DETAILS */);
|
|
83
|
+
this.UUIDManager.RegisterName("capabilities@ddnet.tw", 65541 /* NETMSG.System.NETMSG_CAPABILITIES */);
|
|
84
|
+
this.UUIDManager.RegisterName("clientver@ddnet.tw", 65542 /* NETMSG.System.NETMSG_CLIENTVER */);
|
|
85
|
+
this.UUIDManager.RegisterName("ping@ddnet.tw", 22 /* NETMSG.System.NETMSG_PING */);
|
|
86
|
+
this.UUIDManager.RegisterName("pong@ddnet.tw", 65544 /* NETMSG.System.NETMSG_PONGEX */);
|
|
87
|
+
this.UUIDManager.RegisterName("checksum-request@ddnet.tw", 65545 /* NETMSG.System.NETMSG_CHECKSUM_REQUEST */);
|
|
88
|
+
this.UUIDManager.RegisterName("checksum-response@ddnet.tw", 65546 /* NETMSG.System.NETMSG_CHECKSUM_RESPONSE */);
|
|
89
|
+
this.UUIDManager.RegisterName("checksum-error@ddnet.tw", 65547 /* NETMSG.System.NETMSG_CHECKSUM_ERROR */);
|
|
90
|
+
this.UUIDManager.RegisterName("redirect@ddnet.org", 65548 /* NETMSG.System.NETMSG_REDIRECT */);
|
|
91
|
+
this.UUIDManager.RegisterName("rcon-cmd-group-start@ddnet.org", 65549 /* NETMSG.System.NETMSG_RCON_CMD_GROUP_START */); // not implemented
|
|
92
|
+
this.UUIDManager.RegisterName("rcon-cmd-group-end@ddnet.org", 65550 /* NETMSG.System.NETMSG_RCON_CMD_GROUP_END */); // not implemented
|
|
93
|
+
this.UUIDManager.RegisterName("map-reload@ddnet.org", 65551 /* NETMSG.System.NETMSG_MAP_RELOAD */); // not implemented
|
|
94
|
+
this.UUIDManager.RegisterName("reconnect@ddnet.org", 65552 /* NETMSG.System.NETMSG_RECONNECT */); // implemented
|
|
95
|
+
this.UUIDManager.RegisterName("sv-maplist-add@ddnet.org", 65553 /* NETMSG.System.NETMSG_MAPLIST_ADD */); // not implemented
|
|
96
|
+
this.UUIDManager.RegisterName("sv-maplist-start@ddnet.org", 65554 /* NETMSG.System.NETMSG_MAPLIST_GROUP_START */); // not implemented
|
|
97
|
+
this.UUIDManager.RegisterName("sv-maplist-end@ddnet.org", 65555 /* NETMSG.System.NETMSG_MAPLIST_GROUP_END */); // not implemented
|
|
98
|
+
this.UUIDManager.RegisterName("i-am-npm-package@swarfey.gitlab.io", 65556 /* NETMSG.System.NETMSG_I_AM_NPM_PACKAGE */);
|
|
99
|
+
}
|
|
100
|
+
OnEnterGame() {
|
|
101
|
+
this.snaps = [];
|
|
102
|
+
this.SnapUnpacker = new snapshot_1.Snapshot(this);
|
|
103
|
+
this.SnapshotParts = 0;
|
|
104
|
+
this.receivedSnaps = 0;
|
|
105
|
+
this.SnapshotUnpacker = new snapshot_2.SnapshotWrapper(this);
|
|
106
|
+
this.currentSnapshotGameTick = 0;
|
|
107
|
+
this.AckGameTick = -1;
|
|
108
|
+
this.PredGameTick = 0;
|
|
109
|
+
}
|
|
110
|
+
ResendAfter(lastAck) {
|
|
111
|
+
this.clientAck = lastAck;
|
|
112
|
+
let toResend = [];
|
|
113
|
+
this.lastSentMessages.forEach(msg => {
|
|
114
|
+
if (msg.ack > lastAck)
|
|
115
|
+
toResend.push(msg.msg);
|
|
116
|
+
});
|
|
117
|
+
toResend.forEach(a => a.flag = 1 | 2);
|
|
118
|
+
this.SendMsgEx(toResend);
|
|
119
|
+
}
|
|
120
|
+
Unpack(packet) {
|
|
121
|
+
var unpacked = { twprotocol: { flags: packet[0] >> 4, ack: ((packet[0] & 0xf) << 8) | packet[1], chunkAmount: packet[2], size: packet.byteLength - 3 }, chunks: [] };
|
|
122
|
+
if (packet.indexOf(Buffer.from([0xff, 0xff, 0xff, 0xff])) == 0) // !(unpacked.twprotocol.flags & 8) || unpacked.twprotocol.flags == 255) // flags == 255 is connectionless (used for sending usernames)
|
|
123
|
+
return unpacked;
|
|
124
|
+
if (unpacked.twprotocol.flags & 4) { // resend flag
|
|
125
|
+
this.ResendAfter(unpacked.twprotocol.ack);
|
|
126
|
+
}
|
|
127
|
+
packet = packet.slice(3);
|
|
128
|
+
if (unpacked.twprotocol.flags & 8 && !(unpacked.twprotocol.flags & 1)) { // compression flag
|
|
129
|
+
packet = huff.decompress(packet);
|
|
130
|
+
if (packet.length == 1 && packet[0] == -1)
|
|
131
|
+
return unpacked;
|
|
132
|
+
}
|
|
133
|
+
for (let i = 0; i < unpacked.twprotocol.chunkAmount; i++) {
|
|
134
|
+
var chunk = {};
|
|
135
|
+
chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1));
|
|
136
|
+
chunk.flags = (packet[0] >> 6) & 3;
|
|
137
|
+
if (chunk.flags & 1) {
|
|
138
|
+
chunk.seq = ((packet[1] & 0xf0) << 2) | packet[2];
|
|
139
|
+
packet = packet.slice(3); // remove flags & size
|
|
140
|
+
}
|
|
141
|
+
else
|
|
142
|
+
packet = packet.slice(2);
|
|
143
|
+
// chunk.type = packet[0] & 1 ? "sys" : "game"; // & 1 = binary, ****_***1. e.g 0001_0111 sys, 0001_0110 game
|
|
144
|
+
chunk.sys = Boolean(packet[0] & 1); // & 1 = binary, ****_***1. e.g 0001_0111 sys, 0001_0110 game
|
|
145
|
+
chunk.msgid = (packet[0] - (packet[0] & 1)) / 2;
|
|
146
|
+
chunk.msg = messageTypes[packet[0] & 1][chunk.msgid];
|
|
147
|
+
chunk.raw = packet.slice(1, chunk.bytes);
|
|
148
|
+
if (chunk.msgid == 0 && chunk.raw.byteLength >= 16) {
|
|
149
|
+
let uuid = this.UUIDManager.LookupUUID(chunk.raw.slice(0, 16));
|
|
150
|
+
if (uuid !== undefined) {
|
|
151
|
+
chunk.extended_msgid = uuid.hash;
|
|
152
|
+
chunk.msg = uuid.name;
|
|
153
|
+
chunk.raw = chunk.raw.slice(16);
|
|
154
|
+
chunk.msgid = uuid.type_id;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
packet = packet.slice(chunk.bytes);
|
|
158
|
+
unpacked.chunks.push(chunk);
|
|
159
|
+
}
|
|
160
|
+
return unpacked;
|
|
161
|
+
}
|
|
162
|
+
/** Send a Control Msg to the server. (used for disconnect)*/
|
|
163
|
+
SendControlMsg(msg, ExtraMsg = "") {
|
|
164
|
+
this.lastSendTime = new Date().getTime();
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
if (this.socket) {
|
|
167
|
+
var latestBuf = Buffer.from([0x10 + (((16 << 4) & 0xf0) | ((this.ack >> 8) & 0xf)), this.ack & 0xff, 0x00, msg]);
|
|
168
|
+
latestBuf = Buffer.concat([latestBuf, Buffer.from(ExtraMsg), this.TKEN]); // move header (latestBuf), optional extraMsg & TKEN into 1 buffer
|
|
169
|
+
this.socket.send(latestBuf, 0, latestBuf.length, this.port, this.host, (err, bytes) => {
|
|
170
|
+
resolve(bytes);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
setTimeout(() => { resolve("failed, rip"); }, 2000);
|
|
174
|
+
/* after 2 seconds it was probably not able to send,
|
|
175
|
+
so when sending a quit message the user doesnt
|
|
176
|
+
stay stuck not being able to ctrl + c
|
|
177
|
+
*/
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** Send a Msg (or Msg[]) to the server.*/
|
|
181
|
+
SendMsgEx(Msgs, flags = 0) {
|
|
182
|
+
if (this.State == protocol_1.States.STATE_OFFLINE)
|
|
183
|
+
return;
|
|
184
|
+
if (!this.socket)
|
|
185
|
+
return;
|
|
186
|
+
let _Msgs;
|
|
187
|
+
if (Msgs instanceof Array)
|
|
188
|
+
_Msgs = Msgs;
|
|
189
|
+
else
|
|
190
|
+
_Msgs = [Msgs];
|
|
191
|
+
if (this.queueChunkEx.length > 0) {
|
|
192
|
+
_Msgs.push(...this.queueChunkEx);
|
|
193
|
+
this.queueChunkEx = [];
|
|
194
|
+
}
|
|
195
|
+
this.lastSendTime = new Date().getTime();
|
|
196
|
+
var header = [];
|
|
197
|
+
if (this.clientAck == 0)
|
|
198
|
+
this.lastSentMessages = [];
|
|
199
|
+
_Msgs.forEach((Msg, index) => {
|
|
200
|
+
header[index] = Buffer.alloc((Msg.flag & 1 ? 3 : 2));
|
|
201
|
+
header[index][0] = ((Msg.flag & 3) << 6) | ((Msg.size >> 4) & 0x3f);
|
|
202
|
+
header[index][1] = (Msg.size & 0xf);
|
|
203
|
+
if (Msg.flag & 1) {
|
|
204
|
+
this.clientAck = (this.clientAck + 1) % (1 << 10);
|
|
205
|
+
if (this.clientAck == 0)
|
|
206
|
+
this.lastSentMessages = [];
|
|
207
|
+
header[index][1] |= (this.clientAck >> 2) & 0xf0;
|
|
208
|
+
header[index][2] = this.clientAck & 0xff;
|
|
209
|
+
header[index][0] = (((Msg.flag | 2) & 3) << 6) | ((Msg.size >> 4) & 0x3f); // 2 is resend flag (ugly hack for queue)
|
|
210
|
+
if ((Msg.flag & 2) == 0)
|
|
211
|
+
this.sentChunkQueue.push(Buffer.concat([header[index], Msg.buffer]));
|
|
212
|
+
header[index][0] = (((Msg.flag) & 3) << 6) | ((Msg.size >> 4) & 0x3f);
|
|
213
|
+
if ((Msg.flag & 2) == 0)
|
|
214
|
+
this.lastSentMessages.push({ msg: Msg, ack: this.clientAck });
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
// let flags = 0;
|
|
218
|
+
if (this.requestResend)
|
|
219
|
+
flags |= 4;
|
|
220
|
+
var packetHeader = Buffer.from([((flags << 4) & 0xf0) | ((this.ack >> 8) & 0xf), this.ack & 0xff, _Msgs.length]);
|
|
221
|
+
var chunks = Buffer.from([]);
|
|
222
|
+
let skip = false;
|
|
223
|
+
_Msgs.forEach((Msg, index) => {
|
|
224
|
+
if (skip)
|
|
225
|
+
return;
|
|
226
|
+
if (chunks.byteLength < 1300)
|
|
227
|
+
chunks = Buffer.concat([chunks, Buffer.from(header[index]), Msg.buffer]);
|
|
228
|
+
else {
|
|
229
|
+
skip = true;
|
|
230
|
+
this.SendMsgEx(_Msgs.slice(index));
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
var packet = Buffer.concat([(packetHeader), chunks, this.TKEN]);
|
|
234
|
+
if (chunks.length < 0)
|
|
235
|
+
return;
|
|
236
|
+
this.socket.send(packet, 0, packet.length, this.port, this.host);
|
|
237
|
+
}
|
|
238
|
+
/** Queue a chunk (instantly sent if flush flag is set - otherwise it will be sent in the next packet). */
|
|
239
|
+
QueueChunkEx(Msg) {
|
|
240
|
+
if (Msg instanceof Array) {
|
|
241
|
+
for (let chunk of Msg)
|
|
242
|
+
this.QueueChunkEx(chunk);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (this.queueChunkEx.length > 0) {
|
|
246
|
+
let total_size = 0;
|
|
247
|
+
for (let chunk of this.queueChunkEx)
|
|
248
|
+
total_size += chunk.size;
|
|
249
|
+
if (total_size + Msg.size + 3 > 1394 - 4)
|
|
250
|
+
this.Flush();
|
|
251
|
+
}
|
|
252
|
+
this.queueChunkEx.push(Msg);
|
|
253
|
+
if (Msg.flag & 4)
|
|
254
|
+
this.Flush();
|
|
255
|
+
}
|
|
256
|
+
/** Send a Raw Buffer (as chunk) to the server. */
|
|
257
|
+
SendMsgRaw(chunks) {
|
|
258
|
+
if (this.State == protocol_1.States.STATE_OFFLINE)
|
|
259
|
+
return;
|
|
260
|
+
if (!this.socket)
|
|
261
|
+
return;
|
|
262
|
+
this.lastSendTime = new Date().getTime();
|
|
263
|
+
var packetHeader = Buffer.from([0x0 + (((16 << 4) & 0xf0) | ((this.ack >> 8) & 0xf)), this.ack & 0xff, chunks.length]);
|
|
264
|
+
var packet = Buffer.concat([(packetHeader), Buffer.concat(chunks), this.TKEN]);
|
|
265
|
+
if (chunks.length < 0)
|
|
266
|
+
return;
|
|
267
|
+
this.socket.send(packet, 0, packet.length, this.port, this.host);
|
|
268
|
+
}
|
|
269
|
+
MsgToChunk(packet) {
|
|
270
|
+
var chunk = {};
|
|
271
|
+
chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1));
|
|
272
|
+
chunk.flags = (packet[0] >> 6) & 3;
|
|
273
|
+
if (chunk.flags & 1) {
|
|
274
|
+
chunk.seq = ((packet[1] & 0xf0) << 2) | packet[2];
|
|
275
|
+
packet = packet.slice(3); // remove flags & size
|
|
276
|
+
}
|
|
277
|
+
else
|
|
278
|
+
packet = packet.slice(2);
|
|
279
|
+
// chunk.type = packet[0] & 1 ? "sys" : "game"; // & 1 = binary, ****_***1. e.g 0001_0111 sys, 0001_0110 game
|
|
280
|
+
chunk.sys = Boolean(packet[0] & 1); // & 1 = binary, ****_***1. e.g 0001_0111 sys, 0001_0110 game
|
|
281
|
+
chunk.msgid = (packet[0] - (packet[0] & 1)) / 2;
|
|
282
|
+
chunk.msg = messageTypes[packet[0] & 1][chunk.msgid];
|
|
283
|
+
chunk.raw = packet.slice(1, chunk.bytes);
|
|
284
|
+
if (chunk.msgid == 0) {
|
|
285
|
+
let uuid = this.UUIDManager.LookupUUID(chunk.raw.slice(0, 16));
|
|
286
|
+
if (uuid !== undefined) {
|
|
287
|
+
chunk.extended_msgid = uuid.hash;
|
|
288
|
+
chunk.msgid = uuid.type_id;
|
|
289
|
+
chunk.msg = uuid.name;
|
|
290
|
+
chunk.raw = chunk.raw.slice(16);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return chunk;
|
|
294
|
+
}
|
|
295
|
+
Flush() {
|
|
296
|
+
// if (this.queueChunkEx.length == 0)
|
|
297
|
+
console.log("flushing");
|
|
298
|
+
this.SendMsgEx(this.queueChunkEx);
|
|
299
|
+
this.queueChunkEx = [];
|
|
300
|
+
this.ack = this.lastCheckedChunkAck;
|
|
301
|
+
}
|
|
302
|
+
/** Connect the client to the server. */
|
|
303
|
+
connect() {
|
|
304
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
305
|
+
var _a;
|
|
306
|
+
// test via regex whether or not this.host is a domain or an ip
|
|
307
|
+
// if not, resolve it
|
|
308
|
+
if (!this.host.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
|
|
309
|
+
(0, dns_1.lookup)(this.host, 4, (err, address, family) => {
|
|
310
|
+
if (err)
|
|
311
|
+
throw err;
|
|
312
|
+
this.host = address;
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
this.State = protocol_1.States.STATE_CONNECTING;
|
|
316
|
+
let predTimer = setInterval(() => {
|
|
317
|
+
if (this.State == protocol_1.States.STATE_ONLINE) {
|
|
318
|
+
if (this.AckGameTick > 0)
|
|
319
|
+
this.PredGameTick++;
|
|
320
|
+
}
|
|
321
|
+
else if (this.State == protocol_1.States.STATE_OFFLINE)
|
|
322
|
+
clearInterval(predTimer);
|
|
323
|
+
}, 1000 / 50); // 50 ticks per second
|
|
324
|
+
this.SendControlMsg(1, "TKEN");
|
|
325
|
+
let connectInterval = setInterval(() => {
|
|
326
|
+
if (this.State == protocol_1.States.STATE_CONNECTING)
|
|
327
|
+
this.SendControlMsg(1, "TKEN");
|
|
328
|
+
else
|
|
329
|
+
clearInterval(connectInterval);
|
|
330
|
+
}, 500);
|
|
331
|
+
let inputInterval;
|
|
332
|
+
if (!((_a = this.options) === null || _a === void 0 ? void 0 : _a.lightweight)) {
|
|
333
|
+
inputInterval = setInterval(() => {
|
|
334
|
+
if (this.State == protocol_1.States.STATE_OFFLINE) {
|
|
335
|
+
clearInterval(inputInterval);
|
|
336
|
+
// console.log("???");
|
|
337
|
+
}
|
|
338
|
+
if (this.State != protocol_1.States.STATE_ONLINE)
|
|
339
|
+
return;
|
|
340
|
+
this.time = new Date().getTime();
|
|
341
|
+
this.sendInput();
|
|
342
|
+
}, 50);
|
|
343
|
+
}
|
|
344
|
+
let resendTimeout = setInterval(() => {
|
|
345
|
+
if (this.State != protocol_1.States.STATE_OFFLINE) {
|
|
346
|
+
if (((new Date().getTime()) - this.lastSendTime) > 900 && this.sentChunkQueue.length > 0) {
|
|
347
|
+
this.SendMsgRaw([this.sentChunkQueue[0]]);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
else
|
|
351
|
+
clearInterval(resendTimeout);
|
|
352
|
+
}, 1000);
|
|
353
|
+
let Timeout = setInterval(() => {
|
|
354
|
+
var _a;
|
|
355
|
+
let timeoutTime = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.timeout) ? this.options.timeout : 15000;
|
|
356
|
+
if ((new Date().getTime() - this.lastRecvTime) > timeoutTime) {
|
|
357
|
+
this.State = protocol_1.States.STATE_OFFLINE;
|
|
358
|
+
this.emit("disconnect", "Timed Out. (no packets received for " + (new Date().getTime() - this.lastRecvTime) + "ms)");
|
|
359
|
+
clearInterval(Timeout);
|
|
360
|
+
}
|
|
361
|
+
}, 5000);
|
|
362
|
+
this.time = new Date().getTime() + 2000; // start sending keepalives after 2s
|
|
363
|
+
if (this.socket)
|
|
364
|
+
this.socket.on("message", (packet, rinfo) => {
|
|
365
|
+
var _a, _b, _c, _d, _e, _f;
|
|
366
|
+
if (this.State == 0 || rinfo.address != this.host || rinfo.port != this.port)
|
|
367
|
+
return;
|
|
368
|
+
clearInterval(connectInterval);
|
|
369
|
+
if (packet[0] == 0x10) {
|
|
370
|
+
if (packet.toString().includes("TKEN") || packet[3] == 0x2) {
|
|
371
|
+
clearInterval(connectInterval);
|
|
372
|
+
this.TKEN = packet.slice(-4);
|
|
373
|
+
this.SendControlMsg(3);
|
|
374
|
+
this.State = protocol_1.States.STATE_LOADING; // loading state
|
|
375
|
+
this.receivedSnaps = 0;
|
|
376
|
+
var info = new MsgPacker_1.MsgPacker(1, true, 1);
|
|
377
|
+
info.AddString(((_a = this.options) === null || _a === void 0 ? void 0 : _a.NET_VERSION) ? this.options.NET_VERSION : "0.6 626fce9a778df4d4");
|
|
378
|
+
info.AddString(((_b = this.options) === null || _b === void 0 ? void 0 : _b.password) === undefined ? "" : (_c = this.options) === null || _c === void 0 ? void 0 : _c.password); // password
|
|
379
|
+
var client_version = new MsgPacker_1.MsgPacker(0, true, 1);
|
|
380
|
+
client_version.AddBuffer(Buffer.from("8c00130484613e478787f672b3835bd4", 'hex'));
|
|
381
|
+
let randomUuid = (0, crypto_1.randomBytes)(16);
|
|
382
|
+
client_version.AddBuffer(randomUuid);
|
|
383
|
+
if (((_d = this.options) === null || _d === void 0 ? void 0 : _d.ddnet_version) !== undefined) {
|
|
384
|
+
client_version.AddInt((_e = this.options) === null || _e === void 0 ? void 0 : _e.ddnet_version.version);
|
|
385
|
+
client_version.AddString(`DDNet ${(_f = this.options) === null || _f === void 0 ? void 0 : _f.ddnet_version.release_version}; https://www.npmjs.com/package/teeworlds/v/${package_json_1.version}`);
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
client_version.AddInt(16050);
|
|
389
|
+
client_version.AddString(`DDNet 16.5.0; https://www.npmjs.com/package/teeworlds/v/${package_json_1.version}`);
|
|
390
|
+
}
|
|
391
|
+
var i_am_npm_package = new MsgPacker_1.MsgPacker(0, true, 1);
|
|
392
|
+
i_am_npm_package.AddBuffer(this.UUIDManager.LookupType(65556 /* NETMSG.System.NETMSG_I_AM_NPM_PACKAGE */).hash);
|
|
393
|
+
i_am_npm_package.AddString(`https://www.npmjs.com/package/teeworlds/v/${package_json_1.version}`);
|
|
394
|
+
this.SendMsgEx([i_am_npm_package, client_version, info]);
|
|
395
|
+
}
|
|
396
|
+
else if (packet[3] == 0x4) {
|
|
397
|
+
// disconnected
|
|
398
|
+
this.State = protocol_1.States.STATE_OFFLINE;
|
|
399
|
+
let reason = ((0, MsgUnpacker_1.unpackString)(packet.slice(4)).result);
|
|
400
|
+
this.emit("disconnect", reason);
|
|
401
|
+
}
|
|
402
|
+
if (packet[3] !== 0x0) { // keepalive
|
|
403
|
+
this.lastRecvTime = new Date().getTime();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
this.lastRecvTime = new Date().getTime();
|
|
408
|
+
}
|
|
409
|
+
var unpacked = this.Unpack(packet);
|
|
410
|
+
// unpacked.chunks = unpacked.chunks.filter(chunk => ((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq! > this.ack : true); // filter out already received chunks
|
|
411
|
+
this.sentChunkQueue.forEach((buff, i) => {
|
|
412
|
+
let chunkFlags = (buff[0] >> 6) & 3;
|
|
413
|
+
if (chunkFlags & 1) {
|
|
414
|
+
let chunk = this.MsgToChunk(buff);
|
|
415
|
+
if (chunk.seq && chunk.seq >= this.ack)
|
|
416
|
+
this.sentChunkQueue.splice(i, 1);
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
unpacked.chunks.forEach(chunk => {
|
|
420
|
+
var _a, _b;
|
|
421
|
+
if (!(((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq > this.ack : true))
|
|
422
|
+
return; // filter out already received chunks
|
|
423
|
+
if (chunk.flags & 1 && (chunk.flags !== 15)) { // vital and not connless
|
|
424
|
+
this.lastCheckedChunkAck = chunk.seq;
|
|
425
|
+
if (chunk.seq === (this.ack + 1) % (1 << 10)) { // https://github.com/nobody-mb/twchatonly/blob/master/chatonly.cpp#L237
|
|
426
|
+
this.ack = chunk.seq;
|
|
427
|
+
this.requestResend = false;
|
|
428
|
+
}
|
|
429
|
+
else { //IsSeqInBackroom (old packet that we already got)
|
|
430
|
+
let Bottom = (this.ack - (1 << 10) / 2);
|
|
431
|
+
if (Bottom < 0) {
|
|
432
|
+
if ((chunk.seq <= this.ack) || (chunk.seq >= (Bottom + (1 << 10)))) { }
|
|
433
|
+
else
|
|
434
|
+
this.requestResend = true;
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
if (chunk.seq <= this.ack && chunk.seq >= Bottom) { }
|
|
438
|
+
else
|
|
439
|
+
this.requestResend = true;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (chunk.sys) {
|
|
444
|
+
// system messages
|
|
445
|
+
if (chunk.msgid == 22 /* NETMSG.System.NETMSG_PING */) { // ping
|
|
446
|
+
let packer = new MsgPacker_1.MsgPacker(23 /* NETMSG.System.NETMSG_PING_REPLY */, true, 0);
|
|
447
|
+
this.SendMsgEx(packer); // send ping reply
|
|
448
|
+
}
|
|
449
|
+
else if (chunk.msgid == 23 /* NETMSG.System.NETMSG_PING_REPLY */) { // Ping reply
|
|
450
|
+
this.game._ping_resolve(new Date().getTime());
|
|
451
|
+
}
|
|
452
|
+
else if (this.rcon._checkChunks(chunk)) { }
|
|
453
|
+
// packets neccessary for connection
|
|
454
|
+
// https://ddnet.org/docs/libtw2/connection/
|
|
455
|
+
if (chunk.msgid == 2 /* NETMSG.System.NETMSG_MAP_CHANGE */) {
|
|
456
|
+
this.Flush();
|
|
457
|
+
var Msg = new MsgPacker_1.MsgPacker(14 /* NETMSG.System.NETMSG_READY */, true, 1); /* ready */
|
|
458
|
+
this.SendMsgEx(Msg);
|
|
459
|
+
}
|
|
460
|
+
else if (chunk.msgid == 4 /* NETMSG.System.NETMSG_CON_READY */) {
|
|
461
|
+
var info = new MsgPacker_1.MsgPacker(20 /* NETMSG.Game.CL_STARTINFO */, false, 1);
|
|
462
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.identity) {
|
|
463
|
+
info.AddString(this.options.identity.name);
|
|
464
|
+
info.AddString(this.options.identity.clan);
|
|
465
|
+
info.AddInt(this.options.identity.country);
|
|
466
|
+
info.AddString(this.options.identity.skin);
|
|
467
|
+
info.AddInt(this.options.identity.use_custom_color);
|
|
468
|
+
info.AddInt(this.options.identity.color_body);
|
|
469
|
+
info.AddInt(this.options.identity.color_feet);
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
info.AddString(this.name); /* name */
|
|
473
|
+
info.AddString(""); /* clan */
|
|
474
|
+
info.AddInt(-1); /* country */
|
|
475
|
+
info.AddString("greyfox"); /* skin */
|
|
476
|
+
info.AddInt(1); /* use custom color */
|
|
477
|
+
info.AddInt(10346103); /* color body */
|
|
478
|
+
info.AddInt(65535); /* color feet */
|
|
479
|
+
}
|
|
480
|
+
var crashmeplx = new MsgPacker_1.MsgPacker(17, true, 1); // rcon
|
|
481
|
+
crashmeplx.AddString("crashmeplx"); // 64 player support message
|
|
482
|
+
this.SendMsgEx([info, crashmeplx]);
|
|
483
|
+
}
|
|
484
|
+
if (chunk.msgid >= 5 /* NETMSG.System.NETMSG_SNAP */ && chunk.msgid <= 7 /* NETMSG.System.NETMSG_SNAPSINGLE */) {
|
|
485
|
+
this.receivedSnaps++; /* wait for 2 ss before seeing self as connected */
|
|
486
|
+
if (this.receivedSnaps == 2) {
|
|
487
|
+
if (this.State != protocol_1.States.STATE_ONLINE)
|
|
488
|
+
this.emit('connected');
|
|
489
|
+
this.State = protocol_1.States.STATE_ONLINE;
|
|
490
|
+
}
|
|
491
|
+
if (Math.abs(this.PredGameTick - this.AckGameTick) > 10)
|
|
492
|
+
this.PredGameTick = this.AckGameTick + 1;
|
|
493
|
+
// snapChunks.forEach(chunk => {
|
|
494
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
495
|
+
let NumParts = 1;
|
|
496
|
+
let Part = 0;
|
|
497
|
+
let GameTick = unpacker.unpackInt();
|
|
498
|
+
let DeltaTick = GameTick - unpacker.unpackInt();
|
|
499
|
+
let PartSize = 0;
|
|
500
|
+
let Crc = 0;
|
|
501
|
+
let CompleteSize = 0;
|
|
502
|
+
if (chunk.msgid == 5 /* NETMSG.System.NETMSG_SNAP */) {
|
|
503
|
+
NumParts = unpacker.unpackInt();
|
|
504
|
+
Part = unpacker.unpackInt();
|
|
505
|
+
}
|
|
506
|
+
if (chunk.msgid != 6 /* NETMSG.System.NETMSG_SNAPEMPTY */) {
|
|
507
|
+
Crc = unpacker.unpackInt();
|
|
508
|
+
PartSize = unpacker.unpackInt();
|
|
509
|
+
}
|
|
510
|
+
if (NumParts < 1 || NumParts > 64 || Part < 0 || Part >= NumParts || PartSize < 0 || PartSize > 900)
|
|
511
|
+
return;
|
|
512
|
+
if (GameTick >= this.currentSnapshotGameTick) {
|
|
513
|
+
if (GameTick != this.currentSnapshotGameTick) {
|
|
514
|
+
this.snaps = [];
|
|
515
|
+
this.SnapshotParts = 0;
|
|
516
|
+
this.currentSnapshotGameTick = GameTick;
|
|
517
|
+
}
|
|
518
|
+
// chunk.raw = Buffer.from(unpacker.remaining);
|
|
519
|
+
this.snaps[Part] = unpacker.remaining;
|
|
520
|
+
this.SnapshotParts |= 1 << Part;
|
|
521
|
+
if (this.SnapshotParts == ((1 << NumParts) - 1)) {
|
|
522
|
+
let mergedSnaps = Buffer.concat(this.snaps);
|
|
523
|
+
this.SnapshotParts = 0;
|
|
524
|
+
let snapUnpacked = this.SnapUnpacker.unpackSnapshot(mergedSnaps, DeltaTick, GameTick, Crc);
|
|
525
|
+
this.emit("snapshot", snapUnpacked.items);
|
|
526
|
+
this.AckGameTick = snapUnpacked.recvTick;
|
|
527
|
+
if (Math.abs(this.PredGameTick - this.AckGameTick) > 10) {
|
|
528
|
+
this.PredGameTick = this.AckGameTick + 1;
|
|
529
|
+
this.sendInput();
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
// })
|
|
534
|
+
}
|
|
535
|
+
if (chunk.msgid >= 65536 /* NETMSG.System.NETMSG_WHATIS */ && chunk.msgid <= 65547 /* NETMSG.System.NETMSG_CHECKSUM_ERROR */) {
|
|
536
|
+
if (chunk.msgid == 65536 /* NETMSG.System.NETMSG_WHATIS */) {
|
|
537
|
+
let Uuid = chunk.raw.slice(0, 16);
|
|
538
|
+
let uuid = this.UUIDManager.LookupUUID(Uuid);
|
|
539
|
+
let packer = new MsgPacker_1.MsgPacker(0, true, 1);
|
|
540
|
+
if (uuid !== undefined) {
|
|
541
|
+
// IT_IS msg
|
|
542
|
+
packer.AddBuffer(this.UUIDManager.LookupType(65537 /* NETMSG.System.NETMSG_ITIS */).hash);
|
|
543
|
+
packer.AddBuffer(Uuid);
|
|
544
|
+
packer.AddString(uuid.name);
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
// dont_know msg
|
|
548
|
+
packer.AddBuffer(this.UUIDManager.LookupType(65538 /* NETMSG.System.NETMSG_IDONTKNOW */).hash);
|
|
549
|
+
packer.AddBuffer(Uuid);
|
|
550
|
+
}
|
|
551
|
+
this.QueueChunkEx(packer);
|
|
552
|
+
}
|
|
553
|
+
if (chunk.msgid == 65540 /* NETMSG.System.NETMSG_MAP_DETAILS */) { // TODO: option for downloading maps
|
|
554
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
555
|
+
let map_name = unpacker.unpackString();
|
|
556
|
+
let map_sha256 = Buffer.alloc(32);
|
|
557
|
+
if (unpacker.remaining.length >= 32)
|
|
558
|
+
map_sha256 = unpacker.unpackRaw(32);
|
|
559
|
+
let map_crc = unpacker.unpackInt();
|
|
560
|
+
let map_size = unpacker.unpackInt();
|
|
561
|
+
let map_url = "";
|
|
562
|
+
if (unpacker.remaining.length)
|
|
563
|
+
map_url = unpacker.unpackString();
|
|
564
|
+
this.emit("map_details", { map_name, map_sha256, map_crc, map_size, map_url });
|
|
565
|
+
// unpacker.unpack
|
|
566
|
+
}
|
|
567
|
+
else if (chunk.msgid == 65541 /* NETMSG.System.NETMSG_CAPABILITIES */) {
|
|
568
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
569
|
+
let Version = unpacker.unpackInt();
|
|
570
|
+
let Flags = unpacker.unpackInt();
|
|
571
|
+
if (Version <= 0)
|
|
572
|
+
return;
|
|
573
|
+
let DDNet = false;
|
|
574
|
+
if (Version >= 1) {
|
|
575
|
+
DDNet = Boolean(Flags & 1);
|
|
576
|
+
}
|
|
577
|
+
let ChatTimeoutCode = DDNet;
|
|
578
|
+
let AnyPlayerFlag = DDNet;
|
|
579
|
+
let PingEx = false;
|
|
580
|
+
let AllowDummy = true;
|
|
581
|
+
let SyncWeaponInput = false;
|
|
582
|
+
if (Version >= 1) {
|
|
583
|
+
ChatTimeoutCode = Boolean(Flags & 2);
|
|
584
|
+
}
|
|
585
|
+
if (Version >= 2) {
|
|
586
|
+
AnyPlayerFlag = Boolean(Flags & 4);
|
|
587
|
+
}
|
|
588
|
+
if (Version >= 3) {
|
|
589
|
+
PingEx = Boolean(Flags & 8);
|
|
590
|
+
}
|
|
591
|
+
if (Version >= 4) {
|
|
592
|
+
AllowDummy = Boolean(Flags & 16);
|
|
593
|
+
}
|
|
594
|
+
if (Version >= 5) {
|
|
595
|
+
SyncWeaponInput = Boolean(Flags & 32);
|
|
596
|
+
}
|
|
597
|
+
this.emit("capabilities", { ChatTimeoutCode, AnyPlayerFlag, PingEx, AllowDummy, SyncWeaponInput });
|
|
598
|
+
// https://github.com/ddnet/ddnet/blob/06e3eb564150e9ab81b3a5595c48e9fe5952ed32/src/engine/client/client.cpp#L1565
|
|
599
|
+
}
|
|
600
|
+
else if (chunk.msgid == 65543 /* NETMSG.System.NETMSG_PINGEX */) {
|
|
601
|
+
let packer = new MsgPacker_1.MsgPacker(0, true, 2);
|
|
602
|
+
packer.AddBuffer(this.UUIDManager.LookupType(65544 /* NETMSG.System.NETMSG_PONGEX */).hash);
|
|
603
|
+
this.SendMsgEx(packer, 2);
|
|
604
|
+
}
|
|
605
|
+
else if (chunk.msgid == 65552 /* NETMSG.System.NETMSG_RECONNECT */) {
|
|
606
|
+
this.SendControlMsg(4); // sends disconnect packet
|
|
607
|
+
clearInterval(predTimer);
|
|
608
|
+
clearInterval(inputInterval);
|
|
609
|
+
clearInterval(resendTimeout);
|
|
610
|
+
clearInterval(Timeout);
|
|
611
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.removeAllListeners("message");
|
|
612
|
+
this.connect();
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
// game messages
|
|
619
|
+
// vote list:
|
|
620
|
+
if (chunk.msgid == 11 /* NETMSG.Game.SV_VOTECLEAROPTIONS */) {
|
|
621
|
+
this.VoteList = [];
|
|
622
|
+
}
|
|
623
|
+
else if (chunk.msgid == 12 /* NETMSG.Game.SV_VOTEOPTIONLISTADD */) {
|
|
624
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
625
|
+
let NumOptions = unpacker.unpackInt();
|
|
626
|
+
let list = [];
|
|
627
|
+
for (let i = 0; i < 15; i++) {
|
|
628
|
+
list.push(unpacker.unpackString());
|
|
629
|
+
}
|
|
630
|
+
list = list.slice(0, NumOptions);
|
|
631
|
+
this.VoteList.push(...list);
|
|
632
|
+
}
|
|
633
|
+
else if (chunk.msgid == 13 /* NETMSG.Game.SV_VOTEOPTIONADD */) {
|
|
634
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
635
|
+
this.VoteList.push(unpacker.unpackString());
|
|
636
|
+
}
|
|
637
|
+
else if (chunk.msgid == 14 /* NETMSG.Game.SV_VOTEOPTIONREMOVE */) {
|
|
638
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
639
|
+
let index = this.VoteList.indexOf(unpacker.unpackString());
|
|
640
|
+
if (index > -1)
|
|
641
|
+
this.VoteList = this.VoteList.splice(index, 1);
|
|
642
|
+
}
|
|
643
|
+
// events
|
|
644
|
+
if (chunk.msgid == 10 /* NETMSG.Game.SV_EMOTICON */) {
|
|
645
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
646
|
+
let unpacked = {
|
|
647
|
+
client_id: unpacker.unpackInt(),
|
|
648
|
+
emoticon: unpacker.unpackInt()
|
|
649
|
+
};
|
|
650
|
+
if (unpacked.client_id != -1) {
|
|
651
|
+
unpacked.author = {
|
|
652
|
+
ClientInfo: this.SnapshotUnpacker.getObjClientInfo(unpacked.client_id),
|
|
653
|
+
PlayerInfo: this.SnapshotUnpacker.getObjPlayerInfo(unpacked.client_id)
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
this.emit("emote", unpacked);
|
|
657
|
+
}
|
|
658
|
+
else if (chunk.msgid == 2 /* NETMSG.Game.SV_BROADCAST */) {
|
|
659
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
660
|
+
this.emit("broadcast", unpacker.unpackString());
|
|
661
|
+
}
|
|
662
|
+
if (chunk.msgid == 3 /* NETMSG.Game.SV_CHAT */) {
|
|
663
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
664
|
+
let unpacked = {
|
|
665
|
+
team: unpacker.unpackInt(),
|
|
666
|
+
client_id: unpacker.unpackInt(),
|
|
667
|
+
message: unpacker.unpackString()
|
|
668
|
+
};
|
|
669
|
+
if (unpacked.client_id != -1) {
|
|
670
|
+
unpacked.author = {
|
|
671
|
+
ClientInfo: this.SnapshotUnpacker.getObjClientInfo(unpacked.client_id),
|
|
672
|
+
PlayerInfo: this.SnapshotUnpacker.getObjPlayerInfo(unpacked.client_id)
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
this.emit("message", unpacked);
|
|
676
|
+
}
|
|
677
|
+
else if (chunk.msgid == 4 /* NETMSG.Game.SV_KILLMSG */) {
|
|
678
|
+
let unpacked = {};
|
|
679
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
680
|
+
unpacked.killer_id = unpacker.unpackInt();
|
|
681
|
+
unpacked.victim_id = unpacker.unpackInt();
|
|
682
|
+
unpacked.weapon = unpacker.unpackInt();
|
|
683
|
+
unpacked.special_mode = unpacker.unpackInt();
|
|
684
|
+
if (unpacked.victim_id != -1 && unpacked.victim_id < 64) {
|
|
685
|
+
unpacked.victim = { ClientInfo: this.SnapshotUnpacker.getObjClientInfo(unpacked.victim_id), PlayerInfo: this.SnapshotUnpacker.getObjPlayerInfo(unpacked.victim_id) };
|
|
686
|
+
}
|
|
687
|
+
if (unpacked.killer_id != -1 && unpacked.killer_id < 64)
|
|
688
|
+
unpacked.killer = { ClientInfo: this.SnapshotUnpacker.getObjClientInfo(unpacked.killer_id), PlayerInfo: this.SnapshotUnpacker.getObjPlayerInfo(unpacked.killer_id) };
|
|
689
|
+
this.emit("kill", unpacked);
|
|
690
|
+
}
|
|
691
|
+
else if (chunk.msgid == 1 /* NETMSG.Game.SV_MOTD */) {
|
|
692
|
+
let unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
|
|
693
|
+
let message = unpacker.unpackString();
|
|
694
|
+
this.emit("motd", message);
|
|
695
|
+
}
|
|
696
|
+
// packets neccessary for connection
|
|
697
|
+
// https://ddnet.org/docs/libtw2/connection/
|
|
698
|
+
if (chunk.msgid == 8 /* NETMSG.Game.SV_READYTOENTER */) {
|
|
699
|
+
var Msg = new MsgPacker_1.MsgPacker(15 /* NETMSG.System.NETMSG_ENTERGAME */, true, 1); /* entergame */
|
|
700
|
+
this.SendMsgEx(Msg);
|
|
701
|
+
this.OnEnterGame();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
if (this.State == protocol_1.States.STATE_ONLINE) {
|
|
706
|
+
if (new Date().getTime() - this.time >= 500) {
|
|
707
|
+
this.Flush();
|
|
708
|
+
}
|
|
709
|
+
if (new Date().getTime() - this.time >= 1000) {
|
|
710
|
+
this.time = new Date().getTime();
|
|
711
|
+
this.SendControlMsg(0);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
/** Sending the input. (automatically done unless options.lightweight is on) */
|
|
718
|
+
sendInput(input = this.movement.input) {
|
|
719
|
+
if (this.State != protocol_1.States.STATE_ONLINE)
|
|
720
|
+
return;
|
|
721
|
+
let inputMsg = new MsgPacker_1.MsgPacker(16, true, 0);
|
|
722
|
+
inputMsg.AddInt(this.AckGameTick);
|
|
723
|
+
inputMsg.AddInt(this.PredGameTick);
|
|
724
|
+
inputMsg.AddInt(40);
|
|
725
|
+
inputMsg.AddInt(input.m_Direction);
|
|
726
|
+
inputMsg.AddInt(input.m_TargetX);
|
|
727
|
+
inputMsg.AddInt(input.m_TargetY);
|
|
728
|
+
inputMsg.AddInt(input.m_Jump);
|
|
729
|
+
inputMsg.AddInt(input.m_Fire);
|
|
730
|
+
inputMsg.AddInt(input.m_Hook);
|
|
731
|
+
inputMsg.AddInt(input.m_PlayerFlags);
|
|
732
|
+
inputMsg.AddInt(input.m_WantedWeapon);
|
|
733
|
+
inputMsg.AddInt(input.m_NextWeapon);
|
|
734
|
+
inputMsg.AddInt(input.m_PrevWeapon);
|
|
735
|
+
this.SendMsgEx(inputMsg);
|
|
736
|
+
}
|
|
737
|
+
/** returns the movement object of the client */
|
|
738
|
+
get input() {
|
|
739
|
+
return this.movement.input;
|
|
740
|
+
}
|
|
741
|
+
/** Disconnect the client. */
|
|
742
|
+
Disconnect() {
|
|
743
|
+
return new Promise((resolve) => {
|
|
744
|
+
this.SendControlMsg(4).then(() => {
|
|
745
|
+
resolve(true);
|
|
746
|
+
if (this.socket)
|
|
747
|
+
this.socket.close();
|
|
748
|
+
this.socket = undefined;
|
|
749
|
+
this.State = protocol_1.States.STATE_OFFLINE;
|
|
750
|
+
});
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
/** Get all available vote options (for example for map voting) */
|
|
754
|
+
get VoteOptionList() {
|
|
755
|
+
return this.VoteList;
|
|
756
|
+
}
|
|
757
|
+
get rawSnapUnpacker() {
|
|
758
|
+
return this.SnapUnpacker;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
exports.Client = Client;
|
|
762
|
+
//# sourceMappingURL=client.js.map
|