using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using gameapi.Library; using Microsoft.Extensions.Hosting; public class TickController : IHostedService { private Timer timer; private GameState game; private DateTime lastTime; public TickController() { this.game = GameState.GetInstance(); lastTime = DateTime.UtcNow; } public Task StartAsync(CancellationToken cancellationToken) { this.timer = new Timer(o => TickUpdate(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(100)); return Task.CompletedTask; } public async void TickUpdate() { List tasksToComplete = new List(); var population = this.game.GetAllPlayers(); var currentTime = DateTime.UtcNow; var delta = 1 + currentTime.Subtract(lastTime).TotalMilliseconds / 1000; // foreach (var player in population) // { // player.Update(delta); // tasksToComplete.Add(this.game.MessageAllOtherPlayersInSector(player, "move " + player.GetShipData())); // } await Task.WhenAll(tasksToComplete); lastTime = currentTime; } public Task StopAsync(CancellationToken cancellationToken) { timer.Change(Timeout.Infinite, 0); return Task.CompletedTask; } }