Skip to content

Commit

Permalink
Merge pull request #8 from pckv/5-digit-ids
Browse files Browse the repository at this point in the history
Support 5-length IDs
  • Loading branch information
minetoblend authored Sep 29, 2024
2 parents c2ca62d + ebe8373 commit 430cb50
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 33 deletions.
13 changes: 1 addition & 12 deletions src/commands/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Command.Options>({
description: 'Drops 3 cards'
Expand Down Expand Up @@ -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('');
}
2 changes: 1 addition & 1 deletion src/commands/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions src/commands/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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('');
}
7 changes: 0 additions & 7 deletions src/commands/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/entities/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
16 changes: 16 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}

0 comments on commit 430cb50

Please sign in to comment.