Simplified Pokemon Scan logic, accepts separate words as input

This commit is contained in:
Wlowscha 2025-01-04 18:27:55 +01:00
parent b3bda1aaac
commit bb2aea40b9
No known key found for this signature in database
GPG Key ID: 3C8F1AD330565D04
1 changed files with 6 additions and 58 deletions

View File

@ -1,6 +1,5 @@
import { FormModalUiHandler, InputFieldConfig } from "./form-modal-ui-handler"; import { FormModalUiHandler, InputFieldConfig } from "./form-modal-ui-handler";
import { ModalConfig } from "./modal-ui-handler"; import { ModalConfig } from "./modal-ui-handler";
import i18next from "i18next";
import { PlayerPokemon } from "#app/field/pokemon"; import { PlayerPokemon } from "#app/field/pokemon";
import { OptionSelectItem } from "./abstact-option-select-ui-handler"; import { OptionSelectItem } from "./abstact-option-select-ui-handler";
import { isNullOrUndefined } from "#app/utils"; import { isNullOrUndefined } from "#app/utils";
@ -8,12 +7,14 @@ import { Mode } from "./ui";
import { FilterTextRow } from "./filter-text"; import { FilterTextRow } from "./filter-text";
import { allAbilities } from "#app/data/ability"; import { allAbilities } from "#app/data/ability";
import { allMoves } from "#app/data/move"; import { allMoves } from "#app/data/move";
import { allSpecies } from "#app/data/pokemon-species";
export default class PokedexScanUiHandler extends FormModalUiHandler { export default class PokedexScanUiHandler extends FormModalUiHandler {
keys: string[]; keys: string[];
reducedKeys: string[]; reducedKeys: string[];
parallelKeys: string[]; parallelKeys: string[];
nameKeys: string[];
moveKeys: string[]; moveKeys: string[];
abilityKeys: string[]; abilityKeys: string[];
@ -24,30 +25,9 @@ export default class PokedexScanUiHandler extends FormModalUiHandler {
setup() { setup() {
super.setup(); super.setup();
const flattenKeys = (object?: any, topKey?: string, midleKey?: string[]): Array<any> => { this.nameKeys = allSpecies.map(a => a.name).filter((value, index, self) => self.indexOf(value) === index);
return Object.keys(object ?? {}).map((t, i) => {
const value = Object.values(object)[i];
if (typeof value === "object" && !isNullOrUndefined(value)) { // we check for not null or undefined here because if the language json file has a null key, the typeof will still be an object, but that object will be null, causing issues
// If the value is an object, execute the same process
// si el valor es un objeto ejecuta el mismo proceso
return flattenKeys(value, topKey ?? t, topKey ? midleKey ? [ ...midleKey, t ] : [ t ] : undefined).filter((t) => t.length > 0);
} else if (typeof value === "string" || isNullOrUndefined(value)) { // we check for null or undefined here as per above - the typeof is still an object but the value is null so we need to exit out of this and pass the null key
// Return in the format expected by i18next
return midleKey ? `${topKey}:${midleKey.map((m) => m).join(".")}.${t}` : `${topKey}:${t}`;
}
}).filter((t) => t);
};
const keysInArrays = flattenKeys(i18next.getDataByLanguage(String(i18next.resolvedLanguage))).filter((t) => t.length > 0); // Array of arrays
const keys = keysInArrays.flat(Infinity).map(String); // One array of string
this.moveKeys = allMoves.map(a => a.name); this.moveKeys = allMoves.map(a => a.name);
this.abilityKeys = allAbilities.map(a => a.name); this.abilityKeys = allAbilities.map(a => a.name);
this.keys = keys;
} }
getModalTitle(config?: ModalConfig): string { getModalTitle(config?: ModalConfig): string {
@ -84,9 +64,7 @@ export default class PokedexScanUiHandler extends FormModalUiHandler {
console.log(this.keys); console.log(this.keys);
switch (row) { switch (row) {
case FilterTextRow.NAME: { case FilterTextRow.NAME: {
const startString = "pokemon:"; this.reducedKeys = this.nameKeys;
// TODO: nameKeys
this.reducedKeys = this.keys.filter(str => str.startsWith(startString));
break; break;
} }
case FilterTextRow.MOVE_1: case FilterTextRow.MOVE_1:
@ -103,32 +81,8 @@ export default class PokedexScanUiHandler extends FormModalUiHandler {
this.reducedKeys = this.keys; this.reducedKeys = this.keys;
} }
} }
console.log(this.reducedKeys);
// this.parallelKeys = this.reducedKeys.map(key => this.translateKey(key));
this.parallelKeys = this.reducedKeys.map(key => String(i18next.t(key)));
console.log(this.parallelKeys);
} }
translateKey(key: string): string {
const interpolatorOptions: any = {};
const splitArr = key.split(" "); // this splits our inputted text into words to cycle through later
const translatedString = splitArr[0]; // this is our outputted i18 string
const regex = RegExp("\\{\\{(\\w*)\\}\\}", "g"); // this is a regex expression to find all the text between {{ }} in the i18 output
const matches = i18next.t(translatedString).match(regex) ?? [];
if (matches.length > 0) {
for (let match = 0; match < matches.length; match++) {
// we add 1 here because splitArr[0] is our first value for the translatedString, and after that is where the variables are
// the regex here in the replace (/\W/g) is to remove the {{ and }} and just give us all alphanumeric characters
if (typeof splitArr[match + 1] !== "undefined") {
interpolatorOptions[matches[match].replace(/\W/g, "")] = i18next.t(splitArr[match + 1]);
}
}
}
return String(i18next.t(translatedString, interpolatorOptions));
}
// args[2] is an index of FilterTextRow // args[2] is an index of FilterTextRow
//TODO: This logic is probably way more complex than we need, and actually messes things up for moves and abilities with a space like "Leech Seed" //TODO: This logic is probably way more complex than we need, and actually messes things up for moves and abilities with a space like "Leech Seed"
@ -158,20 +112,14 @@ export default class PokedexScanUiHandler extends FormModalUiHandler {
} }
let options: OptionSelectItem[] = []; let options: OptionSelectItem[] = [];
const splitArr = inputObject.text.split(" "); const filteredKeys = this.reducedKeys.filter((command) => command.toLowerCase().includes(inputObject.text.toLowerCase()));
const filteredKeys = this.parallelKeys.filter((command) => command.toLowerCase().includes(splitArr[splitArr.length - 1].toLowerCase()));
if (inputObject.text !== "" && filteredKeys.length > 0) { if (inputObject.text !== "" && filteredKeys.length > 0) {
// if performance is required, you could reduce the number of total results by changing the slice below to not have all ~8000 inputs going
options = filteredKeys.slice(0).map((value) => { options = filteredKeys.slice(0).map((value) => {
return { return {
label: value, label: value,
handler: () => { handler: () => {
// this is here to make sure that if you try to backspace then enter, the last known evt.data (backspace) is picked up
// this is because evt.data is null for backspace, so without this, the autocomplete windows just closes
if (!isNullOrUndefined(evt.data) || evt.inputType?.toLowerCase() === "deletecontentbackward") { if (!isNullOrUndefined(evt.data) || evt.inputType?.toLowerCase() === "deletecontentbackward") {
const separatedArray = inputObject.text.split(" "); inputObject.setText(value);
separatedArray[separatedArray.length - 1] = value;
inputObject.setText(separatedArray.join(" "));
} }
ui.revertMode(); ui.revertMode();
return true; return true;