fix: Solve GS1 non-name key values being badly parsed (#342)

* Add initial fix

* fix: properly parse or not number values

* fix: Dont parse empty strings and add todo! comments
This commit is contained in:
CosminPerRam 2023-08-16 00:44:32 +03:00 committed by GitHub
parent 4365f4ae0a
commit da7d3eebd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 16 deletions

View File

@ -10,21 +10,22 @@ const stringKeys = new Set([
function normalizeEntry([key,value]) { function normalizeEntry([key,value]) {
key = key.toLowerCase(); key = key.toLowerCase();
const split = key.split('_'); const split = key.split('_');
let keyType; let keyType = key;
if (split.length === 2 && !isNaN(parseInt(split[1]))) {
if (split.length === 2 && !isNaN(Number(split[1]))) {
keyType = split[0]; keyType = split[0];
} else {
keyType = key;
} }
if (!stringKeys.has(keyType) && !keyType.includes('name')) {
if (!stringKeys.has(keyType) && !keyType.includes('name')) { // todo! the latter check might be problematic, fails on key "name_tag_distance_scope"
if (value.toLowerCase() === 'true') { if (value.toLowerCase() === 'true') {
value = true; value = true;
} else if (value.toLowerCase() === 'false') { } else if (value.toLowerCase() === 'false') {
value = false; value = false;
} else if (!isNaN(parseInt(value))) { } else if (value.length && !isNaN(Number(value))) {
value = parseInt(value); value = Number(value);
} }
} }
return [key,value]; return [key,value];
} }
@ -43,8 +44,8 @@ class Gamespy1 extends Core {
if ('hostname' in data) state.name = data.hostname; if ('hostname' in data) state.name = data.hostname;
if ('mapname' in data) state.map = data.mapname; if ('mapname' in data) state.map = data.mapname;
if (this.trueTest(data.password)) state.password = true; if (this.trueTest(data.password)) state.password = true;
if ('maxplayers' in data) state.maxplayers = parseInt(data.maxplayers); if ('maxplayers' in data) state.maxplayers = Number(data.maxplayers);
if ('hostport' in data) state.gamePort = parseInt(data.hostport); if ('hostport' in data) state.gamePort = Number(data.hostport);
const teamOffByOne = data.gamename === 'bfield1942'; const teamOffByOne = data.gamename === 'bfield1942';
const playersById = {}; const playersById = {};
@ -53,7 +54,7 @@ class Gamespy1 extends Core {
const split = ident.split('_'); const split = ident.split('_');
if (split.length !== 2) continue; if (split.length !== 2) continue;
let key = split[0].toLowerCase(); let key = split[0].toLowerCase();
const id = parseInt(split[1]); const id = Number(split[1]);
if (isNaN(id)) continue; if (isNaN(id)) continue;
let value = data[ident]; let value = data[ident];
@ -69,16 +70,15 @@ class Gamespy1 extends Core {
} else { } else {
// Info about a player // Info about a player
if (!(id in playersById)) playersById[id] = {}; if (!(id in playersById)) playersById[id] = {};
if (key === 'playername' || key === 'player') { if (key === 'playername' || key === 'player') {
key = 'name'; key = 'name';
} }
if (key === 'team' && !isNaN(parseInt(value))) { if (key === 'team' && !isNaN(value)) { // todo! technically, this NaN check isn't needed.
key = 'teamId'; key = 'teamId';
value = parseInt(value) + (teamOffByOne ? -1 : 0); value += teamOffByOne ? -1 : 0;
}
if (key !== 'name' && !isNaN(parseInt(value))) {
value = parseInt(value);
} }
playersById[id][key] = value; playersById[id][key] = value;
} }
} }
@ -136,7 +136,7 @@ class Gamespy1 extends Core {
if (data.queryid) { if (data.queryid) {
const split = data.queryid.split('.'); const split = data.queryid.split('.');
if (split.length >= 2) { if (split.length >= 2) {
partNum = parseInt(split[1]); partNum = Number(split[1]);
} }
queryId = split[0]; queryId = split[0];
} }