mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-18 09:35:50 +00:00
Convert tabs to spaces
This commit is contained in:
parent
0dd25bfcda
commit
3674d384d0
39 changed files with 2414 additions and 2414 deletions
|
|
@ -1,99 +1,99 @@
|
|||
const varint = require('varint'),
|
||||
async = require('async');
|
||||
async = require('async');
|
||||
|
||||
function varIntBuffer(num) {
|
||||
return Buffer.from(varint.encode(num));
|
||||
return Buffer.from(varint.encode(num));
|
||||
}
|
||||
function buildPacket(id,data) {
|
||||
if(!data) data = Buffer.from([]);
|
||||
if(!data) data = Buffer.from([]);
|
||||
const idBuffer = varIntBuffer(id);
|
||||
return Buffer.concat([
|
||||
varIntBuffer(data.length+idBuffer.length),
|
||||
idBuffer,
|
||||
data
|
||||
]);
|
||||
return Buffer.concat([
|
||||
varIntBuffer(data.length+idBuffer.length),
|
||||
idBuffer,
|
||||
data
|
||||
]);
|
||||
}
|
||||
|
||||
class MinecraftPing extends require('./core') {
|
||||
run(state) {
|
||||
let receivedData;
|
||||
run(state) {
|
||||
let receivedData;
|
||||
|
||||
async.series([
|
||||
(c) => {
|
||||
// build and send handshake and status TCP packet
|
||||
async.series([
|
||||
(c) => {
|
||||
// build and send handshake and status TCP packet
|
||||
|
||||
const portBuf = Buffer.alloc(2);
|
||||
portBuf.writeUInt16BE(this.options.port_query,0);
|
||||
portBuf.writeUInt16BE(this.options.port_query,0);
|
||||
|
||||
const addressBuf = Buffer.from(this.options.address,'utf8');
|
||||
|
||||
const bufs = [
|
||||
varIntBuffer(4),
|
||||
varIntBuffer(addressBuf.length),
|
||||
addressBuf,
|
||||
portBuf,
|
||||
varIntBuffer(1)
|
||||
];
|
||||
varIntBuffer(4),
|
||||
varIntBuffer(addressBuf.length),
|
||||
addressBuf,
|
||||
portBuf,
|
||||
varIntBuffer(1)
|
||||
];
|
||||
|
||||
const outBuffer = Buffer.concat([
|
||||
buildPacket(0,Buffer.concat(bufs)),
|
||||
buildPacket(0)
|
||||
]);
|
||||
buildPacket(0,Buffer.concat(bufs)),
|
||||
buildPacket(0)
|
||||
]);
|
||||
|
||||
this.tcpSend(outBuffer, (data) => {
|
||||
if(data.length < 10) return false;
|
||||
this.tcpSend(outBuffer, (data) => {
|
||||
if(data.length < 10) return false;
|
||||
const expected = varint.decode(data);
|
||||
data = data.slice(varint.decode.bytes);
|
||||
if(data.length < expected) return false;
|
||||
receivedData = data;
|
||||
c();
|
||||
return true;
|
||||
});
|
||||
},
|
||||
(c) => {
|
||||
// parse response
|
||||
data = data.slice(varint.decode.bytes);
|
||||
if(data.length < expected) return false;
|
||||
receivedData = data;
|
||||
c();
|
||||
return true;
|
||||
});
|
||||
},
|
||||
(c) => {
|
||||
// parse response
|
||||
|
||||
let data = receivedData;
|
||||
const packetId = varint.decode(data);
|
||||
if(this.debug) console.log("Packet ID: "+packetId);
|
||||
data = data.slice(varint.decode.bytes);
|
||||
if(this.debug) console.log("Packet ID: "+packetId);
|
||||
data = data.slice(varint.decode.bytes);
|
||||
|
||||
const strLen = varint.decode(data);
|
||||
if(this.debug) console.log("String Length: "+strLen);
|
||||
data = data.slice(varint.decode.bytes);
|
||||
if(this.debug) console.log("String Length: "+strLen);
|
||||
data = data.slice(varint.decode.bytes);
|
||||
|
||||
const str = data.toString('utf8');
|
||||
if(this.debug) {
|
||||
console.log(str);
|
||||
}
|
||||
if(this.debug) {
|
||||
console.log(str);
|
||||
}
|
||||
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(str);
|
||||
delete json.favicon;
|
||||
} catch(e) {
|
||||
return this.fatal('Invalid JSON');
|
||||
}
|
||||
try {
|
||||
json = JSON.parse(str);
|
||||
delete json.favicon;
|
||||
} catch(e) {
|
||||
return this.fatal('Invalid JSON');
|
||||
}
|
||||
|
||||
state.raw.version = json.version.name;
|
||||
state.maxplayers = json.players.max;
|
||||
state.raw.description = json.description.text;
|
||||
if(json.players.sample) {
|
||||
for(const player of json.players.sample) {
|
||||
state.players.push({
|
||||
id: player.id,
|
||||
name: player.name
|
||||
});
|
||||
}
|
||||
}
|
||||
while(state.players.length < json.players.online) {
|
||||
state.players.push({});
|
||||
}
|
||||
state.raw.version = json.version.name;
|
||||
state.maxplayers = json.players.max;
|
||||
state.raw.description = json.description.text;
|
||||
if(json.players.sample) {
|
||||
for(const player of json.players.sample) {
|
||||
state.players.push({
|
||||
id: player.id,
|
||||
name: player.name
|
||||
});
|
||||
}
|
||||
}
|
||||
while(state.players.length < json.players.online) {
|
||||
state.players.push({});
|
||||
}
|
||||
|
||||
this.finish(state);
|
||||
}
|
||||
]);
|
||||
}
|
||||
this.finish(state);
|
||||
}
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MinecraftPing;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue