Skip to content

Commit

Permalink
Fix wrong rank query
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Nov 9, 2024
1 parent 80aaa1a commit 3914a4d
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/database/utils/aliceDb/PlayerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class PlayerInfo extends Manager {
*/
async transferCoins(
amount: number,
thisPlayer: Pick<OfficialDatabaseUser, "pp"> | Player,
thisPlayer: Pick<OfficialDatabaseUser, "id"> | Player,
to: PlayerInfo,
limit?: number,
language: Language = "en",
Expand All @@ -218,7 +218,7 @@ export class PlayerInfo extends Manager {
const rank =
thisPlayer instanceof Player
? thisPlayer.rank
: ((await DroidHelper.getPlayerPPRank(thisPlayer.pp)) ?? 0);
: ((await DroidHelper.getPlayerPPRank(thisPlayer.id)) ?? 0);

switch (true) {
case rank < 10:
Expand Down
3 changes: 1 addition & 2 deletions src/database/utils/elainaDb/Clan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,13 @@ export class Clan extends Manager {
}

const player = await DroidHelper.getPlayer(toAcceptBindInfo.uid, [
"pp",
"id",
]);
const rank =
player instanceof Player
? player.rank
: player !== null
? ((await DroidHelper.getPlayerPPRank(player.pp)) ?? 0)
? ((await DroidHelper.getPlayerPPRank(player.id)) ?? 0)
: 0;

if (!player) {
Expand Down
4 changes: 2 additions & 2 deletions src/events/ready/utils/clanRankUpdating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const run: EventUtil["run"] = async () => {
}

const player = await DroidHelper.getPlayer(member.id, [
"pp",
"id",
]);

if (!player) {
Expand All @@ -54,7 +54,7 @@ export const run: EventUtil["run"] = async () => {
member.rank =
player instanceof Player
? player.rank
: ((await DroidHelper.getPlayerPPRank(player.pp)) ??
: ((await DroidHelper.getPlayerPPRank(player.id)) ??
0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
});
}

const player = await DroidHelper.getPlayer(userPlayerInfo.uid, ["pp"]);
const player = await DroidHelper.getPlayer(userPlayerInfo.uid, ["id"]);

if (!player) {
return InteractionHelper.reply(interaction, {
Expand All @@ -133,7 +133,7 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
const rank =
player instanceof Player
? player.rank
: ((await DroidHelper.getPlayerPPRank(player.pp)) ?? 0);
: ((await DroidHelper.getPlayerPPRank(player.id)) ?? 0);

let limit: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
});
}

const player = await DroidHelper.getPlayer(bindInfo.uid, ["score"]);
const player = await DroidHelper.getPlayer(bindInfo.uid, ["id"]);

if (!player) {
return InteractionHelper.reply(interaction, {
Expand Down Expand Up @@ -136,7 +136,7 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
const rank =
player instanceof Player
? player.rank
: ((await DroidHelper.getPlayerPPRank(player.score)) ?? 0);
: ((await DroidHelper.getPlayerPPRank(player.id)) ?? 0);

await DatabaseManager.elainaDb.collections.clan.insert({
leader: interaction.user.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {

let bindInfo: UserBind | null | undefined;
let player:
| Pick<
OfficialDatabaseUser,
"id" | "username" | "pp" | "region" | "playcount"
>
| Pick<OfficialDatabaseUser, "id" | "username" | "region" | "playcount">
| Player
| null = null;

Expand All @@ -60,7 +57,6 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
player = await DroidHelper.getPlayer(uid!, [
"id",
"username",
"pp",
"region",
"playcount",
]);
Expand All @@ -82,7 +78,12 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
});
}

player = await DroidHelper.getPlayer(username);
player = await DroidHelper.getPlayer(username, [
"id",
"username",
"region",
"playcount",
]);

const localUid = player?.id;

Expand All @@ -100,7 +101,12 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
);

if (bindInfo?.uid) {
player = await DroidHelper.getPlayer(bindInfo.uid);
player = await DroidHelper.getPlayer(bindInfo.uid, [
"id",
"username",
"region",
"playcount",
]);
}

break;
Expand All @@ -123,7 +129,7 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
const rank =
player instanceof Player
? player.rank
: ((await DroidHelper.getPlayerPPRank(player.pp)) ?? 0);
: ((await DroidHelper.getPlayerPPRank(player.id)) ?? 0);

embed
.setAuthor({
Expand Down
1 change: 0 additions & 1 deletion src/utils/creators/EmbedCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ export abstract class EmbedCreator {
static async createPPListEmbed(
interaction: RepliableInteraction,
player: Pick<OfficialDatabaseUser, "id" | "username" | "pp"> | Player,
topScores: Score[],
ppRank?: number | null,
language: Language = "en",
): Promise<EmbedBuilder> {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/creators/ProfileCardCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class ProfileCardCreator {
const rank =
this.player instanceof Player
? this.player.rank
: ((await DroidHelper.getPlayerPPRank(this.player.pp)) ?? 0);
: ((await DroidHelper.getPlayerPPRank(this.player.id)) ?? 0);

this.context.save();

Expand Down
16 changes: 9 additions & 7 deletions src/utils/helpers/DroidHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,25 @@ export abstract class DroidHelper {
}

/**
* Obtains a rank from the official database based on a player's pp.
* Obtains the rank of a player.
*
* In debug mode, this will return `null`.
*
* @param pp The pp to get the rank from.
* @param id The ID of the player to get the rank from.
* @returns The rank of the player, `null` if not found.
*/
static async getPlayerPPRank(pp: number): Promise<number | null> {
static async getPlayerPPRank(id: number): Promise<number | null> {
if (Config.isDebug) {
return null;
}

const table = constructOfficialDatabaseTable(
OfficialDatabaseTables.user,
);

const rankQuery = await officialPool.query<RowDataPacket[]>(
`SELECT COUNT(*) + 1 FROM ${constructOfficialDatabaseTable(
OfficialDatabaseTables.user,
)} WHERE banned = 0 AND restrict_mode = 0 AND archived = 0 AND pp > ?;`,
[pp],
`SELECT COUNT(*) + 1 FROM ${table} WHERE banned = 0 AND restrict_mode = 0 AND archived = 0 AND pp > (SELECT pp FROM ${table} WHERE id = ?);`,
[id],
);

return (
Expand Down
1 change: 0 additions & 1 deletion src/utils/helpers/PPHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export abstract class PPHelper {
const embed = await EmbedCreator.createPPListEmbed(
interaction,
player,
topScores,
undefined,
CommandHelper.getLocale(interaction),
);
Expand Down

0 comments on commit 3914a4d

Please sign in to comment.