diff --git a/server-v2/app/Parsers/MatchParser.ts b/server-v2/app/Parsers/MatchParser.ts index 6271301..706c424 100644 --- a/server-v2/app/Parsers/MatchParser.ts +++ b/server-v2/app/Parsers/MatchParser.ts @@ -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({ diff --git a/server-v2/app/Serializers/BasicMatchSerializer.ts b/server-v2/app/Serializers/BasicMatchSerializer.ts index 605b9fe..9d33ece 100644 --- a/server-v2/app/Serializers/BasicMatchSerializer.ts +++ b/server-v2/app/Serializers/BasicMatchSerializer.ts @@ -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, diff --git a/server-v2/app/Services/CDragonService.ts b/server-v2/app/Services/CDragonService.ts index d604604..12fc911 100644 --- a/server-v2/app/Services/CDragonService.ts +++ b/server-v2/app/Services/CDragonService.ts @@ -10,22 +10,33 @@ import RoleIdentificationService, { ChampionsPlayRate, } from 'App/Services/RoleIdentificationService' +interface Identifiable { + id: number +} + +export interface CDragonCache { + [id: number]: T +} + class CDragonService { - public champions: ChampionDTO[] - public items: ItemDTO[] - public perks: PerkDTO[] - public perkstyles: PerkStyleDTO[] - public summonerSpells: SummonerSpellDTO[] + public champions: CDragonCache + public items: CDragonCache + public perks: CDragonCache + public perkstyles: CDragonCache + public summonerSpells: CDragonCache public championRoles: ChampionsPlayRate + public readonly BASE_URL = + 'https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/' + + private setupCache(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 } }