added today tracker

This commit is contained in:
Miodec 2021-07-05 15:59:10 +01:00
parent fae222f782
commit cf9e29db65
7 changed files with 70 additions and 6 deletions

View file

@ -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",
];

View file

@ -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) => {

View file

@ -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;
}

View file

@ -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();

View file

@ -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;
}

View file

@ -2050,7 +2050,7 @@ key {
}
&.time {
.afk {
.afk, .timeToday {
color: var(--sub-color);
font-size: 0.75rem;
line-height: 0.75rem;

View file

@ -1410,6 +1410,7 @@
<div class="bottom" aria-label="" data-balloon-pos="up">
<div class="text">-</div>
<div class="afk"></div>
<div class="timeToday"></div>
</div>
</div>
<!-- </div> -->