refactor: better cache in CDragonService

This commit is contained in:
Kalane 2021-09-14 13:31:35 +02:00
parent cf54188116
commit 973bc8e29b
3 changed files with 38 additions and 27 deletions

View file

@ -66,7 +66,7 @@ class MatchParser {
} }
} }
const originalChampionData = CDragonService.champions.find((c) => c.id === player.championId)! const originalChampionData = CDragonService.champions[player.championId]
const champRoles = originalChampionData.roles const champRoles = originalChampionData.roles
matchPlayers.push({ matchPlayers.push({

View file

@ -19,17 +19,17 @@ class BasicMatchSerializer extends MatchSerializer {
* @param id of the champion * @param id of the champion
*/ */
protected getChampion(id: number): SerializedMatchChampion { protected getChampion(id: number): SerializedMatchChampion {
const originalChampionData = CDragonService.champions.find((c) => c.id === id) const originalChampionData = CDragonService.champions[id]
const icon = const icon =
'https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/' + CDragonService.BASE_URL +
originalChampionData!.squarePortraitPath.split('/assets/')[1].toLowerCase() originalChampionData.squarePortraitPath.split('/assets/')[1].toLowerCase()
return { return {
icon, icon,
id: originalChampionData!.id, id: originalChampionData.id,
name: originalChampionData!.name, name: originalChampionData.name,
alias: originalChampionData!.alias, alias: originalChampionData.alias,
roles: originalChampionData!.roles, roles: originalChampionData.roles,
} }
} }
@ -51,15 +51,15 @@ class BasicMatchSerializer extends MatchSerializer {
* @param id of the summonerSpell * @param id of the summonerSpell
*/ */
public getSummonerSpell(id: number): SerializedMatchSummonerSpell | null { public getSummonerSpell(id: number): SerializedMatchSummonerSpell | null {
if (id === 0) { const spell = CDragonService.summonerSpells[id]
if (id === 0 || !spell) {
return null return null
} }
const spell = CDragonService.summonerSpells.find((s) => s.id === id)!
const spellName = spell.iconPath.split('/assets/')[1].toLowerCase() const spellName = spell.iconPath.split('/assets/')[1].toLowerCase()
return { return {
name: spell.name, name: spell.name,
description: spell.description, description: spell.description,
icon: `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${spellName}`, icon: `${CDragonService.BASE_URL}${spellName}`,
} }
} }
@ -72,7 +72,7 @@ class BasicMatchSerializer extends MatchSerializer {
continue continue
} }
const item = CDragonService.items.find((i) => i.id === id) const item = CDragonService.items[id]
if (!item) { if (!item) {
items.push(null) items.push(null)
continue continue
@ -80,7 +80,7 @@ class BasicMatchSerializer extends MatchSerializer {
const itemUrl = item.iconPath.split('/assets/')[1].toLowerCase() const itemUrl = item.iconPath.split('/assets/')[1].toLowerCase()
items.push({ items.push({
image: `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${itemUrl}`, image: `${CDragonService.BASE_URL}${itemUrl}`,
name: item.name, name: item.name,
description: item.description, description: item.description,
price: item.priceTotal, price: item.priceTotal,

View file

@ -10,22 +10,33 @@ import RoleIdentificationService, {
ChampionsPlayRate, ChampionsPlayRate,
} from 'App/Services/RoleIdentificationService' } from 'App/Services/RoleIdentificationService'
interface Identifiable {
id: number
}
export interface CDragonCache<T> {
[id: number]: T
}
class CDragonService { class CDragonService {
public champions: ChampionDTO[] public champions: CDragonCache<ChampionDTO>
public items: ItemDTO[] public items: CDragonCache<ItemDTO>
public perks: PerkDTO[] public perks: CDragonCache<PerkDTO>
public perkstyles: PerkStyleDTO[] public perkstyles: CDragonCache<PerkStyleDTO>
public summonerSpells: SummonerSpellDTO[] public summonerSpells: CDragonCache<SummonerSpellDTO>
public championRoles: ChampionsPlayRate public championRoles: ChampionsPlayRate
public readonly BASE_URL =
'https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/'
private setupCache<T extends Identifiable>(dto: T[]) {
return dto.reduce((obj, item) => ((obj[item.id] = item), obj), {})
}
/** /**
* Get global Context with CDragon Data * Get global Context with CDragon Data
*/ */
public async getContext() { public async getContext() {
if (this.champions) {
return
}
const items = await Jax.CDragon.items() const items = await Jax.CDragon.items()
const champions = await Jax.CDragon.champions() const champions = await Jax.CDragon.champions()
const perks = await Jax.CDragon.perks() const perks = await Jax.CDragon.perks()
@ -33,11 +44,11 @@ class CDragonService {
const summonerSpells = await Jax.CDragon.summonerSpells() const summonerSpells = await Jax.CDragon.summonerSpells()
const championRoles = await RoleIdentificationService.pullData().catch(() => {}) const championRoles = await RoleIdentificationService.pullData().catch(() => {})
this.champions = champions this.champions = this.setupCache(champions)
this.items = items this.items = this.setupCache(items)
this.perks = perks this.perks = this.setupCache(perks)
this.perkstyles = perkstyles.styles this.perkstyles = this.setupCache(perkstyles.styles)
this.summonerSpells = summonerSpells this.summonerSpells = this.setupCache(summonerSpells)
this.championRoles = championRoles as ChampionsPlayRate this.championRoles = championRoles as ChampionsPlayRate
} }
} }