Example #1
0
        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());
        }
Example #2
0
        private async Task Loop()
        {
            this.BotActivated?.Invoke(this, null);
            while (!this.cancelSource.Token.IsCancellationRequested)
            {
                try
                {
                    this.Logger?.LogMessage($"{{verb}}State: {this.State}");
                    switch (this.State)
                    {
                    case BotState.Idle:
                        await this.DoIdle();

                        break;

                    case BotState.Resume:
                        await this.DoResume();

                        break;

                    case BotState.OnPlanet:
                        await this.DoOnPlanet();

                        break;

                    case BotState.InZone:
                        await this.DoInZone();

                        break;

                    case BotState.InZoneEnd:
                        await this.DoInZoneEnd();

                        break;

                    case BotState.InBossZone:
                        await this.DoInBossZone();

                        break;

                    case BotState.ForcedZoneLeave:
                        await this.DoForcedZoneLeave();

                        break;

                    case BotState.Invalid:
                    default:
                        await this.DoInvalid();

                        break;
                    }
                }
                catch (OperationCanceledException) { }
                catch (SaliensApiException ex)
                {
                    // Update states
                    this.Logger?.LogMessage($"{{action}}Updating states...");
                    await this.UpdatePlayerInfo(TimeSpan.FromSeconds(5));

                    await SaliensApi.GetPlanetsWithZonesAsync(true, TimeSpan.FromSeconds(5));

                    switch (ex.EResult)
                    {
                    case EResult.Expired:
                    case EResult.NoMatch:
                        if (this.State == BotState.InZone || this.State == BotState.InZoneEnd)
                        {
                            this.State = BotState.ForcedZoneLeave;
                        }
                        break;

                    case EResult.InvalidState:
                    default:
                        this.State = BotState.Invalid;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    this.Logger?.LogException(ex);
                    this.State = BotState.Invalid;
                }
            }
            this.BotDeactivated?.Invoke(this, null);
        }