Update claim event to increment the claimed number when the user claims a drop
This commit is contained in:
parent
94f285541b
commit
18c678a0ca
3 changed files with 51 additions and 16 deletions
|
@ -1,9 +1,12 @@
|
||||||
import { ButtonInteraction } from "discord.js";
|
import { AttachmentBuilder, ButtonInteraction } from "discord.js";
|
||||||
import { ButtonEvent } from "../type/buttonEvent";
|
import { ButtonEvent } from "../type/buttonEvent";
|
||||||
import Inventory from "../database/entities/app/Inventory";
|
import Inventory from "../database/entities/app/Inventory";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
import { default as eClaim } from "../database/entities/app/Claim";
|
import { default as eClaim } from "../database/entities/app/Claim";
|
||||||
import AppLogger from "../client/appLogger";
|
import AppLogger from "../client/appLogger";
|
||||||
|
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||||
|
import { readFileSync } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export default class Claim extends ButtonEvent {
|
export default class Claim extends ButtonEvent {
|
||||||
public override async execute(interaction: ButtonInteraction) {
|
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}`);
|
AppLogger.LogSilly("Button/Claim", `Parameters: cardNumber=${cardNumber}, claimId=${claimId}, droppedBy=${droppedBy}, userId=${userId}`);
|
||||||
|
|
||||||
await interaction.deferReply();
|
|
||||||
|
|
||||||
const claimed = await eClaim.FetchOneByClaimId(claimId);
|
const claimed = await eClaim.FetchOneByClaimId(claimId);
|
||||||
|
|
||||||
if (claimed) {
|
if (claimed) {
|
||||||
await interaction.editReply("This card has already been claimed");
|
await interaction.reply("This card has already been claimed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (claimId == CoreClient.ClaimId && userId != droppedBy) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +46,24 @@ export default class Claim extends ButtonEvent {
|
||||||
|
|
||||||
await claim.Save(eClaim, claim);
|
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 ],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -56,16 +56,16 @@ export default class Give extends Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let inventory = await Inventory.FetchOneByCardNumberAndUserId(user.id, card.id);
|
let inventory = await Inventory.FetchOneByCardNumberAndUserId(user.id, card.card.id);
|
||||||
|
|
||||||
if (!inventory) {
|
if (!inventory) {
|
||||||
inventory = new Inventory(user.id, card.id, 1);
|
inventory = new Inventory(user.id, card.card.id, 1);
|
||||||
} else {
|
} else {
|
||||||
inventory.SetQuantity(inventory.Quantity + 1);
|
inventory.SetQuantity(inventory.Quantity + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
await inventory.Save(Inventory, inventory);
|
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}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
|
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
|
||||||
import { CardRarity, CardRarityToColour, CardRarityToString } from "../constants/CardRarity";
|
import { CardRarity, CardRarityToColour, CardRarityToString } from "../constants/CardRarity";
|
||||||
import CardRarityChances from "../constants/CardRarityChances";
|
import CardRarityChances from "../constants/CardRarityChances";
|
||||||
import { CardMetadata, DropResult } from "../contracts/SeriesMetadata";
|
import { DropResult } from "../contracts/SeriesMetadata";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
import AppLogger from "../client/appLogger";
|
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}`);
|
AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Parameters: cardNumber=${cardNumber}`);
|
||||||
|
|
||||||
const card = CoreClient.Cards
|
const card = CoreClient.Cards
|
||||||
.flatMap(x => x.cards)
|
.flatMap(x => x.cards)
|
||||||
.find(x => x.id == cardNumber);
|
.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}`);
|
AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropEmbed", `Parameters: drop=${drop.card.id}, quantityClaimed=${quantityClaimed}, imageFileName=${imageFileName}`);
|
||||||
|
|
||||||
let description = "";
|
let description = "";
|
||||||
description += `Series: ${drop.series.name}\n`;
|
description += `Series: ${drop.series.name}\n`;
|
||||||
description += `Claimed: ${quantityClaimed}\n`;
|
description += `Claimed: ${quantityClaimed}\n`;
|
||||||
|
|
||||||
|
if (claimedBy != null) {
|
||||||
|
description += `Claimed by: ${claimedBy}\n`;
|
||||||
|
} else {
|
||||||
|
description += "Claimed by: (UNCLAIMED)\n";
|
||||||
|
}
|
||||||
|
|
||||||
return new EmbedBuilder()
|
return new EmbedBuilder()
|
||||||
.setTitle(drop.card.name)
|
.setTitle(drop.card.name)
|
||||||
.setDescription(description)
|
.setDescription(description)
|
||||||
|
@ -83,7 +98,7 @@ export default class CardDropHelperMetadata {
|
||||||
.setImage(`attachment://${imageFileName}`);
|
.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}`);
|
AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropButtons", `Parameters: drop=${drop.card.id}, claimId=${claimId}, userId=${userId}`);
|
||||||
|
|
||||||
return new ActionRowBuilder<ButtonBuilder>()
|
return new ActionRowBuilder<ButtonBuilder>()
|
||||||
|
@ -91,7 +106,8 @@ export default class CardDropHelperMetadata {
|
||||||
new ButtonBuilder()
|
new ButtonBuilder()
|
||||||
.setCustomId(`claim ${drop.card.id} ${claimId} ${userId}`)
|
.setCustomId(`claim ${drop.card.id} ${claimId} ${userId}`)
|
||||||
.setLabel("Claim")
|
.setLabel("Claim")
|
||||||
.setStyle(ButtonStyle.Primary),
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(disabled),
|
||||||
new ButtonBuilder()
|
new ButtonBuilder()
|
||||||
.setCustomId("reroll")
|
.setCustomId("reroll")
|
||||||
.setLabel("Reroll")
|
.setLabel("Reroll")
|
||||||
|
|
Loading…
Reference in a new issue