2020-05-10 06:33:48 +08:00
|
|
|
const db = firebase.firestore();
|
|
|
|
|
2020-05-12 07:59:12 +08:00
|
|
|
let dbSnapshot = null;
|
2020-05-10 06:33:48 +08:00
|
|
|
|
2020-05-12 07:59:12 +08:00
|
|
|
function db_testCompleted(obj) {
|
|
|
|
|
2020-05-29 03:49:50 +08:00
|
|
|
if (obj.wpm == 0 || obj.wpm > 350 || obj.acc < 50 || obj.acc > 100) return false;
|
2020-05-10 06:33:48 +08:00
|
|
|
|
|
|
|
let uid = "";
|
|
|
|
let user = firebase.auth().currentUser;
|
|
|
|
if (user) {
|
|
|
|
uid = user.uid;
|
|
|
|
}
|
2020-06-01 04:18:34 +08:00
|
|
|
try{
|
2020-06-06 01:17:59 +08:00
|
|
|
db.collection(`users/${uid}/results`).add(obj);
|
|
|
|
// db.collection(`results`).add(obj);
|
2020-06-01 04:18:34 +08:00
|
|
|
}catch(e){
|
|
|
|
showNotification("Error saving result! Please contact Miodec on Discord.",5000);
|
|
|
|
}
|
2020-05-10 06:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function db_getUserResults() {
|
|
|
|
let user = firebase.auth().currentUser;
|
|
|
|
if (user == null) return false;
|
|
|
|
let ret = [];
|
2020-06-06 01:17:59 +08:00
|
|
|
// await db.collection('results')
|
|
|
|
// .orderBy('timestamp', 'desc')
|
|
|
|
// .where('uid', '==', user.uid)
|
|
|
|
// .get()
|
|
|
|
// .then(data => {
|
|
|
|
// // console.log('getting data from db!');
|
|
|
|
// data.docs.forEach(doc => {
|
|
|
|
// ret.push(doc.data());
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
await db.collection(`users/${user.uid}/results/`)
|
2020-05-12 07:59:12 +08:00
|
|
|
.orderBy('timestamp', 'desc')
|
|
|
|
.get()
|
|
|
|
.then(data => {
|
2020-05-15 09:58:10 +08:00
|
|
|
// console.log('getting data from db!');
|
2020-05-12 07:59:12 +08:00
|
|
|
data.docs.forEach(doc => {
|
|
|
|
ret.push(doc.data());
|
|
|
|
})
|
2020-05-10 06:33:48 +08:00
|
|
|
})
|
2020-05-12 07:59:12 +08:00
|
|
|
dbSnapshot = ret;
|
2020-05-10 06:33:48 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-29 01:41:01 +08:00
|
|
|
async function db_getUserHighestWpm(mode, mode2, punctuation, language, difficulty) {
|
2020-05-12 07:59:12 +08:00
|
|
|
|
|
|
|
function cont() {
|
|
|
|
let topWpm = 0;
|
|
|
|
dbSnapshot.forEach(result => {
|
2020-05-29 01:41:01 +08:00
|
|
|
if (result.mode == mode && result.mode2 == mode2 && result.punctuation == punctuation && result.language == language && result.difficulty == difficulty) {
|
2020-05-12 07:59:12 +08:00
|
|
|
if (result.wpm > topWpm) {
|
|
|
|
topWpm = result.wpm;
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 06:33:48 +08:00
|
|
|
})
|
2020-05-12 07:59:12 +08:00
|
|
|
return topWpm;
|
|
|
|
}
|
2020-05-10 06:33:48 +08:00
|
|
|
|
2020-05-12 07:59:12 +08:00
|
|
|
let retval;
|
|
|
|
if (dbSnapshot == null) {
|
|
|
|
await db_getUserResults().then(data => {
|
|
|
|
retval = cont();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
retval = cont();
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|