From ebe83734610f4c93a315738e806b2c05bfceacbe Mon Sep 17 00:00:00 2001 From: Per Christian Kvalvik Date: Sat, 28 Sep 2024 17:36:50 +0200 Subject: [PATCH] Support 5 digit IDs --- src/commands/drop.ts | 13 +------------ src/commands/jobs.ts | 2 +- src/commands/use.ts | 13 +------------ src/commands/view.ts | 7 ------- src/entities/card.ts | 2 +- src/lib/utils.ts | 16 ++++++++++++++++ 6 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/commands/drop.ts b/src/commands/drop.ts index ed6a6b7..73f2c2d 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -13,6 +13,7 @@ import { getNextCardId } from '../services/getNextCardId.js'; import { GuildSettings } from '../entities/guildSettings.js'; import { EventLogService } from '../services/eventLogService.js'; import { unlink } from 'fs/promises'; +import { stringId } from '../lib/utils.js'; @ApplyOptions({ description: 'Drops 3 cards' @@ -204,15 +205,3 @@ export class DropCommand extends Command { } } } - -function stringId(id: number) { - let stringId = ''; - const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; - - for (let i = 0; i < 4; i++) { - stringId += chars[id % chars.length]; - id = Math.floor(id / chars.length); - } - - return stringId.split('').reverse().join(''); -} diff --git a/src/commands/jobs.ts b/src/commands/jobs.ts index 78b58f8..aaf631a 100644 --- a/src/commands/jobs.ts +++ b/src/commands/jobs.ts @@ -51,7 +51,7 @@ export class JobsCommand extends Subcommand { .setName('assign') .setDescription('Assign card to slot') .addStringOption((arg) => - arg.setName('card').setDescription('Card to assign').setRequired(true).setMaxLength(4).setMinLength(4) + arg.setName('card').setDescription('Card to assign').setRequired(true).setMaxLength(5).setMinLength(4) ) .addStringOption((arg) => arg diff --git a/src/commands/use.ts b/src/commands/use.ts index 5ba53e2..827df59 100644 --- a/src/commands/use.ts +++ b/src/commands/use.ts @@ -12,6 +12,7 @@ import { DiscordUser } from '../entities/discordUser.js'; import { getNextCardId } from '../services/getNextCardId.js'; import { EventLogService } from '../services/eventLogService.js'; import { unlink } from 'fs/promises'; +import { stringId } from '../lib/utils.js'; export class UseCommand extends Command { registerApplicationCommands(registry: ApplicationCommandRegistry) { @@ -192,15 +193,3 @@ export class UseCommand extends Command { }, 1000 * 60); } } - -function stringId(id: number) { - let stringId = ''; - const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; - - for (let i = 0; i < 4; i++) { - stringId += chars[id % chars.length]; - id = Math.floor(id / chars.length); - } - - return stringId.split('').reverse().join(''); -} diff --git a/src/commands/view.ts b/src/commands/view.ts index 5b56d13..6ea8455 100644 --- a/src/commands/view.ts +++ b/src/commands/view.ts @@ -21,13 +21,6 @@ export class ViewCommand extends Command { public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) { const id = interaction.options.getString('card')! - if(id.length != 4) { - await interaction.reply({ - content: 'Invalid card code', - ephemeral: true - }) - return; - } const card = await db .getRepository(Card) diff --git a/src/entities/card.ts b/src/entities/card.ts index 3914c15..3221e7c 100644 --- a/src/entities/card.ts +++ b/src/entities/card.ts @@ -5,7 +5,7 @@ import { CardCondition } from './cardCondition.js'; @Entity('card') export class Card { - @PrimaryColumn('char', { length: 4 }) + @PrimaryColumn('char', { length: 5 }) id!: string; @ManyToOne(() => Mapper, { eager: false, nullable: false }) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 35cf195..b9705f0 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -51,3 +51,19 @@ function getGuildInfo(guild: Guild | null, channel: | Channel | null) { return text } + +export function stringId(id: number) { + let stringId = ''; + const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; + + const is5Digit = id >= 36 ** 4; + const length = is5Digit ? 5 : 4; + if (is5Digit) id -= 36 ** 4; + + for (let i = 0; i < length; i++) { + stringId += chars[id % chars.length]; + id = Math.floor(id / chars.length); + } + + return stringId.split('').reverse().join(''); +}