From 85f035b53dd0cb4500ae1f87958e52564143653e Mon Sep 17 00:00:00 2001 From: Dustin Date: Sun, 23 Mar 2025 10:40:01 -0700 Subject: [PATCH 1/2] Added small change to return 0 characters per minute if the words per minute is above a certain threshold, aka if the user is using a program to type for them --- .../src/results/services/result-calculation.service.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/back-nest/src/results/services/result-calculation.service.ts b/packages/back-nest/src/results/services/result-calculation.service.ts index f2a298aa..1aba510e 100644 --- a/packages/back-nest/src/results/services/result-calculation.service.ts +++ b/packages/back-nest/src/results/services/result-calculation.service.ts @@ -17,6 +17,16 @@ export class ResultCalculationService { const strippedCode = Challenge.getStrippedCode(code); const cps = strippedCode.length / timeSeconds; const cpm = cps * 60; + const words = strippedCode.trim().split(' '); + if (words.length === 0) { // Avoid division by zero + return 0; + } + const totalWordLength = words.reduce((sum, word) => sum + word.length, 0); + const avgWordLength = totalWordLength / words.length; + const wpm = cpm / avgWordLength; + if (wpm > 300) { // Abitrary value, change as needed, if cpm is greater they are most likely using an automation program + return 0; // Simply give them no score + } return Math.floor(cpm); } From 08277f9103373384f68d6b041d75586e05bce9ef Mon Sep 17 00:00:00 2001 From: Dustin Date: Mon, 19 Jan 2026 12:49:08 -0800 Subject: [PATCH 2/2] Using 5 as the average word lenth instead of calculating it, more strict error handling / refactor --- .../services/result-calculation.service.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/back-nest/src/results/services/result-calculation.service.ts b/packages/back-nest/src/results/services/result-calculation.service.ts index 1aba510e..59234eda 100644 --- a/packages/back-nest/src/results/services/result-calculation.service.ts +++ b/packages/back-nest/src/results/services/result-calculation.service.ts @@ -13,20 +13,20 @@ export class ResultCalculationService { } getCPM(code: string, timeMS: number): number { + if (timeMS <= 0) return 0; + const timeSeconds = timeMS / 1000; const strippedCode = Challenge.getStrippedCode(code); - const cps = strippedCode.length / timeSeconds; - const cpm = cps * 60; - const words = strippedCode.trim().split(' '); - if (words.length === 0) { // Avoid division by zero + + if (strippedCode.length === 0) return 0; + + const cpm = (strippedCode.length / timeSeconds) * 60; + const wpm = cpm / 5; + + if (wpm >= 300) { return 0; } - const totalWordLength = words.reduce((sum, word) => sum + word.length, 0); - const avgWordLength = totalWordLength / words.length; - const wpm = cpm / avgWordLength; - if (wpm > 300) { // Abitrary value, change as needed, if cpm is greater they are most likely using an automation program - return 0; // Simply give them no score - } + return Math.floor(cpm); }