From 12304bf80b8114a2fe646790a0aafee67615b93f Mon Sep 17 00:00:00 2001 From: Sean Gilliam Date: Tue, 7 Nov 2023 05:32:19 +0000 Subject: [PATCH] Made game ticks runtime changeable --- .../Timers/GameTicker.cs | 2 +- .../Timers/GameTimer.cs | 49 +++++++++++++++++-- .../CommandersCall.Kernel/KernelService.cs | 8 +-- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTicker.cs b/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTicker.cs index 095e427..d6a3d30 100644 --- a/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTicker.cs +++ b/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTicker.cs @@ -24,7 +24,7 @@ public void OnError(Exception error) public void OnNext(long value) { - _logger.Information("Current tick: " + value.ToString(CultureInfo.InvariantCulture)); + _logger.Information("Game server tick..."); } } } diff --git a/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTimer.cs b/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTimer.cs index a831dd9..330eb41 100644 --- a/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTimer.cs +++ b/src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTimer.cs @@ -1,15 +1,58 @@ namespace CommandersCall.Kernel.Shared.Timers { using System; + using System.Reactive.Disposables; using System.Reactive.Linq; + using Serilog; public class GameTimer { - public readonly IObservable Tick; + private const int _minTickValue = 1000; // 1s + private const int _maxTickValue = 60000; // 60s - public GameTimer(int tickInterval) + private readonly ILogger _logger; + private int _tickInterval; + private IObservable _tick; + private IDisposable _subscriber; + private readonly GameTicker _ticker; + private CancellationTokenSource _cancelToken; + + public IObservable Tick => _tick; + + public GameTimer(ILogger logger, int tickInterval) + { + _logger = logger; + _tickInterval = Math.Clamp(tickInterval, _minTickValue, _maxTickValue); + + _tick = Observable.Empty(); + _subscriber = Disposable.Empty; + _ticker = new GameTicker(_logger); + _cancelToken = new CancellationTokenSource(); + } + + public void Update(int tickInterval) + { + _tickInterval = Math.Clamp(tickInterval, _minTickValue, _maxTickValue); + } + + public void Start() + { + _cancelToken = new CancellationTokenSource(); + + _tick = Observable + // .Interval(TimeSpan.FromMilliseconds(1000)) + .Generate(0L, i => !_cancelToken.IsCancellationRequested, i => i + 1, i => i, i => TimeSpan.FromMilliseconds(_minTickValue)) + .Window(() => Observable.Interval(TimeSpan.FromMilliseconds(_tickInterval))) + .Select(x => x.LastAsync()) + .Switch(); + + _subscriber = _tick.Subscribe(_ticker); + } + + public void Stop() { - Tick = Observable.Interval(TimeSpan.FromMilliseconds(tickInterval)); + _subscriber.Dispose(); + _cancelToken.Cancel(); } } } diff --git a/src/CommandersCall.Kernel/CommandersCall.Kernel/KernelService.cs b/src/CommandersCall.Kernel/CommandersCall.Kernel/KernelService.cs index cfecfb6..ddd9b3c 100644 --- a/src/CommandersCall.Kernel/CommandersCall.Kernel/KernelService.cs +++ b/src/CommandersCall.Kernel/CommandersCall.Kernel/KernelService.cs @@ -19,16 +19,16 @@ public KernelService(IOptions settings, ILogger logger) protected async override Task ExecuteAsync(CancellationToken cancellingToken) { _logger.Information("Starting game timer..."); - var timer = new GameTimer(_settings.gameTickInterval); - //var ticker = timer.Tick.Publish(); - var ticker = timer.Tick.Subscribe(new GameTicker(_logger)); + var timer = new GameTimer(_logger, _settings.gameTickInterval); + timer.Start(); while (!cancellingToken.IsCancellationRequested) { + //timer.Update(10000); await Task.Delay(Timeout.InfiniteTimeSpan, cancellingToken); } - ticker.Dispose(); + timer.Stop(); //TODO: develop more once tasks are implmemented // var tasks = AppDomain.CurrentDomain // .GetAssemblies()