diff --git a/src/types.ts b/src/types.ts index 5b2ae87..8386435 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,6 +17,7 @@ export type LureIDs = StringOrNumber< > export interface UiconsIndex { + background?: T device?: T gym?: T invasion?: T diff --git a/src/uicons.test.ts b/src/uicons.test.ts index 4087080..cef21bb 100644 --- a/src/uicons.test.ts +++ b/src/uicons.test.ts @@ -7,6 +7,10 @@ const BASE_AUDIO_URL = 'https://raw.githubusercontent.com/WatWowMap/wwm-uaudio/main' const icons = new UICONS(BASE_ICON_URL) +const backgroundIcons = new UICONS({ + path: BASE_ICON_URL, + data: { background: ['0.webp', '1.webp'] }, +}) describe('webp format', () => { test('should fetch remotely', async () => { @@ -75,6 +79,19 @@ describe('misc', () => { }) }) +describe('background', () => { + test('fallback icon', () => { + expect(backgroundIcons.background(999)).toBe( + `${BASE_ICON_URL}/background/0.webp` + ) + }) + test('specific background', () => { + expect(backgroundIcons.background(1)).toBe( + `${BASE_ICON_URL}/background/1.webp` + ) + }) +}) + describe('nest', () => { test('grass - string', () => { expect(icons.nest('12')).toBe(`${BASE_ICON_URL}/nest/12.webp`) diff --git a/src/uicons.ts b/src/uicons.ts index 3a6e99c..c1092eb 100644 --- a/src/uicons.ts +++ b/src/uicons.ts @@ -42,6 +42,7 @@ export class UICONS { #extensionMap: ExtensionMap #label: string + #background: Set #device: Set #gym: Set #invasion: Set @@ -124,6 +125,23 @@ export class UICONS { return [`${flag}${Number(value) || ''}`, flag, ''] } + /** + * @param backgroundId the background ID + * @returns the src of the background icon + */ + background(backgroundId?: string | number): string + background(backgroundId = 0): string { + if (!this.#isReady('background')) return '' + + const baseUrl = `${this.#path}/background` + + const result = `${backgroundId}.${this.#extensionMap.background}` + if (this.#background.has(result)) { + return `${baseUrl}/${result}` + } + return `${baseUrl}/0.${this.#extensionMap.background}` + } + /** * This is used to initialize the UICONS class asynchronously by automatically fetching the index.json file * from the remote UICONS repository provided in the constructor @@ -144,6 +162,7 @@ export class UICONS { * @param data The index.json file from the UICONS repository */ init(data: Index) { + this.#background = new Set(data.background || []) this.#device = new Set(data.device || []) this.#gym = new Set(data.gym || []) this.#invasion = new Set(data.invasion || []) @@ -177,6 +196,10 @@ export class UICONS { this.#isReady() const [first, second] = location.split('.', 2) switch (first) { + case 'background': + return this.#background.has( + `${fileName}.${this.#extensionMap.background}` + ) case 'device': return this.#device.has(`${fileName}.${this.#extensionMap.device}`) case 'gym':