From 486c2d06321cb4949b9814c14789680f6dc6848c Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 3 Apr 2023 14:57:59 +0200 Subject: [PATCH] using new duration calculation added keyoverlap --- backend/src/api/controllers/result.ts | 6 ++- backend/src/api/schemas/result-schema.ts | 1 + .../src/ts/controllers/input-controller.ts | 9 ---- frontend/src/ts/test/test-input.ts | 42 +++++++------------ frontend/src/ts/test/test-logic.ts | 6 +++ frontend/src/ts/test/test-stats.ts | 1 - 6 files changed, 27 insertions(+), 38 deletions(-) diff --git a/backend/src/api/controllers/result.ts b/backend/src/api/controllers/result.ts index 4013505ff..6396836cf 100644 --- a/backend/src/api/controllers/result.ts +++ b/backend/src/api/controllers/result.ts @@ -183,7 +183,7 @@ export async function addResult( }; } catch (e) { // - } + } try { result.keyDurationStats = { average: @@ -274,6 +274,9 @@ export async function addResult( const status = MonkeyStatusCodes.MISSING_KEY_DATA; throw new MonkeyError(status.code, "Missing key data"); } + if (result.keyOverlap === undefined) { + throw new MonkeyError(400, "Duration is using old calculation"); + } if (anticheatImplemented()) { if (!validateKeys(result, uid)) { //autoban @@ -309,6 +312,7 @@ export async function addResult( delete result.keyDuration; delete result.smoothConsistency; delete result.wpmConsistency; + delete result.keyOverlap; if (req.ctx.configuration.users.lastHashesCheck.enabled) { let lastHashes = user.lastReultHashes ?? []; diff --git a/backend/src/api/schemas/result-schema.ts b/backend/src/api/schemas/result-schema.ts index 0073145f2..bcd5bafef 100644 --- a/backend/src/api/schemas/result-schema.ts +++ b/backend/src/api/schemas/result-schema.ts @@ -53,6 +53,7 @@ const RESULT_SCHEMA = joi joi.array().items(joi.number().min(0)), joi.string().valid("toolong") ), + keyOverlap: joi.number().min(0), lang: joi.string(), stringified: joi.string(), language: joi.string().required(), diff --git a/frontend/src/ts/controllers/input-controller.ts b/frontend/src/ts/controllers/input-controller.ts index 4316fa51d..aaa848dd6 100644 --- a/frontend/src/ts/controllers/input-controller.ts +++ b/frontend/src/ts/controllers/input-controller.ts @@ -834,7 +834,6 @@ $(document).keydown(async (event) => { ); } TestInput.recordKeypressSpacing(); - TestInput.setKeypressDuration(performance.now()); TestInput.setKeypressNotAfk(); //blocking firefox from going back in history with backspace @@ -988,14 +987,6 @@ $("#wordsInput").keyup((event) => { if (IgnoredKeys.includes(event.key)) return; if (TestUI.resultVisible) return; - const now: number = performance.now(); - if (TestInput.keypressTimings.duration.current !== -1) { - const diff: number = Math.abs( - TestInput.keypressTimings.duration.current - now - ); - TestInput.pushKeypressDuration(diff); - } - TestInput.setKeypressDuration(now); Monkey.stop(); }); diff --git a/frontend/src/ts/test/test-input.ts b/frontend/src/ts/test/test-input.ts index eceb4af54..18216c61f 100644 --- a/frontend/src/ts/test/test-input.ts +++ b/frontend/src/ts/test/test-input.ts @@ -1,5 +1,5 @@ import * as TestWords from "./test-words"; -import { roundTo2, stdDev, mean } from "../utils/misc"; +import { roundTo2 } from "../utils/misc"; interface Keypress { count: number; @@ -14,7 +14,6 @@ interface KeypressTimings { array: number[] | "toolong"; }; duration: { - current: number; array: number[] | "toolong"; }; } @@ -162,10 +161,13 @@ export let keypressTimings: KeypressTimings = { array: [], }, duration: { - current: -1, array: [], }, }; +export let keyOverlap = { + total: 0, + lastStartTime: -1, +}; export let wpmHistory: number[] = []; export let rawHistory: number[] = []; export let burstHistory: number[] = []; @@ -229,16 +231,6 @@ export function setKeypressTimingsTooLong(): void { let keysObj: Record = {}; -export function pushKeypressDuration(val: number): void { - (keypressTimings.duration.array as number[]).push(roundTo2(val)); -} - -export function setKeypressDuration(val: number): void { - keypressTimings.duration.current = roundTo2(val); -} - -let newKeypresDurationArray: number[] = []; - const keysToTrack = [ "Backquote", "Digit1", @@ -296,7 +288,7 @@ export function recordKeyupTime(key: string): void { } const now = performance.now(); const diff = Math.abs(keysObj[key] - now); - newKeypresDurationArray.push(roundTo2(diff)); + (keypressTimings.duration.array as number[]).push(roundTo2(diff)); delete keysObj[key]; updateOverlap(); @@ -311,23 +303,20 @@ export function recordKeydownTime(key: string): void { updateOverlap(); } -let totalOverlap = 0; -let lastOverlapStartTime = -1; function updateOverlap(): void { const now = performance.now(); const keys = Object.keys(keysObj); if (keys.length > 1) { - if (lastOverlapStartTime === -1) { - lastOverlapStartTime = now; + if (keyOverlap.lastStartTime === -1) { + keyOverlap.lastStartTime = now; } } else { - if (lastOverlapStartTime !== -1) { - totalOverlap += now - lastOverlapStartTime; - lastOverlapStartTime = -1; + if (keyOverlap.lastStartTime !== -1) { + keyOverlap.total += now - keyOverlap.lastStartTime; + keyOverlap.lastStartTime = -1; } } } -} function pushKeypressSpacing(val: number): void { (keypressTimings.spacing.array as number[]).push(roundTo2(val)); @@ -379,13 +368,13 @@ export function resetKeypressTimings(): void { array: [], }, duration: { - current: performance.now(), array: [], }, }; - newKeypresDurationArray = []; - totalOverlap = 0; - lastOverlapStartTime = -1; + keyOverlap = { + total: 0, + lastStartTime: -1, + }; keysObj = {}; if (spacingDebug) console.clear(); } @@ -438,7 +427,6 @@ export function restart(): void { array: [], }, duration: { - current: -1, array: [], }, }; diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index bd829011b..e3cc0fae8 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -1304,6 +1304,7 @@ interface CompletedEvent extends MonkeyTypes.Result { wpmConsistency: number; lang: string; challenge?: string | null; + keyOverlap: number; } type PartialCompletedEvent = Omit, "chartData"> & { @@ -1376,6 +1377,7 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent { tags: undefined, keySpacing: TestInput.keypressTimings.spacing.array, keyDuration: TestInput.keypressTimings.duration.array, + keyOverlap: Misc.roundTo2(TestInput.keyOverlap.total), consistency: undefined, keyConsistency: undefined, funbox: Config.funbox, @@ -1821,6 +1823,10 @@ async function saveResult( } } console.log("Error saving result", completedEvent); + if (response.message === "Duration is using old calculation") { + response.message = + "Duration is using old calculation. Please refresh the page to download the new update. If the problem persists, please contact support."; + } return Notifications.add("Failed to save result: " + response.message, -1); } diff --git a/frontend/src/ts/test/test-stats.ts b/frontend/src/ts/test/test-stats.ts index 588264e59..5ad5cca17 100644 --- a/frontend/src/ts/test/test-stats.ts +++ b/frontend/src/ts/test/test-stats.ts @@ -28,7 +28,6 @@ interface KeypressTimings { array: number[] | "toolong"; }; duration: { - current: number; array: number[] | "toolong"; }; }