public override async Task RunAsync(string parameters, CancellationToken cancellationToken) { if (!string.IsNullOrWhiteSpace(parameters) && parameters != "all" && parameters != "live") { this.Logger?.LogCommandOutput("{err}Invalid parameter."); return; } var planetDetails = (await SaliensApi.GetPlanetsAsync()).Values.OrderBy(p => p.State.Priority); var active = planetDetails.Where(p => p.State.Running); var future = planetDetails.Where(p => !p.State.Active && !p.State.Captured); if (parameters == "all") { var captured = planetDetails.Where(p => p.State.Captured); this.Logger?.LogCommandOutput("Captured planets:"); await PrintPlanets(captured); this.Logger?.LogCommandOutput(""); this.Logger?.LogCommandOutput("Upcoming planets:"); await PrintPlanets(future); this.Logger?.LogCommandOutput(""); } this.Logger?.LogCommandOutput("Active planets:"); await PrintPlanets(active); this.Logger?.LogCommandOutput(""); if (string.IsNullOrWhiteSpace(parameters)) { this.Logger?.LogCommandOutput("Upcoming planets:"); await PrintPlanets(future.Take(2)); this.Logger?.LogCommandOutput(""); } this.Logger?.LogCommandOutput($"To get a list of all planets, use the command: {{command}}planets {{param}}all{{reset}}{Environment.NewLine}{Environment.NewLine}" + $"To see more information about a planet, use the command: {{command}}planet {{param}}<id>{{reset}}{Environment.NewLine}" + $"where {{param}}<id>{{reset}} is replaced with the planet id."); async Task PrintPlanets(IEnumerable <Planet> planets) { var results = await Task.WhenAll(planets.Select(p => p.Zones == null ? SaliensApi.GetPlanetAsync(p.Id) : Task.FromResult(p))); foreach (var planet in results) { this.Logger?.LogCommandOutput(planet.ToConsoleLine()); } } }
private async Task <List <Planet> > GetActivePlanets() { var planets = (await SaliensApi.GetPlanetsAsync()).Values .OrderBy(p => p.State.Priority); var activePlanets = await Task.WhenAll(planets .Where(p => p.State.Running) .Select(p => SaliensApi.GetPlanetAsync(p.Id))); // Get the next 2 future planets, if available var lastPlanetIndex = planets.ToList().FindIndex(p => p.Id == activePlanets.Last().Id); var lastPlanets = (await Task.WhenAll(planets.Skip(lastPlanetIndex + 1) .Take(2) .Select(p => SaliensApi.GetPlanetAsync(p.Id)))); return(activePlanets.Concat(lastPlanets).ToList()); }