From 8cd579df9189673b38bbb4fd4bce3d6b6222a666 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 5 Oct 2024 19:32:01 +0100 Subject: [PATCH 1/2] Create base command --- src/commands/multidrop.ts | 14 +++++++++++++- src/helpers/CardDropHelperMetadata.ts | 9 +++++---- src/registry.ts | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/commands/multidrop.ts b/src/commands/multidrop.ts index 77cd4b2..f35b921 100644 --- a/src/commands/multidrop.ts +++ b/src/commands/multidrop.ts @@ -16,7 +16,7 @@ export default class Multidrop extends Command { super(); this.CommandBuilder = new SlashCommandBuilder() - .setName("mutlidrop") + .setName("multidrop") .setDescription("Drop 11 cards for the price of 10!"); } @@ -70,6 +70,18 @@ export default class Multidrop extends Command { const quantityClaimed = inventory ? inventory.Quantity : 0; const embed = CardDropHelperMetadata.GenerateMultidropEmbed(randomCard, quantityClaimed, imageFileName, cardsRemaining, undefined, user.Currency); + + const row = CardDropHelperMetadata.GenerateMultidropButtons(randomCard, cardsRemaining, interaction.user.id); + + await interaction.editReply({ + embeds: [ embed ], + files: [ attachment ], + components: [ row ], + }); + } catch (e) { + AppLogger.LogError("Commands/Multidrop", `Error sending next drop for card ${randomCard.card.id}: ${e}`); + + await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`); } } } \ No newline at end of file diff --git a/src/helpers/CardDropHelperMetadata.ts b/src/helpers/CardDropHelperMetadata.ts index bc88c88..8b9b273 100644 --- a/src/helpers/CardDropHelperMetadata.ts +++ b/src/helpers/CardDropHelperMetadata.ts @@ -1,5 +1,5 @@ import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js"; -import { CardRarity, CardRarityToColour, CardRarityToString } from "../constants/CardRarity"; +import { CardRarity, CardRarityToColour, CardRarityToString, GetSacrificeAmount } from "../constants/CardRarity"; import CardRarityChances from "../constants/CardRarityChances"; import { DropResult } from "../contracts/SeriesMetadata"; import { CoreClient } from "../client/client"; @@ -158,16 +158,17 @@ export default class CardDropHelperMetadata { return dropEmbed; } - public static GenerateMultidropButtons(drop: DropResult, cardsRemaining: number, disabled = false): ActionRowBuilder { + public static GenerateMultidropButtons(drop: DropResult, cardsRemaining: number, userId: string, disabled = false): ActionRowBuilder { return new ActionRowBuilder() .addComponents( new ButtonBuilder() - .setCustomId(`multidrop keep ${drop.card.id} ${cardsRemaining}`) + .setCustomId(`multidrop keep ${drop.card.id} ${cardsRemaining} ${userId}`) .setLabel("Keep") .setStyle(ButtonStyle.Primary) .setDisabled(disabled), new ButtonBuilder() - .setCustomId(`multidrop sacrifice ${drop.card.id} ${cardsRemaining}`) + .setCustomId(`multidrop sacrifice ${drop.card.id} ${cardsRemaining} ${userId}`) + .setLabel(`Sacrifice (+${GetSacrificeAmount(drop.card.type)} 🪙)`) .setStyle(ButtonStyle.Secondary)); } } diff --git a/src/registry.ts b/src/registry.ts index 67936c8..7b87777 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -11,6 +11,7 @@ import Gdrivesync from "./commands/gdrivesync"; import Give from "./commands/give"; import Id from "./commands/id"; import Inventory from "./commands/inventory"; +import Multidrop from "./commands/multidrop"; import Resync from "./commands/resync"; import Sacrifice from "./commands/sacrifice"; import Series from "./commands/series"; @@ -46,6 +47,7 @@ export default class Registry { CoreClient.RegisterCommand("give", new Give()); CoreClient.RegisterCommand("id", new Id()); CoreClient.RegisterCommand("inventory", new Inventory()); + CoreClient.RegisterCommand("multidrop", new Multidrop()); CoreClient.RegisterCommand("resync", new Resync()); CoreClient.RegisterCommand("sacrifice", new Sacrifice()); CoreClient.RegisterCommand("series", new Series()); From cb5dfad1c43328b383bbfc53ef5416b488f64d9e Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 5 Oct 2024 19:47:47 +0100 Subject: [PATCH 2/2] WIP: Start creating keep command handler --- src/buttonEvents/Claim.ts | 2 +- src/buttonEvents/Multidrop.ts | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/buttonEvents/Multidrop.ts diff --git a/src/buttonEvents/Claim.ts b/src/buttonEvents/Claim.ts index 4d5d97a..190a09e 100644 --- a/src/buttonEvents/Claim.ts +++ b/src/buttonEvents/Claim.ts @@ -58,7 +58,7 @@ export default class Claim extends ButtonEvent { if (!inventory) { inventory = new Inventory(userId, cardNumber, 1); } else { - inventory.SetQuantity(inventory.Quantity + 1); + inventory.AddQuantity(1); } await inventory.Save(Inventory, inventory); diff --git a/src/buttonEvents/Multidrop.ts b/src/buttonEvents/Multidrop.ts new file mode 100644 index 0000000..88bd240 --- /dev/null +++ b/src/buttonEvents/Multidrop.ts @@ -0,0 +1,78 @@ +import { ButtonInteraction, EmbedBuilder } from "discord.js"; +import { ButtonEvent } from "../type/buttonEvent"; +import AppLogger from "../client/appLogger"; +import { CoreClient } from "../client/client"; +import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata"; +import Inventory from "../database/entities/app/Inventory"; +import EmbedColours from "../constants/EmbedColours"; +import { readFileSync } from "fs"; +import path from "path"; + +export default class Multidrop extends ButtonEvent { + public override async execute(interaction: ButtonInteraction) { + const action = interaction.customId.split(" ")[1]; + + switch (action) { + case "keep": + await this.Keep(interaction); + break; + default: + await interaction.reply("Invalid action"); + AppLogger.LogError("Button/Multidrop", `Invalid action, ${action}`); + } + } + + public async Keep(interaction: ButtonInteraction) { + const cardNumber = interaction.customId.split(" ")[2]; + let cardsRemaining = Number(interaction.customId.split(" ")[3]) || 0; + const userId = interaction.customId.split(" ")[4]; + + if (interaction.user.id != userId) { + await interaction.reply("You're not the user this drop was made for!"); + return; + } + + const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); + + if (!card) { + await interaction.reply("Unable to find card."); + AppLogger.LogWarn("Button/Multidrop/Keep", `Card not found, ${cardNumber}`); + return; + } + + if (cardsRemaining < 0) { + await interaction.reply("Your multidrop has ran out! Please buy a new one!"); + return; + } + + // Claim + let inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, cardNumber); + + if (!inventory) { + inventory = new Inventory(interaction.user.id, cardNumber, 1); + } else { + inventory.AddQuantity(1); + } + + await inventory.Save(Inventory, inventory); + + // Pack has ran out + if (cardsRemaining == 0) { + const embed = new EmbedBuilder() + .setDescription("Your multidrop has ran out! Please buy a new one!") + .setColor(EmbedColours.Ok); + + await interaction.update({ embeds: [ embed ]}); + return; + } + + // Drop next card + cardsRemaining -= 1; + + await interaction.deferUpdate(); + + try { + const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", )) + } + } +} \ No newline at end of file