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>
This commit is contained in:
Ethan Lane 2024-09-15 16:55:18 +01:00 committed by Vylpes
parent 8683c1e58a
commit 52c93c7803
3 changed files with 44 additions and 23 deletions

View file

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

View file

@ -26,13 +26,26 @@ export default class Trade extends Command {
x x
.setName("receive") .setName("receive")
.setDescription("Item to 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) { public override async execute(interaction: CommandInteraction) {
const user = interaction.options.get("user", true).user!; const user = interaction.options.get("user", true).user!;
const give = interaction.options.get("give", true); const give = interaction.options.get("give", true);
const receive = interaction.options.get("receive", 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}`); 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 user1ItemEntity = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, give.value!.toString());
const user2ItemEntity = await Inventory.FetchOneByCardNumberAndUserId(user.id, receive.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."); await interaction.reply("You do not have the item you are trying to trade.");
return; 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."); await interaction.reply("The user you are trying to trade with does not have the item you are trying to trade for.");
return; return;
} }
@ -78,12 +91,12 @@ export default class Trade extends Command {
.addFields([ .addFields([
{ {
name: `${interaction.user.username} Receives`, name: `${interaction.user.username} Receives`,
value: `${user2Item.id}: ${user2Item.name}`, value: `${user2Item.id}: ${user2Item.name} x${receivequantity}`,
inline: true, inline: true,
}, },
{ {
name: `${user.username} Receives`, name: `${user.username} Receives`,
value: `${user1Item.id}: ${user1Item.name}`, value: `${user1Item.id}: ${user1Item.name} x${givequantity}`,
inline: true, 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>() const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents([ .addComponents([
new ButtonBuilder() 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") .setLabel("Accept")
.setStyle(ButtonStyle.Success), .setStyle(ButtonStyle.Success),
new ButtonBuilder() 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") .setLabel("Decline")
.setStyle(ButtonStyle.Danger), .setStyle(ButtonStyle.Danger),
]); ]);
@ -109,7 +122,7 @@ export default class Trade extends Command {
await interaction.reply({ content: `${user}`, embeds: [ tradeEmbed ], components: [ row ] }); 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}`); AppLogger.LogSilly("Commands/Trade/AutoDecline", `Auto declining trade between ${user1Username} and ${user2Username}`);
const tradeEmbed = new EmbedBuilder() const tradeEmbed = new EmbedBuilder()
@ -120,12 +133,12 @@ export default class Trade extends Command {
.addFields([ .addFields([
{ {
name: `${user1Username} Receives`, name: `${user1Username} Receives`,
value: `${user2CardNumber}: ${user2CardName}`, value: `${user2CardNumber}: ${user2CardName} x${user2Quantity}`,
inline: true, inline: true,
}, },
{ {
name: `${user2Username} Receives`, name: `${user2Username} Receives`,
value: `${user1CardNumber}: ${user1CardName}`, value: `${user1CardNumber}: ${user1CardName} x${user1Quantity}`,
inline: true, inline: true,
}, },
{ {

View file

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