using new duration calculation

added keyoverlap
This commit is contained in:
Miodec 2023-04-03 14:57:59 +02:00
parent 84a8b3f898
commit 486c2d0632
6 changed files with 27 additions and 38 deletions

View file

@ -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 ?? [];

View file

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

View file

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

View file

@ -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<string, number> = {};
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: [],
},
};

View file

@ -1304,6 +1304,7 @@ interface CompletedEvent extends MonkeyTypes.Result<MonkeyTypes.Mode> {
wpmConsistency: number;
lang: string;
challenge?: string | null;
keyOverlap: number;
}
type PartialCompletedEvent = Omit<Partial<CompletedEvent>, "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);
}

View file

@ -28,7 +28,6 @@ interface KeypressTimings {
array: number[] | "toolong";
};
duration: {
current: number;
array: number[] | "toolong";
};
}