monkeytype/backend/handlers/pb.js
Bruce Berrios f9d6f52c15
Api overhaul (#2555) by Bruception
* Feat:Update response structure (#2427)

* Fix:response and error structure

* update:response message

* update:response class

* update

* Update response message

Co-authored-by: Mustafiz Kaifee Mumtaz <mustafiz.mumtaz@freecharge.com>

* Add MonkeyToken foundation (#2487) by Bruception

* Api changes (#2492)

* API changes

* Remove unused import

* Add Ape client (#2513)

* Add all endpoints (#2514)

* Merged backend typescript into api overhaul (#2515)

* Install typescript and add backend tsconfig

Cannot yet build due to a number of compilation errors in JS code

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Fix typescript compilation errors

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Migrated backend to ES modules

Switched to import export syntax

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Add typescript declaration for anticheat

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Rename top level files to .ts

Fix service account json file typing

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Add dev build scripts for backend typescript

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Removed empty lines and switched to using db

Cleaned up imports by removing needless empty lines and migrated to the new db.js instead of mongodb.js.

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Fixed backend commonjs syntax to ES module syntax

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Add build to backend start script

Signed-off-by: Brian Evans <ebrian101@gmail.com>

* Migrate some endpoints to Ape

* Strict equals

* Remove artifact

* ape -> Ape

* Ape migration p2 (#2522)

* Migrate leaderboard endpoints to ape

* Fixed comment

* Init backend types

* Fail

* Return

* Migrate Quotes to Ape (#2528)

* Migrate quotes to Ape

* Fix backend response

* Fix issue

* Fix rate limit (#2533)

* fix rate limit

* Fix import

* Fix issues

* Ape migration p4 (#2547)

* Migrate results endpoints to ape

* Remove unused import

* Remove unused import

* Fix loaders

* Make function async

* Hide try saving results

* Migrate some users endpoints to Ape (#2548)

* Complete Ape Migration (#2553)

* Complete ape migration

* Fix preset

* Return preset data

* Add typings

* Move captcha reset

* Read from params

* Fix result tags endpoint

* Fix stuck loader

* fixed lb memory not saving

* fixed quote rating popup not showing up for new users

Co-authored-by: Mustafiz Kaifee <49086821+Mustafiz04@users.noreply.github.com>
Co-authored-by: Mustafiz Kaifee Mumtaz <mustafiz.mumtaz@freecharge.com>
Co-authored-by: Brian Evans <53117772+mrbrianevans@users.noreply.github.com>
Co-authored-by: Miodec <bartnikjack@gmail.com>
2022-02-22 20:55:48 +01:00

149 lines
2.8 KiB
JavaScript

/*
obj structure
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
}
]
},
words: {
10: [
{}
]
},
zen: {
zen: [
{}
]
},
custom: {
custom: {
[]
}
}
*/
export function checkAndUpdatePb(
obj,
lbObj,
mode,
mode2,
acc,
consistency,
difficulty,
lazyMode = false,
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;
let found = false;
//find a pb
obj[mode][mode2].forEach((pb) => {
//check if we should compare first
if (
(pb.lazyMode === lazyMode ||
(pb.lazyMode === undefined && lazyMode === false)) &&
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.difficulty = difficulty;
pb.language = language;
pb.punctuation = punctuation;
pb.lazyMode = lazyMode;
pb.raw = raw;
pb.wpm = wpm;
pb.timestamp = Date.now();
}
}
});
//if not found push a new one
if (!found) {
isPb = true;
obj[mode][mode2].push({
acc,
consistency,
difficulty,
lazyMode,
language,
punctuation,
raw,
wpm,
timestamp: Date.now(),
});
}
if (
lbObj &&
mode === "time" &&
(mode2 == "15" || mode2 == "60") &&
!lazyMode
) {
//updating lbpersonalbests object
//verify structure first
if (lbObj[mode] === undefined) lbObj[mode] = {};
if (lbObj[mode][mode2] === undefined || Array.isArray(lbObj[mode][mode2]))
lbObj[mode][mode2] = {};
let bestForEveryLanguage = {};
if (obj?.[mode]?.[mode2]) {
obj[mode][mode2].forEach((pb) => {
if (!bestForEveryLanguage[pb.language]) {
bestForEveryLanguage[pb.language] = pb;
} else {
if (bestForEveryLanguage[pb.language].wpm < pb.wpm) {
bestForEveryLanguage[pb.language] = pb;
}
}
});
Object.keys(bestForEveryLanguage).forEach((key) => {
if (lbObj[mode][mode2][key] === undefined) {
lbObj[mode][mode2][key] = bestForEveryLanguage[key];
} else {
if (lbObj[mode][mode2][key].wpm < bestForEveryLanguage[key].wpm) {
lbObj[mode][mode2][key] = bestForEveryLanguage[key];
}
}
});
bestForEveryLanguage = {};
}
}
return {
isPb,
obj,
lbObj,
};
}