Skip to content

Commit 4a977ea

Browse files
fehmerShizukoVMiodec
authored
refactor: move challenges to typescript and improve types (@fehmer) (#8159)
Co-authored-by: Shizuko <83967781+ShizukoV@users.noreply.github.com> Co-authored-by: Miodec <jack@monkeytype.com>
1 parent 2b5f26d commit 4a977ea

36 files changed

Lines changed: 1221 additions & 1123 deletions

File tree

backend/__tests__/setup-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ beforeAll(async () => {
99
//don't add any configuration here, add to global-setup.ts instead.
1010

1111
vi.mock("../src/init/configuration", async (importOriginal) => {
12-
const orig = (await importOriginal()) as { __testing: typeof __testing };
12+
const orig = (await importOriginal()) as any;
1313

1414
return {
1515
__testing: orig.__testing,

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"dependencies": {
2323
"@date-fns/utc": "1.2.0",
24+
"@monkeytype/challenges": "workspace:*",
2425
"@monkeytype/contracts": "workspace:*",
2526
"@monkeytype/funbox": "workspace:*",
2627
"@monkeytype/schemas": "workspace:*",

backend/src/api/controllers/result.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
} from "../../utils/prometheus";
2525
import GeorgeQueue from "../../queues/george-queue";
2626
import { getDailyLeaderboard } from "../../utils/daily-leaderboards";
27-
import AutoRoleList from "../../constants/auto-roles";
2827
import * as UserDAL from "../../dal/user";
2928
import { buildMonkeyMail } from "../../utils/monkey-mail";
3029
import * as WeeklyXpLeaderboard from "../../services/weekly-xp-leaderboard";
@@ -64,6 +63,7 @@ import { MonkeyRequest } from "../types";
6463
import { getFunbox, checkCompatibility } from "@monkeytype/funbox";
6564
import { tryCatch } from "@monkeytype/util/trycatch";
6665
import { getCachedConfiguration } from "../../init/configuration";
66+
import { getChallenges } from "@monkeytype/challenges";
6767

6868
try {
6969
if (!anticheatImplemented()) throw new Error("undefined");
@@ -81,6 +81,12 @@ try {
8181
}
8282
}
8383

84+
const autoRoleChallengeNames = new Set(
85+
getChallenges()
86+
.filter((it) => it.settings?.autoRole)
87+
.map((it) => it.name),
88+
);
89+
8490
export async function getResults(
8591
req: MonkeyRequest<GetResultsQuery>,
8692
): Promise<GetResultsResponse> {
@@ -459,7 +465,7 @@ export async function addResult(
459465
if (
460466
completedEvent.challenge !== null &&
461467
completedEvent.challenge !== undefined &&
462-
AutoRoleList.includes(completedEvent.challenge) &&
468+
autoRoleChallengeNames.has(completedEvent.challenge) &&
463469
user.discordId !== undefined &&
464470
user.discordId !== ""
465471
) {

backend/src/constants/auto-roles.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"@date-fns/utc": "1.2.0",
3030
"@leonabcd123/modern-caps-lock": "3.1.3",
31+
"@monkeytype/challenges": "workspace:*",
3132
"@monkeytype/contracts": "workspace:*",
3233
"@monkeytype/funbox": "workspace:*",
3334
"@monkeytype/schemas": "workspace:*",

frontend/scripts/check-assets.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Example usage in root or frontend:
33
* pnpm check-assets (npm run check-assets)
44
* pnpm check-assets -- -- quotes others (npm run check-assets -- -- quotes others)
5-
* pnpm check-assets -- -- challenges sound -p (npm run check-assets -- -- challenges sound -p)
65
*/
76

87
import * as fs from "fs";
@@ -18,7 +17,6 @@ import { KnownFontName } from "@monkeytype/schemas/fonts";
1817
import { Fonts } from "../src/ts/constants/fonts";
1918
import { themes, ThemeSchema, ThemesList } from "../src/ts/constants/themes";
2019
import { z } from "zod";
21-
import { ChallengeSchema, Challenge } from "@monkeytype/schemas/challenges";
2220
import { LayoutObject, LayoutObjectSchema } from "@monkeytype/schemas/layouts";
2321
import { QuoteDataSchema, QuoteData } from "@monkeytype/schemas/quotes";
2422
import { clickSoundConfig } from "../src/ts/constants/sounds";
@@ -99,24 +97,6 @@ function findDuplicates<T>(items: T[]): T[] {
9997
return Array.from(duplicates);
10098
}
10199

102-
async function validateChallenges(): Promise<void> {
103-
const problems = new Problems<"_list.json", never>("Challenges", {});
104-
105-
const challengesData = JSON.parse(
106-
fs.readFileSync("./static/challenges/_list.json", {
107-
encoding: "utf8",
108-
flag: "r",
109-
}),
110-
) as Challenge;
111-
const validationResult = z.array(ChallengeSchema).safeParse(challengesData);
112-
problems.addValidation("_list.json", validationResult);
113-
114-
console.log(problems.toString());
115-
if (problems.hasError()) {
116-
throw new Error("challenges with errors");
117-
}
118-
}
119-
120100
async function validateLayouts(): Promise<void> {
121101
const problems = new Problems<Layout, "_additional">("Layouts", {
122102
_additional:
@@ -496,17 +476,10 @@ async function main(): Promise<void> {
496476
quotes: [validateQuotes],
497477
languages: [validateLanguages],
498478
layouts: [validateLayouts],
499-
challenges: [validateChallenges],
500479
fonts: [validateFonts],
501480
themes: [validateThemes],
502481
sounds: [validateSounds],
503-
others: [
504-
validateChallenges,
505-
validateLayouts,
506-
validateFonts,
507-
validateThemes,
508-
validateSounds,
509-
],
482+
others: [validateLayouts, validateFonts, validateThemes, validateSounds],
510483
};
511484

512485
// flags

frontend/src/ts/auth.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NewPasswordSchema, PasswordSchema } from "@monkeytype/schemas/users";
2+
import { typedKeys } from "@monkeytype/util/objects";
23
import { tryCatch } from "@monkeytype/util/trycatch";
34
import { FirebaseError } from "firebase/app";
45
import {
@@ -47,7 +48,6 @@ import {
4748
import { FaObject } from "./types/font-awesome";
4849
import { isDevEnvironment } from "./utils/env";
4950
import { createErrorMessage } from "./utils/error";
50-
import { typedKeys } from "./utils/misc";
5151
import { SnapshotInitError } from "./utils/snapshot-init-error";
5252
import { OneOf } from "./utils/types";
5353

frontend/src/ts/collections/ape-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import {
1313
setLastGeneratedApeKey,
1414
} from "../states/account-settings";
1515
import { applyIdWorkaround, tempId } from "./utils/misc";
16-
import { typedEntries } from "../utils/misc";
1716
import { ApeKey } from "@monkeytype/schemas/ape-keys";
1817
import { showSuccessNotification } from "../states/notifications";
1918
import {
2019
replaceSpacesWithUnderscores,
2120
replaceUnderscoresWithSpaces,
2221
} from "../utils/strings";
22+
import { typedEntries } from "@monkeytype/util/objects";
2323

2424
export type ApeKeyEntry = ApeKey & { _id: string };
2525
const queryKeys = {

frontend/src/ts/commandline/commandline-metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { getActivePage, isAuthenticated } from "../states/core";
1313
import { Fonts } from "../constants/fonts";
1414
import { KnownFontName } from "@monkeytype/schemas/fonts";
1515
import * as UI from "../ui";
16-
import { typedKeys } from "../utils/misc";
1716
import { Validation } from "../types/validation";
17+
import { typedKeys } from "@monkeytype/util/objects";
1818

1919
//TODO: remove display property and instead use optionsMetadata from configMetadata
2020
// eventually this file should be fully merged into config metadata, probably under the 'commandline' property

frontend/src/ts/commandline/lists.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ import CustomThemesListCommands from "./lists/custom-themes-list";
1212
import PresetsCommands from "./lists/presets";
1313
import FunboxCommands from "./lists/funbox";
1414
import ThemesCommands from "./lists/themes";
15-
import LoadChallengeCommands, {
16-
update as updateLoadChallengeCommands,
17-
} from "./lists/load-challenge";
15+
import LoadChallengeCommands from "./lists/load-challenge";
1816

1917
import { Config } from "../config/store";
2018
import { setConfig } from "../config/setters";
21-
import * as getErrorMessage from "../utils/error";
22-
import * as JSONData from "../utils/json-data";
2319
import { randomizeTheme } from "../controllers/theme-controller";
2420
import { showModal } from "../states/modals";
2521
import {
@@ -41,20 +37,6 @@ import {
4137
import { applyConfigFromJson } from "../config/lifecycle";
4238
import { lastEventLog } from "../test/test-state";
4339

44-
const challengesPromise = JSONData.getChallengeList();
45-
challengesPromise
46-
.then((challenges) => {
47-
updateLoadChallengeCommands(challenges);
48-
})
49-
.catch((e: unknown) => {
50-
console.error(
51-
getErrorMessage.createErrorMessage(
52-
e,
53-
"Failed to update challenges commands",
54-
),
55-
);
56-
});
57-
5840
const adsCommands = buildCommands("ads");
5941

6042
export const commands: CommandsSubgroup = {
@@ -406,8 +388,6 @@ export function doesListExist(listName: string): boolean {
406388
export async function getList(
407389
listName: CommandlineListKey | ConfigKey,
408390
): Promise<CommandsSubgroup> {
409-
await Promise.allSettled([challengesPromise]);
410-
411391
const subGroup = subgroupByConfigKey[listName];
412392
if (subGroup !== undefined) {
413393
return subGroup;
@@ -451,7 +431,6 @@ export function getTopOfStack(): CommandsSubgroup {
451431

452432
let singleList: CommandsSubgroup | undefined;
453433
export async function getSingleSubgroup(): Promise<CommandsSubgroup> {
454-
await Promise.allSettled([challengesPromise]);
455434
const singleCommands: Command[] = [];
456435
for (const command of commands.list) {
457436
const ret = buildSingleListCommands(command);

0 commit comments

Comments
 (0)