monkeytype/backend/handlers/pb.js

105 lines
1.6 KiB
JavaScript
Raw Normal View History

/*
obj structure
2021-06-07 21:15:19 +08:00
time: {
10: [ - this is a list because there can be
different personal bests for different difficulties, languages and punctuation
{
acc,
consistency,
difficulty,
language,
punctuation,
raw,
timestamp,
wpm
2021-06-07 21:15:19 +08:00
}
]
},
words: {
10: [
{}
]
},
zen: {
zen: [
{}
]
},
custom: {
custom: {
[]
}
}
2021-06-07 21:15:19 +08:00
*/
module.exports = {
checkAndUpdatePb(
obj,
mode,
mode2,
acc,
consistency,
difficulty,
language,
punctuation,
raw,
wpm
) {
//verify structure first
if (obj === undefined) obj = {};
if (obj[mode] === undefined) obj[mode] = {};
if (obj[mode][mode2] === undefined) obj[mode][mode2] = [];
let isPb = false;
2021-06-07 21:15:19 +08:00
let found = false;
//find a pb
obj[mode][mode2].forEach((pb) => {
//check if we should compare first
if (
pb.difficulty === difficulty &&
pb.language === language &&
pb.punctuation === punctuation
) {
found = true;
//compare
if (pb.wpm < wpm) {
//update
isPb = true;
pb.acc = acc;
pb.consistency = consistency;
pb.raw = raw;
pb.wpm = wpm;
pb.timestamp = Date.now();
2021-06-07 21:15:19 +08:00
}
}
});
//if not found push a new one
if (!found) {
isPb = true;
obj[mode][mode2].push({
acc,
consistency,
difficulty,
language,
punctuation,
raw,
wpm,
timestamp: Date.now(),
});
2021-06-07 21:15:19 +08:00
}
return {
isPb,
obj,
};
},
};