From 055b4bd7bc59d894c970005446850175b670c3ef Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Sun, 7 Jul 2024 12:24:51 +0700 Subject: [PATCH] MISC: Add threshold for warning about system clock (#1463) --- src/engine.tsx | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/engine.tsx b/src/engine.tsx index 0dab3bcc4..bb7378e8b 100644 --- a/src/engine.tsx +++ b/src/engine.tsx @@ -46,8 +46,13 @@ import { SnackbarEvents } from "./ui/React/Snackbar"; import { SaveData } from "./types"; import { Go } from "./Go/Go"; -function showWarningAboutSystemClock() { - AlertEvents.emit("Warning: The system clock moved backward."); +// Only show warning if the time diff is greater than this value. +const thresholdOfTimeDiffForShowingWarningAboutSystemClock = CONSTANTS.MillisecondsPerFiveMinutes; + +function showWarningAboutSystemClock(timeDiff: number) { + AlertEvents.emit( + `Warning: The system clock moved backward: ${convertTimeMsToTimeElapsedString(Math.abs(timeDiff))}.`, + ); } /** Game engine. Handles the main game loop. */ @@ -253,10 +258,13 @@ const Engine: { const lastUpdate = Player.lastUpdate; let timeOffline = Engine._lastUpdate - lastUpdate; if (timeOffline < 0) { + if (Math.abs(timeOffline) > thresholdOfTimeDiffForShowingWarningAboutSystemClock) { + const timeDiff = timeOffline; + setTimeout(() => { + showWarningAboutSystemClock(timeDiff); + }, 250); + } timeOffline = 0; - setTimeout(() => { - showWarningAboutSystemClock(); - }, 250); } const numCyclesOffline = Math.floor(timeOffline / CONSTANTS.MilliPerCycle); @@ -404,10 +412,12 @@ const Engine: { const _thisUpdate = new Date().getTime(); let diff = _thisUpdate - Engine._lastUpdate; if (diff < 0) { + if (Math.abs(diff) > thresholdOfTimeDiffForShowingWarningAboutSystemClock) { + showWarningAboutSystemClock(diff); + } diff = 0; Engine._lastUpdate = _thisUpdate; Player.lastUpdate = _thisUpdate; - showWarningAboutSystemClock(); } const offset = diff % CONSTANTS.MilliPerCycle;