Compare commits

..

5 commits

Author SHA1 Message Date
98bfd79a00 Fix suggested changes
All checks were successful
Test / build (push) Successful in 10s
2024-09-15 15:11:24 +01:00
e1ba8e97d9 Merge branch 'develop' into feature/337-sacrifice-multiple 2024-09-15 15:04:29 +01:00
f086ca4bda Add default if not supplied
All checks were successful
Test / build (push) Successful in 8s
2024-09-06 18:09:23 +01:00
7106b16aec Update the button events to take into account the quantity field 2024-09-06 18:07:55 +01:00
9f5042d696 Update the sacrifice command to allow input of quantity
All checks were successful
Test / build (push) Successful in 8s
2024-09-05 19:40:15 +01:00
3 changed files with 23 additions and 44 deletions

View file

@ -28,10 +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} user1Quantity=${user1Quantity} user2Quantity=${user2Quantity}`); AppLogger.LogSilly("Button/Trade/AcceptTrade", `Parameters: user1UserId=${user1UserId}, user2UserId=${user2UserId}, user1CardNumber=${user1CardNumber}, user2CardNumber=${user2CardNumber}, expiry=${expiry}, timeoutId=${timeoutId}`);
const expiryDate = new Date(expiry); const expiryDate = new Date(expiry);
@ -69,13 +67,13 @@ export default class Trade extends ButtonEvent {
return; return;
} }
if (user1UserInventory1.Quantity < user1Quantity || user2UserInventory1.Quantity < user2Quantity) { if (user1UserInventory1.Quantity < 1 || user2UserInventory1.Quantity < 1) {
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.RemoveQuantity(user1Quantity); user1UserInventory1.SetQuantity(user1UserInventory1.Quantity - 1);
user2UserInventory1.RemoveQuantity(user2Quantity); user2UserInventory1.SetQuantity(user2UserInventory1.Quantity - 1);
await user1UserInventory1.Save(Inventory, user1UserInventory1); await user1UserInventory1.Save(Inventory, user1UserInventory1);
await user2UserInventory1.Save(Inventory, user2UserInventory1); await user2UserInventory1.Save(Inventory, user2UserInventory1);
@ -84,15 +82,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, user2CardNumber, user2Quantity); user1UserInventory2 = new Inventory(user1UserId, user1CardNumber, 1);
} else { } else {
user1UserInventory2.AddQuantity(user2Quantity); user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + 1);
} }
if (!user2UserInventory2) { if (!user2UserInventory2) {
user2UserInventory2 = new Inventory(user2UserId, user1CardNumber, user1Quantity); user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, 1);
} else { } else {
user2UserInventory2.AddQuantity(user1Quantity); user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + 1);
} }
await user1UserInventory2.Save(Inventory, user1UserInventory2); await user1UserInventory2.Save(Inventory, user1UserInventory2);
@ -108,12 +106,12 @@ export default class Trade extends ButtonEvent {
.addFields([ .addFields([
{ {
name: `${user1User.username} Receives`, name: `${user1User.username} Receives`,
value: `${user2Item.id}: ${user2Item.name} x${user2Quantity}`, value: `${user2Item.id}: ${user2Item.name}`,
inline: true, inline: true,
}, },
{ {
name: `${user2User.username} Receives`, name: `${user2User.username} Receives`,
value: `${user1Item.id}: ${user1Item.name} x${user1Quantity}`, value: `${user1Item.id}: ${user1Item.name}`,
inline: true, inline: true,
}, },
{ {
@ -146,8 +144,6 @@ 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}`);
@ -182,12 +178,12 @@ export default class Trade extends ButtonEvent {
.addFields([ .addFields([
{ {
name: `${user1User.username} Receives`, name: `${user1User.username} Receives`,
value: `${user2Item.id}: ${user2Item.name} x${user2Quantity}`, value: `${user2Item.id}: ${user2Item.name}`,
inline: true, inline: true,
}, },
{ {
name: `${user2User.username} Receives`, name: `${user2User.username} Receives`,
value: `${user1Item.id}: ${user1Item.name} x${user1Quantity}`, value: `${user1Item.id}: ${user1Item.name}`,
inline: true, inline: true,
}, },
{ {

View file

@ -26,26 +26,13 @@ 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}`);
@ -57,12 +44,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 || user1ItemEntity.Quantity < givequantity) { if (!user1ItemEntity) {
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 || user2ItemEntity.Quantity < receivequantity) { if (!user2ItemEntity) {
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;
} }
@ -91,12 +78,12 @@ export default class Trade extends Command {
.addFields([ .addFields([
{ {
name: `${interaction.user.username} Receives`, name: `${interaction.user.username} Receives`,
value: `${user2Item.id}: ${user2Item.name} x${receivequantity}`, value: `${user2Item.id}: ${user2Item.name}`,
inline: true, inline: true,
}, },
{ {
name: `${user.username} Receives`, name: `${user.username} Receives`,
value: `${user1Item.id}: ${user1Item.name} x${givequantity}`, value: `${user1Item.id}: ${user1Item.name}`,
inline: true, inline: true,
}, },
{ {
@ -105,16 +92,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, givequantity, receivequantity), 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), 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} ${givequantity} ${receivequantity}`) .setCustomId(`trade accept ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId}`)
.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} ${givequantity} ${receivequantity}`) .setCustomId(`trade decline ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId}`)
.setLabel("Decline") .setLabel("Decline")
.setStyle(ButtonStyle.Danger), .setStyle(ButtonStyle.Danger),
]); ]);
@ -122,7 +109,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, user1Quantity: number, user2Quantity: number) { private async autoDecline(interaction: CommandInteraction, user1Username: string, user2Username: string, user1CardNumber: string, user2CardNumber: string, user1CardName: string, user2CardName: string) {
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()
@ -133,12 +120,12 @@ export default class Trade extends Command {
.addFields([ .addFields([
{ {
name: `${user1Username} Receives`, name: `${user1Username} Receives`,
value: `${user2CardNumber}: ${user2CardName} x${user2Quantity}`, value: `${user2CardNumber}: ${user2CardName}`,
inline: true, inline: true,
}, },
{ {
name: `${user2Username} Receives`, name: `${user2Username} Receives`,
value: `${user1CardNumber}: ${user1CardName} x${user1Quantity}`, value: `${user1CardNumber}: ${user1CardName}`,
inline: true, inline: true,
}, },
{ {

View file

@ -35,10 +35,6 @@ 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);
} }