Skip to content

Commit

Permalink
Merging
Browse files Browse the repository at this point in the history
  • Loading branch information
BornSupercharged committed Jul 31, 2016
2 parents 8399e09 + 87ecdb0 commit af17df2
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 62 deletions.
1 change: 0 additions & 1 deletion PoGo.NecroBot.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ private static void Main(string[] args)

CultureInfo.DefaultThreadCurrentCulture = culture;
Thread.CurrentThread.CurrentCulture = culture;

var subPath = "";
if (args.Length > 0)
subPath = args[0];
Expand Down
25 changes: 25 additions & 0 deletions PoGo.NecroBot.CLI/WebSocketInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ private async void HandleMessage(WebSocketSession session, string message)
case "EggsList":
await EggsListTask.Execute(_session);
break;
case "InventoryList":
await InventoryListTask.Execute(_session);
break;
}

// Setup to only send data back to the session that requested it.
try
{
dynamic decodedMessage = JObject.Parse(message);
await _websocketHandler?.Handle(_session, session, decodedMessage);
}
catch (JsonException ex)
{


}

// Setup to only send data back to the session that requested it.
Expand All @@ -113,6 +128,16 @@ private void HandleSession(WebSocketSession session)

if (_lastPokeStopList != null)
session.Send(Serialize(_lastPokeStopList));

try
{
session.Send(Serialize(new UpdatePositionEvent()
{
Latitude = _session.Client.CurrentLatitude,
Longitude = _session.Client.CurrentLongitude
}));
}
catch { }
}

public void Listen(IEvent evt, Session session)
Expand Down
14 changes: 14 additions & 0 deletions PoGo.NecroBot.Logic/Event/InventoryListEvent.Cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using POGOProtos.Inventory.Item;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PoGo.NecroBot.Logic.Event
{
public class InventoryListEvent : IEvent
{
public List<ItemData> Items;
}
}
2 changes: 2 additions & 0 deletions PoGo.NecroBot.Logic/Event/PokemonCaptureEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public class PokemonCaptureEvent : IEvent
public double Probability;
public int Stardust;
public CatchPokemonResponse.Types.CatchStatus Status;
public double Latitude;
public double Longitude;
}
}
1 change: 1 addition & 0 deletions PoGo.NecroBot.Logic/ILogicSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public TransferFilter(int keepMinCp, float keepMinIvPercentage, int keepMinDupli

public interface ILogicSettings
{
bool DisableHumanWalking { get; }
bool AutoUpdate { get; }
bool TransferConfigAndAuthOnUpdate { get; }
float KeepMinIvPercentage { get; }
Expand Down
100 changes: 56 additions & 44 deletions PoGo.NecroBot.Logic/Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,70 +31,82 @@ public Navigation(Client client)
_client = client;
}

public async Task<PlayerUpdateResponse> HumanLikeWalking(GeoCoordinate targetLocation,
public async Task<PlayerUpdateResponse> Move(GeoCoordinate targetLocation,
double walkingSpeedInKilometersPerHour, Func<Task<bool>> functionExecutedWhileWalking,
CancellationToken cancellationToken)
CancellationToken cancellationToken, bool disableHumanLikeWalking)
{
cancellationToken.ThrowIfCancellationRequested();

var speedInMetersPerSecond = walkingSpeedInKilometersPerHour/3.6;
if (!disableHumanLikeWalking)
{
var speedInMetersPerSecond = walkingSpeedInKilometersPerHour / 3.6;

var sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation);
// Logger.Write($"Distance to target location: {distanceToTarget:0.##} meters. Will take {distanceToTarget/speedInMetersPerSecond:0.##} seconds!", LogLevel.Info);
var sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation);
// Logger.Write($"Distance to target location: {distanceToTarget:0.##} meters. Will take {distanceToTarget/speedInMetersPerSecond:0.##} seconds!", LogLevel.Info);

var nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
var nextWaypointDistance = speedInMetersPerSecond;
var waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);
var nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
var nextWaypointDistance = speedInMetersPerSecond;
var waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);

//Initial walking
var requestSendDateTime = DateTime.Now;
var result =
await
_client.Player.UpdatePlayerLocation(waypoint.Latitude, waypoint.Longitude,
_client.Settings.DefaultAltitude);
//Initial walking
var requestSendDateTime = DateTime.Now;
var result =
await
_client.Player.UpdatePlayerLocation(waypoint.Latitude, waypoint.Longitude,
_client.Settings.DefaultAltitude);

UpdatePositionEvent?.Invoke(waypoint.Latitude, waypoint.Longitude);
UpdatePositionEvent?.Invoke(waypoint.Latitude, waypoint.Longitude);

do
{
cancellationToken.ThrowIfCancellationRequested();
do
{
cancellationToken.ThrowIfCancellationRequested();

var millisecondsUntilGetUpdatePlayerLocationResponse =
(DateTime.Now - requestSendDateTime).TotalMilliseconds;
var millisecondsUntilGetUpdatePlayerLocationResponse =
(DateTime.Now - requestSendDateTime).TotalMilliseconds;

sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
var currentDistanceToTarget = LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation);
sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
var currentDistanceToTarget = LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation);

