Compare commits

...

5 commits

Author SHA1 Message Date
4359cbba1e Fix suggested changes
All checks were successful
Test / build (push) Successful in 8s
2024-09-15 16:56:15 +01:00
0e5489f0d1 Add default if not supplied 2024-09-15 16:56:15 +01:00
ca6ccea16f Update the button events to take into account the quantity field 2024-09-15 16:56:15 +01:00
08949d0af3 Update the sacrifice command to allow input of quantity 2024-09-15 16:56:15 +01:00
52c93c7803 Ability to trade multiple cards at once (#360)
All checks were successful
Deploy To Stage / build (push) Successful in 9s
Deploy To Stage / deploy (push) Successful in 18s
- Add the ability to trade multiple cards at once using an optional quantity field

#338

Reviewed-on: #360
Reviewed-by: VylpesTester <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2024-09-15 16:55:18 +01:00
5 changed files with 80 additions and 32 deletions

View file

@ -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]) || 1;
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]) || 1;
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}`,
];

View file

@ -28,8 +28,10 @@ export default class Trade extends ButtonEvent {
const user2CardNumber = interaction.customId.split(" ")[5];
const expiry = interaction.customId.split(" ")[6];
const timeoutId = interaction.customId.split(" ")[7];
const user1Quantity = Number(interaction.customId.split(" ")[8]) || 1;
const user2Quantity = Number(interaction.customId.split(" ")[9]) || 1;
AppLogger.LogSilly("Button/Trade/AcceptTrade", `Parameters: user1UserId=${user1UserId}, user2UserId=${user2UserId}, user1CardNumber=${user1CardNumber}, user2CardNumber=${user2CardNumber}, expiry=${expiry}, timeoutId=${timeoutId}`);
AppLogger.LogSilly("Button/Trade/AcceptTrade", `Parameters: user1UserId=${user1UserId}, user2UserId=${user2UserId}, user1CardNumber=${user1CardNumber}, user2CardNumber=${user2CardNumber}, expiry=${expiry}, timeoutId=${timeoutId} user1Quantity=${user1Quantity} user2Quantity=${user2Quantity}`);
const expiryDate = new Date(expiry);
@ -67,13 +69,13 @@ export default class Trade extends ButtonEvent {
return;
}
if (user1UserInventory1.Quantity < 1 || user2UserInventory1.Quantity < 1) {
if (user1UserInventory1.Quantity < user1Quantity || user2UserInventory1.Quantity < user2Quantity) {
await interaction.reply("One or more of the items you are trying to trade does not exist.");
return;
}
user1UserInventory1.SetQuantity(user1UserInventory1.Quantity - 1);
user2UserInventory1.SetQuantity(user2UserInventory1.Quantity - 1);
user1UserInventory1.RemoveQuantity(user1Quantity);
user2UserInventory1.RemoveQuantity(user2Quantity);
await user1UserInventory1.Save(Inventory, user1UserInventory1);
await user2UserInventory1.Save(Inventory, user2UserInventory1);
@ -82,15 +84,15 @@ export default class Trade extends ButtonEvent {
let user2UserInventory2 = await Inventory.FetchOneByCardNumberAndUserId(user2UserId, user1CardNumber);
if (!user1UserInventory2) {
user1UserInventory2 = new Inventory(user1UserId, user1CardNumber, 1);
user1UserInventory2 = new Inventory(user1UserId, user2CardNumber, user2Quantity);
} else {
user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + 1);
user1UserInventory2.AddQuantity(user2Quantity);
}
if (!user2UserInventory2) {
user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, 1);
user2UserInventory2 = new Inventory(user2UserId, user1CardNumber, user1Quantity);
} else {
user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + 1);
user2UserInventory2.AddQuantity(user1Quantity);
}
await user1UserInventory2.Save(Inventory, user1UserInventory2);
@ -106,12 +108,12 @@ export default class Trade extends ButtonEvent {
.addFields([
{
name: `${user1User.username} Receives`,
value: `${user2Item.id}: ${user2Item.name}`,
value: `${user2Item.id}: ${user2Item.name} x${user2Quantity}`,
inline: true,
},
{
name: `${user2User.username} Receives`,
value: `${user1Item.id}: ${user1Item.name}`,
value: `${user1Item.id}: ${user1Item.name} x${user1Quantity}`,
inline: true,
},
{
@ -144,6 +146,8 @@ export default class Trade extends ButtonEvent {
const user2CardNumber = interaction.customId.split(" ")[5];
// No need to get expiry date
const timeoutId = interaction.customId.split(" ")[7];
const user1Quantity = Number(interaction.customId.split(" ")[8]) || 1;
const user2Quantity = Number(interaction.customId.split(" ")[9]) || 1;
AppLogger.LogSilly("Button/Trade/DeclineTrade", `Parameters: user1UserId=${user1UserId}, user2UserId=${user2UserId}, user1CardNumber=${user1CardNumber}, user2CardNumber=${user2CardNumber}, timeoutId=${timeoutId}`);
@ -178,12 +182,12 @@ export default class Trade extends ButtonEvent {
.addFields([
{
name: `${user1User.username} Receives`,
value: `${user2Item.id}: ${user2Item.name}`,
value: `${user2Item.id}: ${user2Item.name} x${user2Quantity}`,
inline: true,
},
{
name: `${user2User.username} Receives`,
value: `${user1Item.id}: ${user1Item.name}`,
value: `${user1Item.id}: ${user1Item.name} x${user1Quantity}`,
inline: true,
},
{

View file

@ -16,11 +16,18 @@ 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<CacheType>): Promise<void> {
const cardnumber = interaction.options.get("cardnumber", true);
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);
@ -29,6 +36,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) {
@ -36,7 +48,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 = [
@ -44,6 +56,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 +69,11 @@ export default class Sacrifice extends Command {
const row = new ActionRowBuilder<ButtonBuilder>()
.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),
]);

View file

@ -26,13 +26,26 @@ export default class Trade extends Command {
x
.setName("receive")
.setDescription("Item to receive")
.setRequired(true));
.setRequired(true))
.addNumberOption(x =>
x
.setName("givequantity")
.setDescription("Amount to give"))
.addNumberOption(x =>
x
.setName("receivequantity")
.setDescription("Amount to receive"));
}
public override async execute(interaction: CommandInteraction) {
const user = interaction.options.get("user", true).user!;
const give = interaction.options.get("give", true);
const receive = interaction.options.get("receive", true);
const givequantityInput = interaction.options.get("givequantity")?.value ?? 1;
const receivequantityInput = interaction.options.get("receivequantity")?.value ?? 1;
const givequantity = Number(givequantityInput) || 1;
const receivequantity = Number(receivequantityInput) || 1;
AppLogger.LogSilly("Commands/Trade", `Parameters: user=${user.id}, give=${give.value}, receive=${receive.value}`);
@ -44,12 +57,12 @@ export default class Trade extends Command {
const user1ItemEntity = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, give.value!.toString());
const user2ItemEntity = await Inventory.FetchOneByCardNumberAndUserId(user.id, receive.value!.toString());
if (!user1ItemEntity) {
if (!user1ItemEntity || user1ItemEntity.Quantity < givequantity) {
await interaction.reply("You do not have the item you are trying to trade.");
return;
}
if (!user2ItemEntity) {
if (!user2ItemEntity || user2ItemEntity.Quantity < receivequantity) {
await interaction.reply("The user you are trying to trade with does not have the item you are trying to trade for.");
return;
}
@ -78,12 +91,12 @@ export default class Trade extends Command {
.addFields([
{
name: `${interaction.user.username} Receives`,
value: `${user2Item.id}: ${user2Item.name}`,
value: `${user2Item.id}: ${user2Item.name} x${receivequantity}`,
inline: true,
},
{
name: `${user.username} Receives`,
value: `${user1Item.id}: ${user1Item.name}`,
value: `${user1Item.id}: ${user1Item.name} x${givequantity}`,
inline: true,
},
{
@ -92,16 +105,16 @@ export default class Trade extends Command {
}
]);
const timeoutId = setTimeout(async () => this.autoDecline(interaction, interaction.user.username, user.username, user1Item.id, user2Item.id, user1Item.name, user2Item.name), 1000 * 60 * 15); // 15 minutes
const timeoutId = setTimeout(async () => this.autoDecline(interaction, interaction.user.username, user.username, user1Item.id, user2Item.id, user1Item.name, user2Item.name, givequantity, receivequantity), 1000 * 60 * 15); // 15 minutes
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents([
new ButtonBuilder()
.setCustomId(`trade accept ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId}`)
.setCustomId(`trade accept ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId} ${givequantity} ${receivequantity}`)
.setLabel("Accept")
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId(`trade decline ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId}`)
.setCustomId(`trade decline ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId} ${givequantity} ${receivequantity}`)
.setLabel("Decline")
.setStyle(ButtonStyle.Danger),
]);
@ -109,7 +122,7 @@ export default class Trade extends Command {
await interaction.reply({ content: `${user}`, embeds: [ tradeEmbed ], components: [ row ] });
}
private async autoDecline(interaction: CommandInteraction, user1Username: string, user2Username: string, user1CardNumber: string, user2CardNumber: string, user1CardName: string, user2CardName: string) {
private async autoDecline(interaction: CommandInteraction, user1Username: string, user2Username: string, user1CardNumber: string, user2CardNumber: string, user1CardName: string, user2CardName: string, user1Quantity: number, user2Quantity: number) {
AppLogger.LogSilly("Commands/Trade/AutoDecline", `Auto declining trade between ${user1Username} and ${user2Username}`);
const tradeEmbed = new EmbedBuilder()
@ -120,12 +133,12 @@ export default class Trade extends Command {
.addFields([
{
name: `${user1Username} Receives`,
value: `${user2CardNumber}: ${user2CardName}`,
value: `${user2CardNumber}: ${user2CardName} x${user2Quantity}`,
inline: true,
},
{
name: `${user2Username} Receives`,
value: `${user1CardNumber}: ${user1CardName}`,
value: `${user1CardNumber}: ${user1CardName} x${user1Quantity}`,
inline: true,
},
{

View file

@ -35,6 +35,10 @@ export default class Inventory extends AppBaseEntity {
this.Quantity -= amount;
}
public AddQuantity(amount: number) {
this.Quantity += amount;
}
public AddClaim(claim: Claim) {
this.Claims.push(claim);
}