From cf9e29db65f0e9436c2aced7837dae7a72972f63 Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 5 Jul 2021 15:59:10 +0100 Subject: [PATCH] added today tracker --- gulpfile.js | 1 + src/js/db.js | 4 ++- src/js/misc.js | 8 +++--- src/js/test/test-logic.js | 5 ++++ src/js/test/today-tracker.js | 55 ++++++++++++++++++++++++++++++++++++ src/sass/style.scss | 2 +- static/index.html | 1 + 7 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 src/js/test/today-tracker.js diff --git a/gulpfile.js b/gulpfile.js index acbc9d15e..a9b51f19e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -167,6 +167,7 @@ const refactoredSrc = [ "./src/js/test/test-config.js", "./src/js/test/layout-emulator.js", "./src/js/test/poetry.js", + "./src/js/test/today-tracker.js", "./src/js/replay.js", ]; diff --git a/src/js/db.js b/src/js/db.js index 3dbd8168c..f2e8e7ecc 100644 --- a/src/js/db.js +++ b/src/js/db.js @@ -2,6 +2,7 @@ import { loadTags } from "./result-filters"; import * as AccountButton from "./account-button"; import * as CloudFunctions from "./cloud-functions"; import * as Notifications from "./notifications"; +import * as TodayTracker from "./today-tracker"; const db = firebase.firestore(); db.settings({ experimentalForceLongPolling: true }); @@ -165,7 +166,7 @@ export async function getUserResults() { .orderBy("timestamp", "desc") .limit(1000) .get() - .then((data) => { + .then(async (data) => { dbSnapshot.results = []; data.docs.forEach((doc) => { let result = doc.data(); @@ -181,6 +182,7 @@ export async function getUserResults() { dbSnapshot.results.push(result); }); + await TodayTracker.addAllFromToday(); return true; }) .catch((e) => { diff --git a/src/js/misc.js b/src/js/misc.js index 7b159badd..0101799dd 100644 --- a/src/js/misc.js +++ b/src/js/misc.js @@ -506,7 +506,7 @@ export function getGibberish() { return ret; } -export function secondsToString(sec) { +export function secondsToString(sec, full = false) { const hours = Math.floor(sec / 3600); const minutes = Math.floor((sec % 3600) / 60); const seconds = roundTo2((sec % 3600) % 60); @@ -515,13 +515,13 @@ export function secondsToString(sec) { let secondsString; hours < 10 ? (hoursString = "0" + hours) : (hoursString = hours); minutes < 10 ? (minutesString = "0" + minutes) : (minutesString = minutes); - seconds < 10 && (minutes > 0 || hours > 0) + seconds < 10 && (minutes > 0 || hours > 0 || full) ? (secondsString = "0" + seconds) : (secondsString = seconds); let ret = ""; - if (hours > 0) ret += hoursString + ":"; - if (minutes > 0 || hours > 0) ret += minutesString + ":"; + if (hours > 0 || full) ret += hoursString + ":"; + if (minutes > 0 || hours > 0 || full) ret += minutesString + ":"; ret += secondsString; return ret; } diff --git a/src/js/test/test-logic.js b/src/js/test/test-logic.js index 7c0516e7c..7a907b906 100644 --- a/src/js/test/test-logic.js +++ b/src/js/test/test-logic.js @@ -31,6 +31,7 @@ import * as TestLeaderboards from "./test-leaderboards"; import * as Replay from "./replay.js"; import * as MonkeyPower from "./monkey-power"; import * as Poetry from "./poetry.js"; +import * as TodayTracker from './today-tracker'; let glarsesMode = false; @@ -1208,6 +1209,10 @@ export function finish(difficultyFailed = false) { if (afkSecondsPercent > 0) { $("#result .stats .time .bottom .afk").text(afkSecondsPercent + "% afk"); } + TodayTracker.addSeconds(testtime + (TestStats.incompleteSeconds < 0 + ? 0 + : Misc.roundTo2(TestStats.incompleteSeconds)) - afkseconds); + $("#result .stats .time .bottom .timeToday").text(TodayTracker.getString()); $("#result .stats .key .bottom").text(testtime + "s"); $("#words").removeClass("blurred"); OutOfFocus.hide(); diff --git a/src/js/test/today-tracker.js b/src/js/test/today-tracker.js new file mode 100644 index 000000000..2dd7dfca2 --- /dev/null +++ b/src/js/test/today-tracker.js @@ -0,0 +1,55 @@ +import * as Misc from './misc'; +import * as DB from './db'; + +let seconds = 0; +let addedAllToday = false; +let dayToday = null; + +export function addSeconds(s){ + if (addedAllToday){ + let nowDate = new Date(); + nowDate = nowDate.getDate(); + if(nowDate > dayToday){ + seconds = s; + return + } + } + seconds += s; +} + +export function getString(){ + let secString = Misc.secondsToString(Math.round(seconds), true); + return secString + (addedAllToday === true ? " today" : " session"); +} + +export async function addAllFromToday(){ + + let todayDate = new Date(); + todayDate.setSeconds(0); + todayDate.setMinutes(0); + todayDate.setHours(0); + todayDate.setMilliseconds(0); + dayToday = todayDate.getDate(); + todayDate = todayDate.getTime(); + + seconds = 0; + + let results = await DB.getSnapshot().results; + + results.forEach((result) => { + + let resultDate = new Date(result.timestamp); + resultDate.setSeconds(0); + resultDate.setMinutes(0); + resultDate.setHours(0); + resultDate.setMilliseconds(0); + resultDate = resultDate.getTime(); + + if(resultDate >= todayDate){ + seconds += result.testDuration + result.incompleteTestSeconds - result.afkDuration; + } + + }); + + addedAllToday = true; +} \ No newline at end of file diff --git a/src/sass/style.scss b/src/sass/style.scss index 6ad97dffc..f9e85db44 100644 --- a/src/sass/style.scss +++ b/src/sass/style.scss @@ -2050,7 +2050,7 @@ key { } &.time { - .afk { + .afk, .timeToday { color: var(--sub-color); font-size: 0.75rem; line-height: 0.75rem; diff --git a/static/index.html b/static/index.html index c84ca09cd..9a728c5fb 100644 --- a/static/index.html +++ b/static/index.html @@ -1410,6 +1410,7 @@
-
+