private static async Task UpdateFarmingPokestop(ISession session, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var nearestStop = session.VisibleForts.OrderBy(i => LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude, session.Client.CurrentLongitude, i.Latitude, i.Longitude)).FirstOrDefault(); if (nearestStop != null) { var walkedDistance = LocationUtils.CalculateDistanceInMeters(nearestStop.Latitude, nearestStop.Longitude, session.Client.CurrentLatitude, session.Client.CurrentLongitude); if (walkedDistance > session.LogicSettings.HumanWalkingSnipeWalkbackDistanceLimit) { await Task.Delay(3000, cancellationToken); var nearbyPokeStops = await UseNearbyPokestopsTask.UpdateFortsData(session); var notexists = nearbyPokeStops.Where(p => !session.VisibleForts.Exists(x => x.Id == p.Id)).ToList(); session.AddVisibleForts(notexists); session.EventDispatcher.Send(new PokeStopListEvent { Forts = notexists }); session.EventDispatcher.Send(new HumanWalkSnipeEvent { Type = HumanWalkSnipeEventTypes.PokestopUpdated, Pokestops = notexists, NearestDistance = walkedDistance }); } } }
private static async Task StartGymAttackLogic(ISession session, FortDetailsResponse fortInfo, GetGymDetailsResponse fortDetails, FortData gym, CancellationToken cancellationToken) { var defenders = fortDetails.GymState.Memberships.Select(x => x.PokemonData).ToList(); if (session.Profile.PlayerData.Team != fortInfo.TeamColor) { if (session.LogicSettings.GymConfig.MaxGymLevelToAttack < GetGymLevel(gym.GymPoints)) { Logger.Write($"This is gym level {GetGymLevel(gym.GymPoints)} > {session.LogicSettings.GymConfig.MaxGymLevelToAttack} in your config. Bot walk away...", LogLevel.Gym, ConsoleColor.Red); return; } if (session.LogicSettings.GymConfig.MaxDefendersToAttack < defenders.Count) { Logger.Write($"This is gym has {defenders.Count} defender > {session.LogicSettings.GymConfig.MaxDefendersToAttack} in your config. Bot walk away...", LogLevel.Gym, ConsoleColor.Red); return; } } await session.Inventory.RefreshCachedInventory(); var badassPokemon = await session.Inventory.GetHighestCpForGym(6); var pokemonDatas = badassPokemon as PokemonData[] ?? badassPokemon.ToArray(); if (defenders.Count == 0) { return; } Logger.Write("Start battle with : " + string.Join(", ", defenders.Select(x => x.PokemonId.ToString()))); // Heal pokemon foreach (var pokemon in pokemonDatas) { if (pokemon.Stamina <= 0) { await RevivePokemon(session, pokemon); } if (pokemon.Stamina <= 0) { Logger.Write("You are out of revive potions! Can't resurect attacker", LogLevel.Gym, ConsoleColor.Magenta); return; } if (pokemon.Stamina < pokemon.StaminaMax) { await HealPokemon(session, pokemon); } if (pokemon.Stamina < pokemon.StaminaMax) { Logger.Write(string.Format("You are out of healing potions! {0} ({1} CP) haven't got fully healed", pokemon.PokemonId, pokemon.Cp), LogLevel.Gym, ConsoleColor.Magenta); } } await Task.Delay(2000); var index = 0; bool isVictory = true; bool isFailedToStart = false; List <BattleAction> battleActions = new List <BattleAction>(); ulong defenderPokemonId = defenders.First().Id; while (index < defenders.Count()) { cancellationToken.ThrowIfCancellationRequested(); var thisAttackActions = new List <BattleAction>(); StartGymBattleResponse result = null; try { await Task.Delay(2000); result = await StartBattle(session, gym, pokemonDatas, defenders.FirstOrDefault(x => x.Id == defenderPokemonId)); } #pragma warning disable 0168 catch (APIBadRequestException e) #pragma warning restore 0168 { Logger.Write("Can't start battle", LogLevel.Gym); isFailedToStart = true; isVictory = false; _startBattleCounter--; var newFots = await UseNearbyPokestopsTask.UpdateFortsData(session); gym = newFots.FirstOrDefault(w => w.Id == gym.Id); break; } index++; // If we can't start battle in 10 tries, let's skip the gym if (result == null || result.Result == StartGymBattleResponse.Types.Result.Unset) { session.EventDispatcher.Send(new GymErrorUnset { GymName = fortInfo.Name }); isVictory = false; break; } if (result.Result != StartGymBattleResponse.Types.Result.Success) { break; } switch (result.BattleLog.State) { case BattleState.Active: Logger.Write($"Time to start Attack Mode", LogLevel.Gym, ConsoleColor.DarkYellow); thisAttackActions = await AttackGym(session, cancellationToken, gym, result); battleActions.AddRange(thisAttackActions); break; case BattleState.Defeated: isVictory = false; break; case BattleState.StateUnset: isVictory = false; break; case BattleState.TimedOut: isVictory = false; break; case BattleState.Victory: break; default: Logger.Write($"Unhandled result starting gym battle:\n{result}"); break; } var rewarded = battleActions.Select(x => x.BattleResults?.PlayerExperienceAwarded).Where(x => x != null); var lastAction = battleActions.LastOrDefault(); if (lastAction.Type == BattleActionType.ActionTimedOut || lastAction.Type == BattleActionType.ActionUnset || lastAction.Type == BattleActionType.ActionDefeat) { isVictory = false; break; } var faintedPKM = battleActions.Where(x => x != null && x.Type == BattleActionType.ActionFaint).Select(x => x.ActivePokemonId).Distinct(); var livePokemons = pokemonDatas.Where(x => !faintedPKM.Any(y => y == x.Id)); var faintedPokemons = pokemonDatas.Where(x => faintedPKM.Any(y => y == x.Id)); pokemonDatas = livePokemons.Concat(faintedPokemons).ToArray(); if (lastAction.Type == BattleActionType.ActionVictory) { if (lastAction.BattleResults != null) { var exp = lastAction.BattleResults.PlayerExperienceAwarded; var point = lastAction.BattleResults.GymPointsDelta; gym.GymPoints += point; defenderPokemonId = unchecked ((ulong)lastAction.BattleResults.NextDefenderPokemonId); Logger.Write(string.Format("Exp: {0}, Gym points: {1}" /*, Next defender id: {2}"*/, exp, point, defenderPokemonId), LogLevel.Gym, ConsoleColor.Magenta); } continue; } } if (isVictory) { if (gym.GymPoints < 0) { gym.GymPoints = 0; } await Execute(session, cancellationToken, gym, fortInfo); } if (isFailedToStart && _startBattleCounter > 0) { //session.ReInitSessionWithNextBot(); await Execute(session, cancellationToken, gym, fortInfo); } if (_startBattleCounter <= 0) { _startBattleCounter = 3; } }