Skip to content

Commit

Permalink
Softban detection and Auto-Resolution!
Browse files Browse the repository at this point in the history
  • Loading branch information
disdain13 committed Jul 29, 2016
1 parent de45fc1 commit b61e357
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 24 deletions.
147 changes: 124 additions & 23 deletions PokemonGo.RocketAPI.Logic/Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ public class Logic
private readonly Navigation _navigation;
private GetPlayerResponse _playerProfile;
private string configs_path = Path.Combine(Directory.GetCurrentDirectory(), "Configs");

private int recycleCounter = 0;
private bool IsInitialized = false;

private int fleeCounter = 0;
private DateTime? fleeLast;
private bool softBan = false;

public Logic(ISettings clientSettings)
{
_clientSettings = clientSettings;
Expand Down Expand Up @@ -99,7 +102,7 @@ public async Task Execute()
{
if (e.Message.Contains("NeedsBrowser"))
{
Logger.Write("Please login to your google account and turn off 'Two-Step Authentication' under security settings (temporarily). We are working on fixing this bug. " + e.Message + " trying automatic restart in 15 seconds...", LogLevel.LoginError);
Logger.Write("Please login to your google account and turn off 'Two-Step Authentication' under security settings (temporarily). If you do NOT want to disable your two-factor auth, please visit the following link and setup an app password. This is the only way of using the bot without disabling two-factor authentication: https://security.google.com/settings/security/apppasswords. Trying automatic restart in 15 seconds...", LogLevel.LoginError);
await Task.Delay(15000);
}
else if (e.Message.Contains("BadAuthentication"))
Expand All @@ -113,7 +116,6 @@ public async Task Execute()

}
}

await Task.Delay(2000);
}
else
Expand Down Expand Up @@ -286,7 +288,7 @@ private async Task ExecuteFarmingPokestopsAndPokemons(bool path)
}

Func<Task> del = null;
if (_clientSettings.CatchPokemon)
if (_clientSettings.CatchPokemon && !softBan)
del = ExecuteCatchAllNearbyPokemons;
await
_navigation.HumanPathWalking(trackPoints.ElementAt(curTrkPt),
Expand Down Expand Up @@ -337,7 +339,7 @@ private async Task ExecuteFarmingPokestopsAndPokemons()

await Task.Delay(500);
Func<Task> del = null;
if (_clientSettings.CatchPokemon && !_clientSettings.FlyingEnabled || (_clientSettings.FlyingEnabled && _clientSettings.CatchWhileFlying)) del = ExecuteCatchAllNearbyPokemons;
if (_clientSettings.CatchPokemon && !softBan && !_clientSettings.FlyingEnabled || (_clientSettings.FlyingEnabled && _clientSettings.CatchWhileFlying)) del = ExecuteCatchAllNearbyPokemons;
var ToStart = await _navigation.HumanLikeWalking(
new GeoCoordinate(_client.StartLat, _client.StartLng,_client.StartAltitude),
_clientSettings.FlyingEnabled ? _clientSettings.FlyingSpeed: _clientSettings.MinSpeed, del);
Expand Down Expand Up @@ -382,7 +384,7 @@ private async Task ExecuteFarmingPokestopsAndPokemons()

//fly to location
Func<Task> del = null;
if (_clientSettings.CatchPokemon && !_clientSettings.FlyingEnabled || (_clientSettings.FlyingEnabled && _clientSettings.CatchWhileFlying)) del = ExecuteCatchAllNearbyPokemons;
if (_clientSettings.CatchPokemon && !softBan && !_clientSettings.FlyingEnabled || (_clientSettings.FlyingEnabled && _clientSettings.CatchWhileFlying)) del = ExecuteCatchAllNearbyPokemons;
var ToStart = await _navigation.HumanLikeWalking(
new GeoCoordinate(_client.StartLat, _client.StartLng, _client.StartAltitude),
_clientSettings.FlyingEnabled ? _clientSettings.FlyingSpeed : _clientSettings.MinSpeed, del);
Expand Down Expand Up @@ -468,7 +470,7 @@ private async Task ExecuteFarmingPokestopsAndPokemons()

await WriteStats();

if (_clientSettings.CatchPokemon)
if (_clientSettings.CatchPokemon && !softBan)
await ExecuteCatchAllNearbyPokemons();

var pokeStop = pokestopList[0];
Expand Down Expand Up @@ -511,7 +513,7 @@ private async Task ExecuteFarmingPokestopsAndPokemons()
Logger.Write(name, LogLevel.Pokestop);

Func<Task> del = null;
if (_clientSettings.CatchPokemon) del = ExecuteCatchAllNearbyPokemons;
if (_clientSettings.CatchPokemon && !softBan) del = ExecuteCatchAllNearbyPokemons;
var update = await _navigation.HumanLikeWalking(new GeoCoordinate(pokeStop.Latitude, pokeStop.Longitude), _clientSettings.MinSpeed, del);

var fortSearch = await _client.Fort.SearchFort(pokeStop.Id, pokeStop.Latitude, pokeStop.Longitude);
Expand All @@ -520,29 +522,94 @@ private async Task ExecuteFarmingPokestopsAndPokemons()
_stats.AddExperience(fortSearch.ExperienceAwarded);
_stats.UpdateConsoleTitle(_client, _inventory);
string EggReward = fortSearch.PokemonDataEgg != null ? "1" : "0";
Logger.Write($"XP: {fortSearch.ExperienceAwarded}, Gems: {fortSearch.GemsAwarded}, Eggs: {EggReward}, Items: {StringUtils.GetSummedFriendlyNameOfItemAwardList(fortSearch.ItemsAwarded)}", LogLevel.Pokestop);
if (!softBan) Logger.Write($"XP: {fortSearch.ExperienceAwarded}, Gems: {fortSearch.GemsAwarded}, Eggs: {EggReward}, Items: {StringUtils.GetSummedFriendlyNameOfItemAwardList(fortSearch.ItemsAwarded)}", LogLevel.Pokestop);
recycleCounter++;

//reset ban
if (softBan)
{
softBan = false;
fleeCounter = 0;
fleeLast = null;
Logger.Write("The ban was lifted!", LogLevel.SoftBan);
}

}
else if (fortSearch.Result == FortSearchResponse.Types.Result.Success)
{
fleeCounter++;
if (fleeLast.HasValue && fleeLast.Value.AddMinutes(3) > DateTime.Now && fleeCounter > 3 && !softBan)
{
softBan = true;
Logger.Write("Detected a soft ban, let's walk it off!", LogLevel.SoftBan);
}

fleeLast = DateTime.Now;
fleeLast = DateTime.Now;
}

