Skip to content

Commit

Permalink
Add fallback for card embed thumbnail when Master Duel illustration i…
Browse files Browse the repository at this point in the history
…s not available

Same heuristic as /art: master_duel_rarity
  • Loading branch information
kevinlul committed May 10, 2024
1 parent e2b7407 commit 3a0ae4a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ export function masterDuelIllustrationURL(card: Static<typeof CardSchema>): stri
return yugipediaFileRedirect(masterDuelIllustration(card));
}

export function thumbnail(card: Static<typeof CardSchema>): string | null {
if (card.master_duel_rarity) {
return masterDuelIllustrationURL(card);
}
if (card.images?.length) {
return yugipediaFileRedirect(card.images[0].illustration ?? card.images[0].image);
}
return null;
}

export function createCardEmbed(
card: Static<typeof CardSchema>,
lang: Locale,
Expand All @@ -400,7 +410,7 @@ export function createCardEmbed(
const embed = new EmbedBuilder()
.setTitle(formatCardName(card, lang))
.setURL(ygoprodeck)
.setThumbnail(masterDuelIllustrationURL(card));
.setThumbnail(thumbnail(card));

const links = {
name: t`🔗 Links`,
Expand Down
32 changes: 31 additions & 1 deletion test/unit/card.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { masterDuelIllustration, masterDuelIllustrationURL, yugipediaFileRedirect } from "../../src/card";
import { masterDuelIllustration, masterDuelIllustrationURL, thumbnail, yugipediaFileRedirect } from "../../src/card";
import { MasterDuelRarity } from "../../src/definitions";

describe("Helper functions", () => {
test("yugipediaFileRedirect", () => {
Expand Down Expand Up @@ -39,4 +40,33 @@ describe("Helper functions", () => {
"https://yugipedia.com/wiki/Special:Redirect/file/BlueEyesWhiteDragon-MADU-EN-VG-artwork.png?utm_source=bastion"
);
});
test.each([
{
label: "master_duel_rarity",
card: { master_duel_rarity: MasterDuelRarity.N, name: { en: "S:P Little Knight" } },
expected: "https://yugipedia.com/wiki/Special:Redirect/file/SPLittleKnight-MADU-EN-VG-artwork.png?utm_source=bastion"
},
{
label: "illustration and no master_duel_rarity",
card: {
images: [
{
illustration: "ElementalHEROAirNeos-LOD2-JP-VG-artwork.jpg",
index: 1,
image: "ElementalHEROAirNeos-STON-EN-UR-UE-Reprint.png"
}
]
},
expected: "https://yugipedia.com/wiki/Special:Redirect/file/ElementalHEROAirNeos-LOD2-JP-VG-artwork.jpg?utm_source=bastion"
},
{
label: "no illustration and no master_duel_rarity",
card: { images: [{ index: 1, image: "DiabellzetheOriginalSinkeeper-LEDE-EN-ScR-1E.png" }] },
expected: "https://yugipedia.com/wiki/Special:Redirect/file/DiabellzetheOriginalSinkeeper-LEDE-EN-ScR-1E.png?utm_source=bastion"
},
{ label: "no images", card: {}, expected: null }
])("thumbnail returns $expected for card with $label", ({ card, expected }) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(thumbnail(card as any)).toEqual(expected);
});
});

0 comments on commit 3a0ae4a

Please sign in to comment.