Skip to content

Commit

Permalink
Fix lb skills overall order
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Mar 20, 2024
1 parent 71c2168 commit 83ee33e
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/mahoji/commands/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,22 +452,28 @@ async function skillsLb(
ironmanOnly: boolean
) {
let res = [];
let overallUsers: Record<string, any>[] = [];
let overallUsers: {
id: string;
totalLevel: number;
ironman: boolean;
totalXP: number;
}[] = [];

const skillsVals = Object.values(Skills);

const skill = skillsVals.find(_skill => _skill.aliases.some(name => stringMatches(name, inputSkill)));

if (inputSkill === 'overall') {
const events = await prisma.userEvent.findMany({
where: {
type: 'MaxTotalLevel'
},
orderBy: {
date: 'asc'
}
});
const userEventMap = userEventsToMap(events);
const maxTotalLevelEventMap = await prisma.userEvent
.findMany({
where: {
type: 'MaxTotalLevel'
},
orderBy: {
date: 'asc'
}
})
.then(res => userEventsToMap(res));

Check warning on line 476 in src/mahoji/commands/leaderboard.ts

View workflow job for this annotation

GitHub Actions / Node v18.12.0 - ubuntu-latest

'res' is already declared in the upper scope on line 454 column 6

Check warning on line 476 in src/mahoji/commands/leaderboard.ts

View workflow job for this annotation

GitHub Actions / Node v20 - ubuntu-latest

'res' is already declared in the upper scope on line 454 column 6
const query = `SELECT
u.id,
${skillsVals.map(s => `"skills.${s.id}"`)},
Expand All @@ -491,14 +497,18 @@ async function skillsLb(
totalXP: Number(user.totalxp!)
};
});
if (type !== 'xp') {
if (type === 'level') {
overallUsers.sort((a, b) => {
const valueDifference = b.totalLevel - a.totalLevel;
if (valueDifference !== 0) {
return valueDifference;
}
const dateA = userEventMap.get(a.id);
const dateB = userEventMap.get(b.id);
const xpDiff = b.totalXP - a.totalXP;
if (xpDiff !== 0) {
return xpDiff;
}
const dateA = maxTotalLevelEventMap.get(a.id);
const dateB = maxTotalLevelEventMap.get(b.id);
if (dateA && dateB) {
return dateA - dateB;
}
Expand Down

0 comments on commit 83ee33e

Please sign in to comment.