Skip to content

Commit

Permalink
fix: personal bests not shown correctly on user tags (fehmer) (#5119)
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmer authored Feb 26, 2024
1 parent f15bc39 commit 2ff0c60
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 51 deletions.
93 changes: 42 additions & 51 deletions frontend/src/ts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,10 @@ export async function initSnapshot(): Promise<
// snap.tags = userDataTags;

snap.tags =
userData.tags?.map((tag) => {
const newTag = {
...tag,
display: tag.name.replaceAll("_", " "),
personalBests: {
time: {},
words: {},
quote: {},
zen: {},
custom: {},
},
};
for (const mode of ["time", "words", "quote", "zen", "custom"]) {
newTag.personalBests[mode as keyof SharedTypes.PersonalBests] ??= {};
}
return newTag;
}) ?? [];
userData.tags?.map((tag) => ({
...tag,
display: tag.name.replaceAll("_", " "),
})) ?? [];

snap.tags = snap.tags?.sort((a, b) => {
if (a.name > b.name) {
Expand Down Expand Up @@ -600,6 +587,7 @@ export async function getLocalPB<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 All @@ -625,6 +613,7 @@ export async function getLocalPB<M extends SharedTypes.Config.Mode>(
).forEach((pb) => {
if (
pb.punctuation === punctuation &&
pb.numbers === numbers &&
pb.difficulty === difficulty &&
pb.language === language &&
(pb.lazyMode === lazyMode || (pb.lazyMode === undefined && !lazyMode))
Expand Down Expand Up @@ -710,12 +699,12 @@ export async function saveLocalPB<M extends SharedTypes.Config.Mode>(
difficulty,
lazyMode,
punctuation,
numbers,
wpm,
acc,
raw,
timestamp: Date.now(),
consistency,
numbers,
});
}
}
Expand All @@ -730,59 +719,58 @@ export async function getLocalTagPB<M extends SharedTypes.Config.Mode>(
mode: M,
mode2: SharedTypes.Config.Mode2<M>,
punctuation: boolean,
numbers: boolean,
language: string,
difficulty: SharedTypes.Config.Difficulty,
lazyMode: boolean
): Promise<number> {
function cont(): number {
let ret = 0;
if (dbSnapshot === null) return 0;

const filteredtag = (getSnapshot()?.tags ?? []).filter(
(t) => t._id === tagId
)[0];
let ret = 0;

if (filteredtag === undefined) return ret;

filteredtag.personalBests ??= {
time: {},
words: {},
quote: {},
zen: {},
custom: {},
};
const filteredtag = (getSnapshot()?.tags ?? []).filter(
(t) => t._id === tagId
)[0];

filteredtag.personalBests[mode] ??= {
[mode2]: [],
};
if (filteredtag === undefined) return ret;

filteredtag.personalBests[mode][mode2] ??=
[] as unknown as SharedTypes.PersonalBests[M][SharedTypes.Config.Mode2<M>];
filteredtag.personalBests ??= {
time: {},
words: {},
quote: {},
zen: {},
custom: {},
};

const personalBests = (filteredtag.personalBests[mode][mode2] ??
[]) as SharedTypes.PersonalBest[];
filteredtag.personalBests[mode] ??= {
[mode2]: [],
};

ret =
personalBests.find(
(pb) =>
pb.punctuation === punctuation &&
pb.difficulty === difficulty &&
pb.language === language &&
(pb.lazyMode === lazyMode || (pb.lazyMode === undefined && !lazyMode))
)?.wpm ?? 0;
filteredtag.personalBests[mode][mode2] ??=
[] as unknown as SharedTypes.PersonalBests[M][SharedTypes.Config.Mode2<M>];

return ret;
}
const personalBests = (filteredtag.personalBests[mode][mode2] ??
[]) as SharedTypes.PersonalBest[];

const retval = dbSnapshot === null ? 0 : cont();
ret =
personalBests.find(
(pb) =>
pb.punctuation === punctuation &&
pb.numbers === numbers &&
pb.difficulty === difficulty &&
pb.language === language &&
(pb.lazyMode === lazyMode || (pb.lazyMode === undefined && !lazyMode))
)?.wpm ?? 0;

return retval;
return ret;
}

export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>(
tagId: string,
mode: M,
mode2: SharedTypes.Config.Mode2<M>,
punctuation: boolean,
numbers: boolean,
language: string,
difficulty: SharedTypes.Config.Difficulty,
lazyMode: boolean,
Expand Down Expand Up @@ -823,6 +811,7 @@ export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>(
).forEach((pb) => {
if (
pb.punctuation === punctuation &&
pb.numbers === numbers &&
pb.difficulty === difficulty &&
pb.language === language &&
(pb.lazyMode === lazyMode || (pb.lazyMode === undefined && !lazyMode))
Expand All @@ -847,6 +836,7 @@ export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>(
difficulty,
lazyMode,
punctuation,
numbers,
wpm,
acc,
raw,
Expand All @@ -869,6 +859,7 @@ export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>(
difficulty: difficulty,
lazyMode: lazyMode,
punctuation: punctuation,
numbers: numbers,
wpm: wpm,
acc: acc,
raw: raw,
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/ts/test/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export async function updateGraphPBLine(): Promise<void> {
result.mode,
result.mode2,
result.punctuation ?? false,
result.numbers ?? false,
result.language,
result.difficulty,
result.lazyMode ?? false,
Expand Down Expand Up @@ -404,6 +405,7 @@ export async function updateCrown(): Promise<void> {
Config.mode,
result.mode2,
Config.punctuation,
Config.numbers,
Config.language,
Config.difficulty,
Config.lazyMode,
Expand Down Expand Up @@ -460,6 +462,7 @@ async function updateTags(dontSave: boolean): Promise<void> {
Config.mode,
result.mode2,
Config.punctuation,
Config.numbers,
Config.language,
Config.difficulty,
Config.lazyMode
Expand All @@ -480,6 +483,7 @@ async function updateTags(dontSave: boolean): Promise<void> {
Config.mode,
result.mode2,
Config.punctuation,
Config.numbers,
Config.language,
Config.difficulty,
Config.lazyMode,
Expand Down

0 comments on commit 2ff0c60

Please sign in to comment.