Skip to content

Commit

Permalink
Limit the scores to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
ttbowen committed Dec 10, 2023
1 parent e49d354 commit 98f89bb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/mrwhale-discord/src/util/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Score } from "../database/models/score";
import { EMBED_COLOR } from "../constants";

const HIGHSCORE_PAGE_LIMIT = 10;
const HIGHSCORE_MAX_LIMIT = 100;

export interface ScoreResult {
scores: MappedScores[];
Expand All @@ -33,6 +34,7 @@ export async function createLeaderboardTable(
const embed = new EmbedBuilder()
.setColor(EMBED_COLOR)
.setTitle(title)
.setTimestamp()
.setFooter({ text: `Page ${page}/${scoreResult.pages}` });
if (scoreResult.scores.length < 1) {
return embed.setDescription("No one is ranked.");
Expand All @@ -56,11 +58,14 @@ export async function getGuildScores(
message: Message | ChatInputCommandInteraction,
page: number
): Promise<ScoreResult> {
const scoreCount = await Score.count({
where: {
guildId: message.guildId,
},
});
const scoreCount = (
await Score.findAll({
where: {
guildId: message.guildId,
},
limit: HIGHSCORE_MAX_LIMIT,
})
).length;
const pages = Math.ceil(scoreCount / HIGHSCORE_PAGE_LIMIT);
const offset = HIGHSCORE_PAGE_LIMIT * (page - 1);
const scores = await Score.findAll({
Expand Down Expand Up @@ -93,6 +98,7 @@ export async function getGlobalScores(
const scoreCount = (
await Score.findAll({
group: ["Score.userId"],
limit: HIGHSCORE_MAX_LIMIT,
})
).length;
const pages = Math.ceil(scoreCount / HIGHSCORE_PAGE_LIMIT);
Expand Down

0 comments on commit 98f89bb

Please sign in to comment.