Fix crash in Rulesets

Crash introduced in #8195 and has been plaguing us with thousands of
crashes per month for the past two years, but 9cd64cba15 finally
let us track it down.

Special thanks to everyone who spent so much time and effort trying
to identify the source of this crash over the past few years. It's
over. It's finally over.
This commit is contained in:
Guangcong Luo 2023-12-07 16:34:51 -05:00
parent 9d45b67207
commit 9713dc6db5

View File

@ -1,6 +1,7 @@
// Note: These are the rules that formats use
import {Utils} from "../lib";
import type {Learnset} from "../sim/dex-species";
import {Pokemon} from "../sim/pokemon";
// The list of formats is stored in config/formats.js
@ -1750,24 +1751,19 @@ export const Rulesets: {[k: string]: FormatData} = {
},
onValidateSet(set) {
const species = this.dex.species.get(set.species);
const learnsetData = {...(this.dex.data.Learnsets[species.id]?.learnset || {})};
let prevo = species.prevo;
while (prevo) {
const prevoSpecies = this.dex.species.get(prevo);
const prevoLsetData = this.dex.data.Learnsets[prevoSpecies.id]?.learnset || {};
for (const moveid in prevoLsetData) {
if (!(moveid in learnsetData)) {
learnsetData[moveid] = prevoLsetData[moveid];
} else {
learnsetData[moveid].push(...prevoLsetData[moveid]);
}
const learnset: NonNullable<Learnset['learnset']> = {};
let curSpecies: Species | null = species;
while (curSpecies) {
const curLearnset = this.dex.species.getLearnset(curSpecies.id) || {};
for (const moveid in curLearnset) {
learnset[moveid] = [...(learnset[moveid] || []), ...curLearnset[moveid]];
}
prevo = prevoSpecies.prevo;
curSpecies = this.learnsetParent(curSpecies);
}
const problems = [];
if (set.moves?.length) {
for (const move of set.moves) {
if (learnsetData[this.toID(move)] && !learnsetData[this.toID(move)].filter(v => !v.includes('S')).length) {
if (learnset[this.toID(move)]?.every(learned => learned.includes('S'))) {
problems.push(`${species.name}'s move ${move} is obtainable only through events.`);
}
}