From f475f5fb424820ad82d0ebee81db48cd2aa31122 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 19 Dec 2023 14:53:36 +0000 Subject: [PATCH] Get basic pagination working --- .dev.env | 2 +- .prod.env | 2 +- .stage.env | 2 +- src/client/client.ts | 7 +- src/commands/drop.ts | 2 +- src/commands/inventory.ts | 16 ++++ src/database/entities/app/Inventory.ts | 8 ++ src/helpers/InventoryHelper.ts | 112 +++++++++++++++++++++++++ 8 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 src/commands/inventory.ts create mode 100644 src/helpers/InventoryHelper.ts diff --git a/.dev.env b/.dev.env index d1b74b1..14ad89c 100644 --- a/.dev.env +++ b/.dev.env @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.3.1 +BOT_VER=0.4.0 BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=682942374040961060 diff --git a/.prod.env b/.prod.env index 0755c1e..df28009 100644 --- a/.prod.env +++ b/.prod.env @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.3.1 +BOT_VER=0.4.0 BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1093810443589529631 diff --git a/.stage.env b/.stage.env index 4f70991..79dcc48 100644 --- a/.stage.env +++ b/.stage.env @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.3.1 +BOT_VER=0.4.0 BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1147976642942214235 diff --git a/src/client/client.ts b/src/client/client.ts index 643036d..18ac69f 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -14,6 +14,7 @@ import { Environment } from "../constants/Environment"; import Webhooks from "../webhooks"; import CardMetadataFunction from "../Functions/CardMetadataFunction"; import SeriesMetadata from "../contracts/SeriesMetadata"; +import InventoryHelper from "../helpers/InventoryHelper"; export class CoreClient extends Client { private static _commandItems: ICommandItem[]; @@ -83,7 +84,11 @@ export class CoreClient extends Client { console.log(`Registered Events: ${CoreClient._eventItems.flatMap(x => x.EventType).join(", ")}`); console.log(`Registered Buttons: ${CoreClient._buttonEvents.flatMap(x => x.ButtonId).join(", ")}`); - await super.login(process.env.BOT_TOKEN); + const page = await InventoryHelper.GenerateInventoryPage('125776189666230272', 11); + + console.log(page); + + // await super.login(process.env.BOT_TOKEN); } public static RegisterCommand(name: string, command: Command, environment: Environment = Environment.All, serverId?: string) { diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 5a8ccd9..2ecd936 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -1,4 +1,4 @@ -import { AttachmentBuilder, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js"; +import { AttachmentBuilder, CommandInteraction, SlashCommandBuilder } from "discord.js"; import { Command } from "../type/command"; import { readFileSync } from "fs"; import { CoreClient } from "../client/client"; diff --git a/src/commands/inventory.ts b/src/commands/inventory.ts new file mode 100644 index 0000000..37b23c8 --- /dev/null +++ b/src/commands/inventory.ts @@ -0,0 +1,16 @@ +import { CommandInteraction, SlashCommandBuilder } from "discord.js"; +import { Command } from "../type/command"; + +export default class Inventory extends Command { + constructor() { + super(); + + this.CommandBuilder = new SlashCommandBuilder() + .setName('inventory') + .setDescription('View your inventory'); + } + + public override async execute(interaction: CommandInteraction) { + + } +} \ No newline at end of file diff --git a/src/database/entities/app/Inventory.ts b/src/database/entities/app/Inventory.ts index adec824..cfe8c3d 100644 --- a/src/database/entities/app/Inventory.ts +++ b/src/database/entities/app/Inventory.ts @@ -40,4 +40,12 @@ export default class Inventory extends AppBaseEntity { return single; } + + public static async FetchAllByUserId(userId: string): Promise { + const repository = AppDataSource.getRepository(Inventory); + + const all = await repository.find({ where: { UserId: userId }}); + + return all; + } } \ No newline at end of file diff --git a/src/helpers/InventoryHelper.ts b/src/helpers/InventoryHelper.ts new file mode 100644 index 0000000..7a4e66a --- /dev/null +++ b/src/helpers/InventoryHelper.ts @@ -0,0 +1,112 @@ +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js"; +import Inventory from "../database/entities/app/Inventory"; +import { CoreClient } from "../client/client"; +import SeriesMetadata, { CardMetadata } from "../contracts/SeriesMetadata"; +import EmbedColours from "../constants/EmbedColours"; + +export default class InventoryHelper { + public static async GenerateInventoryPage(userid: string, page: number): Promise<{ embed: EmbedBuilder, row: ActionRowBuilder }> { + const cardsPerPage = 10; + + const inventory = await Inventory.FetchAllByUserId(userid); + + const allSeries: SeriesMetadata[] = []; + + for (let item of inventory) { + const series = CoreClient.Cards.find(x => x.cards.find(y => y.id == item.CardNumber)); + + if (!series || allSeries.includes(series)) { + continue; + } + + allSeries.push(series); + } + + const seriesToDisplay: SeriesMetadata[] = []; + + for (let series of allSeries.sort((a, b) => a.id - b.id)) { + const seriesCards = series.cards.filter(x => inventory.find(y => y.CardNumber == x.id)); + + series.cards = seriesCards; + + seriesToDisplay.push(series); + } + + const pages: { + id: number, + name: string, + cards: { + cardNumber: string, + name: string, + quantity: number, + }[], + seriesSubpage: number, + }[] = []; + + for (let series of seriesToDisplay) { + for (let i = 0; i < series.cards.length; i+= cardsPerPage) { + const cards = series.cards.slice(i, i + cardsPerPage); + const pageCards: { + cardNumber: string, + name: string, + quantity: number, + }[] = []; + + for (let card of cards) { + const item = inventory.find(x => x.CardNumber == card.id); + + if (!item) { + continue; + } + + pageCards.push({ + cardNumber: card.id, + name: card.name, + quantity: item.Quantity, + }); + } + + pages.push({ + id: series.id, + name: series.name, + cards: pageCards, + seriesSubpage: i / cardsPerPage, + }); + } + } + + const currentPage = pages[page]; + + if (!currentPage) { + console.error("Unable to find page"); + return Promise.reject("Unable to find page"); + } + + const embedDescription: string[] = []; + + for (let card of currentPage.cards) { + embedDescription.push(`[${card.cardNumber}] ${card.name} (${card.quantity})`); + } + + const embed = new EmbedBuilder() + .setTitle(`${currentPage.name} (${currentPage.seriesSubpage + 1})`) + .setDescription(embedDescription.join('\n')) + .setFooter({ text: `Page ${page} of ${pages.length}` }) + .setColor(EmbedColours.Ok); + + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`inventory ${userid} ${page - 1}`) + .setLabel("Previous") + .setStyle(ButtonStyle.Primary) + .setDisabled(page == 0), + new ButtonBuilder() + .setCustomId(`inventory ${userid} ${page + 1}`) + .setLabel("Next") + .setStyle(ButtonStyle.Primary) + .setDisabled(page == pages.length)); + + return { embed, row }; + } +} \ No newline at end of file