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
@@ -0,0 +1,102 @@
|
||||
import { RedisService } from '../global/redis/redis.service';
|
||||
import { RiotDdragonService } from './riot-ddragon.service';
|
||||
|
||||
function jsonResponse(payload: unknown): Response {
|
||||
return {
|
||||
ok: true,
|
||||
json: jest.fn().mockResolvedValue(payload),
|
||||
} as unknown as Response;
|
||||
}
|
||||
|
||||
function makeRedisService() {
|
||||
return {
|
||||
getJson: jest.fn().mockResolvedValue(null),
|
||||
setJson: jest.fn().mockResolvedValue(undefined),
|
||||
} as unknown as jest.Mocked<RedisService>;
|
||||
}
|
||||
|
||||
describe('RiotDdragonService', () => {
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('includes localized item metadata in the manifest', async () => {
|
||||
const fetchMock = jest.fn(async (url: string) => {
|
||||
if (url.endsWith('/api/versions.json')) {
|
||||
return jsonResponse(['15.24.1']);
|
||||
}
|
||||
if (url.endsWith('/champion.json')) {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
Aatrox: { key: '266', id: 'Aatrox', name: '아트록스' },
|
||||
},
|
||||
});
|
||||
}
|
||||
if (url.endsWith('/summoner.json')) {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
SummonerFlash: { key: '4', id: 'SummonerFlash', name: '점멸' },
|
||||
},
|
||||
});
|
||||
}
|
||||
if (url.endsWith('/runesReforged.json')) {
|
||||
return jsonResponse([]);
|
||||
}
|
||||
if (url.endsWith('/item.json')) {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
'1055': {
|
||||
name: '도란의 검',
|
||||
description:
|
||||
'<mainText><stats>공격력 <attention>10</attention></stats></mainText>',
|
||||
plaintext: '공격력과 체력을 올려줍니다.',
|
||||
gold: {
|
||||
base: 450,
|
||||
total: 450,
|
||||
sell: 180,
|
||||
purchasable: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
throw new Error(`Unexpected URL: ${url}`);
|
||||
});
|
||||
jest.spyOn(global, 'fetch').mockImplementation(fetchMock as typeof fetch);
|
||||
|
||||
const redis = makeRedisService();
|
||||
const service = new RiotDdragonService(redis);
|
||||
|
||||
await expect(service.getManifest('ko_KR')).resolves.toMatchObject({
|
||||
version: '15.24.1',
|
||||
locale: 'ko_KR',
|
||||
items: {
|
||||
'1055': {
|
||||
name: '도란의 검',
|
||||
description:
|
||||
'<mainText><stats>공격력 <attention>10</attention></stats></mainText>',
|
||||
plaintext: '공격력과 체력을 올려줍니다.',
|
||||
gold: {
|
||||
base: 450,
|
||||
total: 450,
|
||||
sell: 180,
|
||||
purchasable: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
expect.stringContaining('/15.24.1/data/ko_KR/item.json'),
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(redis.setJson).toHaveBeenCalledWith(
|
||||
'ddragon:manifest:v4:ko_KR',
|
||||
expect.objectContaining({
|
||||
items: expect.objectContaining({
|
||||
'1055': expect.objectContaining({ name: '도란의 검' }),
|
||||
}),
|
||||
}),
|
||||
86_400,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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