-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5daab6
commit 12304bf
Showing
3 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 46 additions & 3 deletions
49
src/CommandersCall.Kernel/CommandersCall.Kernel.Shared/Timers/GameTimer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<long> 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<long> _tick; | ||
private IDisposable _subscriber; | ||
private readonly GameTicker _ticker; | ||
private CancellationTokenSource _cancelToken; | ||
|
||
public IObservable<long> Tick => _tick; | ||
|
||
public GameTimer(ILogger logger, int tickInterval) | ||
{ | ||
_logger = logger; | ||
_tickInterval = Math.Clamp(tickInterval, _minTickValue, _maxTickValue); | ||
|
||
_tick = Observable.Empty<long>(); | ||
_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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters