Add ability to sacrifice multiple cards at once #354

Merged
Vylpes merged 4 commits from feature/337-sacrifice-multiple into develop 2024-09-15 16:57:25 +01:00
2 changed files with 36 additions and 9 deletions

View file

@ -23,6 +23,7 @@ export default class Sacrifice extends ButtonEvent {
private async confirm(interaction: ButtonInteraction) { private async confirm(interaction: ButtonInteraction) {
const userId = interaction.customId.split(" ")[2]; const userId = interaction.customId.split(" ")[2];
const cardNumber = interaction.customId.split(" ")[3]; const cardNumber = interaction.customId.split(" ")[3];
const quantity = Number(interaction.customId.split(" ")[4]) || 1;
Vylpes marked this conversation as resolved Outdated

I don't actually think this works, try using || instead of ??

I don't actually think this works, try using `||` instead of `??`
if (userId != interaction.user.id) { if (userId != interaction.user.id) {
await interaction.reply("Only the user who created this sacrifice can confirm it."); 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); const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
if (!cardInInventory) { if (!cardInInventory || cardInInventory.Quantity == 0) {
await interaction.reply("Unable to find card in inventory."); await interaction.reply("Unable to find card in inventory.");
return; return;
} }
if (cardInInventory.Quantity < quantity) {
await interaction.reply("You can only sacrifice what you own.");
return;
}
const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
if (!cardData) { if (!cardData) {
@ -50,11 +56,11 @@ export default class Sacrifice extends ButtonEvent {
return; return;
} }
cardInInventory.RemoveQuantity(1); cardInInventory.RemoveQuantity(quantity);
await cardInInventory.Save(Inventory, cardInInventory); await cardInInventory.Save(Inventory, cardInInventory);
const cardValue = GetSacrificeAmount(cardData.card.type); const cardValue = GetSacrificeAmount(cardData.card.type) * quantity;
const cardRarityString = CardRarityToString(cardData.card.type); const cardRarityString = CardRarityToString(cardData.card.type);
user.AddCurrency(cardValue); user.AddCurrency(cardValue);
@ -66,6 +72,7 @@ export default class Sacrifice extends ButtonEvent {
`Series: ${cardData.series.name}`, `Series: ${cardData.series.name}`,
`Rarity: ${cardRarityString}`, `Rarity: ${cardRarityString}`,
`Quantity Owned: ${cardInInventory.Quantity}`, `Quantity Owned: ${cardInInventory.Quantity}`,
`Quantity To Sacrifice: ${quantity}`,
`Sacrifice Amount: ${cardValue}`, `Sacrifice Amount: ${cardValue}`,
]; ];
@ -98,6 +105,7 @@ export default class Sacrifice extends ButtonEvent {
private async cancel(interaction: ButtonInteraction) { private async cancel(interaction: ButtonInteraction) {
const userId = interaction.customId.split(" ")[2]; const userId = interaction.customId.split(" ")[2];
const cardNumber = interaction.customId.split(" ")[3]; const cardNumber = interaction.customId.split(" ")[3];
const quantity = Number(interaction.customId.split(" ")[4]) || 1;
Vylpes marked this conversation as resolved Outdated

Same here

Same here
if (userId != interaction.user.id) { if (userId != interaction.user.id) {
await interaction.reply("Only the user who created this sacrifice can cancel it."); 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); const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
if (!cardInInventory) { if (!cardInInventory || cardInInventory.Quantity == 0) {
await interaction.reply("Unable to find card in inventory."); await interaction.reply("Unable to find card in inventory.");
return; return;
} }
if (cardInInventory.Quantity < quantity) {
await interaction.reply("You can only sacrifice what you own.");
return;
}
const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
if (!cardData) { if (!cardData) {
@ -118,7 +131,7 @@ export default class Sacrifice extends ButtonEvent {
return; return;
} }
const cardValue = GetSacrificeAmount(cardData.card.type); const cardValue = GetSacrificeAmount(cardData.card.type) * quantity;
const cardRarityString = CardRarityToString(cardData.card.type); const cardRarityString = CardRarityToString(cardData.card.type);
const description = [ const description = [
@ -126,6 +139,7 @@ export default class Sacrifice extends ButtonEvent {
`Series: ${cardData.series.name}`, `Series: ${cardData.series.name}`,
`Rarity: ${cardRarityString}`, `Rarity: ${cardRarityString}`,
`Quantity Owned: ${cardInInventory.Quantity}`, `Quantity Owned: ${cardInInventory.Quantity}`,
`Quantity To Sacrifice: ${quantity}`,
`Sacrifice Amount: ${cardValue}`, `Sacrifice Amount: ${cardValue}`,
]; ];

View file

@ -16,11 +16,18 @@ export default class Sacrifice extends Command {
x x
.setName("cardnumber") .setName("cardnumber")
.setDescription("The card to sacrifice from your inventory") .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<CacheType>): Promise<void> { public override async execute(interaction: CommandInteraction<CacheType>): Promise<void> {
const cardnumber = interaction.options.get("cardnumber", true); const cardnumber = interaction.options.get("cardnumber", true);
const quantityInput = interaction.options.get("quantity")?.value ?? 1;
Vylpes marked this conversation as resolved Outdated

Same here

Same here
const quantity = Number(quantityInput) || 1;
const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, cardnumber.value! as string); const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, cardnumber.value! as string);
@ -29,6 +36,11 @@ export default class Sacrifice extends Command {
return; 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); const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardnumber.value! as string);
if (!cardData) { if (!cardData) {
@ -36,7 +48,7 @@ export default class Sacrifice extends Command {
return; return;
} }
const cardValue = GetSacrificeAmount(cardData.card.type); const cardValue = GetSacrificeAmount(cardData.card.type) * quantity;
const cardRarityString = CardRarityToString(cardData.card.type); const cardRarityString = CardRarityToString(cardData.card.type);
const description = [ const description = [
@ -44,6 +56,7 @@ export default class Sacrifice extends Command {
`Series: ${cardData.series.name}`, `Series: ${cardData.series.name}`,
`Rarity: ${cardRarityString}`, `Rarity: ${cardRarityString}`,
`Quantity Owned: ${cardInInventory.Quantity}`, `Quantity Owned: ${cardInInventory.Quantity}`,
`Quantity To Sacrifice: ${quantity}`,
`Sacrifice Amount: ${cardValue}`, `Sacrifice Amount: ${cardValue}`,
]; ];
@ -56,11 +69,11 @@ export default class Sacrifice extends Command {
const row = new ActionRowBuilder<ButtonBuilder>() const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents([ .addComponents([
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`sacrifice confirm ${interaction.user.id} ${cardnumber.value!}`) .setCustomId(`sacrifice confirm ${interaction.user.id} ${cardnumber.value!} ${quantity}`)
.setLabel("Confirm") .setLabel("Confirm")
.setStyle(ButtonStyle.Success), .setStyle(ButtonStyle.Success),
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`sacrifice cancel ${interaction.user.id} ${cardnumber.value!}`) .setCustomId(`sacrifice cancel ${interaction.user.id} ${cardnumber.value!} ${quantity}`)
.setLabel("Cancel") .setLabel("Cancel")
.setStyle(ButtonStyle.Secondary), .setStyle(ButtonStyle.Secondary),
]); ]);