Skip to content

Commit

Permalink
impr: save pbs for numbers mode separately (notreallystatic, miodec) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
notreallystatic authored Feb 22, 2024
1 parent 3e59163 commit 1429c2c
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 14 deletions.
77 changes: 77 additions & 0 deletions backend/__tests__/utils/pb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,81 @@ describe("Pb Utils", () => {
expect(result).toBe(expected);
});
});
describe("checkAndUpdatePb", () => {
it("should update personal best", () => {
const userPbs: SharedTypes.PersonalBests = {
time: {},
words: {},
custom: {},
quote: {},
zen: {},
};
const result = {
difficulty: "normal",
language: "english",
punctuation: false,
lazyMode: false,
acc: 100,
consistency: 100,
rawWpm: 100,
wpm: 110,
numbers: false,
mode: "time",
mode2: "15",
} as unknown as SharedTypes.Result<SharedTypes.Config.Mode>;

const run = pb.checkAndUpdatePb(userPbs, undefined, result);

expect(run.isPb).toBe(true);
expect(run.personalBests?.["time"]?.["15"]?.[0]).not.toBe(undefined);
});
it("should not override default pb when saving numbers test", () => {
const userPbs: SharedTypes.PersonalBests = {
time: {
"15": [
{
acc: 100,
consistency: 100,
difficulty: "normal",
lazyMode: false,
language: "english",
numbers: false,
punctuation: false,
raw: 100,
timestamp: 0,
wpm: 100,
},
],
},
words: {},
custom: {},
quote: {},
zen: {},
};
const result = {
difficulty: "normal",
language: "english",
punctuation: false,
lazyMode: false,
acc: 100,
consistency: 100,
rawWpm: 100,
wpm: 110,
numbers: true,
mode: "time",
mode2: "15",
} as unknown as SharedTypes.Result<SharedTypes.Config.Mode>;

const run = pb.checkAndUpdatePb(userPbs, undefined, result);

expect(run.isPb).toBe(true);

expect(run.personalBests?.["time"]?.["15"]).toEqual(
expect.arrayContaining([
expect.objectContaining({ numbers: false, wpm: 100 }),
expect.objectContaining({ numbers: true, wpm: 110 }),
])
);
});
});
});
29 changes: 21 additions & 8 deletions backend/src/utils/pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,28 @@ 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)");
}

const sameLazyMode =
result.lazyMode === personalBest.lazyMode ||
(!result.lazyMode && !personalBest.lazyMode);
const samePunctuation = result.punctuation === personalBest.punctuation;
(result.lazyMode ?? false) === (personalBest.lazyMode ?? false);
const samePunctuation =
(result.punctuation ?? false) === (personalBest.punctuation ?? false);
const sameDifficulty = result.difficulty === personalBest.difficulty;
const sameLanguage = result.language === personalBest.language;

return sameLazyMode && samePunctuation && sameDifficulty && sameLanguage;
const sameNumbers =
(result.numbers ?? false) === (personalBest.numbers ?? false);

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

function updatePersonalBest(
Expand All @@ -104,7 +113,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 @@ -117,6 +127,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 @@ -131,7 +142,8 @@ function buildPersonalBest(result: Result): SharedTypes.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 @@ -144,6 +156,7 @@ function buildPersonalBest(result: Result): SharedTypes.PersonalBest {
punctuation: result.punctuation,
raw: result.rawWpm,
wpm: result.wpm,
numbers: result.numbers,
timestamp: Date.now(),
};
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/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
2 changes: 1 addition & 1 deletion frontend/src/styles/popups.scss
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@
.title {
color: var(--text-color);
}
min-width: 50rem;
width: 100%;
max-height: calc(100vh - 10rem);
overflow-y: scroll;
table {
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/ts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ export async function saveLocalPB<M extends SharedTypes.Config.Mode>(
mode: M,
mode2: SharedTypes.Config.Mode2<M>,
punctuation: boolean,
numbers: boolean,
language: string,
difficulty: SharedTypes.Config.Difficulty,
lazyMode: boolean,
Expand Down Expand Up @@ -683,10 +684,11 @@ export async function saveLocalPB<M extends SharedTypes.Config.Mode>(
] as unknown as SharedTypes.PersonalBest[]
).forEach((pb) => {
if (
pb.punctuation === punctuation &&
(pb.punctuation ?? false) === punctuation &&
(pb.numbers ?? false) === numbers &&
pb.difficulty === difficulty &&
pb.language === language &&
(pb.lazyMode === lazyMode || (pb.lazyMode === undefined && !lazyMode))
(pb.lazyMode ?? false) === lazyMode
) {
found = true;
pb.wpm = wpm;
Expand All @@ -713,6 +715,7 @@ export async function saveLocalPB<M extends SharedTypes.Config.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 @@ -73,6 +73,7 @@ function update(mode: SharedTypes.Config.Mode): void {
<td>${pb.difficulty}</td>
<td>${pb.language ? getLanguageDisplayString(pb.language) : "-"}</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 @@ -1250,6 +1250,7 @@ async function saveResult(
Config.mode,
completedEvent.mode2,
Config.punctuation,
Config.numbers,
Config.language,
Config.difficulty,
Config.lazyMode,
Expand Down
7 changes: 4 additions & 3 deletions shared-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ declare namespace SharedTypes {

interface PersonalBest {
acc: number;
consistency: number;
consistency?: number;
difficulty: SharedTypes.Config.Difficulty;
lazyMode: boolean;
lazyMode?: boolean;
language: string;
punctuation: boolean;
punctuation?: boolean;
numbers?: boolean;
raw: number;
wpm: number;
timestamp: number;
Expand Down

0 comments on commit 1429c2c

Please sign in to comment.