Skip to content

Commit

Permalink
add scores property to Season
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Oct 6, 2023
1 parent e02a718 commit 89fbd9a
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 232 deletions.
14 changes: 6 additions & 8 deletions api/dailies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type APIEmbed } from "lc-dailies/deps.ts";
import * as api from "lc-dailies/api/mod.ts";
import * as discord from "lc-dailies/lib/discord/mod.ts";
import * as router from "lc-dailies/lib/router/mod.ts";
import * as lc from "lc-dailies/lib/lc/mod.ts";
Expand Down Expand Up @@ -85,7 +86,7 @@ async function executeDailyWebhook(
const season = seasonID
? await leaderboardClient.getSeason(seasonID)
: isSunday
? await leaderboardClient.getCurrentSeason()
? await leaderboardClient.getLatestSeason()
: null;

// Format the webhook embed.
Expand All @@ -108,12 +109,12 @@ export interface DailyWebhookOptions {
/**
* question is the daily question.
*/
question: lc.DailyQuestion;
question: api.LCQuestion;

/**
* season is the season to recap.
*/
season: leaderboard.Season | null;
season: api.Season | null;
}

/**
Expand Down Expand Up @@ -158,13 +159,10 @@ export function makeDailyWebhookEmbeds(
/**
* formatScores formats the scores of all players in a season.
*/
export function formatScores(season: leaderboard.Season): string {
const scores = leaderboard.calculateSeasonScores(
leaderboard.makeDefaultCalculateScoresOptions(season),
);
export function formatScores(season: api.Season): string {
return [
"```",
...Object.entries(scores)
...Object.entries(season.scores)
.sort(({ 1: scoreA }, { 1: scoreB }) => scoreB - scoreA)
.map(([playerID, score], i) => {
const player = season.players[playerID];
Expand Down
4 changes: 2 additions & 2 deletions api/discord_app/sub/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ApplicationCommandOptionType,
InteractionResponseType,
} from "lc-dailies/deps.ts";
import type * as leaderboard from "lc-dailies/lib/leaderboard/mod.ts";
import type * as api from "lc-dailies/api/mod.ts";

export const REGISTER = "register";
export const REGISTER_DESCRIPTION = "Register your Leetcode account";
Expand Down Expand Up @@ -68,7 +68,7 @@ export function parseRegisterOptions(
* makeRegisterInteractionResponse makes the interaction response for the register subcommand.
*/
export function makeRegisterInteractionResponse(
r: leaderboard.RegisterResponse,
r: api.RegisterResponse,
): APIInteractionResponse {
return {
type: InteractionResponseType.ChannelMessageWithSource,
Expand Down
4 changes: 2 additions & 2 deletions api/discord_app/sub/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ApplicationCommandOptionType,
InteractionResponseType,
} from "lc-dailies/deps.ts";
import type * as leaderboard from "lc-dailies/lib/leaderboard/mod.ts";
import * as api from "lc-dailies/api/mod.ts";

export const SUBMIT = "submit";
export const SUBMIT_DESCRIPTION =
Expand Down Expand Up @@ -69,7 +69,7 @@ export function parseSubmitOptions(
* makeSubmitInteractionResponse makes the interaction response for the register subcommand.
*/
export function makeSubmitInteractionResponse(
r: leaderboard.SubmitResponse,
r: api.SubmitResponse,
): APIInteractionResponse {
return {
type: InteractionResponseType.ChannelMessageWithSource,
Expand Down
1 change: 1 addition & 0 deletions api/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./api.ts";
export * from "./types.ts";
2 changes: 1 addition & 1 deletion api/seasons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function makeSeasonGetHandler(

const season =
await (seasonID === "latest"
? leaderboardClient.getCurrentSeason()
? leaderboardClient.getLatestSeason()
: leaderboardClient.getSeason(seasonID));
return new Response(JSON.stringify(season));
};
Expand Down
118 changes: 118 additions & 0 deletions api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* LCPlayer is a registered player from Leetcode.
*/
export interface LCPlayer {
/**
* discord_user_id is the Discord user ID of the player.
*/
discord_user_id: string;

/**
* lc_username is the Leetcode username of the player.
*/
lc_username: string;
}

/**
* LCSubmission is a Leetcode submission.
*/
export interface LCSubmission {
/**
* id is the ID of the submission.
*/
id: string;

/**
* date is the timestamp of the Leetcode submission.
*/
date: string;
}

/**
* LCQuestion is a Leetcode question.
*/
export interface LCQuestion {
/**
* name is the name of the daily question.
*/
name: string;

/**
* date is the date the daily question was posted in the format of YYYY-MM-DD.
*/
date: string;

/**
* title is the title of the daily question.
*/
title: string;

/**
* difficulty is the difficulty of the daily question.
*/
difficulty: string;

/**
* url is the link of the daily question.
*/
url: string;
}

/**
* Season is a season of the leaderboard.
*/
export interface Season {
/**
* id is the ID of the season.
*/
id: string;

/**
* start_date is the start date of the season.
*/
start_date: string;

/**
* scores is the map of scores in the season.
*/
scores: { [discord_user_id: string]: number };

/**
* players is the map of players in the season.
*/
players: { [discord_user_id: string]: LCPlayer };

/**
* questions is the map of questions in the season.
*/
questions: { [lc_question_name: string]: LCQuestion };

/**
* submissions is the map of submissions in the season.
*/
submissions: {
[discord_user_id: string]: {
[lc_question_name: string]: LCSubmission;
};
};
}

/**
* RegisterResponse is the response for the register subcommand.
*/
export interface RegisterResponse {
/**
* ok is whether the registration was successful.
*/
ok: boolean;
}

/**
* SubmitResponse is the response for the submit subcommand.
*/
export interface SubmitResponse {
/**
* ok is whether the submission was successful.
*/
ok: boolean;
}
51 changes: 9 additions & 42 deletions lib/lc/client.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
import type { LCQuestion } from "lc-dailies/api/mod.ts";
import { makeQuestionURL } from "./urls.ts";
import { gql } from "./gql.ts";

/**
* DailyQuestion is the representation of Leetcode's daily question.
*
* Sample:
* Daily Leetcode Question for 2021-10-20 (date)
* Question: Find Eventual Safe States (question.title)
* Difficulty: Medium (question.difficulty)
* Link: https://leetcode.com/problems/find-eventual-safe-states/ (link)
*/
export interface DailyQuestion {
/**
* name is the name of the daily question.
*/
name: string;

/**
* date is the date the daily question was posted in the format of YYYY-MM-DD.
*/
date: string;

/**
* title is the title of the daily question.
*/
title: string;

/**
* difficulty is the difficulty of the daily question.
*/
difficulty: string;

/**
* url is the link of the daily question.
*/
url: string;
}
export type { LCQuestion };

/**
* RecentSubmission is the representation of Leetcode's recent submission per user.
* LCSubmission is the representation of Leetcode's recent submission per user.
*/
export interface RecentSubmission {
export interface LCSubmission {
/**
* id is the id details of the submission.
*/
Expand Down Expand Up @@ -77,7 +44,7 @@ export class LCClient {
/**
* getDailyQuestion gets the daily question from Leetcode.
*/
public async getDailyQuestion(): Promise<DailyQuestion> {
public async getDailyQuestion(): Promise<LCQuestion> {
const date = new Date();
const [question] = await this.listDailyQuestions(
1,
Expand All @@ -98,8 +65,8 @@ export class LCClient {
limit: number,
asOfYear: number,
asOfMonth: number,
): Promise<DailyQuestion[]> {
const dailies: DailyQuestion[] = [];
): Promise<LCQuestion[]> {
const dailies: LCQuestion[] = [];
let currentYear = asOfYear;
let currentMonth = asOfMonth;

Expand Down Expand Up @@ -153,7 +120,7 @@ export class LCClient {
public async getRecentAcceptedSubmissions(
username: string,
limit: number,
): Promise<RecentSubmission[]> {
): Promise<LCSubmission[]> {
return await gql(
JSON.stringify({
operationName: "recentAcSubmissions",
Expand All @@ -175,7 +142,7 @@ export class LCClient {
timestamp: string;
titleSlug: string;
},
): RecentSubmission => ({
): LCSubmission => ({
id: acSubmission.id,
name: acSubmission.titleSlug,
title: acSubmission.title,
Expand Down
16 changes: 8 additions & 8 deletions lib/lc/fake_client.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import type { DailyQuestion, LCClient, RecentSubmission } from "./client.ts";
import type { LCClient, LCQuestion, LCSubmission } from "./client.ts";

export const FAKE_LC_USERNAME = "fake_lc_username";
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: DailyQuestion = {
export const FAKE_LC_QUESTION: LCQuestion = {
name: FAKE_LC_QUESTION_NAME,
title: FAKE_LC_QUESTION_TITLE,
url: FAKE_LC_QUESTION_URL,
difficulty: FAKE_LC_QUESTION_DIFFICULTY,
date: FAKE_LC_QUESTION_DATE,
};
export const FAKE_LC_QUESTIONS: DailyQuestion[] = [FAKE_LC_QUESTION];
export const FAKE_LC_QUESTIONS: LCQuestion[] = [FAKE_LC_QUESTION];
export const FAKE_RECENT_SUBMISSION_ID = "1031839418";
export const FAKE_RECENT_SUBMISSION_TIMESTAMP = "1690761600";
export const FAKE_RECENT_SUBMISSION: RecentSubmission = {
export const FAKE_RECENT_SUBMISSION: LCSubmission = {
id: FAKE_RECENT_SUBMISSION_ID,
name: FAKE_LC_QUESTION_NAME,
title: FAKE_LC_QUESTION_TITLE,
timestamp: FAKE_RECENT_SUBMISSION_TIMESTAMP,
};
export const FAKE_RECENT_SUBMISSIONS: RecentSubmission[] = [
export const FAKE_RECENT_SUBMISSIONS: LCSubmission[] = [
FAKE_RECENT_SUBMISSION,
];

Expand All @@ -38,18 +38,18 @@ export class FakeLCClient implements LCClient {
_: number,
__: number,
___: number,
): Promise<DailyQuestion[]> {
): Promise<LCQuestion[]> {
return Promise.resolve(FAKE_LC_QUESTIONS);
}

public getRecentAcceptedSubmissions(
_: string,
__: number,
): Promise<RecentSubmission[]> {
): Promise<LCSubmission[]> {
return Promise.resolve(FAKE_RECENT_SUBMISSIONS);
}

public getDailyQuestion(): Promise<DailyQuestion> {
public getDailyQuestion(): Promise<LCQuestion> {
return Promise.resolve(FAKE_LC_QUESTION);
}
}
Loading

0 comments on commit 89fbd9a

Please sign in to comment.