mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-12-11 22:06:41 +08:00
moved bananas into userdao
This commit is contained in:
parent
7d2b6e1cac
commit
35262ba080
2 changed files with 25 additions and 35 deletions
|
|
@ -206,6 +206,31 @@ class UsersDAO {
|
|||
.collection("users")
|
||||
.updateOne({ uid }, { $set: { discordId: null } });
|
||||
}
|
||||
|
||||
static async incrementBananas(uid, wpm){
|
||||
const user = await mongoDB().collection("users").findOne({ uid });
|
||||
if (!user) throw new MonkeyError(404, "User not found");
|
||||
|
||||
let best60;
|
||||
try{
|
||||
best60 = Math.max(...user.personalBests.time[60].map((best) => best.wpm));
|
||||
}catch(e){
|
||||
best60 = undefined;
|
||||
}
|
||||
|
||||
if(best60 === undefined || wpm >= (best60 - best60 * 0.25)){
|
||||
//increment when no record found or wpm is within 25% of the record
|
||||
return await mongoDB()
|
||||
.collection("users")
|
||||
.updateOne(
|
||||
{ uid },
|
||||
{ $inc: { bananas: 1 } }
|
||||
);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = UsersDAO;
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
|
||||
function incrementT60Bananas(uid, result, userData) {
|
||||
try {
|
||||
let best60;
|
||||
try {
|
||||
best60 = Math.max(
|
||||
...userData.personalBests.time[60].map((best) => best.wpm)
|
||||
);
|
||||
if (!Number.isFinite(best60)) {
|
||||
throw "Not finite";
|
||||
}
|
||||
} catch (e) {
|
||||
best60 = undefined;
|
||||
}
|
||||
|
||||
if (best60 != undefined && result.wpm < best60 - best60 * 0.25) {
|
||||
// console.log("returning");
|
||||
} else {
|
||||
//increment
|
||||
// console.log("checking");
|
||||
User.findOne({ uid: uid }, (err, user) => {
|
||||
if (user.bananas === undefined) {
|
||||
user.bananas.t60bananas = 1;
|
||||
} else {
|
||||
user.bananas.t60bananas += 1;
|
||||
}
|
||||
user.save();
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(
|
||||
"something went wrong when trying to increment bananas " + e.message
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue