Replace deprecated substr with substring (#355)
* Make the QueryRunner more readable * Remove use of deprecated substr and replace with substring, and some formatting
This commit is contained in:
parent
65dd876252
commit
5b01e1be17
|
@ -16,5 +16,5 @@ let start = readme.indexOf(marker_top);
|
||||||
start += marker_top.length;
|
start += marker_top.length;
|
||||||
const end = readme.indexOf(marker_bottom);
|
const end = readme.indexOf(marker_bottom);
|
||||||
|
|
||||||
const updated = readme.substr(0,start)+"\n\n"+generated+"\n"+readme.substr(end);
|
const updated = readme.substring(0,start)+"\n\n"+generated+"\n"+readme.substring(end);
|
||||||
fs.writeFileSync(readmeFilename, updated);
|
fs.writeFileSync(readmeFilename, updated);
|
||||||
|
|
|
@ -9,16 +9,20 @@ class GameResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
lookup(type) {
|
lookup(type) {
|
||||||
if(!type) throw Error('No game specified');
|
if(!type)
|
||||||
|
throw Error('No game specified');
|
||||||
|
|
||||||
if(type.substr(0,9) === 'protocol-') {
|
if(type.substring(0,9) === 'protocol-') {
|
||||||
return {
|
return {
|
||||||
protocol: type.substr(9)
|
protocol: type.substring(9)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const game = this.gamesByKey.get(type);
|
const game = this.gamesByKey.get(type);
|
||||||
if(!game) throw Error('Invalid game: '+type);
|
|
||||||
|
if(!game)
|
||||||
|
throw Error('Invalid game: '+type);
|
||||||
|
|
||||||
return game.options;
|
return game.options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +65,7 @@ class GameResolver {
|
||||||
for (let line of lines) {
|
for (let line of lines) {
|
||||||
// strip comments
|
// strip comments
|
||||||
const comment = line.indexOf('#');
|
const comment = line.indexOf('#');
|
||||||
if(comment !== -1) line = line.substr(0,comment);
|
if(comment !== -1) line = line.substring(0,comment);
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
if(!line) continue;
|
if(!line) continue;
|
||||||
|
|
||||||
|
@ -82,27 +86,34 @@ class GameResolver {
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
gamesByKey.set(key, game);
|
gamesByKey.set(key, game);
|
||||||
}
|
}
|
||||||
|
|
||||||
games.push(game);
|
games.push(game);
|
||||||
}
|
}
|
||||||
return { gamesByKey, games };
|
return { gamesByKey, games };
|
||||||
}
|
}
|
||||||
|
|
||||||
_parseList(str) {
|
_parseList(str) {
|
||||||
if(!str) return {};
|
if(!str)
|
||||||
const out = {};
|
return {};
|
||||||
|
|
||||||
|
let out = {};
|
||||||
for (const one of str.split(',')) {
|
for (const one of str.split(',')) {
|
||||||
const equals = one.indexOf('=');
|
const equals = one.indexOf('=');
|
||||||
const key = equals === -1 ? one : one.substr(0,equals);
|
const key = equals === -1 ? one : one.substring(0, equals);
|
||||||
|
|
||||||
/** @type {string|number|boolean} */
|
/** @type {string|number|boolean} */
|
||||||
let value = equals === -1 ? '' : one.substr(equals+1);
|
let value = equals === -1 ? '' : one.substring(equals + 1);
|
||||||
|
|
||||||
if(value === 'true' || value === '') value = true;
|
if(value === 'true' || value === '')
|
||||||
else if(value === 'false') value = false;
|
value = true;
|
||||||
else if(!isNaN(parseInt(value))) value = parseInt(value);
|
else if(value === 'false')
|
||||||
|
value = false;
|
||||||
|
else if(!isNaN(parseInt(value)))
|
||||||
|
value = parseInt(value);
|
||||||
|
|
||||||
out[key] = value;
|
out[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,8 @@ class GlobalUdpSocket {
|
||||||
log(fromAddress + ':' + fromPort + " <--UDP(" + this.port + ")");
|
log(fromAddress + ':' + fromPort + " <--UDP(" + this.port + ")");
|
||||||
log(HexUtil.debugDump(buffer));
|
log(HexUtil.debugDump(buffer));
|
||||||
});
|
});
|
||||||
for (const cb of this.callbacks) {
|
for (const callback of this.callbacks) {
|
||||||
cb(fromAddress, fromPort, buffer);
|
callback(fromAddress, fromPort, buffer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
udpSocket.on('error', e => {
|
udpSocket.on('error', e => {
|
||||||
|
@ -42,12 +42,14 @@ class GlobalUdpSocket {
|
||||||
|
|
||||||
async send(buffer, address, port, debug) {
|
async send(buffer, address, port, debug) {
|
||||||
const socket = await this._getSocket();
|
const socket = await this._getSocket();
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
this.logger._print(log => {
|
this.logger._print(log => {
|
||||||
log(address + ':' + port + " UDP(" + this.port + ")-->");
|
log(address + ':' + port + " UDP(" + this.port + ")-->");
|
||||||
log(HexUtil.debugDump(buffer));
|
log(HexUtil.debugDump(buffer));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await util.promisify(socket.send).bind(socket)(buffer,0,buffer.length,port,address);
|
await util.promisify(socket.send).bind(socket)(buffer,0,buffer.length,port,address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ class QueryRunner {
|
||||||
this.gameResolver = new GameResolver();
|
this.gameResolver = new GameResolver();
|
||||||
this.protocolResolver = new ProtocolResolver();
|
this.protocolResolver = new ProtocolResolver();
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(userOptions) {
|
async run(userOptions) {
|
||||||
for (const key of Object.keys(userOptions)) {
|
for (const key of Object.keys(userOptions)) {
|
||||||
const value = userOptions[key];
|
const value = userOptions[key];
|
||||||
|
@ -30,51 +31,38 @@ class QueryRunner {
|
||||||
port_query_offset: gameQueryPortOffset,
|
port_query_offset: gameQueryPortOffset,
|
||||||
...gameOptions
|
...gameOptions
|
||||||
} = this.gameResolver.lookup(userOptions.type);
|
} = this.gameResolver.lookup(userOptions.type);
|
||||||
const attempts = [];
|
let attempts = [];
|
||||||
|
|
||||||
|
const optionsCollection = {
|
||||||
|
...defaultOptions,
|
||||||
|
...gameOptions,
|
||||||
|
...userOptions
|
||||||
|
};
|
||||||
|
|
||||||
|
const addAttemptWithPort = port => {
|
||||||
|
attempts.push({
|
||||||
|
...optionsCollection,
|
||||||
|
port
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (userOptions.port) {
|
if (userOptions.port) {
|
||||||
if (gameQueryPortOffset && !userOptions.givenPortOnly) {
|
if(!userOptions.givenPortOnly) {
|
||||||
attempts.push({
|
if (gameQueryPortOffset)
|
||||||
...defaultOptions,
|
addAttemptWithPort(userOptions.port + gameQueryPortOffset);
|
||||||
...gameOptions,
|
|
||||||
...userOptions,
|
if (userOptions.port === gameOptions.port && gameQueryPort)
|
||||||
port: userOptions.port + gameQueryPortOffset
|
addAttemptWithPort(gameQueryPort);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
if (userOptions.port === gameOptions.port && gameQueryPort && !userOptions.givenPortOnly) {
|
|
||||||
attempts.push({
|
attempts.push(optionsCollection);
|
||||||
...defaultOptions,
|
|
||||||
...gameOptions,
|
|
||||||
...userOptions,
|
|
||||||
port: gameQueryPort
|
|
||||||
});
|
|
||||||
}
|
|
||||||
attempts.push({
|
|
||||||
...defaultOptions,
|
|
||||||
...gameOptions,
|
|
||||||
...userOptions
|
|
||||||
});
|
|
||||||
} else if (gameQueryPort) {
|
} else if (gameQueryPort) {
|
||||||
attempts.push({
|
addAttemptWithPort(gameQueryPort);
|
||||||
...defaultOptions,
|
|
||||||
...gameOptions,
|
|
||||||
...userOptions,
|
|
||||||
port: gameQueryPort
|
|
||||||
});
|
|
||||||
} else if (gameOptions.port) {
|
} else if (gameOptions.port) {
|
||||||
attempts.push({
|
addAttemptWithPort(gameOptions.port + (gameQueryPortOffset || 0));
|
||||||
...defaultOptions,
|
|
||||||
...gameOptions,
|
|
||||||
...userOptions,
|
|
||||||
port: gameOptions.port + (gameQueryPortOffset || 0)
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// Hopefully the request doesn't need a port. If it does, it'll fail when making the request.
|
// Hopefully the request doesn't need a port. If it does, it'll fail when making the request.
|
||||||
attempts.push({
|
attempts.push(optionsCollection);
|
||||||
...defaultOptions,
|
|
||||||
...gameOptions,
|
|
||||||
...userOptions
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const numRetries = userOptions.maxAttempts || gameOptions.maxAttempts || defaultOptions.maxAttempts;
|
const numRetries = userOptions.maxAttempts || gameOptions.maxAttempts || defaultOptions.maxAttempts;
|
||||||
|
@ -97,6 +85,7 @@ class QueryRunner {
|
||||||
for (const e of errors) {
|
for (const e of errors) {
|
||||||
err.stack += '\n' + e.stack;
|
err.stack += '\n' + e.stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,12 @@ class Gamedig {
|
||||||
}
|
}
|
||||||
|
|
||||||
static getInstance() {
|
static getInstance() {
|
||||||
if (!singleton) singleton = new Gamedig();
|
if (!singleton)
|
||||||
|
singleton = new Gamedig();
|
||||||
|
|
||||||
return singleton;
|
return singleton;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async query(...args) {
|
static async query(...args) {
|
||||||
return await Gamedig.getInstance().query(...args);
|
return await Gamedig.getInstance().query(...args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@ class Teamspeak2 extends Core {
|
||||||
const data = await this.sendCommand(socket, 'si');
|
const data = await this.sendCommand(socket, 'si');
|
||||||
for (const line of data.split('\r\n')) {
|
for (const line of data.split('\r\n')) {
|
||||||
const equals = line.indexOf('=');
|
const equals = line.indexOf('=');
|
||||||
const key = equals === -1 ? line : line.substr(0,equals);
|
const key = equals === -1 ? line : line.substring(0,equals);
|
||||||
const value = equals === -1 ? '' : line.substr(equals+1);
|
const value = equals === -1 ? '' : line.substring(equals+1);
|
||||||
state.raw[key] = value;
|
state.raw[key] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,8 +53,8 @@ class Teamspeak3 extends Core {
|
||||||
const unit = {};
|
const unit = {};
|
||||||
for (const field of split) {
|
for (const field of split) {
|
||||||
const equals = field.indexOf('=');
|
const equals = field.indexOf('=');
|
||||||
const key = equals === -1 ? field : field.substr(0,equals);
|
const key = equals === -1 ? field : field.substring(0, equals);
|
||||||
const value = equals === -1 ? '' : field.substr(equals+1)
|
const value = equals === -1 ? '' : field.substring(equals + 1)
|
||||||
.replace(/\\s/g,' ').replace(/\\\//g,'/');
|
.replace(/\\s/g,' ').replace(/\\\//g,'/');
|
||||||
unit[key] = value;
|
unit[key] = value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,7 +133,7 @@ class Tribes1 extends Core {
|
||||||
if (!str) return [];
|
if (!str) return [];
|
||||||
return ('?'+str)
|
return ('?'+str)
|
||||||
.split('\t')
|
.split('\t')
|
||||||
.map((a) => a.substr(1).trim().toLowerCase())
|
.map((a) => a.substring(1).trim().toLowerCase())
|
||||||
.map((a) => a === 'team name' ? 'name' : a)
|
.map((a) => a === 'team name' ? 'name' : a)
|
||||||
.map((a) => a === 'player name' ? 'name' : a);
|
.map((a) => a === 'player name' ? 'name' : a);
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,9 +64,9 @@ function splitFields(str,subMode) {
|
||||||
}
|
}
|
||||||
for (const one of split) {
|
for (const one of split) {
|
||||||
const equal = one.indexOf(splitter);
|
const equal = one.indexOf(splitter);
|
||||||
const key = equal === -1 ? one : one.substr(0,equal);
|
const key = equal === -1 ? one : one.substring(0,equal);
|
||||||
if(!key || key === '\0') continue;
|
if(!key || key === '\0') continue;
|
||||||
const value = equal === -1 ? '' : one.substr(equal+splitter.length);
|
const value = equal === -1 ? '' : one.substring(equal+splitter.length);
|
||||||
if(!subMode && key === 'CHANNEL') out.CHANNELS.push(splitFields(value,true));
|
if(!subMode && key === 'CHANNEL') out.CHANNELS.push(splitFields(value,true));
|
||||||
else if(!subMode && key === 'CLIENT') out.CLIENTS.push(splitFields(value,true));
|
else if(!subMode && key === 'CLIENT') out.CLIENTS.push(splitFields(value,true));
|
||||||
else out[key] = value;
|
else out[key] = value;
|
||||||
|
|
Loading…
Reference in New Issue