//do
//{
// var fortSearch = await _client.Fort.SearchFort(pokeStop.Id, pokeStop.Latitude, pokeStop.Longitude);
// if (fortSearch.ExperienceAwarded > 0)
// {
// _stats.AddExperience(fortSearch.ExperienceAwarded);
// _stats.UpdateConsoleTitle(_client, _inventory);
// string EggReward = fortSearch.PokemonDataEgg != null ? "1" : "0";
// if (!softBan) Logger.Write($"XP: {fortSearch.ExperienceAwarded}, Gems: {fortSearch.GemsAwarded}, Eggs: {EggReward}, Items: {StringUtils.GetSummedFriendlyNameOfItemAwardList(fortSearch.ItemsAwarded)}", LogLevel.Pokestop);
// recycleCounter++;

// //reset ban
// if (softBan)
// {
// softBan = false;
// fleeCounter = 0;
// fleeLast = null;
// break;
// }

// }
// else if (fortSearch.Result == FortSearchResponse.Types.Result.Success)
// {
// fleeCounter++;
// if (fleeLast.HasValue && fleeLast.Value.AddMinutes(3) > DateTime.Now && fleeCounter > 3 && !softBan)
// {
// softBan = true;
// Logger.Write("Detected a soft ban, let's walk it off!", LogLevel.SoftBan);
// }

// fleeLast = DateTime.Now;
// fleeLast = DateTime.Now;
// }
// if (softBan)
// {
// for (int x = 0; x < 15; x++)
// {
// Console.Write(".");
// await RandomHelper.RandomDelay(800, 1200);
// }
// }
//}
//while (softBan);

