From 738574ed331a0defeeae70b8ea8222c7eca03032 Mon Sep 17 00:00:00 2001 From: Nad Alaba <37968805+NadAlaba@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:50:56 +0300 Subject: [PATCH] impr(pace caret): add tags pb mode (@NadAlaba) (#5715) * impr(pace caret): add tags pb mode (@NadAlaba) * update modes notice when clear tags from commandline * remove all mode because it grabs only 1k results * short circuit tag.active * update setting name --------- Co-authored-by: Miodec --- frontend/src/html/pages/settings.html | 6 ++-- .../src/ts/commandline/lists/pace-caret.ts | 9 ++++++ frontend/src/ts/commandline/lists/tags.ts | 18 ++++++++--- frontend/src/ts/config.ts | 7 +++-- frontend/src/ts/db.ts | 31 +++++++++++++++++++ frontend/src/ts/elements/modes-notice.ts | 2 ++ frontend/src/ts/test/pace-caret.ts | 10 ++++++ packages/contracts/src/schemas/configs.ts | 1 + 8 files changed, 76 insertions(+), 8 deletions(-) diff --git a/frontend/src/html/pages/settings.html b/frontend/src/html/pages/settings.html index 4cc0680c5..db8520dca 100644 --- a/frontend/src/html/pages/settings.html +++ b/frontend/src/html/pages/settings.html @@ -674,8 +674,9 @@
Displays a second caret that moves at constant speed. The 'average' - option averages the speed of last 10 results. The 'daily' option takes - the highest speed of the last 24 hours. + option averages the speed of last 10 results. The 'tag pb' option takes + the highest PB of any active tag. The 'daily' option takes the highest + speed of the last 24 hours.
@@ -696,6 +697,7 @@ + diff --git a/frontend/src/ts/commandline/lists/pace-caret.ts b/frontend/src/ts/commandline/lists/pace-caret.ts index aefbc55fb..98be56512 100644 --- a/frontend/src/ts/commandline/lists/pace-caret.ts +++ b/frontend/src/ts/commandline/lists/pace-caret.ts @@ -24,6 +24,15 @@ const subgroup: MonkeyTypes.CommandsSubgroup = { TestLogic.restart(); }, }, + { + id: "setPaceCaretTagPb", + display: "tag pb", + configValue: "tagPb", + exec: (): void => { + UpdateConfig.setPaceCaret("tagPb"); + TestLogic.restart(); + }, + }, { id: "setPaceCaretLast", display: "last", diff --git a/frontend/src/ts/commandline/lists/tags.ts b/frontend/src/ts/commandline/lists/tags.ts index aae12c719..3233baf02 100644 --- a/frontend/src/ts/commandline/lists/tags.ts +++ b/frontend/src/ts/commandline/lists/tags.ts @@ -56,7 +56,7 @@ function update(): void { display: `Clear tags`, icon: "fa-times", sticky: true, - exec: (): void => { + exec: async (): Promise => { const snapshot = DB.getSnapshot(); if (!snapshot) return; @@ -67,6 +67,13 @@ function update(): void { }); DB.setSnapshot(snapshot); + if ( + Config.paceCaret === "average" || + Config.paceCaret === "tagPb" || + Config.paceCaret === "daily" + ) { + await PaceCaret.init(); + } void ModesNotice.update(); TagController.saveActiveToLocalStorage(); }, @@ -85,12 +92,15 @@ function update(): void { }, exec: async (): Promise => { TagController.toggle(tag._id); - void ModesNotice.update(); - if (Config.paceCaret === "average") { + if ( + Config.paceCaret === "average" || + Config.paceCaret === "tagPb" || + Config.paceCaret === "daily" + ) { await PaceCaret.init(); - void ModesNotice.update(); } + void ModesNotice.update(); }, }); } diff --git a/frontend/src/ts/config.ts b/frontend/src/ts/config.ts index 6657b6238..e1e8b2b9a 100644 --- a/frontend/src/ts/config.ts +++ b/frontend/src/ts/config.ts @@ -474,8 +474,11 @@ export function setPaceCaret( } if (document.readyState === "complete") { - if (val === "pb" && !isAuthenticated()) { - Notifications.add("PB pace caret is unavailable without an account", 0); + if ((val === "pb" || val === "tagPb") && !isAuthenticated()) { + Notifications.add( + `Pace caret "pb" and "tag pb" are unavailable without an account`, + 0 + ); return false; } } diff --git a/frontend/src/ts/db.ts b/frontend/src/ts/db.ts index 61b12bf42..01d9a4104 100644 --- a/frontend/src/ts/db.ts +++ b/frontend/src/ts/db.ts @@ -580,6 +580,37 @@ export async function getUserDailyBest( return retval; } +export async function getActiveTagsPB( + mode: M, + mode2: Mode2, + punctuation: boolean, + numbers: boolean, + language: string, + difficulty: Difficulty, + lazyMode: boolean +): Promise { + const snapshot = getSnapshot(); + if (!snapshot) return 0; + + let tagPbWpm = 0; + for (const tag of snapshot.tags) { + if (!tag.active) continue; + const currTagPB = await getLocalTagPB( + tag._id, + mode, + mode2, + punctuation, + numbers, + language, + difficulty, + lazyMode + ); + if (currTagPB > tagPbWpm) tagPbWpm = currTagPB; + } + + return tagPbWpm; +} + export async function getLocalPB( mode: M, mode2: Mode2, diff --git a/frontend/src/ts/elements/modes-notice.ts b/frontend/src/ts/elements/modes-notice.ts index 4456dbc10..38a9c3d35 100644 --- a/frontend/src/ts/elements/modes-notice.ts +++ b/frontend/src/ts/elements/modes-notice.ts @@ -137,6 +137,8 @@ export async function update(): Promise { ? "average" : Config.paceCaret === "pb" ? "pb" + : Config.paceCaret === "tagPb" + ? "tag pb" : Config.paceCaret === "last" ? "last" : Config.paceCaret === "daily" diff --git a/frontend/src/ts/test/pace-caret.ts b/frontend/src/ts/test/pace-caret.ts index 847181c06..cfb996e4e 100644 --- a/frontend/src/ts/test/pace-caret.ts +++ b/frontend/src/ts/test/pace-caret.ts @@ -82,6 +82,16 @@ export async function init(): Promise { Config.lazyMode, Config.funbox ); + } else if (Config.paceCaret === "tagPb") { + wpm = await DB.getActiveTagsPB( + Config.mode, + mode2, + Config.punctuation, + Config.numbers, + Config.language, + Config.difficulty, + Config.lazyMode + ); } else if (Config.paceCaret === "average") { [wpm] = await DB.getUserAverage10( Config.mode, diff --git a/packages/contracts/src/schemas/configs.ts b/packages/contracts/src/schemas/configs.ts index f3c2e360e..8579a1492 100644 --- a/packages/contracts/src/schemas/configs.ts +++ b/packages/contracts/src/schemas/configs.ts @@ -127,6 +127,7 @@ export const PaceCaretSchema = z.enum([ "off", "average", "pb", + "tagPb", "last", "custom", "daily",