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/README.md +95 -0
- package/index.js +7 -0
- package/index.ts +5 -0
- package/lib/MsgPacker.js +53 -0
- package/lib/MsgPacker.ts +47 -0
- package/lib/MsgUnpacker.js +37 -0
- package/lib/MsgUnpacker.ts +44 -0
- package/lib/client.js +487 -0
- package/lib/client.ts +480 -0
- package/lib/huffman.py +224 -0
- package/lib/snapshot.js +397 -0
- package/lib/snapshot.ts +402 -0
- package/lib/snapshots.d.ts +181 -0
- package/package.json +24 -0
- package/tsconfig.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# tw-chatonly
|
|
2
|
+
Library to connect a bot to a Teeworlds server.
|
|
3
|
+
|
|
4
|
+
# Usage
|
|
5
|
+
Example file:
|
|
6
|
+
```const test = require('@swarfey/teeworlds');
|
|
7
|
+
let client = new test.Client("127.0.0.1", 8303, "test");
|
|
8
|
+
|
|
9
|
+
client.connect();
|
|
10
|
+
|
|
11
|
+
client.on("connected", () => {
|
|
12
|
+
console.log("Connected!");
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
client.on("disconnect", reason => {
|
|
16
|
+
// if you got kicked from the server
|
|
17
|
+
console.log("Disconnected: " + reason);
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
client.on("message", message => {
|
|
21
|
+
/* {
|
|
22
|
+
team: 0,
|
|
23
|
+
client_id: 14,
|
|
24
|
+
message: 'a',
|
|
25
|
+
author: {
|
|
26
|
+
ClientInfo: {
|
|
27
|
+
name: 'Nudelsaft c:',
|
|
28
|
+
clan: '',
|
|
29
|
+
country: 276,
|
|
30
|
+
skin: 'coala_toptri',
|
|
31
|
+
use_custom_color: 0,
|
|
32
|
+
color_body: 4718592,
|
|
33
|
+
color_feet: 5046016
|
|
34
|
+
},
|
|
35
|
+
PlayerInfo: { local: 0, client_id: 4, team: 0, score: 36, latency: 0 }
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
*/
|
|
39
|
+
console.log(message);
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
client.on("kill", info => {
|
|
43
|
+
/* {
|
|
44
|
+
killer_id: 14,
|
|
45
|
+
victim_id: 14,
|
|
46
|
+
weapon: -3,
|
|
47
|
+
special_mode: 0,
|
|
48
|
+
victim: {
|
|
49
|
+
ClientInfo: {
|
|
50
|
+
name: 'Nudelsaft c:',
|
|
51
|
+
clan: '',
|
|
52
|
+
country: 276,
|
|
53
|
+
skin: 'coala_toptri',
|
|
54
|
+
use_custom_color: 0,
|
|
55
|
+
color_body: 4718592,
|
|
56
|
+
color_feet: 5046016
|
|
57
|
+
},
|
|
58
|
+
PlayerInfo: { local: 0, client_id: 4, team: 0, score: 36, latency: 0 }
|
|
59
|
+
},
|
|
60
|
+
killer: {
|
|
61
|
+
ClientInfo: {
|
|
62
|
+
name: 'Nudelsaft c:',
|
|
63
|
+
clan: '',
|
|
64
|
+
country: 276,
|
|
65
|
+
skin: 'coala_toptri',
|
|
66
|
+
use_custom_color: 0,
|
|
67
|
+
color_body: 4718592,
|
|
68
|
+
color_feet: 5046016
|
|
69
|
+
},
|
|
70
|
+
PlayerInfo: { local: 0, client_id: 4, team: 0, score: 36, latency: 0 }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
*/
|
|
74
|
+
console.log(info)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
process.on("SIGINT", () => {
|
|
78
|
+
client.disconnect(); // disconnect on ctrl + c
|
|
79
|
+
})
|
|
80
|
+
process.stdin.on("data", data => {
|
|
81
|
+
client.Say(data.toString()); // write input in chat
|
|
82
|
+
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
function change() {
|
|
86
|
+
client.ChangePlayerInfo({
|
|
87
|
+
name: "new name",
|
|
88
|
+
clan: "new clan",
|
|
89
|
+
country: 12,
|
|
90
|
+
skin: "default",
|
|
91
|
+
use_custom_color: true,
|
|
92
|
+
color_body: 12,
|
|
93
|
+
color_feet: 1111
|
|
94
|
+
})
|
|
95
|
+
}```
|
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MsgPacker = exports.Client = void 0;
|
|
4
|
+
var _Client = require("./lib/client");
|
|
5
|
+
var _MsgPacker = require("./lib/MsgPacker");
|
|
6
|
+
exports.Client = _Client;
|
|
7
|
+
exports.MsgPacker = _MsgPacker;
|
package/index.ts
ADDED
package/lib/MsgPacker.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var MsgPacker = /** @class */ (function () {
|
|
3
|
+
function MsgPacker(msg, sys) {
|
|
4
|
+
this.result = Buffer.from([2 * msg + (sys ? 1 : 0)]); // booleans turn into int automatically.
|
|
5
|
+
this.sys = sys;
|
|
6
|
+
}
|
|
7
|
+
MsgPacker.prototype.AddString = function (str) {
|
|
8
|
+
this.result = Buffer.concat([this.result, Buffer.from(str), Buffer.from([0x00])]);
|
|
9
|
+
};
|
|
10
|
+
MsgPacker.prototype.AddBuffer = function (buffer) {
|
|
11
|
+
this.result = Buffer.concat([this.result, buffer]);
|
|
12
|
+
};
|
|
13
|
+
MsgPacker.prototype.AddInt = function (i) {
|
|
14
|
+
var result = [];
|
|
15
|
+
var pDst = (i >> 25) & 0x40;
|
|
16
|
+
var i = i ^ (i >> 31);
|
|
17
|
+
pDst |= i & 0x3f;
|
|
18
|
+
i >>= 6;
|
|
19
|
+
if (i) {
|
|
20
|
+
pDst |= 0x80;
|
|
21
|
+
result.push(pDst);
|
|
22
|
+
while (true) {
|
|
23
|
+
pDst++;
|
|
24
|
+
pDst = i & (0x7f);
|
|
25
|
+
i >>= 7;
|
|
26
|
+
pDst |= (Number(i != 0)) << 7;
|
|
27
|
+
result.push(pDst);
|
|
28
|
+
if (!i)
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else
|
|
33
|
+
result.push(pDst);
|
|
34
|
+
// ... i'll just stop trying to understand.
|
|
35
|
+
this.result = Buffer.concat([this.result, Buffer.from(result)]);
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(MsgPacker.prototype, "size", {
|
|
38
|
+
get: function () {
|
|
39
|
+
return this.result.byteLength;
|
|
40
|
+
},
|
|
41
|
+
enumerable: false,
|
|
42
|
+
configurable: true
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(MsgPacker.prototype, "buffer", {
|
|
45
|
+
get: function () {
|
|
46
|
+
return this.result;
|
|
47
|
+
},
|
|
48
|
+
enumerable: false,
|
|
49
|
+
configurable: true
|
|
50
|
+
});
|
|
51
|
+
return MsgPacker;
|
|
52
|
+
}());
|
|
53
|
+
module.exports = MsgPacker;
|
package/lib/MsgPacker.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
class MsgPacker {
|
|
2
|
+
result: Buffer;
|
|
3
|
+
sys: boolean;
|
|
4
|
+
constructor(msg: number, sys: boolean) {
|
|
5
|
+
this.result = Buffer.from([2*msg + (sys ? 1 : 0)]) // booleans turn into int automatically.
|
|
6
|
+
this.sys = sys;
|
|
7
|
+
}
|
|
8
|
+
AddString(str: string) {
|
|
9
|
+
this.result = Buffer.concat([this.result, Buffer.from(str), Buffer.from([0x00])])
|
|
10
|
+
}
|
|
11
|
+
AddBuffer(buffer: Buffer) {
|
|
12
|
+
this.result = Buffer.concat([this.result, buffer])
|
|
13
|
+
}
|
|
14
|
+
AddInt(i: number) {
|
|
15
|
+
var result = []
|
|
16
|
+
var pDst = (i >> 25) & 0x40
|
|
17
|
+
var i = i ^(i>>31)
|
|
18
|
+
pDst |= i & 0x3f
|
|
19
|
+
i >>= 6
|
|
20
|
+
if (i) {
|
|
21
|
+
pDst |= 0x80
|
|
22
|
+
result.push(pDst)
|
|
23
|
+
while (true) {
|
|
24
|
+
pDst++;
|
|
25
|
+
pDst = i & (0x7f)
|
|
26
|
+
i>>= 7;
|
|
27
|
+
pDst |= (Number(i != 0)) << 7
|
|
28
|
+
result.push(pDst)
|
|
29
|
+
if (!i)
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
} else
|
|
33
|
+
result.push(pDst)
|
|
34
|
+
|
|
35
|
+
// ... i'll just stop trying to understand.
|
|
36
|
+
|
|
37
|
+
this.result = Buffer.concat([this.result, Buffer.from(result)])
|
|
38
|
+
}
|
|
39
|
+
get size() {
|
|
40
|
+
return this.result.byteLength;
|
|
41
|
+
}
|
|
42
|
+
get buffer() {
|
|
43
|
+
return this.result
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// module.exports = MsgPacker;
|
|
47
|
+
export = MsgPacker;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var decoder = new TextDecoder('utf-8');
|
|
3
|
+
function unpackInt(pSrc) {
|
|
4
|
+
var result = 0;
|
|
5
|
+
var len = 1;
|
|
6
|
+
var iter = pSrc[Symbol.iterator]();
|
|
7
|
+
var src = iter.next();
|
|
8
|
+
// if (src.done)
|
|
9
|
+
// console.warn("Unexpected end", src)
|
|
10
|
+
src = src.value;
|
|
11
|
+
var sign = ((src >> 6) & 1);
|
|
12
|
+
result |= (src & 63);
|
|
13
|
+
for (var i = 0; i < 4; i++) {
|
|
14
|
+
if ((src & 128) == 0)
|
|
15
|
+
break;
|
|
16
|
+
src = iter.next();
|
|
17
|
+
// console.log(src & 0b1000_0000)
|
|
18
|
+
// if (src.done)
|
|
19
|
+
// console.warn("Unexpected end", src);
|
|
20
|
+
src = src.value;
|
|
21
|
+
len += 1;
|
|
22
|
+
if (i == 3 && (src & 240) != 0)
|
|
23
|
+
console.warn("NonZeroIntPadding");
|
|
24
|
+
result |= ((src & 127)) << (6 + 7 * i);
|
|
25
|
+
}
|
|
26
|
+
if (len > 1 && src == 0) {
|
|
27
|
+
console.warn("OverlongIntEncoding");
|
|
28
|
+
}
|
|
29
|
+
result ^= -sign;
|
|
30
|
+
return { result: result, remaining: Array.from(iter) };
|
|
31
|
+
}
|
|
32
|
+
function unpackString(pSrc) {
|
|
33
|
+
var result = pSrc.slice(0, pSrc.indexOf(0));
|
|
34
|
+
pSrc = pSrc.slice(pSrc.indexOf(0), pSrc.length);
|
|
35
|
+
return { result: decoder.decode(new Uint8Array(result)), remaining: pSrc };
|
|
36
|
+
}
|
|
37
|
+
module.exports = { unpackInt: unpackInt, unpackString: unpackString };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
var decoder = new TextDecoder('utf-8');
|
|
2
|
+
function unpackInt(pSrc: number[]): {result: number, remaining: number[]} {
|
|
3
|
+
var result = 0;
|
|
4
|
+
var len = 1;
|
|
5
|
+
|
|
6
|
+
var iter = pSrc[Symbol.iterator]()
|
|
7
|
+
var src: any = iter.next()
|
|
8
|
+
// if (src.done)
|
|
9
|
+
// console.warn("Unexpected end", src)
|
|
10
|
+
src = src.value
|
|
11
|
+
var sign = ((src >> 6) & 1)
|
|
12
|
+
|
|
13
|
+
result |= (src & 0b0011_1111)
|
|
14
|
+
for (let i = 0; i < 4; i++) {
|
|
15
|
+
|
|
16
|
+
if ((src & 0b1000_0000) == 0)
|
|
17
|
+
break;
|
|
18
|
+
src = iter.next()
|
|
19
|
+
|
|
20
|
+
// console.log(src & 0b1000_0000)
|
|
21
|
+
// if (src.done)
|
|
22
|
+
// console.warn("Unexpected end", src);
|
|
23
|
+
src = src.value
|
|
24
|
+
len += 1;
|
|
25
|
+
if (i == 3 && (src & 0b1111_0000) != 0)
|
|
26
|
+
console.warn("NonZeroIntPadding")
|
|
27
|
+
result |= ((src & 0b0111_1111)) << (6+7*i)
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
if (len > 1 && src == 0b0000_0000) {
|
|
31
|
+
console.warn("OverlongIntEncoding")
|
|
32
|
+
}
|
|
33
|
+
result ^= -sign;
|
|
34
|
+
|
|
35
|
+
return {result, remaining: Array.from(iter)};
|
|
36
|
+
}
|
|
37
|
+
function unpackString(pSrc: number[]): {result: string, remaining: number[]} {
|
|
38
|
+
var result = pSrc.slice(0, pSrc.indexOf(0))
|
|
39
|
+
pSrc = pSrc.slice(pSrc.indexOf(0), pSrc.length)
|
|
40
|
+
return {result: decoder.decode(new Uint8Array(result)), remaining: pSrc}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
export = {unpackInt, unpackString};
|