if (_clientSettings.LoiteringActive && pokeStop.LureInfo != null)
{
Logger.Write($"Loitering: {fortInfo.Name} has a lure we can milk!", LogLevel.Info);
while (_clientSettings.LoiteringActive && pokeStop.LureInfo != null)
{
if (_clientSettings.CatchPokemon)
if (_clientSettings.CatchPokemon && !softBan)
await ExecuteCatchAllNearbyPokemons();

fortSearch = await _client.Fort.SearchFort(pokeStop.Id, pokeStop.Latitude, pokeStop.Longitude);
if (fortSearch.ExperienceAwarded > 0)
var fortSearch2 = await _client.Fort.SearchFort(pokeStop.Id, pokeStop.Latitude, pokeStop.Longitude);
if (fortSearch2.ExperienceAwarded > 0)
{
_stats.AddExperience(fortSearch.ExperienceAwarded);
_stats.AddExperience(fortSearch2.ExperienceAwarded);
_stats.UpdateConsoleTitle(_client, _inventory);
string EggReward = fortSearch.PokemonDataEgg != null ? "1" : "0";
Logger.Write($"XP: {fortSearch.ExperienceAwarded}, Gems: {fortSearch.GemsAwarded}, Eggs: {EggReward}, Items: {StringUtils.GetSummedFriendlyNameOfItemAwardList(fortSearch.ItemsAwarded)}", LogLevel.Pokestop);
string EggReward = fortSearch2.PokemonDataEgg != null ? "1" : "0";
Logger.Write($"XP: {fortSearch2.ExperienceAwarded}, Gems: {fortSearch2.GemsAwarded}, Eggs: {EggReward}, Items: {StringUtils.GetSummedFriendlyNameOfItemAwardList(fortSearch2.ItemsAwarded)}", LogLevel.Pokestop);
recycleCounter++;
}


if (recycleCounter >= 5)
await RecycleItems();

Expand Down Expand Up @@ -574,29 +641,50 @@ private async Task CatchEncounter(EncounterResponse encounter, MapPokemon pokemo
var attemptCounter = 1;
do
{

//if there has not been a consistent flee, reset
if (fleeCounter > 0 && fleeLast.HasValue && fleeLast.Value.AddMinutes(3) < DateTime.Now && !softBan)
{
fleeCounter = 0;
fleeLast = null;
}

var probability = encounter?.CaptureProbability?.CaptureProbability_?.FirstOrDefault();
var bestPokeball = await GetBestBall(encounter);
if (bestPokeball == ItemId.ItemUnknown)
{
Logger.Write($"You don't own any Pokeballs :( - We missed a {pokemon.PokemonId} with CP {encounter?.WildPokemon?.PokemonData?.Cp}", LogLevel.Warning);
return;
}

//only use crappy pokeballs when they are fleeing
if (fleeCounter > 1) bestPokeball = ItemId.ItemPokeBall;

var bestBerry = await GetBestBerry(encounter);
var inventoryBerries = await _inventory.GetItems();
var berries = inventoryBerries.Where(p => p.ItemId == bestBerry).FirstOrDefault();
if (bestBerry != ItemId.ItemUnknown && probability.HasValue && probability.Value < 0.35)
//only use berries when they are fleeing
if (fleeCounter == 0)
{
await _client.Encounter.UseCaptureItem(pokemon.EncounterId, bestBerry, pokemon.SpawnPointId);
berries.Count--;
Logger.Write($"{bestBerry} used, remaining: {berries.Count}", LogLevel.Berry);
await RandomHelper.RandomDelay(50, 200);
var inventoryBerries = await _inventory.GetItems();
var berries = inventoryBerries.Where(p => p.ItemId == bestBerry).FirstOrDefault();
if (bestBerry != ItemId.ItemUnknown && probability.HasValue && probability.Value < 0.35)
{
await _client.Encounter.UseCaptureItem(pokemon.EncounterId, bestBerry, pokemon.SpawnPointId);
berries.Count--;
Logger.Write($"{bestBerry} used, remaining: {berries.Count}", LogLevel.Berry);
await RandomHelper.RandomDelay(50, 200);
}
}

var distance = LocationUtils.CalculateDistanceInMeters(_client.CurrentLatitude, _client.CurrentLongitude, pokemon.Latitude, pokemon.Longitude);
caughtPokemonResponse = await _client.Encounter.CatchPokemon(pokemon.EncounterId, pokemon.SpawnPointId, bestPokeball);

if (caughtPokemonResponse.Status == CatchPokemonResponse.Types.CatchStatus.CatchSuccess)
{
//reset soft ban info
fleeCounter = 0;
fleeLast = null;
softBan = false;

foreach (var xp in caughtPokemonResponse.CaptureAward.Xp)
_stats.AddExperience(xp);
_stats.IncreasePokemons();
Expand All @@ -605,6 +693,19 @@ private async Task CatchEncounter(EncounterResponse encounter, MapPokemon pokemo
}
_stats.UpdateConsoleTitle(_client, _inventory);

//calculate if we are in a soft ban
if (caughtPokemonResponse.Status == CatchPokemonResponse.Types.CatchStatus.CatchFlee)
{
fleeCounter++;
if (fleeLast.HasValue && fleeLast.Value.AddMinutes(3) > DateTime.Now && fleeCounter > 3 && !softBan)
{
softBan = true;
Logger.Write("Detected a soft ban, let's walk it off!", LogLevel.SoftBan);

}
fleeLast = DateTime.Now;
}

if (encounter?.CaptureProbability?.CaptureProbability_ != null)
{
Func<ItemId, string> returnRealBallName = a =>
Expand Down
7 changes: 6 additions & 1 deletion PokemonGo.RocketAPI/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public static void Write(string message, LogLevel level = LogLevel.None, Console
System.Console.ForegroundColor = ConsoleColor.DarkMagenta;
System.Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss")}] (RECYCLING) {message}");
break;
case LogLevel.SoftBan:
System.Console.ForegroundColor = ConsoleColor.DarkRed;
System.Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss")}] (SOFT BAN) {message}");
break;
case LogLevel.None:
System.Console.ForegroundColor = color;
System.Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss")}] {message}");
Expand Down Expand Up @@ -136,6 +140,7 @@ public enum LogLevel
Berry = 10,
Egg = 11,
Recycling = 12,
LoginError = 13
LoginError = 13,
SoftBan = 14
}
}

0 comments on commit b61e357

Please sign in to comment.