2020-05-10 06:33:48 +08:00
|
|
|
const db = firebase.firestore();
|
|
|
|
|
2020-06-09 03:12:01 +08:00
|
|
|
let dbSnapshot = null;
|
2020-05-10 06:33:48 +08:00
|
|
|
|
|
|
|
|
2020-06-08 09:24:50 +08:00
|
|
|
async function db_getUserSnapshot() {
|
2020-05-10 06:33:48 +08:00
|
|
|
let user = firebase.auth().currentUser;
|
|
|
|
if (user == null) return false;
|
2020-06-08 10:10:02 +08:00
|
|
|
let snap = {
|
|
|
|
results: [],
|
2020-06-12 01:02:04 +08:00
|
|
|
personalBests: {},
|
|
|
|
tags: []
|
2020-06-08 10:10:02 +08:00
|
|
|
};
|
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-06-08 10:10:02 +08:00
|
|
|
.orderBy('timestamp', 'desc')
|
|
|
|
.get()
|
|
|
|
.then(data => {
|
|
|
|
// console.log('getting data from db!');
|
|
|
|
data.docs.forEach(doc => {
|
|
|
|
let result = doc.data();
|
|
|
|
result.id = doc.id;
|
|
|
|
snap.results.push(result);
|
2020-05-10 06:33:48 +08:00
|
|
|
})
|
2020-06-08 10:10:02 +08:00
|
|
|
})
|
2020-06-12 01:02:04 +08:00
|
|
|
await db.collection(`users/${user.uid}/tags/`)
|
|
|
|
.get()
|
|
|
|
.then(data => {
|
|
|
|
// console.log('getting data from db!');
|
|
|
|
data.docs.forEach(doc => {
|
|
|
|
let tag = doc.data();
|
|
|
|
tag.id = doc.id;
|
|
|
|
snap.tags.push(tag);
|
|
|
|
})
|
|
|
|
})
|
2020-06-08 10:10:02 +08:00
|
|
|
await db.collection('users').doc(user.uid)
|
|
|
|
.get()
|
|
|
|
.then(data => {
|
|
|
|
// console.log('getting data from db!');
|
2020-06-09 02:51:50 +08:00
|
|
|
try{
|
|
|
|
snap.personalBests = data.data().personalBests;
|
|
|
|
}catch(e){
|
|
|
|
//
|
|
|
|
}
|
2020-06-08 10:10:02 +08:00
|
|
|
})
|
|
|
|
dbSnapshot = snap;
|
|
|
|
return dbSnapshot;
|
2020-05-10 06:33:48 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2020-06-09 04:13:08 +08:00
|
|
|
dbSnapshot.results.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) {
|
2020-06-08 09:24:50 +08:00
|
|
|
// await db_getUserResults().then(data => {
|
|
|
|
// retval = cont();
|
|
|
|
// });
|
2020-05-12 07:59:12 +08:00
|
|
|
} else {
|
|
|
|
retval = cont();
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
2020-06-09 02:51:50 +08:00
|
|
|
|
|
|
|
async function db_getLocalPB(mode, mode2, punctuation, language, difficulty){
|
|
|
|
|
|
|
|
function cont() {
|
|
|
|
let ret = 0;
|
|
|
|
try{
|
|
|
|
dbSnapshot.personalBests[mode][mode2].forEach(pb => {
|
|
|
|
if( pb.punctuation == punctuation &&
|
|
|
|
pb.difficulty == difficulty &&
|
|
|
|
pb.language == language){
|
|
|
|
ret = pb.wpm;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return ret;
|
|
|
|
}catch(e){
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let retval;
|
|
|
|
if (dbSnapshot == null) {
|
|
|
|
// await db_getUserResults().then(data => {
|
|
|
|
// retval = cont();
|
|
|
|
// });
|
|
|
|
} else {
|
|
|
|
retval = cont();
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async function db_saveLocalPB(mode, mode2, punctuation, language, difficulty, wpm){
|
|
|
|
|
|
|
|
function cont() {
|
|
|
|
try{
|
|
|
|
let found = false;
|
|
|
|
dbSnapshot.personalBests[mode][mode2].forEach(pb => {
|
|
|
|
if( pb.punctuation == punctuation &&
|
|
|
|
pb.difficulty == difficulty &&
|
|
|
|
pb.language == language){
|
|
|
|
found = true;
|
|
|
|
pb.wpm = wpm;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if(!found){
|
|
|
|
//nothing found
|
|
|
|
dbSnapshot.personalBests[mode][mode2].push({
|
|
|
|
language: language,
|
|
|
|
difficulty: difficulty,
|
|
|
|
punctuation: punctuation,
|
|
|
|
wpm: wpm
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}catch(e){
|
|
|
|
//that mode or mode2 is not found
|
|
|
|
dbSnapshot.personalBests[mode] = {};
|
|
|
|
dbSnapshot.personalBests[mode][mode2] = [{
|
|
|
|
language: language,
|
|
|
|
difficulty: difficulty,
|
|
|
|
punctuation: punctuation,
|
|
|
|
wpm: wpm
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let retval;
|
|
|
|
if (dbSnapshot == null) {
|
|
|
|
// await db_getUserResults().then(data => {
|
|
|
|
// retval = cont();
|
|
|
|
// });
|
|
|
|
} else {
|
|
|
|
cont();
|
|
|
|
}
|
|
|
|
}
|