if (currentDistanceToTarget < 40)
{
if (speedInMetersPerSecond > SpeedDownTo)
if (currentDistanceToTarget < 40)
{
//Logger.Write("We are within 40 meters of the target. Speeding down to 10 km/h to not pass the target.", LogLevel.Info);
speedInMetersPerSecond = SpeedDownTo;
if (speedInMetersPerSecond > SpeedDownTo)
{
//Logger.Write("We are within 40 meters of the target. Speeding down to 10 km/h to not pass the target.", LogLevel.Info);
speedInMetersPerSecond = SpeedDownTo;
}
}
}

nextWaypointDistance = Math.Min(currentDistanceToTarget,
millisecondsUntilGetUpdatePlayerLocationResponse/1000*speedInMetersPerSecond);
nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);
nextWaypointDistance = Math.Min(currentDistanceToTarget,
millisecondsUntilGetUpdatePlayerLocationResponse / 1000 * speedInMetersPerSecond);
nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);

requestSendDateTime = DateTime.Now;
result =
await
_client.Player.UpdatePlayerLocation(waypoint.Latitude, waypoint.Longitude,
_client.Settings.DefaultAltitude);
requestSendDateTime = DateTime.Now;
result =
await
_client.Player.UpdatePlayerLocation(waypoint.Latitude, waypoint.Longitude,
_client.Settings.DefaultAltitude);

UpdatePositionEvent?.Invoke(waypoint.Latitude, waypoint.Longitude);
UpdatePositionEvent?.Invoke(waypoint.Latitude, waypoint.Longitude);


if (functionExecutedWhileWalking != null)
await functionExecutedWhileWalking(); // look for pokemon
await Task.Delay(500, cancellationToken);
} while (LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation) >= 30);
if (functionExecutedWhileWalking != null)
await functionExecutedWhileWalking(); // look for pokemon
await Task.Delay(500, cancellationToken);
} while (LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation) >= 30);

return result;
}
else
{
var result =
await
_client.Player.UpdatePlayerLocation(targetLocation.Latitude, targetLocation.Longitude,
_client.Settings.DefaultAltitude);
return result;
}

return result;
}

