From 35a6b910aa31224a95c19417f1470be890151e43 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sat, 29 Jul 2023 01:49:54 +0200 Subject: [PATCH] moved afk history into its own variable --- .../src/ts/controllers/input-controller.ts | 4 ++-- frontend/src/ts/test/test-input.ts | 18 ++++++++++++------ frontend/src/ts/test/test-logic.ts | 7 +++++-- frontend/src/ts/test/test-stats.ts | 2 +- frontend/src/ts/test/test-timer.ts | 1 + 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/frontend/src/ts/controllers/input-controller.ts b/frontend/src/ts/controllers/input-controller.ts index 8a8f33469..4270b9673 100644 --- a/frontend/src/ts/controllers/input-controller.ts +++ b/frontend/src/ts/controllers/input-controller.ts @@ -871,7 +871,7 @@ $(document).keydown(async (event) => { return; } - TestInput.setKeypressNotAfk(); + TestInput.setCurrentNotAfk(); //blocking firefox from going back in history with backspace if (event.key === "Backspace") { @@ -1108,7 +1108,7 @@ $("#wordsInput").on("input", (event) => { const popupVisible = Misc.isAnyPopupVisible(); if (popupVisible) return; - TestInput.setKeypressNotAfk(); + TestInput.setCurrentNotAfk(); if ( (Config.layout === "default" || Config.layout === "korean") && diff --git a/frontend/src/ts/test/test-input.ts b/frontend/src/ts/test/test-input.ts index 5162bb1f5..7d6211d9c 100644 --- a/frontend/src/ts/test/test-input.ts +++ b/frontend/src/ts/test/test-input.ts @@ -74,7 +74,6 @@ const keysToTrack = [ interface Keypress { count: number; - afk: boolean; } interface KeypressTimings { @@ -224,7 +223,6 @@ export const corrected = new Corrected(); export let keypressPerSecond: Keypress[] = []; let currentSecondKeypressData: Keypress = { count: 0, - afk: true, }; export let currentBurstStart = 0; export let missedWords: { @@ -257,6 +255,9 @@ let currentErrorHistory: ErrorHistoryObject = { words: [], }; +export let afkHistory: boolean[] = []; +let currentAfk = true; + export let spacingDebug = false; export function enableSpacingDebug(): void { spacingDebug = true; @@ -267,8 +268,8 @@ export function incrementKeypressCount(): void { currentSecondKeypressData.count++; } -export function setKeypressNotAfk(): void { - currentSecondKeypressData.afk = false; +export function setCurrentNotAfk(): void { + currentAfk = false; } export function incrementKeypressErrors(): void { @@ -287,10 +288,14 @@ export function pushKeypressesToHistory(): void { keypressPerSecond.push(currentSecondKeypressData); currentSecondKeypressData = { count: 0, - afk: true, }; } +export function pushAfkToHistory(): void { + afkHistory.push(currentAfk); + currentAfk = true; +} + export function pushErrorToHistory(): void { errorHistory.push(currentErrorHistory); currentErrorHistory = { @@ -488,8 +493,9 @@ export function restart(): void { keypressPerSecond = []; currentSecondKeypressData = { count: 0, - afk: true, }; + afkHistory = []; + currentAfk = true; errorHistory = []; currentErrorHistory = { count: 0, diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index 201e9d1de..da8a1e840 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -196,6 +196,7 @@ export function restart(options = {} as RestartOptions): void { if (TestState.savingEnabled) { TestInput.pushKeypressesToHistory(); TestInput.pushErrorToHistory(); + TestInput.pushAfkToHistory(); const testSeconds = TestStats.calculateTestSeconds(performance.now()); const afkseconds = TestStats.calculateAfkSeconds(testSeconds); let tt = Misc.roundTo2(testSeconds - afkseconds); @@ -864,6 +865,7 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent { TestInput.pushToRawHistory(wpmAndRaw.raw); TestInput.pushKeypressesToHistory(); TestInput.pushErrorToHistory(); + TestInput.pushAfkToHistory(); } //consistency @@ -1043,8 +1045,8 @@ export async function finish(difficultyFailed = false): Promise { ///////// completed event ready //afk check - const kps = TestInput.keypressPerSecond.slice(-5); - let afkDetected = kps.every((second) => second.afk); + const kps = TestInput.afkHistory.slice(-5); + let afkDetected = kps.every((afk) => afk === true); if (TestState.bailedOut) afkDetected = false; const mode2Number = parseInt(completedEvent.mode2); @@ -1402,6 +1404,7 @@ export function fail(reason: string): void { // corrected.pushHistory(); TestInput.pushKeypressesToHistory(); TestInput.pushErrorToHistory(); + TestInput.pushAfkToHistory(); finish(true); if (!TestState.savingEnabled) return; const testSeconds = TestStats.calculateTestSeconds(performance.now()); diff --git a/frontend/src/ts/test/test-stats.ts b/frontend/src/ts/test/test-stats.ts index 355e515a7..a1baa83b7 100644 --- a/frontend/src/ts/test/test-stats.ts +++ b/frontend/src/ts/test/test-stats.ts @@ -260,7 +260,7 @@ export function calculateAfkSeconds(testSeconds: number): number { // `gonna add extra ${extraAfk} seconds of afk because of no keypress data` // ); } - const ret = TestInput.keypressPerSecond.filter((x) => x.afk).length; + const ret = TestInput.afkHistory.filter((afk) => afk === true).length; return ret + extraAfk; } diff --git a/frontend/src/ts/test/test-timer.ts b/frontend/src/ts/test/test-timer.ts index e08f33f27..596736bd1 100644 --- a/frontend/src/ts/test/test-timer.ts +++ b/frontend/src/ts/test/test-timer.ts @@ -126,6 +126,7 @@ function checkIfFailed( if (timerDebug) console.time("fail conditions"); TestInput.pushKeypressesToHistory(); TestInput.pushErrorToHistory(); + TestInput.pushAfkToHistory(); if ( Config.minWpm === "custom" && wpmAndRaw.wpm < Config.minWpmCustomSpeed &&