From 5deb6fcc1488a7b8d17edd401eb7f8df72ad9490 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 29 Aug 2024 19:21:53 +0100 Subject: [PATCH] Add a colour override for embed colours - Using a new optional value in the JSON, if set overrides the default one set by the rarity - Added a failsafe if the override colour is invalid to default and log a warning #349 --- src/contracts/SeriesMetadata.ts | 1 + src/helpers/CardDropHelperMetadata.ts | 16 +++++++++++++++- src/helpers/StringTools.ts | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/contracts/SeriesMetadata.ts b/src/contracts/SeriesMetadata.ts index 4a989bc..4dd2eb9 100644 --- a/src/contracts/SeriesMetadata.ts +++ b/src/contracts/SeriesMetadata.ts @@ -12,6 +12,7 @@ export interface CardMetadata { type: CardRarity, path: string, subseries?: string, + colour?: string, } export interface DropResult { diff --git a/src/helpers/CardDropHelperMetadata.ts b/src/helpers/CardDropHelperMetadata.ts index 84bcdfa..bc59e93 100644 --- a/src/helpers/CardDropHelperMetadata.ts +++ b/src/helpers/CardDropHelperMetadata.ts @@ -5,6 +5,7 @@ import { DropResult } from "../contracts/SeriesMetadata"; import { CoreClient } from "../client/client"; import AppLogger from "../client/appLogger"; import CardConstants from "../constants/CardConstants"; +import StringTools from "./StringTools"; export default class CardDropHelperMetadata { public static GetRandomCard(): DropResult | undefined { @@ -82,12 +83,25 @@ export default class CardDropHelperMetadata { AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropEmbed", `Parameters: drop=${drop.card.id}, quantityClaimed=${quantityClaimed}, imageFileName=${imageFileName}`); const description = drop.card.subseries ?? drop.series.name; + let colour = CardRarityToColour(drop.card.type); + + if (drop.card.colour && StringTools.IsHexCode(drop.card.colour)) { + const hexCode = Number("0x" + drop.card.colour); + + if (hexCode) { + colour = hexCode; + } else { + AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`); + } + } else if (drop.card.colour) { + AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`); + } const embed = new EmbedBuilder() .setTitle(drop.card.name) .setDescription(description) .setFooter({ text: `${CardRarityToString(drop.card.type)} ยท ${drop.card.id}` }) - .setColor(CardRarityToColour(drop.card.type)) + .setColor(colour) .setImage(`attachment://${imageFileName}`) .addFields([ { diff --git a/src/helpers/StringTools.ts b/src/helpers/StringTools.ts index cb5591a..1e43ded 100644 --- a/src/helpers/StringTools.ts +++ b/src/helpers/StringTools.ts @@ -39,4 +39,18 @@ export default class StringTools { public static ReplaceAll(str: string, find: string, replace: string) { return str.replace(new RegExp(find, "g"), replace); } + + public static IsHexCode(str: string): boolean { + if (str.length != 6) return false; + + const characters = "0123456789abcdefABCDEF"; + + for (let i = 0; i < 6; i++) { + const char = str[i]; + + if (!characters.includes(char)) return false; + } + + return true; + } } \ No newline at end of file