From 87bef546d35b27093119cd55f7763e57a70324e1 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 10 Nov 2023 18:27:02 +0000 Subject: [PATCH] Add Drop Rarity test command (#73) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Add `/droprarity` test command to help test random rarities - Remove no-longer-needed `DROP_RARITY` environment variable #16 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/73 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- .dev.env | 2 - .prod.env | 2 - .stage.env | 2 - src/commands/drop.ts | 6 +-- src/commands/stage/droprarity.ts | 81 ++++++++++++++++++++++++++++++++ src/constants/CardRarity.ts | 17 +++++++ src/registry.ts | 2 + 7 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 src/commands/stage/droprarity.ts diff --git a/.dev.env b/.dev.env index e9d8362..a8ca023 100644 --- a/.dev.env +++ b/.dev.env @@ -16,8 +16,6 @@ BOT_ENV=4 ABOUT_FUNDING= ABOUT_REPO= -DROP_RARITY=-1 - DB_HOST=127.0.0.1 DB_PORT=3301 DB_NAME=carddrop diff --git a/.prod.env b/.prod.env index a6bc823..97472be 100644 --- a/.prod.env +++ b/.prod.env @@ -16,8 +16,6 @@ BOT_ENV=1 ABOUT_FUNDING= ABOUT_REPO= -DROP_RARITY=-1 - DB_HOST=127.0.0.1 DB_PORT=3321 DB_NAME=carddrop diff --git a/.stage.env b/.stage.env index a2fc5a3..66c4653 100644 --- a/.stage.env +++ b/.stage.env @@ -16,8 +16,6 @@ BOT_ENV=2 ABOUT_FUNDING= ABOUT_REPO= -DROP_RARITY=-1 - DB_HOST=127.0.0.1 DB_PORT=3311 DB_NAME=carddrop diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 031bbe7..c23f699 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -17,11 +17,7 @@ export default class Drop extends Command { } public override async execute(interaction: CommandInteraction) { - let randomCard = await CardDropHelper.GetRandomCard(); - - if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) { - randomCard = await CardDropHelper.GetRandomCardByRarity(Number(process.env.DROP_RARITY)); - } + const randomCard = await CardDropHelper.GetRandomCard(); const image = readFileSync(randomCard.Path); diff --git a/src/commands/stage/droprarity.ts b/src/commands/stage/droprarity.ts new file mode 100644 index 0000000..fa11b78 --- /dev/null +++ b/src/commands/stage/droprarity.ts @@ -0,0 +1,81 @@ +import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js"; +import { Command } from "../../type/command"; +import { CardRarity, CardRarityParse } from "../../constants/CardRarity"; +import CardDropHelper from "../../helpers/CardDropHelper"; +import { readFileSync } from "fs"; +import Inventory from "../../database/entities/app/Inventory"; +import { v4 } from "uuid"; +import { CoreClient } from "../../client/client"; + +export default class Droprarity extends Command { + constructor() { + super(); + + super.CommandBuilder = new SlashCommandBuilder() + .setName('droprarity') + .setDescription('(TEST) Summon a random card of a specific rarity') + .addStringOption(x => + x + .setName('rarity') + .setDescription('The rarity you want to summon') + .setRequired(true)); + } + + public override async execute(interaction: CommandInteraction) { + if (!interaction.isChatInputCommand()) return; + + const rarity = interaction.options.get('rarity'); + + if (!rarity || !rarity.value) { + await interaction.reply('Rarity is required'); + return; + } + + const rarityType = CardRarityParse(rarity.value.toString()); + + if (rarityType == CardRarity.Unknown) { + await interaction.reply('Invalid rarity'); + return; + } + + const card = await CardDropHelper.GetRandomCardByRarity(rarityType); + + if (!card) { + await interaction.reply('Card not found'); + return; + } + + const image = readFileSync(card.Path); + + await interaction.deferReply(); + + const attachment = new AttachmentBuilder(image, { name: card.FileName }); + + const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.CardNumber); + const quantityClaimed = inventory ? inventory.Quantity : 0; + + const embed = CardDropHelper.GenerateDropEmbed(card, quantityClaimed || 0); + + const claimId = v4(); + + const row = CardDropHelper.GenerateDropButtons(card, claimId, interaction.user.id); + + try { + await interaction.editReply({ + embeds: [ embed ], + files: [ attachment ], + components: [ row ], + }); + } catch (e) { + console.error(e); + + if (e instanceof DiscordAPIError) { + await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}`); + } else { + await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: UNKNOWN`); + } + } + + CoreClient.ClaimId = claimId; + } +} \ No newline at end of file diff --git a/src/constants/CardRarity.ts b/src/constants/CardRarity.ts index b4a1f9b..629130a 100644 --- a/src/constants/CardRarity.ts +++ b/src/constants/CardRarity.ts @@ -41,4 +41,21 @@ export function CardRarityToColour(rarity: CardRarity): number { case CardRarity.Manga: return EmbedColours.MangaCard; } +} + +export function CardRarityParse(rarity: string): CardRarity { + switch (rarity.toLowerCase()) { + case "bronze": + return CardRarity.Bronze; + case "silver": + return CardRarity.Silver; + case "gold": + return CardRarity.Gold; + case "legendary": + return CardRarity.Legendary; + case "manga": + return CardRarity.Manga; + default: + return CardRarity.Unknown; + } } \ No newline at end of file diff --git a/src/registry.ts b/src/registry.ts index 9685c5d..05a5485 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -6,6 +6,7 @@ import Drop from "./commands/drop"; // Test Command Imports import Dropnumber from "./commands/stage/dropnumber"; +import Droprarity from "./commands/stage/droprarity"; // Button Event Imports import Claim from "./buttonEvents/Claim"; @@ -20,6 +21,7 @@ export default class Registry { // Test Commands CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test); + CoreClient.RegisterCommand('droprarity', new Droprarity(), Environment.Test); } public static RegisterEvents() {