diff --git a/backend/handlers/pb.js b/backend/handlers/pb.js index b28c1ff58..d3ae2db3e 100644 --- a/backend/handlers/pb.js +++ b/backend/handlers/pb.js @@ -114,7 +114,7 @@ module.exports = { //updating lbpersonalbests object //verify structure first if (lbObj[mode] === undefined) lbObj[mode] = {}; - if (lbObj[mode][mode2] === undefined) lbObj[mode][mode2] = {}; + if (lbObj[mode][mode2] === undefined) lbObj[mode][mode2] = []; let bestForEveryLanguage = {}; if (obj?.[mode]?.[mode2]) { diff --git a/src/js/db.js b/src/js/db.js index bb15a8a79..a18089e76 100644 --- a/src/js/db.js +++ b/src/js/db.js @@ -342,59 +342,43 @@ export async function saveLocalPB( ) { if (mode == "quote") return; function cont() { - try { - let found = false; - if (dbSnapshot.personalBests[mode][mode2] === undefined) { - dbSnapshot.personalBests[mode][mode2] = []; - } - dbSnapshot.personalBests[mode][mode2].forEach((pb) => { - if ( - pb.punctuation == punctuation && - pb.difficulty == difficulty && - pb.language == language && - (pb.lazyMode === lazyMode || - (pb.lazyMode === undefined && lazyMode === false)) - ) { - found = true; - pb.wpm = wpm; - pb.acc = acc; - pb.raw = raw; - pb.timestamp = Date.now(); - pb.consistency = consistency; - pb.lazyMode = lazyMode; - } - }); - if (!found) { - //nothing found - dbSnapshot.personalBests[mode][mode2].push({ - language: language, - difficulty: difficulty, - lazyMode: lazyMode, - punctuation: punctuation, - wpm: wpm, - acc: acc, - raw: raw, - timestamp: Date.now(), - consistency: consistency, - }); - } - } catch (e) { - //that mode or mode2 is not found - dbSnapshot.personalBests = {}; + let found = false; + if (dbSnapshot.personalBests === undefined) dbSnapshot.personalBests = {}; + if (dbSnapshot.personalBests[mode] === undefined) dbSnapshot.personalBests[mode] = {}; - dbSnapshot.personalBests[mode][mode2] = [ - { - language: language, - difficulty: difficulty, - lazyMode: lazyMode, - punctuation: punctuation, - wpm: wpm, - acc: acc, - raw: raw, - timestamp: Date.now(), - consistency: consistency, - }, - ]; + if (dbSnapshot.personalBests[mode][mode2] === undefined) + dbSnapshot.personalBests[mode][mode2] = []; + + dbSnapshot.personalBests[mode][mode2].forEach((pb) => { + if ( + pb.punctuation == punctuation && + pb.difficulty == difficulty && + pb.language == language && + (pb.lazyMode === lazyMode || + (pb.lazyMode === undefined && lazyMode === false)) + ) { + found = true; + pb.wpm = wpm; + pb.acc = acc; + pb.raw = raw; + pb.timestamp = Date.now(); + pb.consistency = consistency; + pb.lazyMode = lazyMode; + } + }); + if (!found) { + //nothing found + dbSnapshot.personalBests[mode][mode2].push({ + language: language, + difficulty: difficulty, + lazyMode: lazyMode, + punctuation: punctuation, + wpm: wpm, + acc: acc, + raw: raw, + timestamp: Date.now(), + consistency: consistency, + }); } }