public async Task<PlayerUpdateResponse> HumanPathWalking(GpxReader.Trkpt trk,
Expand Down
2 changes: 2 additions & 0 deletions PoGo.NecroBot.Logic/PoGo.NecroBot.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Event\FortTargetEvent.cs" />
<Compile Include="Event\FortFailedEvent.cs" />
<Compile Include="Event\FortUsedEvent.cs" />
<Compile Include="Event\InventoryListEvent.cs" />
<Compile Include="Event\ItemRecycledEvent.cs" />
<Compile Include="Event\NoPokeballEvent.cs" />
<Compile Include="Event\SnipeModeEvent.cs" />
Expand All @@ -90,6 +91,7 @@
<Compile Include="Service\BotService.cs" />
<Compile Include="Tasks\EggsListTask.cs" />
<Compile Include="Tasks\FavoritePokemonTask.cs" />
<Compile Include="Tasks\InventoryListTask.cs" />
<Compile Include="Tasks\LevelUpPokemonTask.cs" />
<Compile Include="Tasks\PokemonListTask.cs" />
<Compile Include="Tasks\Farm.cs" />
Expand Down
15 changes: 5 additions & 10 deletions PoGo.NecroBot.Logic/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ internal class AuthSettings
[JsonIgnore] private string _filePath;

public AuthType AuthType;
public string GooglePassword;

public string GoogleRefreshToken;
public string GoogleUsername;
public string GooglePassword;
public string PtcPassword;
public string PtcUsername;

Expand Down Expand Up @@ -109,6 +107,7 @@ public class GlobalSettings
public double DefaultLongitude = -73.968285;
public int DelayBetweenPlayerActions = 5000;
public int DelayBetweenPokemonCatch = 2000;
public bool DisableHumanWalking = false;
public bool DumpPokemonStats = false;
public float EvolveAboveIvValue = 95;
public bool EvolveAllPokemonAboveIv = false;
Expand Down Expand Up @@ -499,14 +498,9 @@ public ClientSettings(GlobalSettings settings)

public string GoogleRefreshToken
{
get { return _settings.Auth.GoogleRefreshToken; }
set
{
_settings.Auth.GoogleRefreshToken = value;
_settings.Auth.Save();
}
get { return null; }
set { GoogleRefreshToken = null; }
}

AuthType ISettings.AuthType
{
get { return _settings.Auth.AuthType; }
Expand Down Expand Up @@ -586,6 +580,7 @@ public LogicSettings(GlobalSettings settings)
public string GeneralConfigPath => _settings.GeneralConfigPath;
public bool AutoUpdate => _settings.AutoUpdate;
public bool TransferConfigAndAuthOnUpdate => _settings.TransferConfigAndAuthOnUpdate;
public bool DisableHumanWalking => _settings.DisableHumanWalking;
public float KeepMinIvPercentage => _settings.KeepMinIvPercentage;
public int KeepMinCp => _settings.KeepMinCp;
public bool AutomaticallyLevelUpPokemon => _settings.AutomaticallyLevelUpPokemon;
Expand Down
13 changes: 13 additions & 0 deletions PoGo.NecroBot.Logic/State/LoginState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public async Task<IState> Execute(ISession session, CancellationToken cancellati
await Task.Delay(20000, cancellationToken);
return this;
}
catch (AccessTokenExpiredException)
{
session.EventDispatcher.Send(new ErrorEvent
{
Message = session.Translation.GetTranslation(TranslationString.PtcOffline) // use ptcoffline for now.
});
session.EventDispatcher.Send(new NoticeEvent
{
Message = session.Translation.GetTranslation(TranslationString.TryingAgainIn, 20)
});
await Task.Delay(20000, cancellationToken);
return this;
}
catch (AccountNotVerifiedException)
{
session.EventDispatcher.Send(new ErrorEvent
Expand Down
14 changes: 12 additions & 2 deletions PoGo.NecroBot.Logic/Tasks/CatchPokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,17 @@ encounter is EncounterResponse || encounter is IncenseEncounterResponse
? pokemon.SpawnPointId
: currentFortData.Id, pokeball);

var evt = new PokemonCaptureEvent {Status = caughtPokemonResponse.Status};
var lat = encounter is EncounterResponse || encounter is IncenseEncounterResponse
? pokemon.Latitude : currentFortData.Latitude;
var lng = encounter is EncounterResponse || encounter is IncenseEncounterResponse
? pokemon.Longitude : currentFortData.Longitude;
var evt = new PokemonCaptureEvent()
{
Status = caughtPokemonResponse.Status,
Latitude = lat,
Longitude = lng
};


if (caughtPokemonResponse.Status == CatchPokemonResponse.Types.CatchStatus.CatchSuccess)
{
Expand Down Expand Up @@ -144,7 +154,7 @@ encounter is EncounterResponse || encounter is IncenseEncounterResponse
? encounter.WildPokemon?.PokemonData
: encounter?.PokemonData));
evt.Probability =
Math.Round(probability*100, 2);
Math.Round(probability * 100, 2);
evt.Distance = distance;
evt.Pokeball = pokeball;
evt.Attempt = attemptCounter;
Expand Down
8 changes: 4 additions & 4 deletions PoGo.NecroBot.Logic/Tasks/FarmPokestopsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static async Task Execute(ISession session, CancellationToken cancellatio

await Task.Delay(1000, cancellationToken);

await session.Navigation.HumanLikeWalking(
await session.Navigation.Move(
new GeoCoordinate(session.Settings.DefaultLatitude, session.Settings.DefaultLongitude),
session.LogicSettings.WalkingSpeedInKilometerPerHour, null, cancellationToken);
session.LogicSettings.WalkingSpeedInKilometerPerHour, null, cancellationToken, session.LogicSettings.DisableHumanWalking);
}

var pokestopList = await GetPokeStops(session);
Expand Down Expand Up @@ -79,7 +79,7 @@ await session.Navigation.HumanLikeWalking(

session.EventDispatcher.Send(new FortTargetEvent {Name = fortInfo.Name, Distance = distance});

await session.Navigation.HumanLikeWalking(new GeoCoordinate(pokeStop.Latitude, pokeStop.Longitude),
await session.Navigation.Move(new GeoCoordinate(pokeStop.Latitude, pokeStop.Longitude),
session.LogicSettings.WalkingSpeedInKilometerPerHour,
async () =>
{
Expand All @@ -88,7 +88,7 @@ await session.Navigation.HumanLikeWalking(new GeoCoordinate(pokeStop.Latitude, p
//Catch Incense Pokemon
await CatchIncensePokemonsTask.Execute(session, cancellationToken);
return true;
}, cancellationToken);
}, cancellationToken, session.LogicSettings.DisableHumanWalking);

//Catch Lure Pokemon
if (pokeStop.LureInfo != null)
Expand Down
24 changes: 24 additions & 0 deletions PoGo.NecroBot.Logic/Tasks/InventoryListTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.State;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PoGo.NecroBot.Logic.Tasks
{
public class InventoryListTask
{
public static async Task Execute(ISession session)
{
var inventory = await session.Inventory.GetItems();

session.EventDispatcher.Send(
new InventoryListEvent
{
Items = inventory.ToList()
});
}
}
}
Loading

1 comment on commit af17df2

@TheTravelingTrainer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful here! Dunno if it was a merge issue or copypasta issue.. But some code got repeated.... Don't think it was me!

Please sign in to comment.