Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/api/src/dtos/guild-leaderboard.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Guild, getLeaderboardFields } from "@statsify/schemas";
import {
IsEnum,
IsInt,
IsNumber,
IsOptional,
IsString,
MaxLength,
Expand Down Expand Up @@ -39,6 +40,12 @@ export class GuildLeaderboardDto {
@ApiProperty({ minimum: 1, type: () => Number, required: false })
public position?: number;

@Transform((params) => +params.value)
@IsOptional()
@IsNumber()
@ApiProperty({ type: () => Number, required: false })
public value?: number;

@IsOptional()
@IsString()
@MinLength(3)
Expand Down
10 changes: 8 additions & 2 deletions apps/api/src/dtos/player-leaderboard.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

import { ApiProperty, PartialType } from "@nestjs/swagger";
import { IsEnum, IsInt, IsOptional, Min } from "class-validator";
import { Player, getLeaderboardFields } from "@statsify/schemas";
import { IsEnum, IsInt, IsNumber, IsOptional, Min } from "class-validator";
import { getLeaderboardFields, Player } from "@statsify/schemas";
import { PlayerDto } from "./player.dto.js";
import { Transform } from "class-transformer";

Expand All @@ -31,4 +31,10 @@ export class PlayerLeaderboardDto extends PartialType(PlayerDto) {
@Min(1)
@ApiProperty({ minimum: 1, type: () => Number, required: false })
public position?: number;

@Transform((params) => +params.value)
@IsOptional()
@IsNumber()
@ApiProperty({ type: () => Number, required: false })
public value?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class GuildLeaderboardController {
@ApiBadRequestResponse({ type: ErrorResponse })
@Auth({ weight: 10 })
public async getGuildLeaderboard(
@Body() { field, page, guild, position }: GuildLeaderboardDto
@Body() { field, page, guild, position, value }: GuildLeaderboardDto
) {
let input: number | string;
let type: LeaderboardQuery;
Expand All @@ -40,6 +40,9 @@ export class GuildLeaderboardController {
} else if (position) {
input = position;
type = LeaderboardQuery.POSITION;
} else if (typeof value === "number") {
input = value;
type = LeaderboardQuery.VALUE;
} else {
input = page;
type = LeaderboardQuery.PAGE;
Expand Down
39 changes: 39 additions & 0 deletions apps/api/src/leaderboards/leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Member

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.

bottom = top + PAGE_SIZE;
break;
}
}

const leaderboard = await this.getLeaderboardFromRedis(
Expand Down Expand Up @@ -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" ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class PlayerLeaderboardsController {
@ApiBadRequestResponse({ type: ErrorResponse })
@Auth({ weight: 3 })
public getPlayerLeaderboard(
@Body() { field, page, player, position }: PlayerLeaderboardDto
@Body() { field, page, player, position, value }: PlayerLeaderboardDto
) {
let input: number | string;
let type: LeaderboardQuery;
Expand All @@ -48,6 +48,9 @@ export class PlayerLeaderboardsController {
} else if (position) {
input = position;
type = LeaderboardQuery.POSITION;
} else if (typeof value === "number") {
input = value;
type = LeaderboardQuery.VALUE;
} else {
input = page;
type = LeaderboardQuery.PAGE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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,
Expand All @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions locales/en-US/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@
"description": "The leaderboard position you entered is invalid. Enter a number greater than 1.",
"title": "Invalid Position"
},
"leaderboardInvalidValue": {
"description": "The leaderboard value you entered is invalid. Enter a positive number. Suffixes like k, m, b, and t are supported.",
"title": "Invalid Value"
},
"leaderboardNotFound": {
"description": "We can't find the `leaderboard` you are looking for!",
"title": "Leaderboard Not Found"
Expand Down Expand Up @@ -525,19 +529,27 @@
},
"leaderboard": {
"guildInput": {
"button": "Guild Name",
"label": "Search by a Guild",
"placeholder": "Enter a guild's name, eg: Quote"
},
"modal": {
"title": "Leaderboard Search"
},
"playerInput": {
"button": "Username",
"label": "Search by a Player",
"placeholder": "Enter a player's name, eg: j4cobi"
},
"positionInput": {
"button": "Position",
"label": "Search by a Position",
"placeholder": "Enter a position, eg: 5000"
},
"valueInput": {
"button": "Value",
"label": "Search by a Value",
"placeholder": "Enter a value, eg: 5k, 15m, 50b"
}
},
"minecraft": {
Expand Down
3 changes: 2 additions & 1 deletion locales/en-US/emojis.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"search": {
"player": "<:searchplayer:995761551913005166>",
"guild": "<:searchguild:996434315938381844>",
"position": "<:searchposition:995761551048966154>"
"position": "<:searchposition:995761551048966154>",
"value": "<:searchvalue:1509622552828317696>"
},
"check": "<:check:995761550310772777>",
"cross": "<:cross:995761549316726884>",
Expand Down
3 changes: 2 additions & 1 deletion packages/api-client/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export enum CacheLevel {
export enum LeaderboardQuery {
PAGE = "page",
INPUT = "input",
POSITION = "position"
POSITION = "position",
VALUE = "value"
}
Loading