diff --git a/TanksServer/Services/GameTickService.cs b/TanksServer/GameTickWorker.cs similarity index 76% rename from TanksServer/Services/GameTickService.cs rename to TanksServer/GameTickWorker.cs index d21b2ca..1ca47e1 100644 --- a/TanksServer/Services/GameTickService.cs +++ b/TanksServer/GameTickWorker.cs @@ -1,11 +1,12 @@ +using System.Diagnostics; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using TanksServer.TickSteps; -namespace TanksServer.Services; +namespace TanksServer; -internal sealed class GameTickService( - IEnumerable steps, IHostApplicationLifetime lifetime, ILogger logger +internal sealed class GameTickWorker( + IEnumerable steps, IHostApplicationLifetime lifetime, ILogger logger ) : IHostedService, IDisposable { private readonly CancellationTokenSource _cancellation = new(); @@ -22,11 +23,16 @@ internal sealed class GameTickService( { try { + var sw = new Stopwatch(); while (!_cancellation.IsCancellationRequested) { + logger.LogTrace("since last frame: {}", sw.Elapsed); + sw.Restart(); + foreach (var step in _steps) await step.TickAsync(); - await Task.Delay(1000 / 25); + + await Task.Delay(TimeSpan.FromMilliseconds(1000 / 25) - sw.Elapsed); } } catch (Exception ex) diff --git a/TanksServer/Program.cs b/TanksServer/Program.cs index 13d2659..9549f55 100644 --- a/TanksServer/Program.cs +++ b/TanksServer/Program.cs @@ -88,7 +88,7 @@ internal static class Program builder.Services.AddSingleton(); builder.Services.AddSingleton(); - builder.Services.AddHostedService(); + builder.Services.AddHostedService(); builder.Services.AddHostedService(sp => sp.GetRequiredService()); builder.Services.AddHostedService(sp => sp.GetRequiredService());