Make accelerated time calculations independent of client (!205)

Changes server accelerated time calculation to be fully independent of client calculations.

Local testing over most of a day showed time acceleration continuing through relogging as a client, and calculated times being synced to about +- 2 seconds between server and client with no drift.

-----------
In #202 I referenced the client side formula for accelerate time:
`In Raid Time = Today's Date + Location Time + Time Since Client Connection * Acceleration`
At the time I didn't know where Location Time was set and conservatively tried to match the server calculations to the client.  Since then I've confirmed that it is set after calling `client/game/start` and holds the accelerated server timestamp from that call.  With this in mind, I'm more confident changing the server calculations and here we are.

Previously each time you started your client, the accelerated time would start counting from your irl time at launch.  This change moves that to the server, so you could leave your server running to have a more live-like experience where you won't be sure of the in raid accelerated time until you log in.

Added benefit of significantly simplifying the `getInRaidTime()` code.

Future work could be done to add save/load support to the server's timestamp to further emulate the live experience where timers won't reset to your irl time unless you wipe the data.  I'd personally lean towards saving it at a server level, not a profile level, to allow multiple profiles to share a single 'wipe'.

-----------

Co-authored-by: OkaMoez <43766412+OkaMoez@users.noreply.github.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/205
Co-authored-by: OkaMoez <okamoez@noreply.dev.sp-tarkov.com>
Co-committed-by: OkaMoez <okamoez@noreply.dev.sp-tarkov.com>
This commit is contained in:
OkaMoez 2024-01-23 10:13:53 +00:00 committed by chomp
parent c51cce269f
commit 1c6b76163b
2 changed files with 7 additions and 20 deletions

View File

@ -100,9 +100,6 @@ export class GameController
*/
public gameStart(_url: string, _info: IEmptyRequestData, sessionID: string, startTimeStampMS: number): void
{
// Store start time in app context
this.applicationContext.addValue(ContextVariableType.CLIENT_START_TIMESTAMP, startTimeStampMS);
if (this.coreConfig.fixes.fixShotgunDispersion)
{
this.fixShotgunDispersions();

View File

@ -17,6 +17,10 @@ export class WeatherGenerator
{
protected weatherConfig: IWeatherConfig;
// Note: If this value gets save/load support, raid time could be tracked across server restarts
// Currently it will set the In Raid time to your current real time on server launch
private serverStartTimestampMS = Date.now();
constructor(
@inject("WeightedRandomHelper") protected weightedRandomHelper: WeightedRandomHelper,
@inject("WinstonLogger") protected logger: ILogger,
@ -67,23 +71,9 @@ export class WeatherGenerator
*/
public getInRaidTime(currentDate: Date): Date
{
// Get timestamp of when client conneted to server
const gameStartTimeStampMS = this.applicationContext.getLatestValue(ContextVariableType.CLIENT_START_TIMESTAMP)
.getValue<number>();
// Get delta between now and when client connected to server in milliseconds
const deltaMSFromNow = currentDate.getTime() - gameStartTimeStampMS;
const acceleratedMS = deltaMSFromNow * (this.weatherConfig.acceleration);
// Match client side time calculations which start from the current date + connection time, not current time
const locationTime = new Date(gameStartTimeStampMS);
locationTime.setFullYear(currentDate.getFullYear());
locationTime.setMonth(currentDate.getMonth());
locationTime.setDate(currentDate.getDate());
const clientAcceleratedDate = new Date(locationTime.getTime() + acceleratedMS);
return clientAcceleratedDate;
const timeSinceServerStartMS = currentDate.getTime() - this.serverStartTimestampMS;
const acceleratedMS = timeSinceServerStartMS * (this.weatherConfig.acceleration);
return new Date(this.serverStartTimestampMS + acceleratedMS);
}
/**