From 00b06985e4daa015a540dbd698fc8c8a57ad53b4 Mon Sep 17 00:00:00 2001 From: Jeff Colombo Date: Wed, 9 Jan 2019 23:43:33 -0500 Subject: [PATCH] determine mon order --- generatePokemonData.ts | 83 ++++++++++++++++++++++++++++++++++++---- src/ts/models/Pokemon.ts | 4 +- 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/generatePokemonData.ts b/generatePokemonData.ts index 68dc8f1..ddb4369 100644 --- a/generatePokemonData.ts +++ b/generatePokemonData.ts @@ -32,20 +32,62 @@ const getClosestCpMultiplierIndex = (value : number) => { return Math.max(i - 1, 0); }; +const familyOrder : Array = []; +const familyEvolutionOrder : { [ key : string ] : Array } = {}; +const familyEncountered : { [ key : string ] : Array } = {}; Pokemon.forEach((mon) => { const baseAtk = mon.stats.baseAttack; const baseDef = mon.stats.baseDefense; const baseHp = mon.stats.baseStamina; - const stats : IPokemon = { + const pokemon : IPokemon = { + id: mon.id, name: mon.name, - id: mon.dex, + dex: mon.dex, stats: mon.stats, + family: mon.family.id, pvp: { great: [], ultra: [], }, }; + // keep track of family order and membership + if (typeof familyEncountered[pokemon.family] === 'undefined') { + familyOrder.push(pokemon.family); + familyEncountered[pokemon.family] = []; + } + familyEncountered[pokemon.family].push(pokemon); + + if (typeof mon.evolution.pastBranch === 'undefined' && + typeof familyEvolutionOrder[pokemon.family] === 'undefined' && + pokemon.id.indexOf('_ALOLA') === -1 // because RAICHU_ALOLA shows up before PICHU, but PICHU is the family origin + ) { + familyEvolutionOrder[pokemon.family] = []; + if (mon.forms.length > 0) { + mon.forms.forEach((form) => { + familyEvolutionOrder[pokemon.family].push(form.id); + }); + } else { + familyEvolutionOrder[pokemon.family].push(pokemon.id); + } + + // TODO: if `mon.forms.length > 0`, there's a chance the order will get weird by doing this: + if (typeof mon.evolution.futureBranches !== 'undefined') { + (function traverseEvolutionBranches(root) { + root.forEach((evolution) => { + familyEvolutionOrder[pokemon.family].push(evolution.id); + // unfortunate workaround for typescript limitation in JSON parsing being TOO good... + // if (typeof evolution !== 'undefined') { + if ('futureBranches' in evolution) { + // traverseEvolutionBranches(evolution.futureBranches); + traverseEvolutionBranches(evolution['futureBranches']); + } + }); + })(mon.evolution.futureBranches); + } + } + + // calculate stats for all possible IVs const combinedStatsDistribution : IStatsDistribution = { great: {}, ultra: {}, @@ -87,7 +129,7 @@ Pokemon.forEach((mon) => { } // process the pokemon stats for league-worthiness - Object.keys(stats.pvp).forEach((key) => { + Object.keys(pokemon.pvp).forEach((key) => { const league = key as League; const orderedCombinedStats = Object.keys(combinedStatsDistribution[league]).map(cpTotal => parseInt(cpTotal)).sort(); @@ -118,7 +160,7 @@ Pokemon.forEach((mon) => { pokemonStats.speciesGrade = Grade.D; } } - stats.pvp[league].push(pokemonStats); + pokemon.pvp[league].push(pokemonStats); }); combinedStatsDistribution[league][combinedStats].sort((a, b) => { if (a.total !== b.total) { @@ -129,16 +171,43 @@ Pokemon.forEach((mon) => { } return 0; }); - stats.pvp[league].push(...combinedStatsDistribution[league][combinedStats]); + pokemon.pvp[league].push(...combinedStatsDistribution[league][combinedStats]); } }); fs.mkdir(outPath, {recursive: true}, () => { - fs.writeFile(outPath + mon.id + '.json', JSON.stringify(stats), (err) => { + fs.writeFile(outPath + mon.id + '.json', JSON.stringify(pokemon), (err) => { if(err) { return console.log(mon.name, err); } - console.log(mon.name); }); }); }); + +const pokemonOrder : Array<{ id : string, name : string }> = []; +familyOrder.forEach((familyId) => { + familyEvolutionOrder[familyId].forEach((id, order) => { + familyEncountered[familyId].some((pokemon, index) => { + if (id === pokemon.id && order !== index) { + familyEncountered[familyId].splice(order, 0, familyEncountered[familyId].splice(index, 1)[0]); + return true; + } + return false; + }); + }); + + familyEncountered[familyId].forEach((pokemon) => { + pokemonOrder.push({ + id: pokemon.id, + name: pokemon.name, + }); + }); +}); + +fs.mkdir(outPath, {recursive: true}, () => { + fs.writeFile(outPath + 'order.json', JSON.stringify(pokemonOrder), (err) => { + if(err) { + return console.log('order', err); + } + }); +}); diff --git a/src/ts/models/Pokemon.ts b/src/ts/models/Pokemon.ts index af11be9..03264aa 100644 --- a/src/ts/models/Pokemon.ts +++ b/src/ts/models/Pokemon.ts @@ -16,7 +16,9 @@ export interface IBaseStats { export type League = 'great' | 'ultra'; export interface IPokemon { name : string; - id : number; + id : string; + family : string; + dex : number; stats : IBaseStats; pvp : { great : Array;