Skip to content

Commit

Permalink
fix function formatScores
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Sep 21, 2024
1 parent 2ae3886 commit 50c2958
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
2 changes: 2 additions & 0 deletions lib/lc/fake_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import type {
} from "./client_interface.ts";

export const FAKE_LC_USERNAME = "fake_lc_username";
export const FAKE_LC_QUESTION_NUMBER = 0;
export const FAKE_LC_QUESTION_NAME = "fake_lc_question_name";
export const FAKE_LC_QUESTION_TITLE = "fake_lc_question_title";
export const FAKE_LC_QUESTION_URL = "fake_lc_question_url";
export const FAKE_LC_QUESTION_DIFFICULTY = "fake_lc_question_difficulty";
export const FAKE_LC_QUESTION_DATE = "2023-07-31";
export const FAKE_LC_QUESTION: LCQuestion = {
number: FAKE_LC_QUESTION_NUMBER,
name: FAKE_LC_QUESTION_NAME,
title: FAKE_LC_QUESTION_TITLE,
url: FAKE_LC_QUESTION_URL,
Expand Down
31 changes: 19 additions & 12 deletions lib/leaderboard/scores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,21 @@ export function defaultModifyScore(score: number): number {
* formatScores formats the scores of all players in a season.
*/
export function formatScores(season: api.Season): string {
return Object.entries(season.scores)
.sort(({ 1: scoreA }, { 1: scoreB }) => scoreB - scoreA)
.map(([playerID, score], i) => {
const player = season.players[playerID];
const formattedScore = String(score).padStart(3, " ");
const formattedSubmissions = formatSubmissions(season, playerID);
const formattedRank = formatRank(i + 1);
return `${formattedScore} ${formattedSubmissions} ${player.lc_username} (${formattedRank})`;
return Object.entries(
Object.groupBy(
Object.entries(season.scores),
([, score]) => score,
),
)
.toSorted((a, b) => b[1]![0][1] - a[1]![0][1])
.flatMap(([score, entries], i) => {
const formattedScore = score.padStart(3, " ");
return entries?.map(([playerID]) => {
const player = season.players[playerID];
const formattedSubmissions = formatSubmissions(season, playerID);
const formattedRank = formatRank(i + 1);
return `${formattedScore} ${formattedSubmissions} ${player.lc_username} (${formattedRank})`;
});
})
.join("\n");
}
Expand Down Expand Up @@ -240,19 +247,19 @@ export function formatRank(rank: number): string {
export function formatDifficulty(difficulty?: string): string {
switch (difficulty) {
case "Easy": {
return "🟢";
return "🟩";
}

case "Medium": {
return "🟠";
return "🟧";
}

case "Hard": {
return "🔴";
return "🟥";
}

default: {
return "·";
return "🔲";
}
}
}
6 changes: 5 additions & 1 deletion lib/leaderboard/scores_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
calculateScores,
makeDefaultCalculateScoresOptions,
} from "./scores.ts";
import type * as api from "lc-dailies/lib/api/types.ts";

const FAKE_SEASON = {
const FAKE_SEASON: api.Season = {
"id": "01H8T4MM00BQHHK7VTTEJE1WAS",
"start_date": "Sun, 27 Aug 2023 00:00:00 GMT",
"players": {
Expand All @@ -20,13 +21,15 @@ const FAKE_SEASON = {
},
"questions": {
"implement-stack-using-queues": {
"number": 225,
"name": "implement-stack-using-queues",
"date": "2023-08-28",
"title": "Implement Stack using Queues",
"difficulty": "Easy",
"url": "https://leetcode.com/problems/implement-stack-using-queues/",
},
"counting-bits": {
"number": 338,
"name": "counting-bits",
"date": "2023-09-01",
"title": "Counting Bits",
Expand Down Expand Up @@ -56,6 +59,7 @@ const FAKE_SEASON = {
},
},
},
scores: {},
};

Deno.test("calculatePlayerScore calculates the score of a player", () => {
Expand Down
16 changes: 11 additions & 5 deletions lib/leaderboard/sync_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { assertEquals } from "@std/assert";
import type { LCSubmission } from "lc-dailies/lib/lc/mod.ts";
import type { LCQuestion, LCSubmission } from "lc-dailies/lib/lc/mod.ts";
import { sync } from "./sync.ts";
import type * as api from "lc-dailies/lib/api/types.ts";

const FAKE_UNSYNCED_SEASON = {
const FAKE_UNSYNCED_SEASON: api.Season = {
"id": "01H8T4MM00BQHHK7VTTEJE1WAS",
"start_date": "Sun, 27 Aug 2023 00:00:00 GMT",
"players": {
Expand All @@ -17,13 +18,15 @@ const FAKE_UNSYNCED_SEASON = {
},
"questions": {
"implement-stack-using-queues": {
"number": 225,
"name": "implement-stack-using-queues",
"date": "2023-08-28",
"title": "Implement Stack using Queues",
"difficulty": "Easy",
"url": "https://leetcode.com/problems/implement-stack-using-queues/",
},
"counting-bits": {
"number": 338,
"name": "counting-bits",
"date": "2023-09-01",
"title": "Counting Bits",
Expand Down Expand Up @@ -56,22 +59,23 @@ const FAKE_UNSYNCED_SEASON = {
"scores": {},
};

const FAKE_QUESTION = {
const FAKE_QUESTION: LCQuestion = {
number: 7,
name: "reverse-integer",
date: "2023-09-02",
title: "Reverse Integer",
difficulty: "Easy",
url: "https://leetcode.com/problems/reverse-integer/",
};

const FAKE_SUBMISSION = {
const FAKE_SUBMISSION: LCSubmission = {
id: "8008569420",
name: "reverse-integer",
title: "Reverse Integer",
timestamp: "1693627483",
};

const FAKE_SYNCED_SEASON = {
const FAKE_SYNCED_SEASON: api.Season = {
"id": "01H8T4MM00BQHHK7VTTEJE1WAS",
"start_date": "Sun, 27 Aug 2023 00:00:00 GMT",
"players": {
Expand All @@ -86,13 +90,15 @@ const FAKE_SYNCED_SEASON = {
},
"questions": {
"implement-stack-using-queues": {
"number": 225,
"name": "implement-stack-using-queues",
"date": "2023-08-28",
"title": "Implement Stack using Queues",
"difficulty": "Easy",
"url": "https://leetcode.com/problems/implement-stack-using-queues/",
},
"counting-bits": {
"number": 338,
"name": "counting-bits",
"date": "2023-09-01",
"title": "Counting Bits",
Expand Down

0 comments on commit 50c2958

Please sign in to comment.