import { AjaxRequest } from 'api/AjaxRequest'; import { AttackTypeEffectivenessSerializingTuple, IConfig, IConfigJson } from 'app/models/Config'; import { ILeaguePokemon } from 'app/models/League'; import { IPokemon, TypeEffectiveness } from 'app/models/Pokemon'; import * as PVPogoProtos from 'common/models/PvPogoProtos'; type Omit = Pick>; export interface ILeaguePokemonJson extends Omit { effectiveness : Array<[PVPogoProtos.PokemonType, TypeEffectiveness]>; } interface IPokemonService { getConfig() : Promise; getPokemonList() : Promise>; getPokemonLeagueStats(pokemonId : PVPogoProtos.PokemonId, form : PVPogoProtos.PokemonForm) : Promise; } export class PokemonService implements IPokemonService { private AjaxRequest : AjaxRequest; constructor() { this.AjaxRequest = new AjaxRequest(XMLHttpRequest); } public async getConfig() { const queryParameters = { }; const response : IConfigJson = await this.AjaxRequest.ajaxGet('/dist/db/config.json', queryParameters); const attackTypeEffectiveness = response.attackTypeEffectiveness.map((tuple) => { const deserialize : AttackTypeEffectivenessSerializingTuple = [ tuple[0], new Map(tuple[1]), ]; return deserialize; }); return { ...response, attackTypeEffectiveness: new Map(attackTypeEffectiveness), combatMoves: new Map(response.combatMoves), }; } public async getPokemonList() { const queryParameters = { }; const response : Array = await this.AjaxRequest.ajaxGet('/dist/db/order.json', queryParameters); return response; } public async getPokemonLeagueStats(pokemonId : PVPogoProtos.PokemonId, form : PVPogoProtos.PokemonForm) { const fileName = form === PVPogoProtos.PokemonForm.FORM_UNSET ? PVPogoProtos.PokemonId[pokemonId] : PVPogoProtos.PokemonForm[form]; const queryParameters = { }; const response : ILeaguePokemonJson = await this.AjaxRequest.ajaxGet(`/dist/db/pokemon/${ fileName }.json`, queryParameters); return { ...response, effectiveness: new Map(response.effectiveness), }; } }