added local pb saving and reading to speed everything up

also checking if pbs are in sync to avoid misinforming the user
This commit is contained in:
Jack 2020-06-08 19:51:50 +01:00
parent 47f3c67769
commit 97b2d4ae43
2 changed files with 96 additions and 22 deletions

View file

@ -35,7 +35,11 @@ async function db_getUserSnapshot() {
.get()
.then(data => {
// console.log('getting data from db!');
snap.personalBests = data.data().personalBests;
try{
snap.personalBests = data.data().personalBests;
}catch(e){
//
}
})
dbSnapshot = snap;
return dbSnapshot;
@ -66,3 +70,78 @@ async function db_getUserHighestWpm(mode, mode2, punctuation, language, difficul
return retval;
}
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();
}
}

View file

@ -669,18 +669,17 @@ function showResult(difficultyFailed = false) {
if (stats.wpm > 0 && stats.wpm < 350 && stats.acc > 50 && stats.acc <= 100) {
if (firebase.auth().currentUser != null) {
completedEvent.uid = firebase.auth().currentUser.uid;
// db_getUserHighestWpm(config.mode, mode2, config.punctuation, config.language, config.difficulty).then(data => {
// if(data < stats.wpm){
// hideCrown();
// showCrown();
// }else{
// wpmOverTimeChart.options.annotation.annotations[0].value = data;
// wpmOverTimeChart.update();
// }
// })
//check local pb
let localPb = false;
db_getLocalPB(config.mode,mode2,config.punctuation,config.language,config.difficulty).then(d => {
if(d < stats.wpm){
//new pb based on local
hideCrown();
showCrown();
localPb = true;
}
})
testCompleted({uid:firebase.auth().currentUser.uid,obj:completedEvent}).then(e => {
// showNotification('done');
@ -693,20 +692,16 @@ function showResult(difficultyFailed = false) {
}catch(e){
console.log("Analytics unavailable");
}
if(e.data === 2){
//new pb
// showNotification('pb',1000);
hideCrown();
showCrown();
//save to local pb
if(!localPb){
showNotification('Local PB data is out of sync! Refresh the page to resync it or contact Miodec on Discord.',15000);
}
db_saveLocalPB(config.mode,mode2,config.punctuation,config.language,config.difficulty,stats.wpm);
}else{
// showNotification('nooooo pb',1000);
if(localPb){
showNotification('Local PB data is out of sync! Refresh the page to resync it or contact Miodec on Discord.',15000);
}
}
}