Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
352 changes: 149 additions & 203 deletions src/classes/PokeApi.ts

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions src/classes/Pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
QuestTypeProto,
TypeProto,
} from '../typings/protos'
import { mergeTempEvolutions, sortTempEvolutions } from '../utils/tempEvolutions'
import Masterfile from './Masterfile'
import PokeApi from './PokeApi'
import PokemonOverrides from './PokemonOverrides'
Expand Down Expand Up @@ -297,9 +298,13 @@ export default class Pokemon extends Masterfile {
const tempEvolutions: TempEvolutions[] = mfObject
.filter((tempEvo) => tempEvo.stats)
.map((tempEvo) => {
const resolvedTempEvoId =
Rpc.HoloTemporaryEvolutionId[tempEvo.tempEvoId as MegaProto] ??
(tempEvo.tempEvoId === 'TEMP_EVOLUTION_MEGA_Z'
? 5
: tempEvo.tempEvoId)
const newTempEvolution: TempEvolutions = {
tempEvoId:
Rpc.HoloTemporaryEvolutionId[tempEvo.tempEvoId as MegaProto],
tempEvoId: resolvedTempEvoId,
}
switch (true) {
case tempEvo.stats.baseAttack !== primaryForm.attack:
Expand Down Expand Up @@ -333,9 +338,7 @@ export default class Pokemon extends Masterfile {
}
return newTempEvolution
})
return tempEvolutions.sort(
(a, b) => (a.tempEvoId as number) - (b.tempEvoId as number),
)
return sortTempEvolutions(tempEvolutions)
} catch (e) {
console.warn(
e,
Expand Down Expand Up @@ -1178,12 +1181,10 @@ export default class Pokemon extends Masterfile {
) {
Object.keys(tempEvos[category]).forEach((id) => {
try {
const tempEvolutions = [
...tempEvos[category][id].tempEvolutions,
...(this.parsedPokemon[id].tempEvolutions
? this.parsedPokemon[id].tempEvolutions
: []),
]
const tempEvolutions = mergeTempEvolutions(
tempEvos[category][id].tempEvolutions,
this.parsedPokemon[id].tempEvolutions,
)
this.parsedPokemon[id] = {
...this.parsedPokemon[id],
tempEvolutions,
Expand Down
13 changes: 13 additions & 0 deletions src/classes/Translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,19 @@ export default class Translations extends Masterfile {
`${this.options.prefix.evolutions}${id}`
] = this.capitalize(name.replace('TEMP_EVOLUTION_', ''))
})
if (
!Object.prototype.hasOwnProperty.call(
Rpc.HoloTemporaryEvolutionId,
'TEMP_EVOLUTION_MEGA_Z',
) &&
!this.parsedTranslations[locale].misc[
`${this.options.prefix.evolutions}5`
]
) {
this.parsedTranslations[locale].misc[
`${this.options.prefix.evolutions}5`
] = this.capitalize('MEGA_Z')
}
Object.entries(Rpc.PokemonDisplayProto.Alignment).forEach((proto) => {
const [name, id] = proto
this.parsedTranslations[locale].misc[
Expand Down
73 changes: 73 additions & 0 deletions src/utils/tempEvolutions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { TempEvolutions } from '../typings/dataTypes'

export type TempEvoId = TempEvolutions['tempEvoId']

/**
* Returns a stable, type-sensitive key for a temp evolution ID.
* This keeps `1` and `'1'` distinct.
*/
export const tempEvoIdKey = (tempEvoId: TempEvoId): string =>
`${typeof tempEvoId}:${tempEvoId}`

/**
* Sort comparator for temp evolution IDs:
* - Numbers sort before strings
* - Numbers sort numerically (ascending)
* - Strings sort lexicographically (localeCompare)
*/
export const compareTempEvoId = (a: TempEvoId, b: TempEvoId): number => {
const aIsNumber = typeof a === 'number'
const bIsNumber = typeof b === 'number'

if (aIsNumber && bIsNumber) return a - b
if (aIsNumber) return -1
if (bIsNumber) return 1
return a.toString().localeCompare(b.toString())
}

/**
* Returns a new array sorted by `tempEvoId` without mutating the input array.
*/
export const sortTempEvolutions = (
tempEvolutions: TempEvolutions[],
): TempEvolutions[] =>
[...tempEvolutions].sort((a, b) => compareTempEvoId(a.tempEvoId, b.tempEvoId))

/**
* Deduplicates temp evolutions by `tempEvoId` (type-sensitive) and returns them sorted.
* Defaults to preferring the last entry when duplicates exist.
*/
export const dedupeTempEvolutions = (
tempEvolutions: (TempEvolutions | undefined | null)[],
options: { prefer?: 'first' | 'last' } = {},
): TempEvolutions[] => {
const prefer = options.prefer ?? 'last'
const deduped = new Map<string, TempEvolutions>()

for (const tempEvo of tempEvolutions) {
if (!tempEvo) continue
const key = tempEvoIdKey(tempEvo.tempEvoId)

if (prefer === 'first') {
if (!deduped.has(key)) deduped.set(key, tempEvo)
continue
}

deduped.set(key, tempEvo)
}

return sortTempEvolutions(Array.from(deduped.values()))
}

/**
* Merges estimated + actual temp evolutions, preferring actual values for duplicates.
*/
export const mergeTempEvolutions = (
estimated: (TempEvolutions | undefined | null)[] | undefined,
actual: (TempEvolutions | undefined | null)[] | undefined,
): TempEvolutions[] => {
const estimatedList = Array.isArray(estimated) ? estimated : []
const actualList = Array.isArray(actual) ? actual : []

return dedupeTempEvolutions([...estimatedList, ...actualList], { prefer: 'last' })
}
6 changes: 4 additions & 2 deletions static/baseStats.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"evoId": 899,
"formId": 3218
}
]
],
"legendary": false,
"mythic": false
},
"290": {
"evolutions": [
Expand All @@ -29,7 +31,7 @@
"evolutions": [
{
"evoId": 1018,
"formId": 0
"formId": 3330
}
],
"legendary": false,
Expand Down
Loading