mirror of
https://github.com/tribufu/node-gamedig
synced 2026-06-01 09:42:41 +00:00
Initial es6 async conversion work
This commit is contained in:
parent
a054557f10
commit
77b2cc1c7f
10 changed files with 773 additions and 748 deletions
|
|
@ -1,5 +1,4 @@
|
|||
const async = require('async'),
|
||||
Core = require('./core');
|
||||
const Core = require('./core');
|
||||
|
||||
class Battlefield extends Core {
|
||||
constructor() {
|
||||
|
|
@ -8,114 +7,128 @@ class Battlefield extends Core {
|
|||
this.isBadCompany2 = false;
|
||||
}
|
||||
|
||||
run(state) {
|
||||
async.series([
|
||||
(c) => {
|
||||
this.query(['serverInfo'], (data) => {
|
||||
if(this.debug) console.log(data);
|
||||
if(data.shift() !== 'OK') return this.fatal('Missing OK');
|
||||
async run(state) {
|
||||
await this.withTcp(async socket => {
|
||||
{
|
||||
const data = await this.query(socket, ['serverInfo']);
|
||||
state.raw.name = data.shift();
|
||||
state.raw.numplayers = parseInt(data.shift());
|
||||
state.maxplayers = parseInt(data.shift());
|
||||
state.raw.gametype = data.shift();
|
||||
state.map = data.shift();
|
||||
state.raw.roundsplayed = parseInt(data.shift());
|
||||
state.raw.roundstotal = parseInt(data.shift());
|
||||
|
||||
state.raw.name = data.shift();
|
||||
state.raw.numplayers = parseInt(data.shift());
|
||||
state.maxplayers = parseInt(data.shift());
|
||||
state.raw.gametype = data.shift();
|
||||
state.map = data.shift();
|
||||
state.raw.roundsplayed = parseInt(data.shift());
|
||||
state.raw.roundstotal = parseInt(data.shift());
|
||||
const teamCount = data.shift();
|
||||
state.raw.teams = [];
|
||||
for (let i = 0; i < teamCount; i++) {
|
||||
const tickets = parseFloat(data.shift());
|
||||
state.raw.teams.push({
|
||||
tickets: tickets
|
||||
});
|
||||
}
|
||||
|
||||
const teamCount = data.shift();
|
||||
state.raw.teams = [];
|
||||
for(let i = 0; i < teamCount; i++) {
|
||||
const tickets = parseFloat(data.shift());
|
||||
state.raw.teams.push({
|
||||
tickets:tickets
|
||||
});
|
||||
}
|
||||
|
||||
state.raw.targetscore = parseInt(data.shift());
|
||||
state.raw.targetscore = parseInt(data.shift());
|
||||
data.shift();
|
||||
state.raw.ranked = (data.shift() === 'true');
|
||||
state.raw.punkbuster = (data.shift() === 'true');
|
||||
state.password = (data.shift() === 'true');
|
||||
state.raw.uptime = parseInt(data.shift());
|
||||
state.raw.roundtime = parseInt(data.shift());
|
||||
if (this.isBadCompany2) {
|
||||
data.shift();
|
||||
state.raw.ranked = (data.shift() === 'true');
|
||||
state.raw.punkbuster = (data.shift() === 'true');
|
||||
state.password = (data.shift() === 'true');
|
||||
state.raw.uptime = parseInt(data.shift());
|
||||
state.raw.roundtime = parseInt(data.shift());
|
||||
if(this.isBadCompany2) {
|
||||
data.shift();
|
||||
data.shift();
|
||||
}
|
||||
state.raw.ip = data.shift();
|
||||
state.raw.punkbusterversion = data.shift();
|
||||
state.raw.joinqueue = (data.shift() === 'true');
|
||||
state.raw.region = data.shift();
|
||||
if(!this.isBadCompany2) {
|
||||
state.raw.pingsite = data.shift();
|
||||
state.raw.country = data.shift();
|
||||
state.raw.quickmatch = (data.shift() === 'true');
|
||||
}
|
||||
|
||||
c();
|
||||
});
|
||||
},
|
||||
(c) => {
|
||||
this.query(['version'], (data) => {
|
||||
if(this.debug) console.log(data);
|
||||
if(data[0] !== 'OK') return this.fatal('Missing OK');
|
||||
|
||||
state.raw.version = data[2];
|
||||
|
||||
c();
|
||||
});
|
||||
},
|
||||
(c) => {
|
||||
this.query(['listPlayers','all'], (data) => {
|
||||
if(this.debug) console.log(data);
|
||||
if(data.shift() !== 'OK') return this.fatal('Missing OK');
|
||||
|
||||
const fieldCount = parseInt(data.shift());
|
||||
const fields = [];
|
||||
for(let i = 0; i < fieldCount; i++) {
|
||||
fields.push(data.shift());
|
||||
}
|
||||
const numplayers = data.shift();
|
||||
for(let i = 0; i < numplayers; i++) {
|
||||
const player = {};
|
||||
for (let key of fields) {
|
||||
let value = data.shift();
|
||||
|
||||
if(key === 'teamId') key = 'team';
|
||||
else if(key === 'squadId') key = 'squad';
|
||||
|
||||
if(
|
||||
key === 'kills'
|
||||
|| key === 'deaths'
|
||||
|| key === 'score'
|
||||
|| key === 'rank'
|
||||
|| key === 'team'
|
||||
|| key === 'squad'
|
||||
|| key === 'ping'
|
||||
|| key === 'type'
|
||||
) {
|
||||
value = parseInt(value);
|
||||
}
|
||||
|
||||
player[key] = value;
|
||||
}
|
||||
state.players.push(player);
|
||||
}
|
||||
|
||||
this.finish(state);
|
||||
});
|
||||
data.shift();
|
||||
}
|
||||
state.raw.ip = data.shift();
|
||||
state.raw.punkbusterversion = data.shift();
|
||||
state.raw.joinqueue = (data.shift() === 'true');
|
||||
state.raw.region = data.shift();
|
||||
if (!this.isBadCompany2) {
|
||||
state.raw.pingsite = data.shift();
|
||||
state.raw.country = data.shift();
|
||||
state.raw.quickmatch = (data.shift() === 'true');
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const data = await this.query(socket, ['version']);
|
||||
data.shift();
|
||||
state.raw.version = data.shift();
|
||||
}
|
||||
|
||||
{
|
||||
const data = await this.query(socket, ['listPlayers', 'all']);
|
||||
const fieldCount = parseInt(data.shift());
|
||||
const fields = [];
|
||||
for (let i = 0; i < fieldCount; i++) {
|
||||
fields.push(data.shift());
|
||||
}
|
||||
const numplayers = data.shift();
|
||||
for (let i = 0; i < numplayers; i++) {
|
||||
const player = {};
|
||||
for (let key of fields) {
|
||||
let value = data.shift();
|
||||
|
||||
if (key === 'teamId') key = 'team';
|
||||
else if (key === 'squadId') key = 'squad';
|
||||
|
||||
if (
|
||||
key === 'kills'
|
||||
|| key === 'deaths'
|
||||
|| key === 'score'
|
||||
|| key === 'rank'
|
||||
|| key === 'team'
|
||||
|| key === 'squad'
|
||||
|| key === 'ping'
|
||||
|| key === 'type'
|
||||
) {
|
||||
value = parseInt(value);
|
||||
}
|
||||
|
||||
player[key] = value;
|
||||
}
|
||||
state.players.push(player);
|
||||
}
|
||||
}
|
||||
]);
|
||||
}
|
||||
query(params,c) {
|
||||
this.tcpSend(buildPacket(params), (data) => {
|
||||
const decoded = this.decodePacket(data);
|
||||
if(!decoded) return false;
|
||||
c(decoded);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
async query(socket, params) {
|
||||
const outPacket = this.buildPacket(params);
|
||||
return await this.tcpSend(socket, outPacket, (data) => {
|
||||
const decoded = this.decodePacket(data);
|
||||
if(decoded) {
|
||||
if(this.debug) console.log(decoded);
|
||||
if(decoded.shift() !== 'OK') throw new Error('Missing OK');
|
||||
return decoded;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
buildPacket(params) {
|
||||
const paramBuffers = [];
|
||||
for (const param of params) {
|
||||
paramBuffers.push(Buffer.from(param,'utf8'));
|
||||
}
|
||||
|
||||
let totalLength = 12;
|
||||
for (const paramBuffer of paramBuffers) {
|
||||
totalLength += paramBuffer.length+1+4;
|
||||
}
|
||||
|
||||
const b = Buffer.alloc(totalLength);
|
||||
b.writeUInt32LE(0,0);
|
||||
b.writeUInt32LE(totalLength,4);
|
||||
b.writeUInt32LE(params.length,8);
|
||||
let offset = 12;
|
||||
for (const paramBuffer of paramBuffers) {
|
||||
b.writeUInt32LE(paramBuffer.length, offset); offset += 4;
|
||||
paramBuffer.copy(b, offset); offset += paramBuffer.length;
|
||||
b.writeUInt8(0, offset); offset += 1;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
decodePacket(buffer) {
|
||||
if(buffer.length < 8) return false;
|
||||
const reader = this.reader(buffer);
|
||||
|
|
@ -134,29 +147,4 @@ class Battlefield extends Core {
|
|||
}
|
||||
}
|
||||
|
||||
function buildPacket(params) {
|
||||
const paramBuffers = [];
|
||||
for (const param of params) {
|
||||
paramBuffers.push(Buffer.from(param,'utf8'));
|
||||
}
|
||||
|
||||
let totalLength = 12;
|
||||
for (const paramBuffer of paramBuffers) {
|
||||
totalLength += paramBuffer.length+1+4;
|
||||
}
|
||||
|
||||
const b = Buffer.alloc(totalLength);
|
||||
b.writeUInt32LE(0,0);
|
||||
b.writeUInt32LE(totalLength,4);
|
||||
b.writeUInt32LE(params.length,8);
|
||||
let offset = 12;
|
||||
for (const paramBuffer of paramBuffers) {
|
||||
b.writeUInt32LE(paramBuffer.length, offset); offset += 4;
|
||||
paramBuffer.copy(b, offset); offset += paramBuffer.length;
|
||||
b.writeUInt8(0, offset); offset += 1;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
module.exports = Battlefield;
|
||||
Loading…
Add table
Add a link
Reference in a new issue