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:
CosminPerRam 2023-09-13 17:31:58 +03:00 committed by GitHub
parent 65dd876252
commit 5b01e1be17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 61 deletions

View file

@ -17,6 +17,7 @@ class QueryRunner {
this.gameResolver = new GameResolver();
this.protocolResolver = new ProtocolResolver();
}
async run(userOptions) {
for (const key of Object.keys(userOptions)) {
const value = userOptions[key];
@ -30,51 +31,38 @@ class QueryRunner {
port_query_offset: gameQueryPortOffset,
...gameOptions
} = 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 (gameQueryPortOffset && !userOptions.givenPortOnly) {
attempts.push({
...defaultOptions,
...gameOptions,
...userOptions,
port: userOptions.port + gameQueryPortOffset
});
if(!userOptions.givenPortOnly) {
if (gameQueryPortOffset)
addAttemptWithPort(userOptions.port + gameQueryPortOffset);
if (userOptions.port === gameOptions.port && gameQueryPort)
addAttemptWithPort(gameQueryPort);
}
if (userOptions.port === gameOptions.port && gameQueryPort && !userOptions.givenPortOnly) {
attempts.push({
...defaultOptions,
...gameOptions,
...userOptions,
port: gameQueryPort
});
}
attempts.push({
...defaultOptions,
...gameOptions,
...userOptions
});
attempts.push(optionsCollection);
} else if (gameQueryPort) {
attempts.push({
...defaultOptions,
...gameOptions,
...userOptions,
port: gameQueryPort
});
addAttemptWithPort(gameQueryPort);
} else if (gameOptions.port) {
attempts.push({
...defaultOptions,
...gameOptions,
...userOptions,
port: gameOptions.port + (gameQueryPortOffset || 0)
});
addAttemptWithPort(gameOptions.port + (gameQueryPortOffset || 0));
} else {
// Hopefully the request doesn't need a port. If it does, it'll fail when making the request.
attempts.push({
...defaultOptions,
...gameOptions,
...userOptions
});
attempts.push(optionsCollection);
}
const numRetries = userOptions.maxAttempts || gameOptions.maxAttempts || defaultOptions.maxAttempts;
@ -97,6 +85,7 @@ class QueryRunner {
for (const e of errors) {
err.stack += '\n' + e.stack;
}
throw err;
}