Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accurately format tied scores #64

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/lc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { gql } from "./gql.ts";
*/
export class LCClient implements LCClientInterface {
constructor(
private readonly fetch: typeof window.fetch = window.fetch.bind(window),
private readonly fetch: typeof globalThis.fetch = globalThis.fetch.bind(
globalThis,
),
) {}

/**
Expand Down
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})`;
});
EthanThatOneKid marked this conversation as resolved.
Show resolved Hide resolved
})
.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
Loading