From 3b5f5d4b102d83a1c5013880cba25145ce316fbc Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 16 Sep 2020 23:15:55 +0100 Subject: [PATCH] added lbmemory --- functions/index.js | 44 ++++++++++++++++++ public/css/style.scss | 3 ++ public/js/account.js | 5 ++ public/js/db.js | 13 ++++++ public/js/script.js | 103 ++++++++++++++++++++++++++++++++++++------ 5 files changed, 155 insertions(+), 13 deletions(-) diff --git a/functions/index.js b/functions/index.js index a527dcef4..1a9c25305 100644 --- a/functions/index.js +++ b/functions/index.js @@ -1164,6 +1164,50 @@ exports.saveConfig = functions.https.onCall((request, response) => { } }); +exports.saveLbMemory = functions.https.onCall((request, response) => { + try { + if (request.uid === undefined || request.obj === undefined) { + console.error(`error saving lb memory for ${request.uid} - missing input`); + return { + returnCode: -1, + message: "Missing input", + }; + } + + let obj = request.obj; + return db + .collection(`users`) + .doc(request.uid) + .set( + { + lbMemory: obj, + }, + { merge: true } + ) + .then((e) => { + return { + returnCode: 1, + message: "Saved", + }; + }) + .catch((e) => { + console.error( + `error saving lb memory to DB for ${request.uid} - ${e.message}` + ); + return { + returnCode: -1, + message: e.message, + }; + }); + } catch (e) { + console.error(`error saving lb memory for ${request.uid} - ${e}`); + return { + resultCode: -999, + message: e, + }; + } +}); + function generate(n) { var add = 1, max = 12 - add; diff --git a/public/css/style.scss b/public/css/style.scss index 026d922c7..f8fdac359 100644 --- a/public/css/style.scss +++ b/public/css/style.scss @@ -1266,6 +1266,9 @@ key { .bottom { font-size: 1rem; line-height: 1rem; + .lbChange .fas{ + margin-right: 0.15rem; + } } } diff --git a/public/js/account.js b/public/js/account.js index debab1d32..d0862a6f0 100644 --- a/public/js/account.js +++ b/public/js/account.js @@ -2486,3 +2486,8 @@ $("#resultEditTagsPanel .confirmButton").click((f) => { } }); }); + +function updateLbMemory(mode, mode2, type, value) { + dbSnapshot.lbMemory[mode + mode2][type] = value; +} + diff --git a/public/js/db.js b/public/js/db.js index ccbb23e38..122b6b895 100644 --- a/public/js/db.js +++ b/public/js/db.js @@ -10,6 +10,16 @@ async function db_getUserSnapshot() { personalBests: {}, tags: [], favouriteThemes: [], + lbMemory: { + time15: { + global: null, + daily: null + }, + time60: { + global: null, + daily: null + } + } }; // await db.collection('results') // .orderBy('timestamp', 'desc') @@ -72,6 +82,9 @@ async function db_getUserSnapshot() { started: data.startedTests, completed: data.completedTests } + if (data.lbMemory !== undefined) { + snap.lbMemory = data.lbMemory; + } } catch (e) { // } diff --git a/public/js/script.js b/public/js/script.js index 19f815d8e..841211592 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -202,6 +202,7 @@ const saveConfig = firebase.functions().httpsCallable("saveConfig"); const generatePairingCode = firebase .functions() .httpsCallable("generatePairingCode"); +const saveLbMemory = firebase.functions().httpsCallable("saveLbMemory"); function refreshThemeColorObject() { let st = getComputedStyle(document.body); @@ -1980,42 +1981,118 @@ function showResult(difficultyFailed = false) { console.log("Analytics unavailable"); } + const lbUpIcon = ``; + const lbDownIcon = ``; + const lbRightIcon = ``; + //global let globalLbString = ""; - if (e.data.globalLeaderboard === null) { + const glb = e.data.globalLeaderboard; + const glbMemory = + dbSnapshot.lbMemory[config.mode + mode2].global; + let dontShowGlobalDiff = glbMemory == null ? true : false; + let globalLbDiff = null; + if (glb === null) { globalLbString = "global: not found"; - } else if (e.data.globalLeaderboard.insertedAt === -1) { + } else if (glb.insertedAt === -1) { + globalLbDiff = glbMemory - glb.insertedAt; + updateLbMemory( + config.mode, + mode2, + "global", + glb.insertedAt + ); + globalLbString = "global: not qualified"; - } else if (e.data.globalLeaderboard.insertedAt >= 0) { - if (e.data.globalLeaderboard.newBest) { - let str = getPositionString(e.data.globalLeaderboard.insertedAt + 1); + } else if (glb.insertedAt >= 0) { + if (glb.newBest) { + globalLbDiff = glbMemory - glb.insertedAt; + updateLbMemory( + config.mode, + mode2, + "global", + glb.insertedAt + ); + let str = getPositionString(glb.insertedAt + 1); globalLbString = `global: ${str} place`; } else { - let str = getPositionString(e.data.globalLeaderboard.foundAt + 1); + globalLbDiff = glbMemory - glb.foundAt; + updateLbMemory(config.mode, mode2, "global", glb.foundAt); + let str = getPositionString(glb.foundAt + 1); globalLbString = `global: already ${str}`; } } + if (!dontShowGlobalDiff) { + let sString = + globalLbDiff === 1 || globalLbDiff === -1 ? "" : "s"; + if (globalLbDiff > 0) { + globalLbString += ` (${lbUpIcon}${globalLbDiff})`; + } else if (globalLbDiff === 0) { + globalLbString += ` (${lbRightIcon}${globalLbDiff})`; + } else if (globalLbDiff < 0) { + globalLbString += ` (${lbDownIcon}${globalLbDiff})`; + } + } //daily let dailyLbString = ""; - if (e.data.dailyLeaderboard === null) { + const dlb = e.data.dailyLeaderboard; + const dlbMemory = + dbSnapshot.lbMemory[config.mode + mode2].daily; + let dontShowDailyDiff = dlbMemory == null ? true : false; + let dailyLbDiff = null; + if (dlb === null) { dailyLbString = "daily: not found"; - } else if (e.data.dailyLeaderboard.insertedAt === -1) { + } else if (dlb.insertedAt === -1) { + dailyLbDiff = dlbMemory - dlb.insertedAt; + updateLbMemory(config.mode, mode2, "daily", dlb.insertedAt); dailyLbString = "daily: not qualified"; - } else if (e.data.dailyLeaderboard.insertedAt >= 0) { - if (e.data.dailyLeaderboard.newBest) { - let str = getPositionString(e.data.dailyLeaderboard.insertedAt + 1); + } else if (dlb.insertedAt >= 0) { + if (dlb.newBest) { + dailyLbDiff = dlbMemory - dlb.insertedAt; + updateLbMemory( + config.mode, + mode2, + "daily", + dlb.insertedAt + ); + let str = getPositionString(dlb.insertedAt + 1); dailyLbString = `daily: ${str} place`; } else { - let str = getPositionString(e.data.dailyLeaderboard.foundAt + 1); + dailyLbDiff = dlbMemory - dlb.foundAt; + updateLbMemory(config.mode, mode2, "daily", dlb.foundAt); + let str = getPositionString(dlb.foundAt + 1); dailyLbString = `daily: already ${str}`; } } - + if (!dontShowDailyDiff) { + let sString = + dailyLbDiff === 1 || dailyLbDiff === -1 ? "" : "s"; + if (dailyLbDiff > 0) { + dailyLbString += ` (${lbUpIcon}${dailyLbDiff})`; + } else if (dailyLbDiff === 0) { + dailyLbString += ` (${lbRightIcon}${dailyLbDiff})`; + } else if (dailyLbDiff < 0) { + dailyLbString += ` (${lbDownIcon}${dailyLbDiff})`; + } + } $("#result .stats .leaderboards .bottom").html( globalLbString + "
" + dailyLbString ); + saveLbMemory({ uid: firebase.auth().currentUser.uid, obj: dbSnapshot.lbMemory }).then( + (d) => { + if (d.data.returnCode === 1) { + // showNotification('config saved to db',1000); + } else { + showNotification( + `Error saving lb memory ${d.data.message}`, + 4000 + ); + } + } + ); + if ( e.data.dailyLeaderboard === null && e.data.globalLeaderboard === null