BUGFIX: Fix negative elapsed time (#1354)

This commit is contained in:
catloversg
2024-06-06 08:11:59 +07:00
committed by GitHub
parent eeab6df718
commit 304a918cc9
2 changed files with 18 additions and 2 deletions

View File

@ -15,7 +15,7 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
//The Player object stores the last update time from when we were online
const thisUpdate = new Date().getTime();
const lastUpdate = Player.lastUpdate;
const timePassed = (thisUpdate - lastUpdate) / 1000; //Seconds
const timePassed = Math.max((thisUpdate - lastUpdate) / 1000, 0); //Seconds
//Calculate the "confidence" rating of the script's true production. This is based
//entirely off of time. We will arbitrarily say that if a script has been running for

View File

@ -46,6 +46,10 @@ 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.");
}
/** Game engine. Handles the main game loop. */
const Engine: {
_lastUpdate: number;
@ -247,7 +251,13 @@ const Engine: {
// Calculate the number of cycles have elapsed while offline
Engine._lastUpdate = new Date().getTime();
const lastUpdate = Player.lastUpdate;
const timeOffline = Engine._lastUpdate - lastUpdate;
let timeOffline = Engine._lastUpdate - lastUpdate;
if (timeOffline < 0) {
timeOffline = 0;
setTimeout(() => {
showWarningAboutSystemClock();
}, 250);
}
const numCyclesOffline = Math.floor(timeOffline / CONSTANTS.MilliPerCycle);
// Calculate the number of chances for a contract the player had whilst offline
@ -393,6 +403,12 @@ const Engine: {
// Get time difference
const _thisUpdate = new Date().getTime();
let diff = _thisUpdate - Engine._lastUpdate;
if (diff < 0) {
diff = 0;
Engine._lastUpdate = _thisUpdate;
Player.lastUpdate = _thisUpdate;
showWarningAboutSystemClock();
}
const offset = diff % CONSTANTS.MilliPerCycle;
// Divide this by cycle time to determine how many cycles have elapsed since last update