-
Notifications
You must be signed in to change notification settings - Fork 22
feat(leaderboards): add value search #894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,18 @@ export abstract class LeaderboardService { | |
| bottom = top + PAGE_SIZE; | ||
| break; | ||
| } | ||
| case LeaderboardQuery.VALUE: { | ||
| const ranking = await this.searchLeaderboardValue( | ||
| constructor, | ||
| field, | ||
| input as number, | ||
| sort | ||
| ); | ||
| highlight = ranking - 1; | ||
| top = highlight - (highlight % 10); | ||
| bottom = top + PAGE_SIZE; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const leaderboard = await this.getLeaderboardFromRedis( | ||
|
|
@@ -302,6 +314,33 @@ export abstract class LeaderboardService { | |
| return response; | ||
| } | ||
|
|
||
| private async searchLeaderboardValue<T>( | ||
| constructor: Constructor<T>, | ||
| field: string, | ||
| value: number, | ||
| sort = "DESC" | ||
| ): Promise<number> { | ||
| const name = constructor.name.toLowerCase(); | ||
| const key = `${name}.${field}`; | ||
|
|
||
| const result = sort === "ASC" ? | ||
| await this.redis.zrangebyscore(key, value, "+inf", "LIMIT", 0, 1) : | ||
| await this.redis.zrevrangebyscore(key, value, "-inf", "LIMIT", 0, 1); | ||
|
|
||
| const fallback = sort === "ASC" ? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we should have a fallback here? If there is no player with the value it should probably just error. |
||
| await this.redis.zrevrange(key, 0, 0) : | ||
| await this.redis.zrange(key, 0, 0); | ||
|
|
||
| const id = result[0] ?? fallback[0]; | ||
| if (!id) return 1; | ||
|
|
||
| const rank = sort === "ASC" ? | ||
| await this.redis.zrank(key, id) : | ||
| await this.redis.zrevrank(key, id); | ||
|
|
||
| return (rank ?? 0) + 1; | ||
| } | ||
|
|
||
| private getLeaderboardExpiryTime(leaderboard: LeaderboardEnabledMetadata): number { | ||
| if (!leaderboard.resetEvery) | ||
| throw new Error("To get a leaderboard expiry time, `resetEvery` must be specified"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,33 @@ type GetLeaderboard = ( | |
|
|
||
| type GetLeaderboardDataIcon = (id: string) => Promise<Image>; | ||
|
|
||
| const parseLeaderboardValue = (input: string): number => { | ||
| const normalized = input.trim().toLowerCase().replaceAll("_", ""); | ||
|
|
||
| const isEuropean = /\d{1,3}(\.\d{3})+(,\d+)?$/.test(normalized.replace(/^-/, "")); | ||
|
|
||
| const sanitized = isEuropean ? | ||
| normalized.replaceAll(".", "").replace(",", ".") : | ||
| normalized.replaceAll(",", ""); | ||
|
|
||
| const match = sanitized.match(/^(-?\d+(?:\.\d+)?)(?:e([+-]?\d+))?([kmbt])?$/); | ||
| if (!match) return Number.NaN; | ||
|
|
||
| const suffixes = { | ||
| k: 1000, | ||
| m: 1_000_000, | ||
| b: 1_000_000_000, | ||
| t: 1_000_000_000_000, | ||
| }; | ||
|
|
||
| const [, value, exponent, suffix] = match; | ||
| const base = exponent ? Number(value) * Math.pow(10, Number(exponent)) : Number(value); | ||
| const multiplier = suffix ? suffixes[suffix as keyof typeof suffixes] : 1; | ||
| const result = base * multiplier; | ||
|
|
||
| return result === 0 ? 0 : result; | ||
| }; | ||
|
|
||
| export interface CreateLeaderboardOptions { | ||
| context: CommandContext; | ||
| background: Image; | ||
|
|
@@ -90,10 +117,17 @@ export class BaseLeaderboardCommand { | |
|
|
||
| const searchDocument = new ButtonBuilder() | ||
| .emoji(t(`emojis:search.${type}`)) | ||
| .label((t) => t(`leaderboard.${type}Input.button`)) | ||
| .style(ButtonStyle.Primary); | ||
|
|
||
| const searchPosition = new ButtonBuilder() | ||
| .emoji(t("emojis:search.position")) | ||
| .label((t) => t("leaderboard.positionInput.button")) | ||
| .style(ButtonStyle.Primary); | ||
|
|
||
| const searchValue = new ButtonBuilder() | ||
| .emoji(t("emojis:search.value")) | ||
| .label((t) => t("leaderboard.valueInput.button")) | ||
| .style(ButtonStyle.Primary); | ||
|
|
||
| let currentPage = 0; | ||
|
|
@@ -117,8 +151,14 @@ export class BaseLeaderboardCommand { | |
|
|
||
| if (interaction.getUserId() === userId && !message.ephemeral) { | ||
| up.disable(page === 0); | ||
| currentPage = page || currentPage; | ||
| const row = new ActionRowBuilder([up, down, searchDocument, searchPosition]); | ||
| currentPage = page ?? currentPage; | ||
| const row = new ActionRowBuilder([ | ||
| up, | ||
| down, | ||
| searchDocument, | ||
| searchPosition, | ||
| searchValue, | ||
| ]); | ||
|
|
||
| context.reply({ | ||
| ...message, | ||
|
|
@@ -174,6 +214,19 @@ export class BaseLeaderboardCommand { | |
| ) | ||
| ); | ||
|
|
||
| const valueModal = new ModalBuilder() | ||
| .title((t) => t("leaderboard.modal.title")) | ||
| .component( | ||
| new ActionRowBuilder().component( | ||
| new TextInputBuilder() | ||
| .label((t) => t("leaderboard.valueInput.label")) | ||
| .placeholder((t) => t("leaderboard.valueInput.placeholder")) | ||
| .minLength(1) | ||
| .maxLength(20) | ||
| .required(true) | ||
| ) | ||
| ); | ||
|
|
||
| listener.addHook(searchDocument.getCustomId(), () => ({ | ||
| type: InteractionResponseType.Modal, | ||
| data: documentModal.build(t), | ||
|
|
@@ -184,6 +237,11 @@ export class BaseLeaderboardCommand { | |
| data: positionModal.build(t), | ||
| })); | ||
|
|
||
| listener.addHook(searchValue.getCustomId(), () => ({ | ||
| type: InteractionResponseType.Modal, | ||
| data: valueModal.build(t), | ||
| })); | ||
|
|
||
| listener.addHook(documentModal.getCustomId(), async (interaction) => { | ||
| const data = interaction.getData(); | ||
| const documentInput = data.components[0].components[0].value; | ||
|
|
@@ -214,7 +272,29 @@ export class BaseLeaderboardCommand { | |
| ); | ||
| }); | ||
|
|
||
| const row = new ActionRowBuilder([up, down, searchDocument, searchPosition]); | ||
| listener.addHook(valueModal.getCustomId(), async (interaction) => { | ||
| const data = interaction.getData(); | ||
| const valueInput = data.components[0].components[0].value; | ||
|
|
||
| const value = parseLeaderboardValue(valueInput); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this actually works? For example if it is a level leaderboard, it is actually sorting by the exp value however the user would probably be searching by the level not the exact exp someone has. Furthermore if it was a leaderboard involving time, you would need to parse the formatted time string like 1:23. I am not sure there is a good way to do this currently and provide a good experience without some changes to the schema system. |
||
|
|
||
| if (user?.locale) interaction.setLocale(user.locale); | ||
|
|
||
| if (Number.isNaN(value) || value < 0) { | ||
| const error = new ErrorMessage("errors.leaderboardInvalidValue"); | ||
|
|
||
| return interaction.sendFollowup({ | ||
| ...error, | ||
| ephemeral: true, | ||
| }); | ||
| } | ||
|
|
||
| changePage(() => ({ input: value, type: LeaderboardQuery.VALUE }))( | ||
| interaction | ||
| ); | ||
| }); | ||
|
|
||
| const row = new ActionRowBuilder([up, down, searchDocument, searchPosition, searchValue]); | ||
|
|
||
| const [message, page] = await this.getLeaderboardMessage( | ||
| user, | ||
|
|
@@ -234,8 +314,10 @@ export class BaseLeaderboardCommand { | |
| listener.removeHook(down.getCustomId()); | ||
| listener.removeHook(searchDocument.getCustomId()); | ||
| listener.removeHook(searchPosition.getCustomId()); | ||
| listener.removeHook(searchValue.getCustomId()); | ||
| listener.removeHook(documentModal.getCustomId()); | ||
| listener.removeHook(positionModal.getCustomId()); | ||
| listener.removeHook(valueModal.getCustomId()); | ||
|
|
||
| context.reply({ embeds: [], components: [] }); | ||
| cache.clear(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be
highlight % PAGE_SIZE. I think the code above also needs to be changed while you are at it.