import POGOProtos from 'pogo-protos'; import React from 'react'; import classNames from 'classnames'; import { IBestWorstStats, ILeaguePokemon, League } from 'app/models/League'; import { Grade, IStats, } from 'app/models/Pokemon'; import { calculateCp, calculateStatAtLevel } from 'app/utils/calculator'; import { formatDexNumber, formatForm, formatType, Forms } from 'app/utils/formatter'; import { IIndividualValues, IndividualValueKey } from './types'; import { LeagueSelector } from './LeagueSelector'; import { LeagueStatsList } from './LeagueStatsList'; import { StatDisplay } from './StatDisplay'; import * as styles from './styles/PokemonExplorer.scss'; export interface IPokemonExplorerProps { isLoading : boolean; activeLeague : League; leaguePokemon : ILeaguePokemon; individualValues : IIndividualValues; handleChangeIndividualValue : (stat : IndividualValueKey, value : number | null) => void; handleMaximizeLevel : () => void; handleChangeLeague : (league : League) => void; } interface IState { form : { level : string; }; } export class PokemonExplorer extends React.Component { private static calculateStatRanks(rankedPokemon : IStats | null, stats : IBestWorstStats) { const rankedHp = rankedPokemon !== null ? rankedPokemon.hp : 0; const rankedAtk = rankedPokemon !== null ? rankedPokemon.atk : 0; const rankedDef = rankedPokemon !== null ? rankedPokemon.def : 0; const maxStamina = stats.stamina; const staminaStatRank = Math.floor(((rankedHp - maxStamina.worst) / (maxStamina.best - maxStamina.worst)) * 100); const maxAttack = stats.attack; const attackStatRank = Math.floor(((rankedAtk - maxAttack.worst) / (maxAttack.best - maxAttack.worst)) * 100); const maxDefense = stats.defense; const defenseStatRank = Math.floor(((rankedDef - maxDefense.worst) / (maxDefense.best - maxDefense.worst)) * 100); return { rankedHp, rankedAtk, rankedDef, staminaStatRank, attackStatRank, defenseStatRank, }; } private readonly MIN_LEVEL = 1; private readonly MAX_LEVEL = 40; private readonly MIN_IV = 0; private readonly MAX_IV = 15; private handleChangeHp : (event : React.ChangeEvent) => void; private handleChangeAtk : (event : React.ChangeEvent) => void; private handleChangeDef : (event : React.ChangeEvent) => void; constructor(props : IPokemonExplorerProps) { super(props); this.state = { form: { level: '', } }; this.handleChangeHp = this.handleChangeIvFactory('hp'); this.handleChangeAtk = this.handleChangeIvFactory('atk'); this.handleChangeDef = this.handleChangeIvFactory('def'); } public render() { const { activeLeague, individualValues, leaguePokemon } = this.props; let rankedPokemon : IStats | null = null; let placeholderLevel = ''; let placeholderHp = ''; let placeholderAtk = ''; let placeholderDef = ''; const dex = formatDexNumber(leaguePokemon.dex); const individualValueLevel = this.state.form.level !== '' ? this.state.form.level : individualValues.level; // default to first pokemon (should be S tier) if (individualValueLevel === null && individualValues.hp === null && individualValues.atk === null && individualValues.def === null ) { rankedPokemon = leaguePokemon.pvp[activeLeague][0]; placeholderLevel = '' + rankedPokemon.level; placeholderHp = '' + rankedPokemon.ivHp; placeholderAtk = '' + rankedPokemon.ivAtk; placeholderDef = '' + rankedPokemon.ivDef; // a full spec'd pokemon has been entered } else if (individualValueLevel !== null && typeof individualValueLevel === 'number' && individualValues.hp !== null && individualValues.atk !== null && individualValues.def !== null ) { leaguePokemon.pvp[activeLeague].some((stats) => { if (individualValueLevel === stats.level && individualValues.hp === stats.ivHp && individualValues.atk === stats.ivAtk && individualValues.def === stats.ivDef ) { rankedPokemon = stats; return true; } return false; }); // we don't have the data for this terrible mon if (rankedPokemon === null) { rankedPokemon = { cp: calculateCp(leaguePokemon.stats, individualValueLevel, individualValues.hp, individualValues.atk, individualValues.def), level: individualValueLevel, ivHp: individualValues.hp, ivAtk: individualValues.atk, ivDef: individualValues.def, hp: calculateStatAtLevel(individualValueLevel, leaguePokemon.stats.baseStamina, individualValues.hp), atk: calculateStatAtLevel(individualValueLevel, leaguePokemon.stats.baseAttack, individualValues.atk), def: calculateStatAtLevel(individualValueLevel, leaguePokemon.stats.baseDefense, individualValues.def), total: 0, speciesGrade: Grade.F, metaGrade: Grade.F, }; rankedPokemon.total = rankedPokemon.hp + rankedPokemon.atk + rankedPokemon.def; } } const rankedGrade = rankedPokemon !== null ? Grade[rankedPokemon.speciesGrade] : '-'; const rankedCp = rankedPokemon !== null ? rankedPokemon.cp : '-'; const { rankedHp, rankedAtk, rankedDef, staminaStatRank, attackStatRank, defenseStatRank, } = PokemonExplorer.calculateStatRanks(rankedPokemon, leaguePokemon.statMax[activeLeague]); const idIvLevelInput = 'iv-level-input'; const idIvHpInput = 'iv-hp-input'; const idIvAtkInput = 'iv-atk-input'; const idIvDefInput = 'iv-def-input'; const containerCss = classNames( 'nes-container', 'with-title', ); const containerRoundCss = classNames( containerCss, 'is-rounded', ); const pokemonType = classNames( containerRoundCss, styles.pokemonType, ); const containerTitleCss = classNames( 'title', ); const baseStatsCss = classNames( styles.pokemonBaseStats, containerCss, ); const formContainerCss = classNames( containerCss, 'form', ); const fieldCss = classNames( 'nes-field', ); const inlineFieldCss = classNames( fieldCss, 'is-inline', styles.fieldRow, ); const inputTextCss = classNames( 'nes-input', styles.ivInput, ); const inputTextLevelCss = classNames( inputTextCss, styles.levelInput, ); const leaugeRankCss = classNames( styles.leaguePokemonRank, containerCss, { 'with-title': false }, ); const maxButtonCss = classNames( 'nes-btn', { 'is-primary': individualValues.hp !== null && individualValues.atk !== null && individualValues.def !== null, 'is-disabled': individualValues.hp === null || individualValues.atk === null || individualValues.def === null, }, ); const pokemonIconCss = classNames( `pokemon-${dex}`, { normal: Forms.normal.indexOf(leaguePokemon.form) > -1, alola: Forms.alola.indexOf(leaguePokemon.form) > -1, plant: Forms.plant.indexOf(leaguePokemon.form) > -1, sandy: Forms.sandy.indexOf(leaguePokemon.form) > -1, trash: Forms.trash.indexOf(leaguePokemon.form) > -1, 'west-sea': Forms.westSea.indexOf(leaguePokemon.form) > -1, 'east-sea': Forms.eastSea.indexOf(leaguePokemon.form) > -1, frost: Forms.frost.indexOf(leaguePokemon.form) > -1, fan: Forms.fan.indexOf(leaguePokemon.form) > -1, mow: Forms.mow.indexOf(leaguePokemon.form) > -1, wash: Forms.wash.indexOf(leaguePokemon.form) > -1, heat: Forms.heat.indexOf(leaguePokemon.form) > -1, sky: Forms.sky.indexOf(leaguePokemon.form) > -1, land: Forms.land.indexOf(leaguePokemon.form) > -1, overcast: Forms.overcast.indexOf(leaguePokemon.form) > -1, sunny: Forms.sunny.indexOf(leaguePokemon.form) > -1, rainy: Forms.rainy.indexOf(leaguePokemon.form) > -1, snowy: Forms.snowy.indexOf(leaguePokemon.form) > -1, attack: Forms.attack.indexOf(leaguePokemon.form) > -1, defense: Forms.defense.indexOf(leaguePokemon.form) > -1, speed: Forms.speed.indexOf(leaguePokemon.form) > -1, altered: Forms.altered.indexOf(leaguePokemon.form) > -1, origin: Forms.origin.indexOf(leaguePokemon.form) > -1, fighting: Forms.fighting.indexOf(leaguePokemon.form) > -1, flying: Forms.flying.indexOf(leaguePokemon.form) > -1, poison: Forms.poison.indexOf(leaguePokemon.form) > -1, ground: Forms.ground.indexOf(leaguePokemon.form) > -1, rock: Forms.rock.indexOf(leaguePokemon.form) > -1, bug: Forms.bug.indexOf(leaguePokemon.form) > -1, ghost: Forms.ghost.indexOf(leaguePokemon.form) > -1, steel: Forms.steel.indexOf(leaguePokemon.form) > -1, fire: Forms.fire.indexOf(leaguePokemon.form) > -1, water: Forms.water.indexOf(leaguePokemon.form) > -1, grass: Forms.grass.indexOf(leaguePokemon.form) > -1, electric: Forms.electric.indexOf(leaguePokemon.form) > -1, psychic: Forms.psychic.indexOf(leaguePokemon.form) > -1, ice: Forms.ice.indexOf(leaguePokemon.form) > -1, dragon: Forms.dragon.indexOf(leaguePokemon.form) > -1, dark: Forms.dark.indexOf(leaguePokemon.form) > -1, fairy: Forms.fairy.indexOf(leaguePokemon.form) > -1, }, ); const type1 : JSX.Element =
{ formatType(leaguePokemon.types.type1) }
; let type2 : JSX.Element | null = null; if (leaguePokemon.types.type2) { type2 =
{ formatType(leaguePokemon.types.type2) }
; } return (

No.{ dex }

{ leaguePokemon.form !== POGOProtos.Enums.Form.FORM_UNSET &&
{ formatForm(leaguePokemon.form) } Form
}
{ type1 } { type2 }

{ leaguePokemon.name }

{ leaguePokemon.genus }

Base Stats

IVs
Rank

{ rankedGrade }

Rank
CP

{ rankedCp }

); } private readonly handleChangeLevel = (event : React.ChangeEvent) => { const raw = event.currentTarget.value; const value = parseFloat(raw); this.setState({ form: { level: '' } }); if (raw === '' + value && value >= this.MIN_LEVEL && value <= this.MAX_LEVEL && value % 0.5 === 0) { this.props.handleChangeIndividualValue('level', value); } else if (raw === '') { this.props.handleChangeIndividualValue('level', null); } else if (raw.charAt(raw.length) === '.') { this.setState({ form: { level: raw } }); } } private readonly handleClickMaximizeLevel = () => { this.props.handleMaximizeLevel(); } private readonly handleChangeIvFactory = (type : IndividualValueKey) => { return (event : React.ChangeEvent) => { const raw = event.currentTarget.value; const value = parseInt(raw, 10); if (raw === '' + value && value >= this.MIN_IV && value <= this.MAX_IV) { this.props.handleChangeIndividualValue(type, value); } else if (raw === '') { this.props.handleChangeIndividualValue(type, null); } }; } private readonly handleActivateLeagueStats = (stats : IStats) => { const { handleChangeIndividualValue } = this.props; handleChangeIndividualValue('level', stats.level); handleChangeIndividualValue('hp', stats.ivHp); handleChangeIndividualValue('atk', stats.ivAtk); handleChangeIndividualValue('def', stats.ivDef); } private readonly handleLeagueSelect = (league : League) => { this.props.handleChangeLeague(league); } }