65 lines
2.8 KiB
TypeScript
65 lines
2.8 KiB
TypeScript
import { action } from 'typesafe-actions';
|
|
|
|
import { PokemonExplorerActionTypes, ThunkResult } from 'app/types';
|
|
|
|
import { calculateMaxLevelForLeague } from 'app/utils/calculator';
|
|
|
|
import { ILeaguePokemon, League } from 'app/models/League';
|
|
import { IMaxStats } from 'app/models/Pokemon';
|
|
|
|
export const setIsLoading = (isLoading : boolean) => action(PokemonExplorerActionTypes.SET_IS_LOADING, { isLoading });
|
|
|
|
export const setMaxPossibleStats = (maxStats : IMaxStats) => action(PokemonExplorerActionTypes.SET_MAX_STATS, { maxStats });
|
|
|
|
export const setLeaguePokemon = (leaguePokemon : ILeaguePokemon | null) => action(PokemonExplorerActionTypes.SET_LEAGUE_POKEMON, { leaguePokemon });
|
|
|
|
export const setIvLevel = (level : number | null) => action(PokemonExplorerActionTypes.SET_IV_LEVEL, { level });
|
|
|
|
export const setIvHp = (ivHp : number | null) => action(PokemonExplorerActionTypes.SET_IV_HP, { ivHp });
|
|
|
|
export const setIvAtk = (ivAtk : number | null) => action(PokemonExplorerActionTypes.SET_IV_ATK, { ivAtk });
|
|
|
|
export const setIvDef = (ivDef : number | null) => action(PokemonExplorerActionTypes.SET_IV_DEF, { ivDef });
|
|
|
|
export const setActiveLeague = (league : League) => action(PokemonExplorerActionTypes.SET_ACTIVE_LEAGUE, { league });
|
|
|
|
export const fetchConfig = (
|
|
) : ThunkResult<Promise<void>> => {
|
|
return async (dispatch, getState, extraArguments) => {
|
|
const config = await extraArguments.services.pokemonService.getConfig();
|
|
dispatch(setMaxPossibleStats(config.maxPossibleStats));
|
|
};
|
|
};
|
|
|
|
export const maximizeLevel = (
|
|
) : ThunkResult<Promise<void>> => {
|
|
return async (dispatch, getState, extraArguments) => {
|
|
const pokemonExplorerState = getState().pokemonExplorerState;
|
|
const {
|
|
ivHp,
|
|
ivAtk,
|
|
ivDef,
|
|
} = pokemonExplorerState.individualValues;
|
|
|
|
if (pokemonExplorerState.leaguePokemon !== null) {
|
|
const pokemonLeagueValues = pokemonExplorerState.leaguePokemon.pvp[pokemonExplorerState.league];
|
|
const statsSet = pokemonLeagueValues.some((stats) => {
|
|
if (((ivHp === null) || (stats.ivHp === ivHp)) &&
|
|
((ivAtk === null) || (stats.ivAtk === ivAtk)) &&
|
|
((ivDef === null) || (stats.ivDef === ivDef))
|
|
) {
|
|
dispatch(setIvHp(stats.ivHp));
|
|
dispatch(setIvAtk(stats.ivAtk));
|
|
dispatch(setIvDef(stats.ivDef));
|
|
dispatch(setIvLevel(stats.level));
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
if (!statsSet && ivHp !== null && ivAtk !== null && ivDef !== null) {
|
|
dispatch(setIvLevel(calculateMaxLevelForLeague(pokemonExplorerState.leaguePokemon.stats, ivHp, ivAtk, ivDef, pokemonExplorerState.league)));
|
|
}
|
|
}
|
|
};
|
|
};
|