diff --git a/frontend/src/components/alcoholGame/index.tsx b/frontend/src/components/alcoholGame/index.tsx index 4a6c0439..d578c038 100644 --- a/frontend/src/components/alcoholGame/index.tsx +++ b/frontend/src/components/alcoholGame/index.tsx @@ -15,6 +15,12 @@ const AlcoholGame = forwardRef(({ quit, next } const [isWaiting, setIsWaiting] = useState(false) const [failCount, setFailCount] = useState(0) const [blocked, setBlocked] = useState(false) + const [benchmarkTime, setBenchmarkTime] = useState(0) + + const failedAttempts = 3 + const reactionTimeThreshold = 500 + const offsetForSlowPhones = 400 + const benchmarkTimeThreshold = 50 const randomTime = 3 const [timeoutHandle, setTimeoutHandle] = useState | null>(null) @@ -30,6 +36,14 @@ const AlcoholGame = forwardRef(({ quit, next } setBlocked(false) } } + + // Benchmark the loop + const startBenchmark = performance.now() + for (let i = 1; i <= 1000000; i++) { + Math.sqrt(i) + } + const endBenchmark = performance.now() + setBenchmarkTime(endBenchmark - startBenchmark) }, []) const startTest = () => { @@ -62,12 +76,13 @@ const AlcoholGame = forwardRef(({ quit, next } const endTest = () => { setIsTestRunning(false) if (startTime) { - const newReactionTime = Date.now() - startTime + const newReactionTime = + Date.now() - startTime - (benchmarkTimeThreshold < benchmarkTime ? offsetForSlowPhones : 0) setReactionTime(newReactionTime) - if (newReactionTime >= 500) { + if (newReactionTime >= reactionTimeThreshold) { const newFailCount = failCount + 1 setFailCount(newFailCount) - if (newFailCount >= 3) { + if (newFailCount >= failedAttempts) { Cookies.set("blockedTime", Date.now().toString()) setBlocked(true) } @@ -117,13 +132,14 @@ const AlcoholGame = forwardRef(({ quit, next } >
{} } > +

{benchmarkTime}

{blocked ? ( <>

You have failed too many times, YOU ARE DRUNK