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-07-03 08:35:45 +08:00
|
|
|
let user = firebase.auth().currentUser;
|
|
|
|
if (user == null) return false;
|
|
|
|
let snap = {
|
2020-08-19 05:56:21 +08:00
|
|
|
results: undefined,
|
2020-07-03 08:35:45 +08:00
|
|
|
personalBests: {},
|
|
|
|
tags: [],
|
2020-08-19 23:21:02 +08:00
|
|
|
favouriteThemes: [],
|
2020-09-17 06:15:55 +08:00
|
|
|
lbMemory: {
|
|
|
|
time15: {
|
|
|
|
global: null,
|
|
|
|
daily: null
|
|
|
|
},
|
|
|
|
time60: {
|
|
|
|
global: null,
|
|
|
|
daily: null
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 08:35:45 +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());
|
|
|
|
// })
|
|
|
|
// })
|
2020-08-09 02:36:41 +08:00
|
|
|
try {
|
2020-08-19 05:56:21 +08:00
|
|
|
// await db
|
|
|
|
// .collection(`users/${user.uid}/results/`)
|
|
|
|
// .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);
|
|
|
|
// });
|
|
|
|
// })
|
|
|
|
// .catch((e) => {
|
|
|
|
// throw e;
|
|
|
|
// });
|
2020-08-09 02:36:41 +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);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
throw e;
|
2020-07-03 08:35:45 +08:00
|
|
|
});
|
2020-08-09 02:36:41 +08:00
|
|
|
await db
|
|
|
|
.collection("users")
|
|
|
|
.doc(user.uid)
|
|
|
|
.get()
|
|
|
|
.then((res) => {
|
|
|
|
// console.log('getting data from db!');
|
|
|
|
let data = res.data();
|
|
|
|
try {
|
|
|
|
if (data.personalBests !== undefined) {
|
|
|
|
snap.personalBests = data.personalBests;
|
|
|
|
}
|
|
|
|
snap.discordId = data.discordId;
|
2020-09-23 23:07:48 +08:00
|
|
|
snap.pairingCode = data.discordPairingCode == null ? undefined : data.discordPairingCode;
|
2020-08-09 02:36:41 +08:00
|
|
|
snap.config = data.config;
|
2020-08-19 23:21:02 +08:00
|
|
|
snap.favouriteThemes =
|
|
|
|
data.favouriteThemes === undefined ? [] : data.favouriteThemes;
|
2020-09-06 09:31:54 +08:00
|
|
|
snap.globalStats = {
|
|
|
|
time: data.timeTyping,
|
|
|
|
started: data.startedTests,
|
|
|
|
completed: data.completedTests
|
|
|
|
}
|
2020-09-17 06:15:55 +08:00
|
|
|
if (data.lbMemory !== undefined) {
|
|
|
|
snap.lbMemory = data.lbMemory;
|
|
|
|
}
|
2020-08-09 02:36:41 +08:00
|
|
|
} catch (e) {
|
2020-10-07 00:10:24 +08:00
|
|
|
throw e;
|
2020-06-09 02:51:50 +08:00
|
|
|
}
|
2020-08-09 02:36:41 +08:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
throw e;
|
|
|
|
});
|
2020-10-07 00:10:24 +08:00
|
|
|
dbSnapshot = snap;
|
2020-08-09 02:36:41 +08:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2020-07-03 08:35:45 +08:00
|
|
|
return dbSnapshot;
|
2020-05-10 06:33:48 +08:00
|
|
|
}
|
|
|
|
|
2020-08-19 05:56:21 +08:00
|
|
|
async function db_getUserResults() {
|
|
|
|
let user = firebase.auth().currentUser;
|
|
|
|
if (user == null) return false;
|
2020-08-24 23:37:26 +08:00
|
|
|
if (dbSnapshot === null) return false;
|
2020-08-19 05:56:21 +08:00
|
|
|
if (dbSnapshot.results !== undefined) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
return await db
|
|
|
|
.collection(`users/${user.uid}/results/`)
|
|
|
|
.orderBy("timestamp", "desc")
|
2020-09-06 09:31:54 +08:00
|
|
|
.limit(1000)
|
2020-08-19 05:56:21 +08:00
|
|
|
.get()
|
|
|
|
.then((data) => {
|
|
|
|
dbSnapshot.results = [];
|
|
|
|
let len = data.docs.length;
|
|
|
|
data.docs.forEach((doc, index) => {
|
|
|
|
let result = doc.data();
|
|
|
|
result.id = doc.id;
|
|
|
|
dbSnapshot.results.push(result);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 08:35:45 +08:00
|
|
|
async function db_getUserHighestWpm(
|
|
|
|
mode,
|
|
|
|
mode2,
|
|
|
|
punctuation,
|
|
|
|
language,
|
|
|
|
difficulty
|
|
|
|
) {
|
|
|
|
function cont() {
|
|
|
|
let topWpm = 0;
|
|
|
|
dbSnapshot.results.forEach((result) => {
|
|
|
|
if (
|
|
|
|
result.mode == mode &&
|
|
|
|
result.mode2 == mode2 &&
|
|
|
|
result.punctuation == punctuation &&
|
|
|
|
result.language == language &&
|
|
|
|
result.difficulty == difficulty
|
|
|
|
) {
|
|
|
|
if (result.wpm > topWpm) {
|
|
|
|
topWpm = result.wpm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return topWpm;
|
|
|
|
}
|
2020-05-12 07:59:12 +08:00
|
|
|
|
2020-07-03 08:35:45 +08:00
|
|
|
let retval;
|
2020-08-19 06:00:39 +08:00
|
|
|
if (dbSnapshot == null || dbSnapshot.results === undefined) {
|
2020-07-03 08:35:45 +08:00
|
|
|
// await db_getUserResults().then(data => {
|
|
|
|
// retval = cont();
|
|
|
|
// });
|
2020-08-19 06:00:39 +08:00
|
|
|
retval = 0;
|
2020-07-03 08:35:45 +08:00
|
|
|
} else {
|
|
|
|
retval = cont();
|
|
|
|
}
|
|
|
|
return retval;
|
2020-05-12 07:59:12 +08:00
|
|
|
}
|
2020-06-09 02:51:50 +08:00
|
|
|
|
2020-07-03 08:35:45 +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;
|
2020-06-09 02:51:50 +08:00
|
|
|
}
|
2020-07-03 08:35:45 +08:00
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
} catch (e) {
|
|
|
|
return ret;
|
2020-06-09 02:51:50 +08:00
|
|
|
}
|
2020-07-03 08:35:45 +08:00
|
|
|
}
|
2020-06-09 02:51:50 +08:00
|
|
|
|
2020-07-03 08:35:45 +08:00
|
|
|
let retval;
|
|
|
|
if (dbSnapshot == null) {
|
|
|
|
// await db_getUserResults().then(data => {
|
|
|
|
// retval = cont();
|
|
|
|
// });
|
|
|
|
} else {
|
|
|
|
retval = cont();
|
|
|
|
}
|
|
|
|
return retval;
|
2020-06-09 02:51:50 +08:00
|
|
|
}
|
|
|
|
|
2020-07-03 08:35:45 +08:00
|
|
|
async function db_saveLocalPB(
|
|
|
|
mode,
|
|
|
|
mode2,
|
|
|
|
punctuation,
|
|
|
|
language,
|
|
|
|
difficulty,
|
2020-08-15 23:09:52 +08:00
|
|
|
wpm,
|
|
|
|
acc,
|
|
|
|
raw
|
2020-07-03 08:35:45 +08:00
|
|
|
) {
|
|
|
|
function cont() {
|
|
|
|
try {
|
|
|
|
let found = false;
|
2020-07-08 23:32:22 +08:00
|
|
|
if (dbSnapshot.personalBests[mode][mode2] === undefined) {
|
|
|
|
dbSnapshot.personalBests[mode][mode2] = [];
|
|
|
|
}
|
2020-07-03 08:35:45 +08:00
|
|
|
dbSnapshot.personalBests[mode][mode2].forEach((pb) => {
|
|
|
|
if (
|
|
|
|
pb.punctuation == punctuation &&
|
|
|
|
pb.difficulty == difficulty &&
|
|
|
|
pb.language == language
|
|
|
|
) {
|
|
|
|
found = true;
|
|
|
|
pb.wpm = wpm;
|
2020-08-15 23:09:52 +08:00
|
|
|
pb.acc = acc;
|
|
|
|
pb.raw = raw;
|
2020-06-09 02:51:50 +08:00
|
|
|
}
|
2020-07-03 08:35:45 +08:00
|
|
|
});
|
|
|
|
if (!found) {
|
|
|
|
//nothing found
|
|
|
|
dbSnapshot.personalBests[mode][mode2].push({
|
|
|
|
language: language,
|
|
|
|
difficulty: difficulty,
|
|
|
|
punctuation: punctuation,
|
|
|
|
wpm: wpm,
|
2020-08-15 23:09:52 +08:00
|
|
|
acc: acc,
|
|
|
|
raw: raw,
|
2020-07-03 08:35:45 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
//that mode or mode2 is not found
|
|
|
|
dbSnapshot.personalBests[mode] = {};
|
|
|
|
dbSnapshot.personalBests[mode][mode2] = [
|
|
|
|
{
|
|
|
|
language: language,
|
|
|
|
difficulty: difficulty,
|
|
|
|
punctuation: punctuation,
|
|
|
|
wpm: wpm,
|
2020-08-15 23:09:52 +08:00
|
|
|
acc: acc,
|
|
|
|
raw: raw,
|
2020-07-03 08:35:45 +08:00
|
|
|
},
|
|
|
|
];
|
2020-06-09 02:51:50 +08:00
|
|
|
}
|
2020-07-03 08:35:45 +08:00
|
|
|
}
|
2020-06-09 02:51:50 +08:00
|
|
|
|
2020-07-03 08:35:45 +08:00
|
|
|
let retval;
|
|
|
|
if (dbSnapshot == null) {
|
|
|
|
// await db_getUserResults().then(data => {
|
|
|
|
// retval = cont();
|
|
|
|
// });
|
|
|
|
} else {
|
|
|
|
cont();
|
|
|
|
}
|
|
|
|
}
|