WIP: Start of allowing users to trade multiple of an item
Some checks failed
Test / build (push) Failing after 15s

This commit is contained in:
Ethan Lane 2024-09-07 15:16:02 +01:00
parent 21d11afd31
commit d3f20e2bba
2 changed files with 40 additions and 26 deletions

View file

@ -28,6 +28,8 @@ 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}`);
@ -38,10 +40,10 @@ export default class Trade extends ButtonEvent {
return; return;
} }
if (interaction.user.id !== user2UserId) { // if (interaction.user.id !== user2UserId) {
await interaction.reply("You are not the user who the trade is intended for"); // await interaction.reply("You are not the user who the trade is intended for");
return; // return;
} // }
const user1Item = CoreClient.Cards const user1Item = CoreClient.Cards
.flatMap(x => x.cards) .flatMap(x => x.cards)
@ -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.SetQuantity(user1UserInventory1.Quantity - user1Quantity);
user2UserInventory1.SetQuantity(user2UserInventory1.Quantity - 1); user2UserInventory1.SetQuantity(user2UserInventory1.Quantity - 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, user1CardNumber, user2Quantity);
} else { } else {
user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + 1); user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + user2Quantity);
} }
if (!user2UserInventory2) { if (!user2UserInventory2) {
user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, 1); user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, user1Quantity);
} else { } else {
user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + 1); user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + user2Quantity);
} }
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,23 @@ 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 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}`); 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 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 +88,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 +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<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 +119,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 +130,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,
}, },
{ {