Skip to content

Commit

Permalink
Merge pull request NecronomiconCoding#1742 from prophetal/master
Browse files Browse the repository at this point in the history
Fix Crash  PtcOfflineException and AccessTokenExpiredException
  • Loading branch information
NecronomiconCoding authored Aug 1, 2016
2 parents 68c5f5a + 323e424 commit cac1b07
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
57 changes: 35 additions & 22 deletions PoGo.NecroBot.Logic/Common/ApiFailureStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.State;
using PokemonGo.RocketAPI.Enums;
using PokemonGo.RocketAPI.Exceptions;
using PokemonGo.RocketAPI.Extensions;

#endregion
Expand All @@ -29,7 +30,7 @@ public async Task<ApiOperation> HandleApiFailure()
await Task.Delay(500);
_retryCount++;

if (_retryCount%5 == 0)
if (_retryCount % 5 == 0)
{
DoLogin();
}
Expand All @@ -44,32 +45,44 @@ public void HandleApiSuccess()

private async void DoLogin()
{
switch (_session.Settings.AuthType)
try
{
case AuthType.Ptc:
try
{
switch (_session.Settings.AuthType)
{
case AuthType.Ptc:
await
_session.Client.Login.DoPtcLogin(_session.Settings.PtcUsername,
_session.Settings.PtcPassword);
}
catch (AggregateException ae)
{
throw ae.Flatten().InnerException;
}
break;
case AuthType.Google:
await
_session.Client.Login.DoGoogleLogin(_session.Settings.GoogleUsername,
_session.Settings.GooglePassword);
break;
default:
_session.EventDispatcher.Send(new ErrorEvent
{
Message = _session.Translation.GetTranslation(TranslationString.WrongAuthType)
});
break;
break;
case AuthType.Google:
await
_session.Client.Login.DoGoogleLogin(_session.Settings.GoogleUsername,
_session.Settings.GooglePassword);
break;
default:
_session.EventDispatcher.Send(new ErrorEvent
{
Message = _session.Translation.GetTranslation(TranslationString.WrongAuthType)
});
break;
}
}
catch (AggregateException ae)
{
throw ae.Flatten().InnerException;
}
catch (Exception ex) when (ex is PtcOfflineException || ex is AccessTokenExpiredException)
{
_session.EventDispatcher.Send(new ErrorEvent
{
Message = _session.Translation.GetTranslation(TranslationString.PtcOffline)
});
_session.EventDispatcher.Send(new NoticeEvent
{
Message = _session.Translation.GetTranslation(TranslationString.TryingAgainIn, 20)
});
}

}
}
}
19 changes: 8 additions & 11 deletions PoGo.NecroBot.Logic/State/LoginState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ public async Task<IState> Execute(ISession session, CancellationToken cancellati
switch (session.Settings.AuthType)
{
case AuthType.Ptc:
try
{
await
session.Client.Login.DoPtcLogin(session.Settings.PtcUsername,
session.Settings.PtcPassword);
}
catch (AggregateException ae)
{
throw ae.Flatten().InnerException;
}
await
session.Client.Login.DoPtcLogin(session.Settings.PtcUsername,
session.Settings.PtcPassword);
break;
case AuthType.Google:
await
Expand All @@ -57,6 +50,10 @@ public async Task<IState> Execute(ISession session, CancellationToken cancellati
return null;
}
}
catch (AggregateException ae)
{
throw ae.Flatten().InnerException;
}
catch (Exception ex) when (ex is PtcOfflineException || ex is AccessTokenExpiredException)
{
session.EventDispatcher.Send(new ErrorEvent
Expand Down Expand Up @@ -173,7 +170,7 @@ private static async Task CheckLogin(ISession session, CancellationToken cancell
public async Task DownloadProfile(ISession session)
{
session.Profile = await session.Client.Player.GetPlayer();
session.EventDispatcher.Send(new ProfileEvent {Profile = session.Profile});
session.EventDispatcher.Send(new ProfileEvent { Profile = session.Profile });
}
}
}

0 comments on commit cac1b07

Please sign in to comment.