teeworlds 2.1.3 → 2.1.5

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/lib/client.js DELETED
@@ -1,509 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
- step((generator = generator.apply(thisArg, _arguments || [])).next());
22
- });
23
- };
24
- var __generator = (this && this.__generator) || function (thisArg, body) {
25
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
27
- function verb(n) { return function (v) { return step([n, v]); }; }
28
- function step(op) {
29
- if (f) throw new TypeError("Generator is already executing.");
30
- while (_) try {
31
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
32
- if (y = 0, t) op = [op[0] & 2, t.value];
33
- switch (op[0]) {
34
- case 0: case 1: t = op; break;
35
- case 4: _.label++; return { value: op[1], done: false };
36
- case 5: _.label++; y = op[1]; op = [0]; continue;
37
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
38
- default:
39
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
40
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
41
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
42
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
43
- if (t[2]) _.ops.pop();
44
- _.trys.pop(); continue;
45
- }
46
- op = body.call(thisArg, _);
47
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
48
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
49
- }
50
- };
51
- var __importDefault = (this && this.__importDefault) || function (mod) {
52
- return (mod && mod.__esModule) ? mod : { "default": mod };
53
- };
54
- var crypto_1 = require("crypto");
55
- var dgram_1 = __importDefault(require("dgram"));
56
- var stream_1 = require("stream");
57
- var MsgUnpacker_1 = require("./MsgUnpacker");
58
- var MsgPacker_1 = __importDefault(require("./MsgPacker"));
59
- var snapshot_1 = require("./snapshot");
60
- var huffman_1 = __importDefault(require("./huffman"));
61
- var huff = new huffman_1.default();
62
- var SnapUnpacker = new snapshot_1.Snapshot();
63
- var NETMSGTYPE;
64
- (function (NETMSGTYPE) {
65
- NETMSGTYPE[NETMSGTYPE["EX"] = 0] = "EX";
66
- NETMSGTYPE[NETMSGTYPE["SV_MOTD"] = 1] = "SV_MOTD";
67
- NETMSGTYPE[NETMSGTYPE["SV_BROADCAST"] = 2] = "SV_BROADCAST";
68
- NETMSGTYPE[NETMSGTYPE["SV_CHAT"] = 3] = "SV_CHAT";
69
- NETMSGTYPE[NETMSGTYPE["SV_KILLMSG"] = 4] = "SV_KILLMSG";
70
- NETMSGTYPE[NETMSGTYPE["SV_SOUNDGLOBAL"] = 5] = "SV_SOUNDGLOBAL";
71
- NETMSGTYPE[NETMSGTYPE["SV_TUNEPARAMS"] = 6] = "SV_TUNEPARAMS";
72
- NETMSGTYPE[NETMSGTYPE["SV_EXTRAPROJECTILE"] = 7] = "SV_EXTRAPROJECTILE";
73
- NETMSGTYPE[NETMSGTYPE["SV_READYTOENTER"] = 8] = "SV_READYTOENTER";
74
- NETMSGTYPE[NETMSGTYPE["SV_WEAPONPICKUP"] = 9] = "SV_WEAPONPICKUP";
75
- NETMSGTYPE[NETMSGTYPE["SV_EMOTICON"] = 10] = "SV_EMOTICON";
76
- NETMSGTYPE[NETMSGTYPE["SV_VOTECLEAROPTIONS"] = 11] = "SV_VOTECLEAROPTIONS";
77
- NETMSGTYPE[NETMSGTYPE["SV_VOTEOPTIONLISTADD"] = 12] = "SV_VOTEOPTIONLISTADD";
78
- NETMSGTYPE[NETMSGTYPE["SV_VOTEOPTIONADD"] = 13] = "SV_VOTEOPTIONADD";
79
- NETMSGTYPE[NETMSGTYPE["SV_VOTEOPTIONREMOVE"] = 14] = "SV_VOTEOPTIONREMOVE";
80
- NETMSGTYPE[NETMSGTYPE["SV_VOTESET"] = 15] = "SV_VOTESET";
81
- NETMSGTYPE[NETMSGTYPE["SV_VOTESTATUS"] = 16] = "SV_VOTESTATUS";
82
- NETMSGTYPE[NETMSGTYPE["CL_SAY"] = 17] = "CL_SAY";
83
- NETMSGTYPE[NETMSGTYPE["CL_SETTEAM"] = 18] = "CL_SETTEAM";
84
- NETMSGTYPE[NETMSGTYPE["CL_SETSPECTATORMODE"] = 19] = "CL_SETSPECTATORMODE";
85
- NETMSGTYPE[NETMSGTYPE["CL_STARTINFO"] = 20] = "CL_STARTINFO";
86
- NETMSGTYPE[NETMSGTYPE["CL_CHANGEINFO"] = 21] = "CL_CHANGEINFO";
87
- NETMSGTYPE[NETMSGTYPE["CL_KILL"] = 22] = "CL_KILL";
88
- NETMSGTYPE[NETMSGTYPE["CL_EMOTICON"] = 23] = "CL_EMOTICON";
89
- NETMSGTYPE[NETMSGTYPE["CL_VOTE"] = 24] = "CL_VOTE";
90
- NETMSGTYPE[NETMSGTYPE["CL_CALLVOTE"] = 25] = "CL_CALLVOTE";
91
- NETMSGTYPE[NETMSGTYPE["CL_ISDDNETLEGACY"] = 26] = "CL_ISDDNETLEGACY";
92
- NETMSGTYPE[NETMSGTYPE["SV_DDRACETIMELEGACY"] = 27] = "SV_DDRACETIMELEGACY";
93
- NETMSGTYPE[NETMSGTYPE["SV_RECORDLEGACY"] = 28] = "SV_RECORDLEGACY";
94
- NETMSGTYPE[NETMSGTYPE["UNUSED"] = 29] = "UNUSED";
95
- NETMSGTYPE[NETMSGTYPE["SV_TEAMSSTATELEGACY"] = 30] = "SV_TEAMSSTATELEGACY";
96
- NETMSGTYPE[NETMSGTYPE["CL_SHOWOTHERSLEGACY"] = 31] = "CL_SHOWOTHERSLEGACY";
97
- NETMSGTYPE[NETMSGTYPE["NUM"] = 32] = "NUM";
98
- })(NETMSGTYPE || (NETMSGTYPE = {}));
99
- var items;
100
- (function (items) {
101
- items[items["OBJ_EX"] = 0] = "OBJ_EX";
102
- items[items["OBJ_PLAYER_INPUT"] = 1] = "OBJ_PLAYER_INPUT";
103
- items[items["OBJ_PROJECTILE"] = 2] = "OBJ_PROJECTILE";
104
- items[items["OBJ_LASER"] = 3] = "OBJ_LASER";
105
- items[items["OBJ_PICKUP"] = 4] = "OBJ_PICKUP";
106
- items[items["OBJ_FLAG"] = 5] = "OBJ_FLAG";
107
- items[items["OBJ_GAME_INFO"] = 6] = "OBJ_GAME_INFO";
108
- items[items["OBJ_GAME_DATA"] = 7] = "OBJ_GAME_DATA";
109
- items[items["OBJ_CHARACTER_CORE"] = 8] = "OBJ_CHARACTER_CORE";
110
- items[items["OBJ_CHARACTER"] = 9] = "OBJ_CHARACTER";
111
- items[items["OBJ_PLAYER_INFO"] = 10] = "OBJ_PLAYER_INFO";
112
- items[items["OBJ_CLIENT_INFO"] = 11] = "OBJ_CLIENT_INFO";
113
- items[items["OBJ_SPECTATOR_INFO"] = 12] = "OBJ_SPECTATOR_INFO";
114
- items[items["EVENT_COMMON"] = 13] = "EVENT_COMMON";
115
- items[items["EVENT_EXPLOSION"] = 14] = "EVENT_EXPLOSION";
116
- items[items["EVENT_SPAWN"] = 15] = "EVENT_SPAWN";
117
- items[items["EVENT_HAMMERHIT"] = 16] = "EVENT_HAMMERHIT";
118
- items[items["EVENT_DEATH"] = 17] = "EVENT_DEATH";
119
- items[items["EVENT_SOUND_GLOBAL"] = 18] = "EVENT_SOUND_GLOBAL";
120
- items[items["EVENT_SOUND_WORLD"] = 19] = "EVENT_SOUND_WORLD";
121
- items[items["EVENT_DAMAGE_INDICATOR"] = 20] = "EVENT_DAMAGE_INDICATOR";
122
- })(items || (items = {}));
123
- function toHexStream(buff) {
124
- return buff.toJSON().data.map(function (a) { return ('0' + (a & 0xff).toString(16)).slice(-2); }).join("");
125
- }
126
- var messageTypes = [
127
- ["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"],
128
- ["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"]
129
- ];
130
- var messageUUIDs = {
131
- "WHAT_IS": Buffer.from([0x24, 0x5e, 0x50, 0x97, 0x9f, 0xe0, 0x39, 0xd6, 0xbf, 0x7d, 0x9a, 0x29, 0xe1, 0x69, 0x1e, 0x4c]),
132
- "IT_IS": Buffer.from([0x69, 0x54, 0x84, 0x7e, 0x2e, 0x87, 0x36, 0x03, 0xb5, 0x62, 0x36, 0xda, 0x29, 0xed, 0x1a, 0xca]),
133
- "I_DONT_KNOW": Buffer.from([0x41, 0x69, 0x11, 0xb5, 0x79, 0x73, 0x33, 0xbf, 0x8d, 0x52, 0x7b, 0xf0, 0x1e, 0x51, 0x9c, 0xf0]),
134
- "RCON_TYPE": Buffer.from([0x12, 0x81, 0x0e, 0x1f, 0xa1, 0xdb, 0x33, 0x78, 0xb4, 0xfb, 0x16, 0x4e, 0xd6, 0x50, 0x59, 0x26]),
135
- "MAP_DETAILS": Buffer.from([0xf9, 0x11, 0x7b, 0x3c, 0x80, 0x39, 0x34, 0x16, 0x9f, 0xc0, 0xae, 0xf2, 0xbc, 0xb7, 0x5c, 0x03]),
136
- "CLIENT_VERSION": Buffer.from([0x8c, 0x00, 0x13, 0x04, 0x84, 0x61, 0x3e, 0x47, 0x87, 0x87, 0xf6, 0x72, 0xb3, 0x83, 0x5b, 0xd4]),
137
- "CAPABILITIES": Buffer.from([0xf6, 0x21, 0xa5, 0xa1, 0xf5, 0x85, 0x37, 0x75, 0x8e, 0x73, 0x41, 0xbe, 0xee, 0x79, 0xf2, 0xb2]),
138
- };
139
- function arrStartsWith(arr, arrStart, start) {
140
- if (start === void 0) { start = 0; }
141
- arr.splice(0, start);
142
- for (var i = 0; i < arrStart.length; i++) {
143
- if (arr[i] == arrStart[i])
144
- continue;
145
- else
146
- return false;
147
- }
148
- return true;
149
- }
150
- var Client = /** @class */ (function (_super) {
151
- __extends(Client, _super);
152
- function Client(ip, port, nickname) {
153
- var _this = _super.call(this) || this;
154
- _this.host = ip;
155
- _this.port = port;
156
- _this.name = nickname;
157
- _this.snaps = [];
158
- _this.client_infos = [];
159
- _this.player_infos = [];
160
- _this.State = 0; // 0 = offline; 1 = STATE_CONNECTING = 1, STATE_LOADING = 2, STATE_ONLINE = 3
161
- _this.ack = 0; // ack of messages the client has received
162
- _this.clientAck = 0; // ack of messages the client has sent
163
- _this.receivedSnaps = 0; /* wait for 2 snaps before seeing self as connected */
164
- _this.lastMsg = "";
165
- _this._port = Math.floor(Math.random() * 65535);
166
- _this.socket = dgram_1.default.createSocket("udp4");
167
- _this.socket.bind();
168
- _this.TKEN = Buffer.from([255, 255, 255, 255]);
169
- _this.time = new Date().getTime() + 2000; // time (used for keepalives, start to send keepalives after 2 seconds)
170
- _this.State = 0;
171
- return _this;
172
- }
173
- Client.prototype.Unpack = function (packet) {
174
- return __awaiter(this, void 0, void 0, function () {
175
- var unpacked, i, chunk;
176
- return __generator(this, function (_a) {
177
- unpacked = { twprotocol: { flags: packet[0], ack: packet[1], chunkAmount: packet[2], size: packet.byteLength - 3 }, chunks: [] };
178
- 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)
179
- return [2 /*return*/, unpacked];
180
- packet = packet.slice(3);
181
- if (unpacked.twprotocol.flags & 128) {
182
- packet = huff.decompress(packet);
183
- if (packet.length == 1 && packet[0] == -1)
184
- return [2 /*return*/, unpacked];
185
- }
186
- // return unpacked;
187
- for (i = 0; i < unpacked.twprotocol.chunkAmount; i++) {
188
- chunk = {};
189
- chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1)); // idk what this shit is but it works
190
- chunk.flags = (packet[0] >> 6) & 3;
191
- chunk.sequence = -1;
192
- if (chunk.flags & 1) {
193
- chunk.seq = ((packet[1] & 0xf0) << 2) | packet[2];
194
- packet = packet.slice(3); // remove flags & size
195
- }
196
- else
197
- packet = packet.slice(2);
198
- chunk.type = packet[0] & 1 ? "sys" : "game"; // & 1 = binary, ****_***1. e.g 0001_0111 sys, 0001_0110 game
199
- chunk.msgid = (packet[0] - (packet[0] & 1)) / 2;
200
- chunk.msg = messageTypes[packet[0] & 1][chunk.msgid];
201
- chunk.raw = packet.slice(1, chunk.bytes);
202
- Object.values(messageUUIDs).forEach(function (a, i) {
203
- if (a.compare(packet.slice(0, 16)) == 0) {
204
- chunk.extended_msgid = a;
205
- // chunk.type = 'sys';
206
- chunk.msg = Object.keys(messageUUIDs)[i];
207
- }
208
- });
209
- packet = packet.slice(chunk.bytes); // +1 cuz it adds an extra \x00 for easier parsing i guess
210
- unpacked.chunks.push(chunk);
211
- }
212
- return [2 /*return*/, unpacked];
213
- });
214
- });
215
- };
216
- Client.prototype.SendControlMsg = function (msg, ExtraMsg) {
217
- var _this = this;
218
- if (ExtraMsg === void 0) { ExtraMsg = ""; }
219
- return new Promise(function (resolve, reject) {
220
- if (_this.socket) {
221
- var latestBuf = Buffer.from([0x10 + (((16 << 4) & 0xf0) | ((_this.ack >> 8) & 0xf)), _this.ack & 0xff, 0x00, msg]);
222
- latestBuf = Buffer.concat([latestBuf, Buffer.from(ExtraMsg), _this.TKEN]); // move header (latestBuf), optional extraMsg & TKEN into 1 buffer
223
- _this.socket.send(latestBuf, 0, latestBuf.length, _this.port, _this.host, function (err, bytes) {
224
- resolve(bytes);
225
- });
226
- }
227
- setTimeout(function () { resolve("failed, rip"); }, 2000);
228
- /* after 2 seconds it was probably not able to send,
229
- so when sending a quit message the user doesnt
230
- stay stuck not being able to ctrl + c
231
- */
232
- });
233
- };
234
- Client.prototype.SendMsgEx = function (Msg, Flags) {
235
- if (this.State == -1)
236
- throw new Error("Client is not connected");
237
- if (!this.socket)
238
- return;
239
- var header = [];
240
- header[0] = ((Flags & 3) << 6) | ((Msg.size >> 4) & 0x3f);
241
- header[1] = (Msg.size & 0xf);
242
- if (Flags & 1) {
243
- this.clientAck = (this.clientAck + 1) % (1 << 10);
244
- header[1] |= (this.clientAck >> 2) & 0xf0;
245
- header[2] = this.clientAck & 0xff;
246
- }
247
- var latestBuf = Buffer.from([0x0 + (((16 << 4) & 0xf0) | ((this.ack >> 8) & 0xf)), this.ack & 0xff, 0x1, header[0], header[1], this.clientAck]);
248
- var latestBuf = Buffer.concat([latestBuf, Msg.buffer, this.TKEN]);
249
- this.socket.send(latestBuf, 0, latestBuf.length, this.port, this.host);
250
- };
251
- Client.prototype.SendMsgExWithChunks = function (Msgs, Flags) {
252
- var _this = this;
253
- if (this.State == -1)
254
- throw new Error("Client is not connected");
255
- if (!this.socket)
256
- return;
257
- var header = [];
258
- Msgs.forEach(function (Msg, index) {
259
- header[index] = new Array(2);
260
- header[index][0] = ((Flags & 3) << 6) | ((Msg.size >> 4) & 0x3f);
261
- header[index][1] = (Msg.size & 0xf);
262
- if (Flags & 1) {
263
- _this.clientAck = (_this.clientAck + 1) % (1 << 10);
264
- header[index][1] |= (_this.clientAck >> 2) & 0xf0;
265
- header[index][2] = _this.clientAck & 0xff;
266
- }
267
- });
268
- var packetHeader = Buffer.from([0x0 + (((16 << 4) & 0xf0) | ((this.ack >> 8) & 0xf)), this.ack & 0xff, Msgs.length]);
269
- var chunks = Buffer.from([]);
270
- Msgs.forEach(function (Msg, index) {
271
- chunks = Buffer.concat([chunks, Buffer.from(header[index]), Msg.buffer]);
272
- });
273
- var packet = Buffer.concat([(packetHeader), chunks, this.TKEN]);
274
- this.socket.send(packet, 0, packet.length, this.port, this.host);
275
- };
276
- Client.prototype.connect = function () {
277
- var _this = this;
278
- this.SendControlMsg(1, "TKEN");
279
- var connectInterval = setInterval(function () {
280
- if (_this.State == 0)
281
- _this.SendControlMsg(1, "TKEN");
282
- else
283
- clearInterval(connectInterval);
284
- }, 500);
285
- this.time = new Date().getTime() + 2000; // start sending keepalives after 2s
286
- if (this.socket)
287
- this.socket.on("message", function (a) { return __awaiter(_this, void 0, void 0, function () {
288
- var unpacked, chunkMessages, chat, chat, info, client_version, randomUuid, reason, Msg, Msg, info, info, chunks, part_1, num_parts_1;
289
- var _this = this;
290
- return __generator(this, function (_a) {
291
- switch (_a.label) {
292
- case 0: return [4 /*yield*/, this.Unpack(a)];
293
- case 1:
294
- unpacked = _a.sent();
295
- if (unpacked.twprotocol.flags != 128 && unpacked.twprotocol.ack) {
296
- unpacked.chunks.forEach(function (a) {
297
- if (a.msg && !a.msg.startsWith("SNAP")) {
298
- if (a.seq != undefined && a.seq != -1)
299
- _this.ack = a.seq;
300
- }
301
- });
302
- }
303
- chunkMessages = unpacked.chunks.map(function (a) { return a.msg; });
304
- if (chunkMessages.includes("SV_CHAT")) {
305
- chat = unpacked.chunks.filter(function (a) { return a.msg == "SV_CHAT"; });
306
- chat.forEach(function (a) {
307
- if (a.msg == "SV_CHAT") {
308
- var unpacked = {};
309
- unpacked.team = MsgUnpacker_1.unpackInt(a.raw.toJSON().data).result;
310
- var remaining = MsgUnpacker_1.unpackInt(a.raw.toJSON().data).remaining;
311
- unpacked.client_id = MsgUnpacker_1.unpackInt(remaining).result;
312
- remaining = MsgUnpacker_1.unpackInt(remaining).remaining;
313
- unpacked.message = MsgUnpacker_1.unpackString(remaining).result;
314
- if (unpacked.client_id != -1)
315
- unpacked.author = { ClientInfo: _this.client_infos[unpacked.client_id], PlayerInfo: _this.player_infos[unpacked.client_id] };
316
- // console.log(unpacked)
317
- _this.emit("message", unpacked);
318
- }
319
- });
320
- }
321
- if (chunkMessages.includes("SV_KILL_MSG")) {
322
- chat = unpacked.chunks.filter(function (a) { return a.msg == "SV_KILL_MSG"; });
323
- chat.forEach(function (a) {
324
- if (a.msg == "SV_KILL_MSG") {
325
- var unpacked = {};
326
- var unpacker = new MsgUnpacker_1.MsgUnpacker(a.raw.toJSON().data);
327
- unpacked.killer_id = unpacker.unpackInt();
328
- unpacked.victim_id = unpacker.unpackInt();
329
- unpacked.weapon = unpacker.unpackInt();
330
- unpacked.special_mode = unpacker.unpackInt();
331
- if (unpacked.victim_id != -1)
332
- unpacked.victim = { ClientInfo: _this.client_infos[unpacked.victim_id], PlayerInfo: _this.player_infos[unpacked.victim_id] };
333
- if (unpacked.killer_id != -1)
334
- unpacked.killer = { ClientInfo: _this.client_infos[unpacked.killer_id], PlayerInfo: _this.player_infos[unpacked.killer_id] };
335
- // console.log(unpacked)
336
- _this.emit("kill", unpacked);
337
- }
338
- });
339
- }
340
- if (a.toJSON().data[0] == 0x10) {
341
- if (a.toString().includes("TKEN") || arrStartsWith(a.toJSON().data, [0x10, 0x0, 0x0, 0x0])) {
342
- clearInterval(connectInterval);
343
- this.TKEN = Buffer.from(a.toJSON().data.slice(a.toJSON().data.length - 4, a.toJSON().data.length));
344
- this.SendControlMsg(3);
345
- this.State = 2; // loading state
346
- info = new MsgPacker_1.default(1, true);
347
- info.AddString("0.6 626fce9a778df4d4");
348
- info.AddString(""); // password
349
- client_version = new MsgPacker_1.default(0, true);
350
- client_version.AddBuffer(Buffer.from("8c00130484613e478787f672b3835bd4", 'hex'));
351
- randomUuid = new Uint8Array(16);
352
- crypto_1.randomBytes(16).copy(randomUuid);
353
- client_version.AddBuffer(Buffer.from(randomUuid));
354
- client_version.AddInt(15091);
355
- client_version.AddString("DDNet 15.9.1");
356
- this.SendMsgExWithChunks([client_version, info], 1);
357
- }
358
- else if (a.toJSON().data[3] == 0x4) {
359
- // disconnected
360
- this.State = 0;
361
- reason = (MsgUnpacker_1.unpackString(a.toJSON().data.slice(4)).result);
362
- this.State = -1;
363
- this.emit("disconnect", reason);
364
- }
365
- }
366
- if (unpacked.chunks[0] && chunkMessages.includes("SV_READY_TO_ENTER")) {
367
- Msg = new MsgPacker_1.default(15, true);
368
- this.SendMsgEx(Msg, 1);
369
- }
370
- else if ((unpacked.chunks[0] && chunkMessages.includes("CAPABILITIES") || unpacked.chunks[0] && chunkMessages.includes("MAP_CHANGE"))) {
371
- Msg = new MsgPacker_1.default(14, true);
372
- this.SendMsgEx(Msg, 1);
373
- }
374
- else if ((unpacked.chunks[0] && chunkMessages.includes("CON_READY") || unpacked.chunks[0] && chunkMessages.includes("SV_MOTD"))) {
375
- info = new MsgPacker_1.default(20, false);
376
- info.AddString(this.name); /* name */
377
- info.AddString(""); /* clan */
378
- info.AddInt(-1); /* country */
379
- info.AddString("greyfox"); /* skin */
380
- info.AddInt(1); /* use custom color */
381
- info.AddInt(10346103); /* color body */
382
- info.AddInt(65535); /* color feet */
383
- this.SendMsgEx(info, 1);
384
- }
385
- else if (unpacked.chunks[0] && chunkMessages.includes("SV_READY_TO_ENTER")) {
386
- if (this.State != 3) {
387
- this.emit('connected');
388
- }
389
- this.State = 3;
390
- }
391
- else if (unpacked.chunks[0] && chunkMessages.includes("PING")) {
392
- info = new MsgPacker_1.default(23, true);
393
- this.SendMsgEx(info, 1);
394
- }
395
- if (chunkMessages.includes("SNAP") || chunkMessages.includes("SNAP_EMPTY") || chunkMessages.includes("SNAP_SINGLE")) {
396
- this.receivedSnaps++; /* wait for 2 ss before seeing self as connected */
397
- if (this.receivedSnaps >= 2) {
398
- if (this.State != 3)
399
- this.emit('connected');
400
- this.State = 3;
401
- }
402
- chunks = unpacked.chunks.filter(function (a) { return a.msg == "SNAP" || a.msg == "SNAP_SINGLE" || a.msg == "SNAP_EMPTY"; });
403
- if (chunks.length > 0) {
404
- part_1 = 0;
405
- num_parts_1 = 1;
406
- chunks.forEach(function (chunk) {
407
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
408
- var AckGameTick = (MsgUnpacker_1.unpackInt(chunk.raw.toJSON().data).result);
409
- chunk.raw = Buffer.from(MsgUnpacker_1.unpackInt((_a = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _a === void 0 ? void 0 : _a.toJSON().data).remaining);
410
- var DeltaTick = MsgUnpacker_1.unpackInt((_b = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _b === void 0 ? void 0 : _b.toJSON().data).result;
411
- if (chunk.msg == "SNAP") {
412
- chunk.raw = Buffer.from(MsgUnpacker_1.unpackInt((_c = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _c === void 0 ? void 0 : _c.toJSON().data).remaining); // delta tick
413
- num_parts_1 = (MsgUnpacker_1.unpackInt((_d = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _d === void 0 ? void 0 : _d.toJSON().data).result);
414
- chunk.raw = Buffer.from(MsgUnpacker_1.unpackInt((_e = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _e === void 0 ? void 0 : _e.toJSON().data).remaining); // num parts
415
- part_1 = (MsgUnpacker_1.unpackInt((_f = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _f === void 0 ? void 0 : _f.toJSON().data).result);
416
- }
417
- chunk.raw = Buffer.from(MsgUnpacker_1.unpackInt((_g = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _g === void 0 ? void 0 : _g.toJSON().data).remaining); // part
418
- if (chunk.msg != "SNAP_EMPTY")
419
- chunk.raw = Buffer.from(MsgUnpacker_1.unpackInt((_h = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _h === void 0 ? void 0 : _h.toJSON().data).remaining); // crc
420
- chunk.raw = Buffer.from(MsgUnpacker_1.unpackInt((_j = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _j === void 0 ? void 0 : _j.toJSON().data).remaining); // crc
421
- if (part_1 == 0 || _this.snaps.length > 30) {
422
- _this.snaps = [];
423
- }
424
- _this.snaps.push(chunk.raw);
425
- if ((num_parts_1 - 1) == part_1 && _this.snaps.length == num_parts_1) {
426
- var mergedSnaps = Buffer.concat(_this.snaps);
427
- var snapUnpacked = SnapUnpacker.unpackSnapshot(mergedSnaps.toJSON().data, 1);
428
- snapUnpacked.items.forEach(function (a, i) {
429
- if (a.type_id == items.OBJ_CLIENT_INFO) {
430
- _this.client_infos[a.id] = a.parsed;
431
- // console.log(a.parsed, i)
432
- // console.log(this.client_infos[a.id])
433
- }
434
- else if (a.type_id == items.OBJ_PLAYER_INFO) {
435
- _this.player_infos[i] = a.parsed;
436
- }
437
- else if (a.type_id == items.OBJ_EX || a.type_id > 0x4000) {
438
- if (a.data.length == 5 && (a.parsed.freeze_end > 0 || a.parsed.freeze_end == -1)) {
439
- // var packer = new MsgPacker(22, false)
440
- // this.SendMsgEx(packer, 1)
441
- }
442
- }
443
- });
444
- }
445
- });
446
- }
447
- }
448
- if (new Date().getTime() - this.time >= 1000) {
449
- this.time = new Date().getTime();
450
- this.SendControlMsg(0);
451
- }
452
- return [2 /*return*/];
453
- }
454
- });
455
- }); });
456
- };
457
- Client.prototype.Disconnect = function () {
458
- var _this = this;
459
- return new Promise(function (resolve) {
460
- _this.SendControlMsg(4).then(function () {
461
- resolve(true);
462
- if (_this.socket)
463
- _this.socket.close();
464
- _this.socket = undefined;
465
- _this.State = -1;
466
- });
467
- });
468
- };
469
- Client.prototype.Say = function (message, team) {
470
- if (team === void 0) { team = false; }
471
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_SAY, false);
472
- packer.AddInt(team ? 1 : 0); // team
473
- packer.AddString(message);
474
- this.SendMsgEx(packer, 1);
475
- };
476
- Client.prototype.Vote = function (vote) {
477
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_VOTE, false);
478
- packer.AddInt(vote ? 1 : 0);
479
- this.SendMsgEx(packer, 1);
480
- };
481
- Client.prototype.ChangePlayerInfo = function (playerInfo) {
482
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_CHANGEINFO, false);
483
- packer.AddString(playerInfo.name); //m_pName);
484
- packer.AddString(playerInfo.clan); //m_pClan);
485
- packer.AddInt(playerInfo.country); //m_Country);
486
- packer.AddString(playerInfo.skin); //m_pSkin);
487
- packer.AddInt(playerInfo.use_custom_color ? 1 : 0); //m_UseCustomColor);
488
- packer.AddInt(playerInfo.color_body); //m_ColorBody);
489
- packer.AddInt(playerInfo.color_feet); //m_ColorFeet);
490
- this.SendMsgEx(packer, 1);
491
- };
492
- Client.prototype.Kill = function () {
493
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_KILL, false);
494
- this.SendMsgEx(packer, 1);
495
- };
496
- Client.prototype.ChangeTeam = function (team) {
497
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_SETTEAM, false);
498
- packer.AddInt(team);
499
- this.SendMsgEx(packer, 1);
500
- };
501
- Client.prototype.Emote = function (emote) {
502
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_EMOTICON, false);
503
- packer.AddInt(emote);
504
- this.SendMsgEx(packer, 1);
505
- };
506
- return Client;
507
- }(stream_1.EventEmitter));
508
- module.exports = Client;
509
- // module.exports = Client;