From 08949d0af33543a99a64272f956c316ff9d7eabf Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 5 Sep 2024 19:40:15 +0100 Subject: [PATCH 1/4] Update the sacrifice command to allow input of quantity --- src/commands/sacrifice.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/commands/sacrifice.ts b/src/commands/sacrifice.ts index 4d1c51a..d6300e4 100644 --- a/src/commands/sacrifice.ts +++ b/src/commands/sacrifice.ts @@ -16,11 +16,16 @@ export default class Sacrifice extends Command { x .setName("cardnumber") .setDescription("The card to sacrifice from your inventory") - .setRequired(true)); + .setRequired(true)) + .addNumberOption(x => + x + .setName("quantity") + .setDescription("The amount to sacrifice (default 1)")); } public override async execute(interaction: CommandInteraction): Promise { const cardnumber = interaction.options.get("cardnumber", true); + const quantity = interaction.options.get("quantity")?.value as number ?? 1; const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, cardnumber.value! as string); @@ -29,6 +34,11 @@ export default class Sacrifice extends Command { return; } + if (cardInInventory.Quantity < quantity) { + await interaction.reply(`You can only sacrifice what you own! You have ${cardInInventory.Quantity} of this card`); + return; + } + const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardnumber.value! as string); if (!cardData) { @@ -44,6 +54,7 @@ export default class Sacrifice extends Command { `Series: ${cardData.series.name}`, `Rarity: ${cardRarityString}`, `Quantity Owned: ${cardInInventory.Quantity}`, + `Quantity To Sacrifice: ${quantity}`, `Sacrifice Amount: ${cardValue}`, ]; @@ -56,11 +67,11 @@ export default class Sacrifice extends Command { const row = new ActionRowBuilder() .addComponents([ new ButtonBuilder() - .setCustomId(`sacrifice confirm ${interaction.user.id} ${cardnumber.value!}`) + .setCustomId(`sacrifice confirm ${interaction.user.id} ${cardnumber.value!} ${quantity}`) .setLabel("Confirm") .setStyle(ButtonStyle.Success), new ButtonBuilder() - .setCustomId(`sacrifice cancel ${interaction.user.id} ${cardnumber.value!}`) + .setCustomId(`sacrifice cancel ${interaction.user.id} ${cardnumber.value!} ${quantity}`) .setLabel("Cancel") .setStyle(ButtonStyle.Secondary), ]); -- 2.45.2 From ca6ccea16f4e3365ddc538f6dbb93c4542bacce4 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 6 Sep 2024 18:07:55 +0100 Subject: [PATCH 2/4] Update the button events to take into account the quantity field --- src/buttonEvents/Sacrifice.ts | 24 +++++++++++++++++++----- src/commands/sacrifice.ts | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/buttonEvents/Sacrifice.ts b/src/buttonEvents/Sacrifice.ts index a82ba81..7b5616e 100644 --- a/src/buttonEvents/Sacrifice.ts +++ b/src/buttonEvents/Sacrifice.ts @@ -23,6 +23,7 @@ export default class Sacrifice extends ButtonEvent { private async confirm(interaction: ButtonInteraction) { const userId = interaction.customId.split(" ")[2]; const cardNumber = interaction.customId.split(" ")[3]; + const quantity = Number(interaction.customId.split(" ")[4]); if (userId != interaction.user.id) { await interaction.reply("Only the user who created this sacrifice can confirm it."); @@ -31,11 +32,16 @@ export default class Sacrifice extends ButtonEvent { const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber); - if (!cardInInventory) { + if (!cardInInventory || cardInInventory.Quantity == 0) { await interaction.reply("Unable to find card in inventory."); return; } + if (cardInInventory.Quantity < quantity) { + await interaction.reply("You can only sacrifice what you own."); + return; + } + const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); if (!cardData) { @@ -50,11 +56,11 @@ export default class Sacrifice extends ButtonEvent { return; } - cardInInventory.RemoveQuantity(1); + cardInInventory.RemoveQuantity(quantity); await cardInInventory.Save(Inventory, cardInInventory); - const cardValue = GetSacrificeAmount(cardData.card.type); + const cardValue = GetSacrificeAmount(cardData.card.type) * quantity; const cardRarityString = CardRarityToString(cardData.card.type); user.AddCurrency(cardValue); @@ -66,6 +72,7 @@ export default class Sacrifice extends ButtonEvent { `Series: ${cardData.series.name}`, `Rarity: ${cardRarityString}`, `Quantity Owned: ${cardInInventory.Quantity}`, + `Quantity To Sacrifice: ${quantity}`, `Sacrifice Amount: ${cardValue}`, ]; @@ -98,6 +105,7 @@ export default class Sacrifice extends ButtonEvent { private async cancel(interaction: ButtonInteraction) { const userId = interaction.customId.split(" ")[2]; const cardNumber = interaction.customId.split(" ")[3]; + const quantity = Number(interaction.customId.split(" ")[4]); if (userId != interaction.user.id) { await interaction.reply("Only the user who created this sacrifice can cancel it."); @@ -106,11 +114,16 @@ export default class Sacrifice extends ButtonEvent { const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber); - if (!cardInInventory) { + if (!cardInInventory || cardInInventory.Quantity == 0) { await interaction.reply("Unable to find card in inventory."); return; } + if (cardInInventory.Quantity < quantity) { + await interaction.reply("You can only sacrifice what you own."); + return; + } + const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); if (!cardData) { @@ -118,7 +131,7 @@ export default class Sacrifice extends ButtonEvent { return; } - const cardValue = GetSacrificeAmount(cardData.card.type); + const cardValue = GetSacrificeAmount(cardData.card.type) * quantity; const cardRarityString = CardRarityToString(cardData.card.type); const description = [ @@ -126,6 +139,7 @@ export default class Sacrifice extends ButtonEvent { `Series: ${cardData.series.name}`, `Rarity: ${cardRarityString}`, `Quantity Owned: ${cardInInventory.Quantity}`, + `Quantity To Sacrifice: ${quantity}`, `Sacrifice Amount: ${cardValue}`, ]; diff --git a/src/commands/sacrifice.ts b/src/commands/sacrifice.ts index d6300e4..40a0095 100644 --- a/src/commands/sacrifice.ts +++ b/src/commands/sacrifice.ts @@ -46,7 +46,7 @@ export default class Sacrifice extends Command { return; } - const cardValue = GetSacrificeAmount(cardData.card.type); + const cardValue = GetSacrificeAmount(cardData.card.type) * quantity; const cardRarityString = CardRarityToString(cardData.card.type); const description = [ -- 2.45.2 From 0e5489f0d12181dd54c1a2da85d1ab7f7160a723 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 6 Sep 2024 18:09:23 +0100 Subject: [PATCH 3/4] Add default if not supplied --- src/buttonEvents/Sacrifice.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/buttonEvents/Sacrifice.ts b/src/buttonEvents/Sacrifice.ts index 7b5616e..63c3971 100644 --- a/src/buttonEvents/Sacrifice.ts +++ b/src/buttonEvents/Sacrifice.ts @@ -23,7 +23,7 @@ export default class Sacrifice extends ButtonEvent { private async confirm(interaction: ButtonInteraction) { const userId = interaction.customId.split(" ")[2]; const cardNumber = interaction.customId.split(" ")[3]; - const quantity = Number(interaction.customId.split(" ")[4]); + const quantity = Number(interaction.customId.split(" ")[4]) ?? 1; if (userId != interaction.user.id) { await interaction.reply("Only the user who created this sacrifice can confirm it."); @@ -105,7 +105,7 @@ export default class Sacrifice extends ButtonEvent { private async cancel(interaction: ButtonInteraction) { const userId = interaction.customId.split(" ")[2]; const cardNumber = interaction.customId.split(" ")[3]; - const quantity = Number(interaction.customId.split(" ")[4]); + const quantity = Number(interaction.customId.split(" ")[4]) ?? 1; if (userId != interaction.user.id) { await interaction.reply("Only the user who created this sacrifice can cancel it."); -- 2.45.2 From 4359cbba1e5f8357a22fc4967877edc3cb1d2bff Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 15 Sep 2024 15:11:24 +0100 Subject: [PATCH 4/4] Fix suggested changes --- src/buttonEvents/Sacrifice.ts | 4 ++-- src/commands/sacrifice.ts | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/buttonEvents/Sacrifice.ts b/src/buttonEvents/Sacrifice.ts index 63c3971..463ca1f 100644 --- a/src/buttonEvents/Sacrifice.ts +++ b/src/buttonEvents/Sacrifice.ts @@ -23,7 +23,7 @@ export default class Sacrifice extends ButtonEvent { private async confirm(interaction: ButtonInteraction) { const userId = interaction.customId.split(" ")[2]; const cardNumber = interaction.customId.split(" ")[3]; - const quantity = Number(interaction.customId.split(" ")[4]) ?? 1; + const quantity = Number(interaction.customId.split(" ")[4]) || 1; if (userId != interaction.user.id) { await interaction.reply("Only the user who created this sacrifice can confirm it."); @@ -105,7 +105,7 @@ export default class Sacrifice extends ButtonEvent { private async cancel(interaction: ButtonInteraction) { const userId = interaction.customId.split(" ")[2]; const cardNumber = interaction.customId.split(" ")[3]; - const quantity = Number(interaction.customId.split(" ")[4]) ?? 1; + const quantity = Number(interaction.customId.split(" ")[4]) || 1; if (userId != interaction.user.id) { await interaction.reply("Only the user who created this sacrifice can cancel it."); diff --git a/src/commands/sacrifice.ts b/src/commands/sacrifice.ts index 40a0095..9683714 100644 --- a/src/commands/sacrifice.ts +++ b/src/commands/sacrifice.ts @@ -25,7 +25,9 @@ export default class Sacrifice extends Command { public override async execute(interaction: CommandInteraction): Promise { const cardnumber = interaction.options.get("cardnumber", true); - const quantity = interaction.options.get("quantity")?.value as number ?? 1; + const quantityInput = interaction.options.get("quantity")?.value ?? 1; + + const quantity = Number(quantityInput) || 1; const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, cardnumber.value! as string); -- 2.45.2