Skip to content

Commit

Permalink
cleanup & CI
Browse files Browse the repository at this point in the history
  • Loading branch information
MBR-0001 committed Dec 27, 2020
1 parent 61abc3d commit 2b6e9ad
Showing 1 changed file with 35 additions and 55 deletions.
90 changes: 35 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,16 @@ class SourceQuery {
this.unpacker = new SQUnpacker(this.client, this.timeout);
}

queryEnded() {
this.queries--;
getInfo() {
return this.getData(this._getInfo.bind(this));
}

if (this.autoclose) setTimeout(() => this.close(), 250);
getPlayers() {
return this.getData(this._getPlayers.bind(this));
}

getRules() {
return this.getData(this._getRules.bind(this));
}

/**
Expand Down Expand Up @@ -180,81 +186,55 @@ class SourceQuery {
});
}

getData(challenge_fn, send_header, receive_header, parse_fn) {
return new Promise((resolve, reject) => {
this.send(challenge_fn(send_header), [ids.S2A_SERVERQUERY_GETCHALLENGE, receive_header]).then(data => {
if (data.header == ids.S2A_SERVERQUERY_GETCHALLENGE) {
this.send(challenge_fn(send_header, data.buffer.readInt32LE()), [receive_header]).then(({ buffer }) => {
resolve(parse_fn(buffer));
}).catch(reject);
}
else if (data.header == receive_header) resolve(parse_fn(data.buffer));
else reject(new Error("Invalid header for " + send_header + ": " + data.header));
}).catch(reject);
});
}

getInfo() {
getData(request_fn) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
let attempts = 0;
let data = null;

do {
data = await this._getInfo().catch(() => {});
data = await request_fn().catch(e => {
if (process.env.CI) console.log(e);
});
attempts++;
}
while (attempts < 10 && !data);

if (!data) reject(new Error("info timed out"));
if (!data) reject(new Error("timed out"));
else resolve(data);
});
}

_getInfo() {
return this.getData(Util.createInfoChallenge, ids.A2S_INFO, ids.S2A_INFO, Util.parseInfoBuffer);
_getData(challenge_fn, send_header, receive_header, parse_fn) {
return new Promise((resolve, reject) => {
this.send(challenge_fn(send_header), [ids.S2A_SERVERQUERY_GETCHALLENGE, receive_header]).then(data => {
if (data.header == ids.S2A_SERVERQUERY_GETCHALLENGE) {
this.send(challenge_fn(send_header, data.buffer.readInt32LE()), [receive_header]).then(({ buffer }) => {
resolve(parse_fn(buffer));
}).catch(reject);
}
else if (data.header == receive_header) resolve(parse_fn(data.buffer));
else reject(new Error("Invalid header for " + send_header + ": " + data.header));
}).catch(reject);
});
}

getPlayers() {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
let attempts = 0;
let data = null;

do {
data = await this._getPlayers().catch(() => {});
attempts++;
}
while (attempts < 10 && !data);

if (!data) reject(new Error("players timed out"));
else resolve(data);
});
_getInfo() {
return this._getData(Util.createInfoChallenge, ids.A2S_INFO, ids.S2A_INFO, Util.parseInfoBuffer);
}

_getPlayers() {
return this.getData(Util.createChallenge, ids.A2S_PLAYER, ids.S2A_PLAYER, Util.parsePlayerBuffer);
return this._getData(Util.createChallenge, ids.A2S_PLAYER, ids.S2A_PLAYER, Util.parsePlayerBuffer);
}

getRules() {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
let attempts = 0;
let data = null;

do {
data = await this._getRules().catch(() => {});
attempts++;
}
while (attempts < 10 && !data);

if (!data) reject(new Error("rules timed out"));
else resolve(data);
});
_getRules() {
return this._getData(Util.createChallenge, ids.A2S_RULES, ids.S2A_RULES, Util.parseRulesBuffer);
}

_getRules() {
return this.getData(Util.createChallenge, ids.A2S_RULES, ids.S2A_RULES, Util.parseRulesBuffer);
queryEnded() {
this.queries--;

if (this.autoclose) setTimeout(() => this.close(), 250);
}

close() {
Expand Down

0 comments on commit 2b6e9ad

Please sign in to comment.