teeworlds 2.5.1 → 2.5.3

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/index.js CHANGED
@@ -10,23 +10,11 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
15
  };
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod;
23
- var result = {};
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
- __setModuleDefault(result, mod);
26
- return result;
27
- };
28
16
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.snapshots = exports.Client = void 0;
17
+ exports.Client = void 0;
30
18
  var client_1 = require("./lib/client");
31
19
  Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
32
20
  __exportStar(require("./lib/MsgPacker"), exports);
@@ -34,4 +22,3 @@ __exportStar(require("./lib/MsgUnpacker"), exports);
34
22
  __exportStar(require("./lib/snapshot"), exports);
35
23
  __exportStar(require("./lib/huffman"), exports);
36
24
  __exportStar(require("./lib/UUIDManager"), exports);
37
- exports.snapshots = __importStar(require("./lib/snapshots"));
package/index.ts CHANGED
@@ -3,5 +3,4 @@ export * from "./lib/MsgPacker";
3
3
  export * from "./lib/MsgUnpacker";
4
4
  export * from "./lib/snapshot";
5
5
  export * from "./lib/huffman"
6
- export * from "./lib/UUIDManager"
7
- export * as snapshots from "./lib/snapshots"
6
+ export * from "./lib/UUIDManager"
package/lib/client.js CHANGED
@@ -156,6 +156,7 @@ var Client = /** @class */ (function (_super) {
156
156
  _this.State = States.STATE_OFFLINE; // 0 = offline; 1 = STATE_CONNECTING = 1, STATE_LOADING = 2, STATE_ONLINE = 3
157
157
  _this.ack = 0; // ack of messages the client has received
158
158
  _this.clientAck = 0; // ack of messages the client has sent
159
+ _this.lastCheckedChunkAck = 0; // this.ack gets reset to this when flushing - used for resetting tick on e.g. map change
159
160
  _this.receivedSnaps = 0; /* wait for 2 snaps before seeing self as connected */
160
161
  _this.socket = dgram_1.default.createSocket("udp4");
161
162
  _this.socket.bind();
@@ -184,6 +185,16 @@ var Client = /** @class */ (function (_super) {
184
185
  _this.UUIDManager.RegisterName("i-am-npm-package@swarfey.gitlab.io", NETMSG_Sys.NETMSG_I_AM_NPM_PACKAGE);
185
186
  return _this;
186
187
  }
188
+ Client.prototype.OnEnterGame = function () {
189
+ this.snaps = [];
190
+ this.SnapUnpacker = new snapshot_1.Snapshot(this);
191
+ this.SnapshotParts = 0;
192
+ this.receivedSnaps = 0;
193
+ this.SnapshotUnpacker = new snapshot_2.SnapshotWrapper(this);
194
+ this.currentSnapshotGameTick = 0;
195
+ this.AckGameTick = -1;
196
+ this.PredGameTick = 0;
197
+ };
187
198
  Client.prototype.ResendAfter = function (lastAck) {
188
199
  this.clientAck = lastAck;
189
200
  var toResend = [];
@@ -211,7 +222,6 @@ var Client = /** @class */ (function (_super) {
211
222
  var chunk = {};
212
223
  chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1));
213
224
  chunk.flags = (packet[0] >> 6) & 3;
214
- chunk.sequence = -1;
215
225
  if (chunk.flags & 1) {
216
226
  chunk.seq = ((packet[1] & 0xf0) << 2) | packet[2];
217
227
  packet = packet.slice(3); // remove flags & size
@@ -317,9 +327,20 @@ var Client = /** @class */ (function (_super) {
317
327
  return;
318
328
  this.socket.send(packet, 0, packet.length, this.port, this.host);
319
329
  };
320
- /** Queue a chunk (It will get sent in the next packet). */
330
+ /** Queue a chunk (instantly sent if flush flag is set - otherwise it will be sent in the next packet). */
321
331
  Client.prototype.QueueChunkEx = function (Msg) {
332
+ if (this.queueChunkEx.length > 0) {
333
+ var total_size = 0;
334
+ for (var _i = 0, _a = this.queueChunkEx; _i < _a.length; _i++) {
335
+ var chunk = _a[_i];
336
+ total_size += chunk.size;
337
+ }
338
+ if (total_size + Msg.size + 3 > 1394 - 4)
339
+ this.Flush();
340
+ }
322
341
  this.queueChunkEx.push(Msg);
342
+ if (Msg.flag & 4)
343
+ this.Flush();
323
344
  };
324
345
  /** Send a Raw Buffer (as chunk) to the server. */
325
346
  Client.prototype.SendMsgRaw = function (chunks) {
@@ -338,7 +359,6 @@ var Client = /** @class */ (function (_super) {
338
359
  var chunk = {};
339
360
  chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1));
340
361
  chunk.flags = (packet[0] >> 6) & 3;
341
- chunk.sequence = -1;
342
362
  if (chunk.flags & 1) {
343
363
  chunk.seq = ((packet[1] & 0xf0) << 2) | packet[2];
344
364
  packet = packet.slice(3); // remove flags & size
@@ -361,6 +381,13 @@ var Client = /** @class */ (function (_super) {
361
381
  }
362
382
  return chunk;
363
383
  };
384
+ Client.prototype.Flush = function () {
385
+ // if (this.queueChunkEx.length == 0)
386
+ console.log("flushing");
387
+ this.SendMsgEx(this.queueChunkEx);
388
+ this.queueChunkEx = [];
389
+ this.ack = this.lastCheckedChunkAck;
390
+ };
364
391
  /** Connect the client to the server. */
365
392
  Client.prototype.connect = function () {
366
393
  var _this = this;
@@ -383,8 +410,10 @@ var Client = /** @class */ (function (_super) {
383
410
  }, 500);
384
411
  if (!((_a = this.options) === null || _a === void 0 ? void 0 : _a.lightweight)) {
385
412
  var inputInterval_1 = setInterval(function () {
386
- if (_this.State == States.STATE_OFFLINE)
413
+ if (_this.State == States.STATE_OFFLINE) {
387
414
  clearInterval(inputInterval_1);
415
+ console.log("???");
416
+ }
388
417
  if (_this.State != States.STATE_ONLINE)
389
418
  return;
390
419
  _this.time = new Date().getTime();
@@ -458,9 +487,22 @@ var Client = /** @class */ (function (_super) {
458
487
  _this.lastRecvTime = new Date().getTime();
459
488
  }
460
489
  var unpacked = _this.Unpack(packet);
461
- unpacked.chunks = unpacked.chunks.filter(function (chunk) { return ((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq > _this.ack : true; }); // filter out already received chunks
490
+ // unpacked.chunks = unpacked.chunks.filter(chunk => ((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq! > this.ack : true); // filter out already received chunks
491
+ _this.sentChunkQueue.forEach(function (buff, i) {
492
+ var chunkFlags = (buff[0] >> 6) & 3;
493
+ if (chunkFlags & 1) {
494
+ var chunk = _this.MsgToChunk(buff);
495
+ if (chunk.seq && chunk.seq >= _this.ack)
496
+ _this.sentChunkQueue.splice(i, 1);
497
+ }
498
+ });
462
499
  unpacked.chunks.forEach(function (chunk) {
500
+ var _a;
501
+ var _b;
502
+ if (!(((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq > _this.ack : true))
503
+ return; // filter out already received chunks
463
504
  if (chunk.flags & 1 && (chunk.flags !== 15)) { // vital and not connless
505
+ _this.lastCheckedChunkAck = chunk.seq;
464
506
  if (chunk.seq === (_this.ack + 1) % (1 << 10)) { // https://github.com/nobody-mb/twchatonly/blob/master/chatonly.cpp#L237
465
507
  _this.ack = chunk.seq;
466
508
  _this.requestResend = false;
@@ -468,28 +510,17 @@ var Client = /** @class */ (function (_super) {
468
510
  else { //IsSeqInBackroom (old packet that we already got)
469
511
  var Bottom = (_this.ack - (1 << 10) / 2);
470
512
  if (Bottom < 0) {
471
- if ((chunk.seq <= _this.ack) || (chunk.seq >= (Bottom + (1 << 10))))
472
- return;
513
+ if ((chunk.seq <= _this.ack) || (chunk.seq >= (Bottom + (1 << 10)))) { }
514
+ else
515
+ _this.requestResend = true;
473
516
  }
474
517
  else {
475
- if (chunk.seq <= _this.ack && chunk.seq >= Bottom)
476
- return;
518
+ if (chunk.seq <= _this.ack && chunk.seq >= Bottom) { }
519
+ else
520
+ _this.requestResend = true;
477
521
  }
478
- _this.requestResend = true;
479
522
  }
480
523
  }
481
- });
482
- _this.sentChunkQueue.forEach(function (buff, i) {
483
- var chunkFlags = (buff[0] >> 6) & 3;
484
- if (chunkFlags & 1) {
485
- var chunk = _this.MsgToChunk(buff);
486
- if (chunk.seq && chunk.seq >= _this.ack)
487
- _this.sentChunkQueue.splice(i, 1);
488
- }
489
- });
490
- unpacked.chunks.forEach(function (chunk, index) {
491
- var _a;
492
- var _b;
493
524
  if (chunk.sys) {
494
525
  // system messages
495
526
  if (chunk.msgid == NETMSG_Sys.NETMSG_PING) { // ping
@@ -502,6 +533,7 @@ var Client = /** @class */ (function (_super) {
502
533
  // packets neccessary for connection
503
534
  // https://ddnet.org/docs/libtw2/connection/
504
535
  if (chunk.msgid == NETMSG_Sys.NETMSG_MAP_CHANGE) {
536
+ _this.Flush();
505
537
  var Msg = new MsgPacker_1.MsgPacker(NETMSG_Sys.NETMSG_READY, true, 1); /* ready */
506
538
  _this.SendMsgEx(Msg);
507
539
  }
@@ -674,9 +706,9 @@ var Client = /** @class */ (function (_super) {
674
706
  }
675
707
  else if (chunk.msgid == NETMSG_Game.SV_VOTEOPTIONREMOVE) {
676
708
  var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
677
- var index_1 = _this.VoteList.indexOf(unpacker.unpackString());
678
- if (index_1 > -1)
679
- _this.VoteList = _this.VoteList.splice(index_1, 1);
709
+ var index = _this.VoteList.indexOf(unpacker.unpackString());
710
+ if (index > -1)
711
+ _this.VoteList = _this.VoteList.splice(index, 1);
680
712
  }
681
713
  // events
682
714
  if (chunk.msgid == NETMSG_Game.SV_EMOTICON) {
@@ -734,14 +766,20 @@ var Client = /** @class */ (function (_super) {
734
766
  // packets neccessary for connection
735
767
  // https://ddnet.org/docs/libtw2/connection/
736
768
  if (chunk.msgid == NETMSG_Game.SV_READYTOENTER) {
737
- var Msg = new MsgPacker_1.MsgPacker(15, true, 1); /* entergame */
769
+ var Msg = new MsgPacker_1.MsgPacker(NETMSG_Sys.NETMSG_ENTERGAME, true, 1); /* entergame */
738
770
  _this.SendMsgEx(Msg);
771
+ _this.OnEnterGame();
739
772
  }
740
773
  }
741
774
  });
742
- if (new Date().getTime() - _this.time >= 1000 && _this.State == States.STATE_ONLINE) {
743
- _this.time = new Date().getTime();
744
- _this.SendControlMsg(0);
775
+ if (_this.State == States.STATE_ONLINE) {
776
+ if (new Date().getTime() - _this.time >= 500) {
777
+ _this.Flush();
778
+ }
779
+ if (new Date().getTime() - _this.time >= 1000) {
780
+ _this.time = new Date().getTime();
781
+ _this.SendControlMsg(0);
782
+ }
745
783
  }
746
784
  });
747
785
  };
package/lib/client.ts CHANGED
@@ -1,3 +1,5 @@
1
+
2
+
1
3
  import { randomBytes } from "crypto";
2
4
 
3
5
  import net from 'dgram';
@@ -133,7 +135,6 @@ enum NETMSG_Sys {
133
135
  interface chunk {
134
136
  bytes: number,
135
137
  flags: number,
136
- sequence?: number,
137
138
  seq?: number,
138
139
  // type: 'sys' | 'game',
139
140
  sys: Boolean,
@@ -211,6 +212,7 @@ export class Client extends EventEmitter {
211
212
  private State: number; // 0 = offline; 1 = STATE_CONNECTING = 1, STATE_LOADING = 2, STATE_ONLINE = 3
212
213
  private ack: number;
213
214
  private clientAck: number;
215
+ private lastCheckedChunkAck: number;
214
216
  private receivedSnaps: number; /* wait for 2 ss before seeing self as connected */
215
217
  private socket: net.Socket | undefined;
216
218
  private TKEN: Buffer;
@@ -277,6 +279,7 @@ export class Client extends EventEmitter {
277
279
  this.State = States.STATE_OFFLINE; // 0 = offline; 1 = STATE_CONNECTING = 1, STATE_LOADING = 2, STATE_ONLINE = 3
278
280
  this.ack = 0; // ack of messages the client has received
279
281
  this.clientAck = 0; // ack of messages the client has sent
282
+ this.lastCheckedChunkAck = 0; // this.ack gets reset to this when flushing - used for resetting tick on e.g. map change
280
283
  this.receivedSnaps = 0; /* wait for 2 snaps before seeing self as connected */
281
284
  this.socket = net.createSocket("udp4");
282
285
  this.socket.bind();
@@ -314,6 +317,16 @@ export class Client extends EventEmitter {
314
317
 
315
318
  }
316
319
 
320
+ private OnEnterGame() {
321
+ this.snaps = [];
322
+ this.SnapUnpacker = new Snapshot(this);
323
+ this.SnapshotParts = 0;
324
+ this.receivedSnaps = 0;
325
+ this.SnapshotUnpacker = new SnapshotWrapper(this);
326
+ this.currentSnapshotGameTick = 0;
327
+ this.AckGameTick = -1;
328
+ this.PredGameTick = 0;
329
+ }
317
330
  private ResendAfter(lastAck: number) {
318
331
  this.clientAck = lastAck;
319
332
 
@@ -349,7 +362,6 @@ export class Client extends EventEmitter {
349
362
  var chunk: chunk = {} as chunk;
350
363
  chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1));
351
364
  chunk.flags = (packet[0] >> 6) & 3;
352
- chunk.sequence = -1;
353
365
 
354
366
  if (chunk.flags & 1) {
355
367
  chunk.seq = ((packet[1] & 0xf0) << 2) | packet[2];
@@ -459,9 +471,18 @@ export class Client extends EventEmitter {
459
471
  this.socket.send(packet, 0, packet.length, this.port, this.host)
460
472
  }
461
473
 
462
- /** Queue a chunk (It will get sent in the next packet). */
474
+ /** Queue a chunk (instantly sent if flush flag is set - otherwise it will be sent in the next packet). */
463
475
  QueueChunkEx(Msg: MsgPacker) {
476
+ if (this.queueChunkEx.length > 0) {
477
+ let total_size = 0;
478
+ for (let chunk of this.queueChunkEx)
479
+ total_size += chunk.size;
480
+ if (total_size + Msg.size + 3 > 1394 - 4)
481
+ this.Flush();
482
+ }
464
483
  this.queueChunkEx.push(Msg);
484
+ if (Msg.flag & 4)
485
+ this.Flush();
465
486
  }
466
487
 
467
488
  /** Send a Raw Buffer (as chunk) to the server. */
@@ -485,7 +506,6 @@ export class Client extends EventEmitter {
485
506
  var chunk: chunk = {} as chunk;
486
507
  chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1));
487
508
  chunk.flags = (packet[0] >> 6) & 3;
488
- chunk.sequence = -1;
489
509
 
490
510
  if (chunk.flags & 1) {
491
511
  chunk.seq = ((packet[1]&0xf0)<<2) | packet[2];
@@ -509,7 +529,14 @@ export class Client extends EventEmitter {
509
529
  }
510
530
  return chunk;
511
531
  }
532
+ Flush() {
533
+ // if (this.queueChunkEx.length == 0)
534
+ console.log("flushing");
535
+ this.SendMsgEx(this.queueChunkEx);
536
+ this.queueChunkEx = [];
537
+ this.ack = this.lastCheckedChunkAck;
512
538
 
539
+ }
513
540
 
514
541
  /** Connect the client to the server. */
515
542
  connect() {
@@ -533,8 +560,10 @@ export class Client extends EventEmitter {
533
560
  }, 500);
534
561
  if (!this.options?.lightweight) {
535
562
  let inputInterval = setInterval(() => {
536
- if (this.State == States.STATE_OFFLINE)
563
+ if (this.State == States.STATE_OFFLINE) {
537
564
  clearInterval(inputInterval)
565
+ console.log("???");
566
+ }
538
567
  if (this.State != States.STATE_ONLINE)
539
568
  return;
540
569
  this.time = new Date().getTime();
@@ -615,10 +644,20 @@ export class Client extends EventEmitter {
615
644
  }
616
645
 
617
646
  var unpacked: _packet = this.Unpack(packet);
618
- unpacked.chunks = unpacked.chunks.filter(chunk => ((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq! > this.ack : true); // filter out already received chunks
619
-
647
+ // unpacked.chunks = unpacked.chunks.filter(chunk => ((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq! > this.ack : true); // filter out already received chunks
648
+ this.sentChunkQueue.forEach((buff, i) => {
649
+ let chunkFlags = (buff[0] >> 6) & 3;
650
+ if (chunkFlags & 1) {
651
+ let chunk = this.MsgToChunk(buff);
652
+ if (chunk.seq && chunk.seq >= this.ack)
653
+ this.sentChunkQueue.splice(i, 1);
654
+ }
655
+ })
620
656
  unpacked.chunks.forEach(chunk => {
657
+ if (!(((chunk.flags & 2) && (chunk.flags & 1)) ? chunk.seq! > this.ack : true))
658
+ return; // filter out already received chunks
621
659
  if (chunk.flags & 1 && (chunk.flags !== 15)) { // vital and not connless
660
+ this.lastCheckedChunkAck = chunk.seq!;
622
661
  if (chunk.seq === (this.ack+1)%(1<<10)) { // https://github.com/nobody-mb/twchatonly/blob/master/chatonly.cpp#L237
623
662
  this.ack = chunk.seq!;
624
663
 
@@ -628,29 +667,17 @@ export class Client extends EventEmitter {
628
667
  let Bottom = (this.ack - (1<<10)/2);
629
668
 
630
669
  if(Bottom < 0) {
631
- if((chunk.seq! <= this.ack) || (chunk.seq! >= (Bottom + (1<<10))))
632
- return;
670
+ if((chunk.seq! <= this.ack) || (chunk.seq! >= (Bottom + (1<<10)))) {}
671
+ else
672
+ this.requestResend = true;
633
673
  } else {
634
- if(chunk.seq! <= this.ack && chunk.seq! >= Bottom)
635
- return;
674
+ if(chunk.seq! <= this.ack && chunk.seq! >= Bottom) {}
675
+ else
676
+ this.requestResend = true;
636
677
  }
637
- this.requestResend = true;
638
-
639
678
  }
640
679
  }
641
680
 
642
- })
643
- this.sentChunkQueue.forEach((buff, i) => {
644
- let chunkFlags = (buff[0] >> 6) & 3;
645
- if (chunkFlags & 1) {
646
- let chunk = this.MsgToChunk(buff);
647
- if (chunk.seq && chunk.seq >= this.ack)
648
- this.sentChunkQueue.splice(i, 1);
649
- }
650
- })
651
-
652
-
653
- unpacked.chunks.forEach((chunk, index) => {
654
681
  if (chunk.sys) {
655
682
  // system messages
656
683
  if (chunk.msgid == NETMSG_Sys.NETMSG_PING) { // ping
@@ -665,6 +692,7 @@ export class Client extends EventEmitter {
665
692
  // https://ddnet.org/docs/libtw2/connection/
666
693
 
667
694
  if (chunk.msgid == NETMSG_Sys.NETMSG_MAP_CHANGE) {
695
+ this.Flush();
668
696
  var Msg = new MsgPacker(NETMSG_Sys.NETMSG_READY, true, 1); /* ready */
669
697
  this.SendMsgEx(Msg);
670
698
  } else if (chunk.msgid == NETMSG_Sys.NETMSG_CON_READY) {
@@ -936,16 +964,22 @@ export class Client extends EventEmitter {
936
964
  // packets neccessary for connection
937
965
  // https://ddnet.org/docs/libtw2/connection/
938
966
  if (chunk.msgid == NETMSG_Game.SV_READYTOENTER) {
939
- var Msg = new MsgPacker(15, true, 1); /* entergame */
967
+ var Msg = new MsgPacker(NETMSG_Sys.NETMSG_ENTERGAME, true, 1); /* entergame */
940
968
  this.SendMsgEx(Msg);
969
+ this.OnEnterGame();
941
970
  }
942
971
  }
943
972
  })
944
973
 
945
-
946
- if (new Date().getTime() - this.time >= 1000 && this.State == States.STATE_ONLINE) {
947
- this.time = new Date().getTime();
948
- this.SendControlMsg(0);
974
+ if (this.State == States.STATE_ONLINE) {
975
+ if (new Date().getTime() - this.time >= 500) {
976
+ this.Flush();
977
+ }
978
+ if (new Date().getTime() - this.time >= 1000) {
979
+ this.time = new Date().getTime();
980
+ this.SendControlMsg(0);
981
+ }
982
+
949
983
  }
950
984
  })
951
985
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teeworlds",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
4
4
  "description": "Library for (ingame) teeworlds bots.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",