teeworlds 2.0.0

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