pvpokemon/generatePokemonData.ts
2019-01-06 20:44:48 -05:00

95 lines
3.8 KiB
TypeScript

import * as fs from 'fs';
import * as Pokemon from 'pokemongo-json-pokedex/output/pokemon.json';
import { IPokemon, IStats, League } from 'src/ts/models/Pokemon';
const GREAT_LEAGUE_CP = 1500;
const ULTRA_LEAGUE_CP = 2500;
const cpMultipliers : Array<number> = JSON.parse(fs.readFileSync('src/db/cpMultipliers.json', 'utf8'));
const getClosestCpMultiplierIndex = (value : number) => {
let i;
for (i = 0; i < cpMultipliers.length; i++) {
if (value < cpMultipliers[i]) {
break;
}
}
return Math.max(i - 1, 0);
};
Pokemon.forEach((mon) => {
const baseAtk = mon.stats.baseAttack;
const baseDef = mon.stats.baseDefense;
const baseHp = mon.stats.baseStamina;
const stats : IPokemon = {
name: mon.name,
id: mon.dex,
stats: mon.stats,
pvp: {
great: [],
ultra: [],
},
};
for (let ivHp = 15; ivHp >= 0; ivHp--) {
for (let ivAtk = 15; ivAtk >= 0; ivAtk--) {
for (let ivDef = 15; ivDef >= 0; ivDef--) {
let pokemonWithIvs : IStats;
const cpMultiplier = (baseAtk + ivAtk) * Math.sqrt(baseDef + ivDef) * Math.sqrt(baseHp + ivHp);
const maxGreatLeagueLevelMultiplierIndex = getClosestCpMultiplierIndex(Math.sqrt((GREAT_LEAGUE_CP * 10) / cpMultiplier));
const maxGreatLeagueLevelMultiplier = cpMultipliers[maxGreatLeagueLevelMultiplierIndex];
const maxGreatLeagueCp = ~~((cpMultiplier * Math.pow(maxGreatLeagueLevelMultiplier, 2)) / 10);
const maxGreatLeagueLevel = (maxGreatLeagueLevelMultiplierIndex + 2) / 2;
pokemonWithIvs = {
cp: maxGreatLeagueCp,
level: maxGreatLeagueLevel,
ivHp: ivHp,
ivAtk: ivAtk,
ivDef: ivDef,
hp: ~~((baseHp + ivHp) * maxGreatLeagueLevelMultiplier),
atk: ~~((baseAtk + ivAtk) * maxGreatLeagueLevelMultiplier),
def: ~~((baseDef + ivDef) * maxGreatLeagueLevelMultiplier),
total: 0
};
pokemonWithIvs.total = pokemonWithIvs.hp + pokemonWithIvs.atk + pokemonWithIvs.def;
stats.pvp.great.push(pokemonWithIvs);
const maxUltraLeagueLevelMultiplierIndex = getClosestCpMultiplierIndex(Math.sqrt((ULTRA_LEAGUE_CP * 10) / cpMultiplier));
const maxUltraLeagueLevelMultiplier = cpMultipliers[maxUltraLeagueLevelMultiplierIndex];
const maxUltraLeagueCp = ~~((cpMultiplier * Math.pow(maxUltraLeagueLevelMultiplier, 2)) / 10);
const maxUltraLeagueLevel = (maxUltraLeagueLevelMultiplierIndex + 2) / 2;
pokemonWithIvs = {
cp: maxUltraLeagueCp,
level: maxUltraLeagueLevel,
ivHp: ivHp,
ivAtk: ivAtk,
ivDef: ivDef,
hp: ~~((baseHp + ivHp) * maxUltraLeagueLevelMultiplier),
atk: ~~((baseAtk + ivAtk) * maxUltraLeagueLevelMultiplier),
def: ~~((baseDef + ivDef) * maxUltraLeagueLevelMultiplier),
total: 0
};
pokemonWithIvs.total = pokemonWithIvs.hp + pokemonWithIvs.atk + pokemonWithIvs.def;
stats.pvp.ultra.push(pokemonWithIvs);
}
}
}
Object.keys(stats.pvp).forEach((league) => {
const keys = league as League;
stats.pvp[keys].sort((a, b) => {
if (a.cp === b.cp) {
return a.cp > b.cp ? 0 : 1;
}
return a.total > b.total ? 0 : 1;
});
});
fs.writeFile('./dist/db/' + mon.name + '.json', JSON.stringify(stats), (err) => {
if(err) {
return console.log(mon.name, err);
}
console.log(mon.name);
});
});