mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-18 09:35:50 +00:00
More async conversions
This commit is contained in:
parent
77b2cc1c7f
commit
9b8423b20a
15 changed files with 859 additions and 704 deletions
|
|
@ -1,68 +1,57 @@
|
|||
const async = require('async'),
|
||||
Core = require('./core');
|
||||
const Core = require('./core');
|
||||
|
||||
class Gamespy1 extends Core {
|
||||
constructor() {
|
||||
super();
|
||||
this.sessionId = 1;
|
||||
this.encoding = 'latin1';
|
||||
this.byteorder = 'be';
|
||||
}
|
||||
|
||||
run(state) {
|
||||
async.series([
|
||||
(c) => {
|
||||
this.sendPacket('info', (data) => {
|
||||
state.raw = data;
|
||||
if('hostname' in state.raw) state.name = state.raw.hostname;
|
||||
if('mapname' in state.raw) state.map = state.raw.mapname;
|
||||
if(this.trueTest(state.raw.password)) state.password = true;
|
||||
if('maxplayers' in state.raw) state.maxplayers = parseInt(state.raw.maxplayers);
|
||||
c();
|
||||
});
|
||||
},
|
||||
(c) => {
|
||||
this.sendPacket('rules', (data) => {
|
||||
state.raw.rules = data;
|
||||
c();
|
||||
});
|
||||
},
|
||||
(c) => {
|
||||
this.sendPacket('players', (data) => {
|
||||
const players = {};
|
||||
const teams = {};
|
||||
for(const ident of Object.keys(data)) {
|
||||
const split = ident.split('_');
|
||||
let key = split[0];
|
||||
const id = split[1];
|
||||
let value = data[ident];
|
||||
async run(state) {
|
||||
{
|
||||
const data = await this.sendPacket('info');
|
||||
state.raw = data;
|
||||
if ('hostname' in state.raw) state.name = state.raw.hostname;
|
||||
if ('mapname' in state.raw) state.map = state.raw.mapname;
|
||||
if (this.trueTest(state.raw.password)) state.password = true;
|
||||
if ('maxplayers' in state.raw) state.maxplayers = parseInt(state.raw.maxplayers);
|
||||
}
|
||||
{
|
||||
const data = await this.sendPacket('rules');
|
||||
state.raw.rules = data;
|
||||
}
|
||||
{
|
||||
const data = await this.sendPacket('players');
|
||||
const players = {};
|
||||
const teams = {};
|
||||
for (const ident of Object.keys(data)) {
|
||||
const split = ident.split('_');
|
||||
let key = split[0];
|
||||
const id = split[1];
|
||||
let value = data[ident];
|
||||
|
||||
if(key === 'teamname') {
|
||||
teams[id] = value;
|
||||
} else {
|
||||
if(!(id in players)) players[id] = {};
|
||||
if(key === 'playername') key = 'name';
|
||||
else if(key === 'team') value = parseInt(value);
|
||||
else if(key === 'score' || key === 'ping' || key === 'deaths') value = parseInt(value);
|
||||
players[id][key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
state.raw.teams = teams;
|
||||
for(const id of Object.keys(players)) {
|
||||
state.players.push(players[id]);
|
||||
}
|
||||
this.finish(state);
|
||||
});
|
||||
if (key === 'teamname') {
|
||||
teams[id] = value;
|
||||
} else {
|
||||
if (!(id in players)) players[id] = {};
|
||||
if (key === 'playername') key = 'name';
|
||||
else if (key === 'team') value = parseInt(value);
|
||||
else if (key === 'score' || key === 'ping' || key === 'deaths') value = parseInt(value);
|
||||
players[id][key] = value;
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
state.raw.teams = teams;
|
||||
for (const id of Object.keys(players)) {
|
||||
state.players.push(players[id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sendPacket(type,callback) {
|
||||
async sendPacket(type) {
|
||||
const queryId = '';
|
||||
const output = {};
|
||||
this.udpSend('\\'+type+'\\', (buffer) => {
|
||||
return await this.udpSend('\\'+type+'\\', buffer => {
|
||||
const reader = this.reader(buffer);
|
||||
const str = reader.string({length:buffer.length});
|
||||
const split = str.split('\\');
|
||||
|
|
@ -79,8 +68,7 @@ class Gamespy1 extends Core {
|
|||
if('final' in output) {
|
||||
delete output.final;
|
||||
delete output.queryid;
|
||||
callback(output);
|
||||
return true;
|
||||
return output;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue