Skip to content

Commit d37ee32

Browse files
committed
prevent millisecond drift in wartime
fix #130
1 parent 1cff46f commit d37ee32

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/Helldivers-2-Core/Mapping/MappingContext.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ internal MappingContext(WarId warId,
6666
InvariantWarStatus = warStatuses.FirstOrDefault().Value
6767
?? throw new InvalidOperationException("No warstatus available");
6868

69+
6970
var gameTime = DateTime.UnixEpoch.AddSeconds(warInfo.StartDate + InvariantWarStatus.Time);
70-
GameTimeDeviation = DateTime.UtcNow.Subtract(gameTime);
71+
GameTimeDeviation = TruncateToSeconds(DateTime.UtcNow).Subtract(gameTime);
7172
RelativeGameStart = DateTime.UnixEpoch.Add(GameTimeDeviation).AddSeconds(warInfo.StartDate);
7273
}
74+
75+
/// <summary>
76+
/// ArrowHead doesn't send timestamps more accurate than seconds, so we truncate our relative time to seconds.
77+
/// This prevents timestamps for the same value from being different (due to milli/micro second differences).
78+
/// </summary>
79+
private static DateTime TruncateToSeconds(DateTime dateTime) => dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerSecond));
7380
}

0 commit comments

Comments
 (0)