Skip to content

Commit

Permalink
Merge pull request #452 from CTDragon/feature - untested need feedback
Browse files Browse the repository at this point in the history
Added feature to reset coords based on distance and time last run
  • Loading branch information
NecronomiconCoding authored Jul 24, 2016
2 parents f235c12 + b59953f commit 493798e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
28 changes: 28 additions & 0 deletions PokemonGo.RocketAPI.Logic/Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using PokemonGo.RocketAPI.Logic.Utils;
// ReSharper disable CyclomaticComplexity
// ReSharper disable FunctionNeverReturns
using System.IO;

#endregion

Expand All @@ -31,12 +32,39 @@ public class Logic
public Logic(ISettings clientSettings)
{
_clientSettings = clientSettings;
ResetCoords();
_client = new Client(_clientSettings);
_inventory = new Inventory(_client);
_navigation = new Navigation(_client);
_stats = new Statistics();
}

/// <summary>
/// Resets coords if someone could realistically get back to the default coords points since they were last updated (program was last run)
/// </summary>
private void ResetCoords()
{
string coordsPath = Directory.GetCurrentDirectory() + "\\Configs\\Coords.ini";
if (!File.Exists(coordsPath)) return;
Tuple<double, double> latLngFromFile = Client.GetLatLngFromFile();
if (latLngFromFile == null) return;
double distance = LocationUtils.CalculateDistanceInMeters(latLngFromFile.Item1, latLngFromFile.Item2, _clientSettings.DefaultLatitude, _clientSettings.DefaultLongitude);
DateTime? lastModified = File.Exists(coordsPath) ? (DateTime?)File.GetLastWriteTime(coordsPath) : null;
if (lastModified == null) return;
double? hoursSinceModified = (DateTime.Now - lastModified).HasValue ? (double?)((DateTime.Now - lastModified).Value.Minutes / 60.0) : null;
if (hoursSinceModified == null || hoursSinceModified == 0) return; // Shouldn't really be null, but can be 0 and that's bad for division.
double kmph = (distance / 1000) / (hoursSinceModified ?? .1);
if (kmph < 80) // If speed required to get to the default location is < 80km/hr
{
File.Delete(coordsPath);
Logger.Write("Kilometers Per Hour to reach default location since last run: " + kmph + ", realistic. Resetting coords to default.", LogLevel.Info);
}
else
{
Logger.Write("Kilometers Per Hour to reach default location since last run: " + kmph + ", not realistic. Continuing from last known location: " + latLngFromFile.Item1 + ", " + latLngFromFile.Item2, LogLevel.Info);
}
}

private async Task CatchEncounter(EncounterResponse encounter, MapPokemon pokemon)
{
CatchPokemonResponse caughtPokemonResponse;
Expand Down
66 changes: 39 additions & 27 deletions PokemonGo.RocketAPI/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ public Client(ISettings settings)

DirectoryInfo di = Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Configs");

Tuple<double, double> latLngFromFile = GetLatLngFromFile();

if (latLngFromFile != null)
{
SetCoordinates(latLngFromFile.Item1, latLngFromFile.Item2, Settings.DefaultAltitude);
}
else
{
SetCoordinates(Settings.DefaultLatitude, Settings.DefaultLongitude, Settings.DefaultAltitude);
}

//Setup HttpClient and create default headers
var handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
AllowAutoRedirect = false
};
_httpClient = new HttpClient(new RetryHandler(handler));
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Niantic App");
//"Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-G900F Build/LMY48G)");
_httpClient.DefaultRequestHeaders.ExpectContinue = false;
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Connection", "keep-alive");
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "*/*");
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type",
"application/x-www-form-urlencoded");
}

/// <summary>
/// Gets the lat LNG from file.
/// </summary>
/// <returns>Tuple&lt;System.Double, System.Double&gt;.</returns>
public static Tuple<double, double> GetLatLngFromFile()
{
if (File.Exists(Directory.GetCurrentDirectory() + "\\Configs\\Coords.ini") &&
File.ReadAllText(Directory.GetCurrentDirectory() + "\\Configs\\Coords.ini").Contains(":"))
{
Expand All @@ -44,49 +77,28 @@ public Client(ISettings settings)
double temp_lat = Convert.ToDouble(latlng[0]);
double temp_long = Convert.ToDouble(latlng[1]);

if(temp_lat >= -90 && temp_lat <= 90 && temp_long >= -180 && temp_long <= 180)
if (temp_lat >= -90 && temp_lat <= 90 && temp_long >= -180 && temp_long <= 180)
{
SetCoordinates(Convert.ToDouble(latlng[0]), Convert.ToDouble(latlng[1]),
Settings.DefaultAltitude);
return new Tuple<double, double>(temp_lat, temp_long);
}
else
{
Logger.Write("Coordinates in \"Coords.ini\" file are invalid, using the default coordinates ",
LogLevel.Warning);
SetCoordinates(Settings.DefaultLatitude, Settings.DefaultLongitude, Settings.DefaultAltitude);
return null;
}
}
catch (FormatException)
{
Logger.Write("Coordinates in \"Coords.ini\" file are invalid, using the default coordinates ",
LogLevel.Warning);
SetCoordinates(Settings.DefaultLatitude, Settings.DefaultLongitude, Settings.DefaultAltitude);
return null;
}
}
else
{
SetCoordinates(Settings.DefaultLatitude, Settings.DefaultLongitude, Settings.DefaultAltitude);
}
}
else
{
SetCoordinates(Settings.DefaultLatitude, Settings.DefaultLongitude, Settings.DefaultAltitude);

}

//Setup HttpClient and create default headers
var handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
AllowAutoRedirect = false
};
_httpClient = new HttpClient(new RetryHandler(handler));
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Niantic App");
//"Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-G900F Build/LMY48G)");
_httpClient.DefaultRequestHeaders.ExpectContinue = false;
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Connection", "keep-alive");
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "*/*");
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type",
"application/x-www-form-urlencoded");
return null;
}

public ISettings Settings { get; }
Expand Down
6 changes: 1 addition & 5 deletions PokemonGo.RocketAPI/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using PokemonGo.RocketAPI.Logging;

#endregion
Expand Down Expand Up @@ -46,8 +45,6 @@ public static void Write(string message, LogLevel level = LogLevel.Info, Console
private static void Log(string message)
{
// maybe do a new log rather than appending?
CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Logs");


Expand All @@ -56,7 +53,6 @@ private static void Log(string message)
log.WriteLine(message);
log.Flush();
}
Thread.CurrentThread.CurrentCulture = originalCulture;
}
}

Expand All @@ -76,4 +72,4 @@ public enum LogLevel
Info = 11,
Debug = 12
}
}
}

0 comments on commit 493798e

Please sign in to comment.