Skip to content

Commit

Permalink
Made game ticks runtime changeable
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-gilliam committed Nov 7, 2023
1 parent c5daab6 commit 12304bf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
}
}
}
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public KernelService(IOptions<Settings> 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()
Expand Down

0 comments on commit 12304bf

Please sign in to comment.