feat: add item tooltips to match history
This commit is contained in:
1 parent
38c2d1f1b4
commit
68afaf0cb8
8 files changed
+477
-49
No files matched your search
@@ -5,7 +5,7 @@ const DDRAGON_BASE = 'https://ddragon.leagueoflegends.com';
|
||||
const DDRAGON_CDN = `${DDRAGON_BASE}/cdn`;
|
||||
const DEFAULT_LOCALE = 'ko_KR';
|
||||
const CACHE_TTL_SECONDS = 24 * 60 * 60;
|
||||
const CACHE_VERSION = 'v3';
|
||||
const CACHE_VERSION = 'v4';
|
||||
const FETCH_TIMEOUT_MS = 10_000;
|
||||
|
||||
export interface DdragonChampionEntry {
|
||||
@@ -29,6 +29,18 @@ export interface DdragonRuneStyleEntry extends DdragonRuneEntry {
|
||||
slots: Array<{ runes: DdragonRuneEntry[] }>;
|
||||
}
|
||||
|
||||
export interface DdragonItemEntry {
|
||||
name: string;
|
||||
description: string;
|
||||
plaintext: string;
|
||||
gold: {
|
||||
base: number;
|
||||
total: number;
|
||||
sell: number;
|
||||
purchasable: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface DdragonManifest {
|
||||
version: string;
|
||||
cdn: string;
|
||||
@@ -36,6 +48,7 @@ export interface DdragonManifest {
|
||||
champions: Record<string, DdragonChampionEntry>;
|
||||
summonerSpells: Record<string, DdragonSummonerSpellEntry>;
|
||||
runesReforged: DdragonRuneStyleEntry[];
|
||||
items: Record<string, DdragonItemEntry>;
|
||||
fetchedAt: string;
|
||||
}
|
||||
|
||||
@@ -70,6 +83,22 @@ interface RuneStyleDataEntry extends RuneDataEntry {
|
||||
slots?: Array<{ runes?: RuneDataEntry[] }>;
|
||||
}
|
||||
|
||||
interface ItemDataEntry {
|
||||
name?: string;
|
||||
description?: string;
|
||||
plaintext?: string;
|
||||
gold?: {
|
||||
base?: number;
|
||||
total?: number;
|
||||
sell?: number;
|
||||
purchasable?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
interface ItemDataResponse {
|
||||
data: Record<string, ItemDataEntry>;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class RiotDdragonService {
|
||||
private readonly logger = new Logger(RiotDdragonService.name);
|
||||
@@ -102,10 +131,11 @@ export class RiotDdragonService {
|
||||
|
||||
private async buildManifest(locale: string): Promise<DdragonManifest> {
|
||||
const version = await this.fetchLatestVersion();
|
||||
const [champions, summonerSpells, runesReforged] = await Promise.all([
|
||||
const [champions, summonerSpells, runesReforged, items] = await Promise.all([
|
||||
this.fetchChampionMap(version, locale),
|
||||
this.fetchSummonerSpellMap(version, locale),
|
||||
this.fetchRuneStyles(version, locale),
|
||||
this.fetchItemMap(version, locale),
|
||||
]);
|
||||
return {
|
||||
version,
|
||||
@@ -114,6 +144,7 @@ export class RiotDdragonService {
|
||||
champions,
|
||||
summonerSpells,
|
||||
runesReforged,
|
||||
items,
|
||||
fetchedAt: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
@@ -200,6 +231,38 @@ export class RiotDdragonService {
|
||||
}));
|
||||
}
|
||||
|
||||
private async fetchItemMap(
|
||||
version: string,
|
||||
locale: string,
|
||||
): Promise<Record<string, DdragonItemEntry>> {
|
||||
const url = `${DDRAGON_CDN}/${version}/data/${locale}/item.json`;
|
||||
const payload = await this.fetchJson<ItemDataResponse>(url);
|
||||
if (!payload || typeof payload !== 'object' || !payload.data) {
|
||||
throw new Error(`ddragon item.json response is malformed (${url}).`);
|
||||
}
|
||||
|
||||
const map: Record<string, DdragonItemEntry> = {};
|
||||
for (const [itemId, entry] of Object.entries(payload.data)) {
|
||||
if (!/^\d+$/.test(itemId) || typeof entry?.name !== 'string') {
|
||||
continue;
|
||||
}
|
||||
|
||||
map[itemId] = {
|
||||
name: entry.name,
|
||||
description:
|
||||
typeof entry.description === 'string' ? entry.description : '',
|
||||
plaintext: typeof entry.plaintext === 'string' ? entry.plaintext : '',
|
||||
gold: {
|
||||
base: numberOrZero(entry.gold?.base),
|
||||
total: numberOrZero(entry.gold?.total),
|
||||
sell: numberOrZero(entry.gold?.sell),
|
||||
purchasable: entry.gold?.purchasable ?? false,
|
||||
},
|
||||
};
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private async fetchJson<T>(url: string): Promise<T> {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
||||
@@ -245,6 +308,10 @@ function isRuneEntry(value: unknown): value is RuneDataEntry {
|
||||
);
|
||||
}
|
||||
|
||||
function numberOrZero(value: unknown): number {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
|
||||
}
|
||||
|
||||
function manifestCacheKey(locale: string): string {
|
||||
return `ddragon:manifest:${CACHE_VERSION}:${locale}`;
|
||||
}
|
||||
Reference in new issue
Block a user