mirror of
https://github.com/gamedig/node-gamedig.git
synced 2024-11-18 01:30:39 +01:00
bc6b5c9225
Move everything around Add another 50 or so games *** 'port' option should now be CONNECT port, not query port *** add reference for many missing games
93 lines
2.1 KiB
JavaScript
93 lines
2.1 KiB
JavaScript
var varint = require('varint'),
|
|
async = require('async');
|
|
|
|
function varIntBuffer(num) {
|
|
return new Buffer(varint.encode(num));
|
|
}
|
|
function buildPacket(id,data) {
|
|
if(!data) data = new Buffer(0);
|
|
var idBuffer = varIntBuffer(id);
|
|
return Buffer.concat([
|
|
varIntBuffer(data.length+idBuffer.length),
|
|
idBuffer,
|
|
data
|
|
]);
|
|
}
|
|
|
|
module.exports = require('./core').extend({
|
|
run: function(state) {
|
|
var self = this;
|
|
var receivedData;
|
|
|
|
async.series([
|
|
function(c) {
|
|
// build and send handshake and status TCP packet
|
|
|
|
var portBuf = new Buffer(2);
|
|
portBuf.writeUInt16BE(self.options.port_query,0);
|
|
|
|
var addressBuf = new Buffer(self.options.address,'utf8');
|
|
|
|
var bufs = [
|
|
varIntBuffer(4),
|
|
varIntBuffer(addressBuf.length),
|
|
addressBuf,
|
|
portBuf,
|
|
varIntBuffer(1)
|
|
];
|
|
|
|
var outBuffer = Buffer.concat([
|
|
buildPacket(0,Buffer.concat(bufs)),
|
|
buildPacket(0)
|
|
]);
|
|
|
|
self.tcpSend(outBuffer, function(data) {
|
|
if(data.length < 10) return false;
|
|
var expected = varint.decode(data);
|
|
data = data.slice(varint.decode.bytesRead);
|
|
if(data.length < expected) return false;
|
|
receivedData = data;
|
|
c();
|
|
return true;
|
|
});
|
|
},
|
|
function(c) {
|
|
// parse response
|
|
|
|
var data = receivedData;
|
|
var packetId = varint.decode(data);
|
|
data = data.slice(varint.decode.bytesRead);
|
|
|
|
var strLen = varint.decode(data);
|
|
data = data.slice(varint.decode.bytesRead);
|
|
|
|
var str = data.toString('utf8');
|
|
var json;
|
|
try {
|
|
json = JSON.parse(str);
|
|
delete json.favicon;
|
|
if(self.debug) console.log(json);
|
|
} catch(e) {
|
|
return self.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(var i = 0; i < json.players.sample.length; i++) {
|
|
state.players.push({
|
|
id: json.players.sample[i].id,
|
|
name: json.players.sample[i].name
|
|
});
|
|
}
|
|
}
|
|
while(state.players.length < json.players.online) {
|
|
state.players.push({});
|
|
}
|
|
|
|
self.finish(state);
|
|
}
|
|
]);
|
|
}
|
|
});
|