Skip to content

Commit 3304d1b

Browse files
committed
fixed tie handling in codeycoin leaderboard
1 parent b127cc5 commit 3304d1b

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/commandDetails/coin/leaderboard.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const getCoinLeaderboardEmbed = async (
2929
let rank = 0;
3030
let offset = 0;
3131
let i = 0;
32+
let absoluteCount = 0;
3233
while (leaderboardArray.length < LEADERBOARD_LIMIT_DISPLAY || position === 0) {
3334
if (i === LEADERBOARD_LIMIT_FETCH) {
3435
offset += LEADERBOARD_LIMIT_FETCH;
@@ -46,10 +47,15 @@ const getCoinLeaderboardEmbed = async (
4647
continue;
4748
}
4849
if (user.bot) continue;
49-
if (previousBalance !== userCoinEntry.balance) {
50+
if (previousBalance === userCoinEntry.balance) {
5051
previousBalance = userCoinEntry.balance;
51-
rank = rank + 1;
52+
// rank does not change
53+
} else {
54+
previousBalance = userCoinEntry.balance;
55+
rank = absoluteCount + 1;
5256
}
57+
// count how many total users have been processed:
58+
absoluteCount++;
5359
if (userCoinEntry.user_id === userId) {
5460
position = rank;
5561
}
@@ -64,24 +70,33 @@ const getCoinLeaderboardEmbed = async (
6470
.join('\\_')
6571
.split('`')
6672
.join('\\`');
67-
const userCoinEntryText = `${rank}. ${cleanUserTag} - ${
73+
console.log("RANK:", rank);
74+
console.log("Position:", position);
75+
console.log("Previous Balance:", previousBalance);
76+
console.log("offset:", offset);
77+
console.log("User Coin Entry Balance:", userCoinEntry.balance);
78+
const userCoinEntryText = `${rank}\\. ${cleanUserTag} - ${
6879
userCoinEntry.balance
6980
} ${getCoinEmoji()}`;
81+
console.log("userCoinEntry is: ", userCoinEntryText);
7082
leaderboardArray.push(userCoinEntryText);
7183
}
7284
}
7385
const leaderboardText = leaderboardArray.join('\n');
86+
console.log("leaderboardtext is: ", leaderboardText);
7487
const leaderboardEmbed = new EmbedBuilder()
7588
.setColor(DEFAULT_EMBED_COLOUR)
7689
.setTitle('Codey Coin Leaderboard')
7790
.setDescription(leaderboardText);
91+
92+
console.log("leaderboard embed is: ", leaderboardEmbed);
7893
leaderboardEmbed.addFields([
7994
{
8095
name: 'Your Position',
8196
value: `You are currently **#${position}** in the leaderboard with ${userBalance} ${getCoinEmoji()}.`,
8297
},
8398
]);
84-
99+
console.log("leaderboard embed is: ", leaderboardEmbed);
85100
return leaderboardEmbed;
86101
};
87102

0 commit comments

Comments
 (0)