Skip to content

Commit 680a5d5

Browse files
committed
Fix duplicate comparison overwriting and error feedback
1 parent b282f37 commit 680a5d5

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

server/chat-plugins/datasearch.ts

+45-42
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface DexOrGroup {
2525
types: { [k: string]: boolean };
2626
resists: { [k: string]: boolean };
2727
weak: { [k: string]: boolean };
28-
stats: { [k: string]: { [k in Direction]: number | string } };
28+
stats: { [k: string]: { [k in Direction]: { [s: string]: number | boolean } } };
2929
skip: boolean;
3030
}
3131

@@ -1162,38 +1162,48 @@ function runDexsearch(target: string, cmd: string, canAll: boolean, message: str
11621162
inequalityString = target.charAt(inequality);
11631163
}
11641164
const targetParts = target.replace(/\s/g, '').split(inequalityString);
1165-
let compareTo;
1166-
let stat;
1165+
let compareType: string;
1166+
let statKey: string;
1167+
let value: number | boolean;
11671168
const directions: Direction[] = [];
11681169
if (!isNaN(parseFloat(targetParts[0]))) {
11691170
// e.g. 100 < spe
1170-
compareTo = parseFloat(targetParts[0]);
1171-
stat = targetParts[1];
1171+
value = parseFloat(targetParts[0]);
1172+
statKey = targetParts[1];
1173+
compareType = 'numeric';
11721174
if (inequalityString.startsWith('>')) directions.push('less');
11731175
if (inequalityString.startsWith('<')) directions.push('greater');
11741176
} else if (!isNaN(parseFloat(targetParts[1]))) {
11751177
// e.g. spe > 100
1176-
compareTo = parseFloat(targetParts[1]);
1177-
stat = targetParts[0];
1178+
value = parseFloat(targetParts[1]);
1179+
statKey = targetParts[0];
1180+
compareType = 'numeric';
11781181
if (inequalityString.startsWith('<')) directions.push('less');
11791182
if (inequalityString.startsWith('>')) directions.push('greater');
11801183
} else {
11811184
// e.g. atk = spatk
1182-
compareTo = targetParts[0];
1183-
stat = targetParts[1];
1184-
if (inequalityString.startsWith('>')) directions.push('less');
1185-
if (inequalityString.startsWith('<')) directions.push('greater');
1186-
1187-
if (compareTo in allStatAliases) compareTo = allStatAliases[compareTo];
1188-
if (!allStats.includes(compareTo)) return { error: `'${target}' did not contain a valid stat.` };
1185+
value = true;
1186+
statKey = targetParts[0];
1187+
compareType = targetParts[1];
1188+
if (inequalityString.startsWith('<')) directions.push('less');
1189+
if (inequalityString.startsWith('>')) directions.push('greater');
1190+
if (statKey in allStatAliases) statKey = allStatAliases[statKey];
1191+
if (compareType in allStatAliases) compareType = allStatAliases[compareType];
1192+
if (!allStats.slice(0, 6).includes(statKey) || !allStats.slice(0, 6).includes(compareType))
1193+
return { error: `'${target}' did not contain a valid stat to compare with another stat.` };
11891194
}
11901195
if (inequalityString.endsWith('=')) directions.push('equal');
1191-
if (stat in allStatAliases) stat = allStatAliases[stat];
1192-
if (!allStats.includes(stat)) return { error: `'${target}' did not contain a valid stat.` };
1193-
if (!orGroup.stats[stat]) orGroup.stats[stat] = Object.create(null);
1196+
if (statKey in allStatAliases) statKey = allStatAliases[statKey];
1197+
if (!allStats.includes(statKey)) return { error: `'${target}' contained an invalid stat.` };
1198+
if (typeof value === 'number' && value <= 0) return { error: `Specify a positive value for numeric comparison.` };
1199+
if (!orGroup.stats[statKey]) orGroup.stats[statKey] = Object.create(null);
1200+
// Prevents numeric searches from being overwritten and prevent duplicate searches of other types.
11941201
for (const direction of directions) {
1195-
if (orGroup.stats[stat][direction]) return { error: `Invalid stat range for ${stat}.` };
1196-
orGroup.stats[stat][direction] = compareTo;
1202+
if (!orGroup.stats[statKey][direction])
1203+
orGroup.stats[statKey][direction] = Object.create(null);
1204+
else if (orGroup.stats[statKey][direction][compareType])
1205+
return { error: `Duplicate stat inequality and type for ${statKey}.` };
1206+
orGroup.stats[statKey][direction][compareType] = value;
11971207
}
11981208
continue;
11991209
}
@@ -1402,11 +1412,9 @@ function runDexsearch(target: string, cmd: string, canAll: boolean, message: str
14021412
}
14031413
if (matched) continue;
14041414

1405-
function retrieveStat(species: Species, stat: string | number) {
1415+
function retrieveStat(species: Species, stat: string) {
14061416
let monStat = 0;
1407-
if (typeof stat === 'number') {
1408-
monStat = stat;
1409-
} else if (stat === 'bst') {
1417+
if (stat === 'bst') {
14101418
monStat = species.bst;
14111419
} else if (stat === 'weight') {
14121420
monStat = species.weighthg / 10;
@@ -1422,27 +1430,22 @@ function runDexsearch(target: string, cmd: string, canAll: boolean, message: str
14221430

14231431
for (const stat in alts.stats) {
14241432
const monStat = retrieveStat(dex[mon], stat);
1425-
if (alts.stats[stat].less) {
1426-
const compareTo = retrieveStat(dex[mon], alts.stats[stat].less);
1427-
if (monStat < compareTo) {
1428-
matched = true;
1429-
break;
1430-
}
1431-
}
1432-
if (alts.stats[stat].greater) {
1433-
const compareTo = retrieveStat(dex[mon], alts.stats[stat].greater);
1434-
if (monStat > compareTo) {
1435-
matched = true;
1436-
break;
1437-
}
1438-
}
1439-
if (alts.stats[stat].equal) {
1440-
const compareTo = retrieveStat(dex[mon], alts.stats[stat].equal);
1441-
if (monStat === compareTo) {
1442-
matched = true;
1443-
break;
1433+
for (const direction in alts.stats[stat]) {
1434+
for (const comparisonStat in alts.stats[stat][direction as Direction]) {
1435+
const checkStat = alts.stats[stat][direction as Direction][comparisonStat];
1436+
if (!checkStat) continue;
1437+
const compareTo = typeof checkStat === 'number' ?
1438+
checkStat : retrieveStat(dex[mon], comparisonStat);
1439+
if ((direction === 'less' && monStat < compareTo) ||
1440+
(direction === 'greater' && monStat > compareTo) ||
1441+
(direction === 'equal' && monStat === compareTo)) {
1442+
matched = true;
1443+
break;
1444+
}
14441445
}
1446+
if (matched) break;
14451447
}
1448+
if (matched) break;
14461449
}
14471450
if (matched) continue;
14481451

0 commit comments

Comments
 (0)