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
matchPlayers.push({

View file

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

View file

@ -10,22 +10,33 @@ import RoleIdentificationService, {
ChampionsPlayRate,
} from 'App/Services/RoleIdentificationService'
interface Identifiable {
id: number
}
export interface CDragonCache<T> {
[id: number]: T
}
class CDragonService {
public champions: ChampionDTO[]
public items: ItemDTO[]
public perks: PerkDTO[]
public perkstyles: PerkStyleDTO[]
public summonerSpells: SummonerSpellDTO[]
public champions: CDragonCache<ChampionDTO>
public items: CDragonCache<ItemDTO>
public perks: CDragonCache<PerkDTO>
public perkstyles: CDragonCache<PerkStyleDTO>
public summonerSpells: CDragonCache<SummonerSpellDTO>
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
*/
public async getContext() {
if (this.champions) {
return
}
const items = await Jax.CDragon.items()
const champions = await Jax.CDragon.champions()
const perks = await Jax.CDragon.perks()
@ -33,11 +44,11 @@ class CDragonService {
const summonerSpells = await Jax.CDragon.summonerSpells()
const championRoles = await RoleIdentificationService.pullData().catch(() => {})
this.champions = champions
this.items = items
this.perks = perks
this.perkstyles = perkstyles.styles
this.summonerSpells = summonerSpells
this.champions = this.setupCache(champions)
this.items = this.setupCache(items)
this.perks = this.setupCache(perks)
this.perkstyles = this.setupCache(perkstyles.styles)
this.summonerSpells = this.setupCache(summonerSpells)
this.championRoles = championRoles as ChampionsPlayRate
}
}