Update claim event to increment the claimed number when the user claims a drop #191

Merged
Vylpes merged 1 commit from feature/89-claim-increment into develop 2024-03-31 15:51:10 +01:00
3 changed files with 51 additions and 16 deletions
Showing only changes of commit 18c678a0ca - Show all commits

View file

@ -1,9 +1,12 @@
import { ButtonInteraction } from "discord.js";
import { AttachmentBuilder, ButtonInteraction } from "discord.js";
import { ButtonEvent } from "../type/buttonEvent";
import Inventory from "../database/entities/app/Inventory";
import { CoreClient } from "../client/client";
import { default as eClaim } from "../database/entities/app/Claim";
import AppLogger from "../client/appLogger";
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
import { readFileSync } from "fs";
import path from "path";
export default class Claim extends ButtonEvent {
public override async execute(interaction: ButtonInteraction) {
@ -16,17 +19,15 @@ export default class Claim extends ButtonEvent {
AppLogger.LogSilly("Button/Claim", `Parameters: cardNumber=${cardNumber}, claimId=${claimId}, droppedBy=${droppedBy}, userId=${userId}`);
await interaction.deferReply();
const claimed = await eClaim.FetchOneByClaimId(claimId);
if (claimed) {
await interaction.editReply("This card has already been claimed");
await interaction.reply("This card has already been claimed");
return;
}
if (claimId == CoreClient.ClaimId && userId != droppedBy) {
await interaction.editReply("The latest dropped card can only be claimed by the user who dropped it");
await interaction.reply("The latest dropped card can only be claimed by the user who dropped it");
return;
}
@ -45,6 +46,24 @@ export default class Claim extends ButtonEvent {
await claim.Save(eClaim, claim);
await interaction.editReply(`Card claimed by ${interaction.user}`);
const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
if (!card) {
return;
}
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path));
const imageFileName = card.card.path.split("/").pop()!;
const attachment = new AttachmentBuilder(image, { name: imageFileName });
const embed = CardDropHelperMetadata.GenerateDropEmbed(card, inventory.Quantity, imageFileName, interaction.user.username);
const row = CardDropHelperMetadata.GenerateDropButtons(card, claimId, interaction.user.id, true);
await interaction.update({
embeds: [ embed ],
files: [ attachment ],
components: [ row ],
});
}
}

View file

@ -56,16 +56,16 @@ export default class Give extends Command {
return;
}
let inventory = await Inventory.FetchOneByCardNumberAndUserId(user.id, card.id);
let inventory = await Inventory.FetchOneByCardNumberAndUserId(user.id, card.card.id);
if (!inventory) {
inventory = new Inventory(user.id, card.id, 1);
inventory = new Inventory(user.id, card.card.id, 1);
} else {
inventory.SetQuantity(inventory.Quantity + 1);
}
await inventory.Save(Inventory, inventory);
await interaction.reply(`${card.name} given to ${user.username}, they now have ${inventory.Quantity}`);
await interaction.reply(`${card.card.name} given to ${user.username}, they now have ${inventory.Quantity}`);
}
}

View file

@ -1,7 +1,7 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import { CardRarity, CardRarityToColour, CardRarityToString } from "../constants/CardRarity";
import CardRarityChances from "../constants/CardRarityChances";
import { CardMetadata, DropResult } from "../contracts/SeriesMetadata";
import { DropResult } from "../contracts/SeriesMetadata";
import { CoreClient } from "../client/client";
import AppLogger from "../client/appLogger";
@ -56,25 +56,40 @@ export default class CardDropHelperMetadata {
};
}
public static GetCardByCardNumber(cardNumber: string): CardMetadata | undefined {
public static GetCardByCardNumber(cardNumber: string): DropResult | undefined {
AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Parameters: cardNumber=${cardNumber}`);
const card = CoreClient.Cards
.flatMap(x => x.cards)
.find(x => x.id == cardNumber);
AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Card: ${card?.id} ${card?.name}`);
const series = CoreClient.Cards
.find(x => x.cards.find(y => y.id == card?.id));
return card;
AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Card: ${card?.id} ${card?.name}`);
AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Series: ${series?.id} ${series?.name}`);
if (!card || !series) {
AppLogger.LogVerbose("CardDropHelperMetadata/GetCardByCardNumber", `Unable to find card metadata: ${cardNumber}`);
return undefined;
}
public static GenerateDropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string): EmbedBuilder {
return { card, series };
}
public static GenerateDropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string, claimedBy?: string): EmbedBuilder {
AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropEmbed", `Parameters: drop=${drop.card.id}, quantityClaimed=${quantityClaimed}, imageFileName=${imageFileName}`);
let description = "";
description += `Series: ${drop.series.name}\n`;
description += `Claimed: ${quantityClaimed}\n`;
if (claimedBy != null) {
description += `Claimed by: ${claimedBy}\n`;
} else {
description += "Claimed by: (UNCLAIMED)\n";
}
return new EmbedBuilder()
.setTitle(drop.card.name)
.setDescription(description)
@ -83,7 +98,7 @@ export default class CardDropHelperMetadata {
.setImage(`attachment://${imageFileName}`);
}
public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string): ActionRowBuilder<ButtonBuilder> {
public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string, disabled: boolean = false): ActionRowBuilder<ButtonBuilder> {
AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropButtons", `Parameters: drop=${drop.card.id}, claimId=${claimId}, userId=${userId}`);
return new ActionRowBuilder<ButtonBuilder>()
@ -91,7 +106,8 @@ export default class CardDropHelperMetadata {
new ButtonBuilder()
.setCustomId(`claim ${drop.card.id} ${claimId} ${userId}`)
.setLabel("Claim")
.setStyle(ButtonStyle.Primary),
.setStyle(ButtonStyle.Primary)
.setDisabled(disabled),
new ButtonBuilder()
.setCustomId("reroll")
.setLabel("Reroll")