-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicStatResponse.js
71 lines (63 loc) · 2.15 KB
/
BasicStatResponse.js
1
2
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
import ProtocolError from "../../Error/ProtocolError.js";
import StatResponse from "./StatResponse.js";
export default class BasicStatResponse extends StatResponse {
/**
* @inheritDoc
*/
getType() {
return 0;
}
/**
* @inheritDoc
*/
readPayload(data) {
let motd, gametype, map, numplayers, maxplayers, hostport, hostip;
let skipNullBytes = 0;
let offset = 0;
do {
offset = 0;
[motd, offset] = this.readStringNTAndSkipNullBytes(data, offset, skipNullBytes++);
[gametype, offset] = this.readStringNT(data, offset);
[map, offset] = this.readStringNT(data, offset);
[numplayers, offset] = this.readStringNT(data, offset);
[maxplayers, offset] = this.readStringNT(data, offset);
hostport = data.readUInt16LE(offset);
offset += 2;
[hostip, offset] = this.readStringNT(data, offset);
} while (offset < data.length);
this.hostname = motd;
this.gametype = gametype;
this.map = map;
this.numplayers = parseInt(numplayers);
this.maxplayers = parseInt(maxplayers);
if (isNaN(this.numplayers) || isNaN(this.maxplayers)) {
throw new ProtocolError("Player count is not a number");
}
this.hostport = hostport;
this.hostip = hostip;
return this;
}
/**
* @inheritDoc
*/
writePayload() {
let motd = this.hostname || "";
let gametype = this.gametype || "";
let map = this.map || "";
let numplayers = this.numplayers.toString();
let maxplayers = this.maxplayers.toString();
let hostport = this.hostport;
let hostip = this.hostip || "";
let portBuffer = Buffer.alloc(2);
portBuffer.writeUInt16LE(hostport, 0);
return Buffer.concat([
this.createStringNT(motd),
this.createStringNT(gametype),
this.createStringNT(map),
this.createStringNT(numplayers),
this.createStringNT(maxplayers),
portBuffer,
this.createStringNT(hostip)
]);
}
}