Skip to content

Commit

Permalink
update: leaderboard numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetfulskybro committed Aug 6, 2024
1 parent a9e7d93 commit c8dd069
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 125 deletions.
79 changes: 46 additions & 33 deletions src/buttons/pagination/paginateLast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "discord.js";
import { Button } from "../../interfaces";

import { assignRanks } from "../../util/Functions/number";
import { UserModel } from "../../util/Models/userModel";

const button: Button = {
Expand Down Expand Up @@ -101,40 +102,52 @@ const button: Button = {
) {
paginate.countedPages.push(paginate.pages.length - 1);

data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip((paginate.pages.length - 1) * 10)
.limit(10);

data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
if (paginate.type === "global") {
data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip((paginate.pages.length - 1) * 10)
.limit(10);

data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
} else {
data = await Promise.all(
guildDb.gameScores.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
}

data = assignRanks(data, (paginate.pages.length - 1) * 10);
data = data.map(
(s: any, i) =>
`${(paginate.pages.length - 1) * 10 + i++}. ${
s.user === "Anonymous"
? `${s.user} • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
: `<@${s.user}> • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
}`,
(s: any) =>
`${s.rank}${s.user === "Anonymous" ? s.user : `<@${s.user}>`} • **${s.score}** ${client.translation.get(
guildDb.language,
"Leaderboard.points",
)}`,
);

embed.data.description = data?.join("\n");
Expand Down
78 changes: 45 additions & 33 deletions src/buttons/pagination/paginateNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MessageActionRowComponentBuilder,
} from "discord.js";
import { Button } from "../../interfaces";
import { assignRanks } from "../../util/Functions/number";
import { UserModel } from "../../util/Models/userModel";

const button: Button = {
Expand Down Expand Up @@ -99,41 +100,52 @@ const button: Button = {
) {
paginate.countedPages.push(paginate.page);

data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip(paginate.page * 10)
.limit(10);

data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
if (paginate.type === "global") {
data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip(paginate.page * 10)
.limit(10);

data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
} else {
data = await Promise.all(
guildDb.gameScores.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
}

data = assignRanks(data, paginate.page * 10);
data = data.map(
(s: any, i) =>
`${paginate.page * 10 + i++}. ${
s.user === "Anonymous"
? `${s.user} • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
: `<@${s.user}> • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
}`,
(s: any) =>
`${s.rank}${s.user === "Anonymous" ? s.user : `<@${s.user}>`} • **${s.score}** ${client.translation.get(
guildDb.language,
"Leaderboard.points",
)}`,
);

embed.data.description = data?.join("\n");
Expand Down
77 changes: 45 additions & 32 deletions src/buttons/pagination/paginatePrev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MessageActionRowComponentBuilder,
} from "discord.js";
import { Button } from "../../interfaces";
import { assignRanks } from "../../util/Functions/number";
import { UserModel } from "../../util/Models/userModel";

const button: Button = {
Expand Down Expand Up @@ -99,40 +100,52 @@ const button: Button = {
) {
paginate.countedPages.push(paginate.page);

data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip(paginate.page * 10)
.limit(10);
if (paginate.type === "global") {
data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip(paginate.page * 10)
.limit(10);

data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
} else {
data = await Promise.all(
guildDb.gameScores.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
}

data = assignRanks(data, paginate.page * 10);
data = data.map(
(s: any, i) =>
`${paginate.page * 10 + i++}. ${
s.user === "Anonymous"
? `${s.user} • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
: `<@${s.user}> • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
}`,
(s: any) =>
`${s.rank}${s.user === "Anonymous" ? s.user : `<@${s.user}>`} • **${s.score}** ${client.translation.get(
guildDb.language,
"Leaderboard.points",
)}`,
);

embed.data.description = data?.join("\n");
Expand Down
Loading

0 comments on commit c8dd069

Please sign in to comment.