From d3f20e2bba26c8b5edeca67c5121b68b4189a082 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 7 Sep 2024 15:16:02 +0100 Subject: [PATCH 1/4] WIP: Start of allowing users to trade multiple of an item --- src/buttonEvents/Trade.ts | 34 +++++++++++++++++++--------------- src/commands/trade.ts | 32 +++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/buttonEvents/Trade.ts b/src/buttonEvents/Trade.ts index d0d7b38..9ca89a9 100644 --- a/src/buttonEvents/Trade.ts +++ b/src/buttonEvents/Trade.ts @@ -28,6 +28,8 @@ 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}`); @@ -38,10 +40,10 @@ export default class Trade extends ButtonEvent { return; } - if (interaction.user.id !== user2UserId) { - await interaction.reply("You are not the user who the trade is intended for"); - return; - } +// if (interaction.user.id !== user2UserId) { +// await interaction.reply("You are not the user who the trade is intended for"); +// return; +// } const user1Item = CoreClient.Cards .flatMap(x => x.cards) @@ -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.SetQuantity(user1UserInventory1.Quantity - user1Quantity); + user2UserInventory1.SetQuantity(user2UserInventory1.Quantity - 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, user1CardNumber, user2Quantity); } else { - user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + 1); + user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + user2Quantity); } if (!user2UserInventory2) { - user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, 1); + user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, user1Quantity); } else { - user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + 1); + user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + user2Quantity); } 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, }, { diff --git a/src/commands/trade.ts b/src/commands/trade.ts index 569e88f..34f7aef 100644 --- a/src/commands/trade.ts +++ b/src/commands/trade.ts @@ -26,13 +26,23 @@ 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 givequantity = interaction.options.get("givequantity")?.value as number ?? 1; + const receivequantity = interaction.options.get("receivequantity")?.value as number ?? 1; AppLogger.LogSilly("Commands/Trade", `Parameters: user=${user.id}, give=${give.value}, receive=${receive.value}`); @@ -44,12 +54,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 +88,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 +102,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() .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 +119,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 +130,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, }, { -- 2.45.2 From a294d408c1328a8f719fc03b02a0d6f36fbf33db Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 13 Sep 2024 18:07:36 +0100 Subject: [PATCH 2/4] Fix cards not being added to the user's inventory if they dont have at least 1 --- src/buttonEvents/Trade.ts | 14 +++++++------- src/database/entities/app/Inventory.ts | 4 ++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/buttonEvents/Trade.ts b/src/buttonEvents/Trade.ts index 9ca89a9..c425fb3 100644 --- a/src/buttonEvents/Trade.ts +++ b/src/buttonEvents/Trade.ts @@ -31,7 +31,7 @@ export default class Trade extends ButtonEvent { 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); @@ -74,8 +74,8 @@ export default class Trade extends ButtonEvent { return; } - user1UserInventory1.SetQuantity(user1UserInventory1.Quantity - user1Quantity); - user2UserInventory1.SetQuantity(user2UserInventory1.Quantity - user2Quantity); + user1UserInventory1.RemoveQuantity(user1Quantity); + user2UserInventory1.RemoveQuantity(user2Quantity); await user1UserInventory1.Save(Inventory, user1UserInventory1); await user2UserInventory1.Save(Inventory, user2UserInventory1); @@ -84,15 +84,15 @@ export default class Trade extends ButtonEvent { let user2UserInventory2 = await Inventory.FetchOneByCardNumberAndUserId(user2UserId, user1CardNumber); if (!user1UserInventory2) { - user1UserInventory2 = new Inventory(user1UserId, user1CardNumber, user2Quantity); + user1UserInventory2 = new Inventory(user1UserId, user2CardNumber, user2Quantity); } else { - user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + user2Quantity); + user1UserInventory2.AddQuantity(user2Quantity); } if (!user2UserInventory2) { - user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, user1Quantity); + user2UserInventory2 = new Inventory(user2UserId, user1CardNumber, user1Quantity); } else { - user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + user2Quantity); + user2UserInventory2.AddQuantity(user1Quantity); } await user1UserInventory2.Save(Inventory, user1UserInventory2); diff --git a/src/database/entities/app/Inventory.ts b/src/database/entities/app/Inventory.ts index 7d659fe..c780dad 100644 --- a/src/database/entities/app/Inventory.ts +++ b/src/database/entities/app/Inventory.ts @@ -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); } -- 2.45.2 From 5c6df39f04ce0dbf81ad5e0a3a95c3c6b5b6265e Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 15 Sep 2024 13:00:52 +0100 Subject: [PATCH 3/4] Re-enable logic disabled for testing --- src/buttonEvents/Trade.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/buttonEvents/Trade.ts b/src/buttonEvents/Trade.ts index c425fb3..a301a3b 100644 --- a/src/buttonEvents/Trade.ts +++ b/src/buttonEvents/Trade.ts @@ -40,10 +40,10 @@ export default class Trade extends ButtonEvent { return; } -// if (interaction.user.id !== user2UserId) { -// await interaction.reply("You are not the user who the trade is intended for"); -// return; -// } + if (interaction.user.id !== user2UserId) { + await interaction.reply("You are not the user who the trade is intended for"); + return; + } const user1Item = CoreClient.Cards .flatMap(x => x.cards) -- 2.45.2 From e7c34ca50e8613e6cf937637e5801b6f5477b3e3 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 15 Sep 2024 15:14:34 +0100 Subject: [PATCH 4/4] Update null checks --- src/buttonEvents/Trade.ts | 8 ++++---- src/commands/trade.ts | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/buttonEvents/Trade.ts b/src/buttonEvents/Trade.ts index a301a3b..fb8449d 100644 --- a/src/buttonEvents/Trade.ts +++ b/src/buttonEvents/Trade.ts @@ -28,8 +28,8 @@ 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; + 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} user1Quantity=${user1Quantity} user2Quantity=${user2Quantity}`); @@ -146,8 +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; + 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}`); diff --git a/src/commands/trade.ts b/src/commands/trade.ts index 34f7aef..fcd312a 100644 --- a/src/commands/trade.ts +++ b/src/commands/trade.ts @@ -41,8 +41,11 @@ export default class Trade extends Command { const user = interaction.options.get("user", true).user!; const give = interaction.options.get("give", true); const receive = interaction.options.get("receive", true); - const givequantity = interaction.options.get("givequantity")?.value as number ?? 1; - const receivequantity = interaction.options.get("receivequantity")?.value as number ?? 1; + 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}`); -- 2.45.2