diff --git a/src/GameLogic/MiniGames/MiniGameContext.cs b/src/GameLogic/MiniGames/MiniGameContext.cs index 556edb768..bd35d504a 100644 --- a/src/GameLogic/MiniGames/MiniGameContext.cs +++ b/src/GameLogic/MiniGames/MiniGameContext.cs @@ -5,6 +5,7 @@ namespace MUnique.OpenMU.GameLogic.MiniGames; using System.Collections.Concurrent; +using System.Diagnostics; using System.Threading; using MUnique.OpenMU.DataModel.Statistics; using MUnique.OpenMU.GameLogic.Attributes; @@ -35,6 +36,8 @@ public class MiniGameContext : AsyncDisposable, IEventStateProvider private readonly HashSet _currentSpawnWaves = new(); private readonly List _remainingEvents = new(); + private Stopwatch? _elapsedTimeSinceStart; + /// /// Initializes a new instance of the class. /// @@ -250,6 +253,9 @@ protected async ValueTask ForEachPlayerAsync(Func playerAction) /// The player which started with the game. protected virtual async ValueTask OnGameStartAsync(ICollection players) { + this._elapsedTimeSinceStart = new Stopwatch(); + this._elapsedTimeSinceStart.Start(); + var startEvents = this.Definition.ChangeEvents .OrderBy(e => e.Index) .TakeWhile(e => e is { Index: <= 0, NumberOfKills: 0 }) @@ -609,9 +615,10 @@ private async ValueTask RunSpawnWaveAsync(MiniGameSpawnWave spawnWave, Cancellat return; } - if (spawnWave.StartTime > TimeSpan.Zero) + var requiredDelay = spawnWave.StartTime - this._elapsedTimeSinceStart?.Elapsed; + if (requiredDelay > TimeSpan.Zero) { - await Task.Delay(spawnWave.StartTime, cancellationToken).ConfigureAwait(false); + await Task.Delay(requiredDelay.Value, cancellationToken).ConfigureAwait(false); } this.Logger.LogDebug("{context}: Starting next wave: {wave}", this, spawnWave.Description);