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

impr: consider numbers in pb #4946

Merged
merged 14 commits into from
Feb 22, 2024
1 change: 1 addition & 0 deletions backend/src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ declare namespace MonkeyTypes {
lazyMode: boolean;
language: string;
punctuation: boolean;
numbers?: boolean;
raw: number;
wpm: number;
timestamp: number;
Expand Down
22 changes: 17 additions & 5 deletions backend/src/utils/pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ function matchesPersonalBest(
result.difficulty === undefined ||
result.language === undefined ||
result.punctuation === undefined ||
result.lazyMode === undefined
result.lazyMode === undefined ||
result.numbers === undefined
) {
throw new Error("Missing result data (matchesPersonalBest)");
}
Expand All @@ -83,8 +84,15 @@ function matchesPersonalBest(
const samePunctuation = result.punctuation === personalBest.punctuation;
const sameDifficulty = result.difficulty === personalBest.difficulty;
const sameLanguage = result.language === personalBest.language;

return sameLazyMode && samePunctuation && sameDifficulty && sameLanguage;
const sameNumbers = result.numbers === !!personalBest.numbers;

return (
sameLazyMode &&
samePunctuation &&
sameDifficulty &&
sameLanguage &&
sameNumbers
);
}

function updatePersonalBest(
Expand All @@ -103,7 +111,8 @@ function updatePersonalBest(
result.acc === undefined ||
result.consistency === undefined ||
result.rawWpm === undefined ||
result.wpm === undefined
result.wpm === undefined ||
result.numbers === undefined
) {
throw new Error("Missing result data (updatePersonalBest)");
}
Expand All @@ -116,6 +125,7 @@ function updatePersonalBest(
personalBest.consistency = result.consistency;
personalBest.raw = result.rawWpm;
personalBest.wpm = result.wpm;
personalBest.numbers = result.numbers;
personalBest.timestamp = Date.now();

return true;
Expand All @@ -130,7 +140,8 @@ function buildPersonalBest(result: Result): MonkeyTypes.PersonalBest {
result.acc === undefined ||
result.consistency === undefined ||
result.rawWpm === undefined ||
result.wpm === undefined
result.wpm === undefined ||
result.numbers === undefined
) {
throw new Error("Missing result data (buildPersonalBest)");
}
Expand All @@ -143,6 +154,7 @@ function buildPersonalBest(result: Result): MonkeyTypes.PersonalBest {
punctuation: result.punctuation,
raw: result.rawWpm,
wpm: result.wpm,
numbers: result.numbers,
timestamp: Date.now(),
};
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/ts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ export async function saveLocalPB<M extends MonkeyTypes.Mode>(
mode: M,
mode2: MonkeyTypes.Mode2<M>,
punctuation: boolean,
numbers: boolean,
language: string,
difficulty: MonkeyTypes.Difficulty,
lazyMode: boolean,
Expand Down Expand Up @@ -636,6 +637,7 @@ export async function saveLocalPB<M extends MonkeyTypes.Mode>(
).forEach((pb) => {
if (
pb.punctuation === punctuation &&
!!pb.numbers === numbers &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think !! is necessary here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked that the older personal-bests in a given mode won't have the numbers field. That's why I added this. Btw, I am also adding support for pace-caret and tags, so still working on that. Could you tell if I am missing anything else as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, youre right. All though maybe i would prefer (pb.numbers ?? false) === number

pb.difficulty === difficulty &&
pb.language === language &&
(pb.lazyMode === lazyMode ||
Expand Down Expand Up @@ -666,6 +668,7 @@ export async function saveLocalPB<M extends MonkeyTypes.Mode>(
raw,
timestamp: Date.now(),
consistency,
numbers,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/ts/popups/pb-tables-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function update(mode: MonkeyTypes.Mode): void {
<td>${pb.difficulty}</td>
<td>${pb.language ? pb.language.replace(/_/g, " ") : "-"}</td>
<td>${pb.punctuation ? '<i class="fas fa-check"></i>' : ""}</td>
<td>${pb.numbers ? '<i class="fas fa-check"></i>' : ""}</td>
<td>${pb.lazyMode ? '<i class="fas fa-check"></i>' : ""}</td>
<td>${dateText}</td>
</tr>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/ts/test/test-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ async function saveResult(
Config.mode,
completedEvent.mode2,
Config.punctuation,
Config.numbers,
Config.language,
Config.difficulty,
Config.lazyMode,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/ts/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ declare namespace MonkeyTypes {
raw: number;
wpm: number;
timestamp: number;
numbers?: boolean;
}

interface PersonalBests {
Expand Down
1 change: 1 addition & 0 deletions frontend/static/html/popups.html
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
<td>difficulty</td>
<td>language</td>
<td>punctuation</td>
<td>numbers</td>
<td>lazy mode</td>
<td>date</td>
</tr>
Expand Down
Loading