private async Task UpdatePlayerInfo(TimeSpan?forceCacheExpiryTime = null) { if (string.IsNullOrEmpty(this.Token)) { return; } this.PlayerInfo = await SaliensApi.GetPlayerInfoAsync(this.Token, forceCacheExpiryTime); this.State = BotState.Idle; if (!string.IsNullOrWhiteSpace(this.PlayerInfo.ActivePlanet)) { this.ActivePlanet = await SaliensApi.GetPlanetAsync(this.PlayerInfo.ActivePlanet, forceCacheExpiryTime); this.State = BotState.OnPlanet; if (int.TryParse(this.PlayerInfo.ActiveZonePosition, out int zonePosition)) { this.ActiveZone = this.ActivePlanet.Zones[zonePosition]; this.ActiveZoneStartDate = DateTime.Now - this.PlayerInfo.TimeInZone; this.State = BotState.InZone; } if (!string.IsNullOrWhiteSpace(this.PlayerInfo.ActiveBossGame)) { this.ActiveZone = this.ActivePlanet.Zones.FirstOrDefault(z => z.GameId == this.PlayerInfo.ActiveBossGame); this.ActiveZoneStartDate = DateTime.Now - this.PlayerInfo.TimeInZone; this.State = BotState.InBossZone; } } this.PresenceUpdateTrigger.SetSaliensPlayerState(this.PlayerInfo); }
public override async Task RunAsync(string parameters, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(Program.Settings.Token)) { this.Logger?.LogCommandOutput("{{warn}}No token has been set."); return; } var playerInfo = await SaliensApi.GetPlayerInfoAsync(Program.Settings.Token); if (string.IsNullOrWhiteSpace(playerInfo?.ActivePlanet)) { this.Logger?.LogCommandOutput("No planet has been joined."); return; } if (string.IsNullOrWhiteSpace(playerInfo?.ActiveZoneGame)) { this.Logger?.LogCommandOutput("No zone has been joined."); return; } if (!int.TryParse(playerInfo.ActiveZonePosition, out int zonePos)) { this.Logger?.LogCommandOutput("Invalid zone."); return; } var planet = await SaliensApi.GetPlanetAsync(playerInfo.ActivePlanet); this.Logger?.LogCommandOutput(planet.Zones[zonePos].ToConsoleBlock()); }
public override async Task RunAsync(string parameters, CancellationToken cancellationToken) { var split = parameters.Split(' '); if (split.Length != 2) { this.Logger?.LogCommandOutput("{err}Invalid amount of parameters."); return; } var planet = await SaliensApi.GetPlanetAsync(split[0]); if (planet == null) { this.Logger?.LogCommandOutput("{err}Unknown planet id."); return; } if (!int.TryParse(split[1], out int zonePos)) { this.Logger?.LogCommandOutput("{err}Invalid zone position."); return; } if (zonePos >= planet.Zones.Count) { this.Logger?.LogCommandOutput("{err}Unknown zone position."); return; } this.Logger?.LogCommandOutput(planet.Zones[zonePos].ToConsoleBlock()); }
private async Task JoinPlanet(string planetId) { for (int i = 0; i < 5; i++) { try { var planet = await SaliensApi.GetPlanetAsync(planetId); this.Logger?.LogMessage($"{{action}}Joining planet {{planet}}{planetId} ({planet.State.Name}){{action}}..."); await SaliensApi.JoinPlanetAsync(this.Token, planetId); // States this.ActivePlanet = planet; this.PlayerInfo.ActivePlanet = planetId; this.State = BotState.OnPlanet; this.PresenceUpdateTrigger.SetSaliensPlayerState(this.PlayerInfo); return; } catch (SaliensApiException ex) { switch (ex.EResult) { case EResult.Fail: case EResult.Busy: case EResult.RateLimitExceeded: this.Logger?.LogMessage($"{{warn}}Failed to join planet: {ex.Message} - Giving it a few seconds ({i + 1}/5)..."); await Task.Delay(2000); continue; case EResult.Expired: case EResult.NoMatch: default: this.Logger?.LogMessage($"{{warn}}Failed to join planet: {ex.Message}"); ResetState(); throw; } } catch (WebException ex) { this.Logger?.LogMessage($"{{warn}}Failed to join planet: {ex.Message} - Giving it a few seconds ({i + 1}/5)..."); await Task.Delay(2000); continue; } } // States, only set when failed ResetState(); void ResetState() { this.State = BotState.Idle; } }
public override async Task RunAsync(string parameters, CancellationToken cancellationToken) { var planet = await SaliensApi.GetPlanetAsync(parameters); if (planet == null) { this.Logger?.LogCommandOutput("{err}Unknown planet id."); return; } this.Logger?.LogCommandOutput(planet.ToConsoleBlock()); }
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()); }
private async Task <List <Zone> > FindMostWantedZones() { var activeZones = (await SaliensApi.GetPlanetAsync(this.ActivePlanet.Id)).Zones.Where(z => !z.Captured); // Filter out blacklisted games var zones = activeZones.OrderBy(p => 0); if (this.Strategy.HasFlag(BotStrategy.FocusBosses)) { zones = zones.ThenByDescending(z => z.BossActive ? 1 : 0); } if (this.Strategy.HasFlag(BotStrategy.MostDifficultZonesFirst)) { zones = zones.ThenByDescending(z => z.RealDifficulty); } else if (this.Strategy.HasFlag(BotStrategy.LeastDifficultZonesFirst)) { zones = zones.ThenBy(z => z.RealDifficulty); } if (this.Strategy.HasFlag(BotStrategy.MostCompletedZonesFirst)) { zones = zones.ThenByDescending(z => z.CaptureProgress); } else if (this.Strategy.HasFlag(BotStrategy.LeastCompletedZonesFirst)) { zones = zones.ThenBy(z => z.CaptureProgress); } if (this.Strategy.HasFlag(BotStrategy.TopDown)) { zones = zones.ThenBy(z => z.ZonePosition); } else if (this.Strategy.HasFlag(BotStrategy.BottomUp)) { zones = zones.ThenByDescending(z => z.ZonePosition); } // Filter out blacklisted games return(zones .Where(z => !string.IsNullOrWhiteSpace(z.GameId) && !(this.BlacklistedGames.ContainsKey(z.GameId) && this.BlacklistedGames[z.GameId] > DateTime.Now)) .ToList()); }
public override async Task RunAsync(string parameters, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(parameters)) { // Show the current overridden planet id if (!string.IsNullOrWhiteSpace(Program.Settings.OverridePlanetId)) { this.Logger?.LogCommandOutput($"The planet id is currently overridden to: {{value}}{Program.Settings.OverridePlanetId}"); } else { this.Logger?.LogCommandOutput("You have currently no planet id override set."); } this.Logger?.LogCommandOutput("You can override the planet id by appending the planet id to this command: {command}overrideplanetid {param}<id>"); this.Logger?.LogCommandOutput("where {param}<id> is replaced with the planet id, or {param}remove{reset} if you wish to remove it."); } else { // Set the overridden planet id if (parameters == "remove") { Program.Settings.OverridePlanetId.Value = null; this.Logger?.LogCommandOutput("Your planet id override has been removed."); } else { var planet = await SaliensApi.GetPlanetAsync(parameters); if (planet == null) { this.Logger?.LogCommandOutput("{err}Invalid planet id. Check the planets for ids."); } else { Program.Settings.OverridePlanetId.Value = parameters; this.Logger?.LogCommandOutput("Your planet id override has been saved."); } } } }
public override async Task RunAsync(string parameters, CancellationToken cancellationToken) { var planet = await SaliensApi.GetPlanetAsync(parameters); if (planet == null) { this.Logger?.LogCommandOutput("{err}Unknown planet id."); return; } var active = planet.Zones.Where(z => !z.Captured); var captured = planet.Zones.Where(z => z.Captured); this.Logger?.LogCommandOutput($"Zones on {{planet}}planet {planet.Id} ({planet.State.Name}){{reset}}{Environment.NewLine}" + $"Captured zones:{Environment.NewLine}" + $"{string.Join(Environment.NewLine, captured.Select(z => z.ToConsoleLine()))}{Environment.NewLine}{Environment.NewLine}" + $"Active zones:{Environment.NewLine}" + $"{string.Join(Environment.NewLine, active.Select(z => z.ToConsoleLine()))}{Environment.NewLine}{Environment.NewLine}" + $"To see more information about a zone, use the command: {{command}}zone {{param}}<planet_id> <zone_pos>{{reset}}{Environment.NewLine}" + $"where {{param}}<planet_id>{{reset}} is replaced with the planet id,{Environment.NewLine}" + $"and {{param}}<zone_pos>{{reset}} is replaced with the zone position"); }
private async Task <List <Planet> > FindMostWantedPlanets() { var mostWantedPlanets = new List <Planet>(); var activePlanets = (await SaliensApi.GetPlanetsWithZonesAsync(true)).Values; // Overridden planet if (!string.IsNullOrWhiteSpace(this.OverridePlanetId)) { try { var planet = await SaliensApi.GetPlanetAsync(this.OverridePlanetId); if (planet.State.Running) { mostWantedPlanets.Add(planet); } } catch (SaliensApiException ex) { switch (ex.EResult) { case EResult.InvalidParam: case EResult.Expired: case EResult.NoMatch: case EResult.ValueOutOfRange: Program.Settings.OverridePlanetId.Value = null; break; default: throw; } } } // Force current joined planet if FocusCurrentPlanet is selected if (this.Strategy.HasFlag(BotStrategy.FocusCurrentPlanet) && this.HasActivePlanet) { var planet = await SaliensApi.GetPlanetAsync(this.ActivePlanet.Id); if (planet.State.Running) { mostWantedPlanets.Add(planet); } } if (this.Strategy.HasFlag(BotStrategy.FocusRandomPlanet)) { mostWantedPlanets.AddRange(activePlanets .Where(p => !mostWantedPlanets.Any(mp => mp.Id == p.Id)) .ToList() .Shuffle()); } else { // As of 26th June, the planet difficulty is always low, so let's skip it for now var planets = activePlanets.OrderBy(p => 0); if (this.Strategy.HasFlag(BotStrategy.FocusBosses)) { planets = planets.ThenByDescending(p => p.Zones.Any(z => z.BossActive) ? 1 : 0); } if (this.Strategy.HasFlag(BotStrategy.MostDifficultPlanetsFirst)) { planets = planets //.ThenByDescending(p => (int)p.State.Difficulty) .ThenByDescending(p => p.MaxFreeZonesDifficulty) .ThenByDescending(p => p.WeightedAverageFreeZonesDifficulty); } else if (this.Strategy.HasFlag(BotStrategy.LeastDifficultPlanetsFirst)) { planets = planets //.ThenBy(p => (int)p.State.Difficulty) .ThenBy(p => p.MaxFreeZonesDifficulty) .ThenBy(p => p.WeightedAverageFreeZonesDifficulty); } if (this.Strategy.HasFlag(BotStrategy.MostCompletedPlanetsFirst)) { planets = planets.ThenByDescending(p => p.State.CaptureProgress); } else if (this.Strategy.HasFlag(BotStrategy.LeastCompletedPlanetsFirst)) { planets = planets.ThenBy(p => p.State.CaptureProgress); } if (this.Strategy.HasFlag(BotStrategy.TopDown)) { planets = planets.ThenBy(p => p.Id); } else if (this.Strategy.HasFlag(BotStrategy.BottomUp)) { planets = planets.ThenByDescending(p => p.Id); } mostWantedPlanets.AddRange(planets); } // Filter out blacklisted games return(mostWantedPlanets .Where(p => !(this.BlacklistedGames.ContainsKey(p.Id) && this.BlacklistedGames[p.Id] > DateTime.Now)) .ToList()); }
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()); } } }