Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #66 from JFouts/forcedDataUpdateBug
Browse files Browse the repository at this point in the history
Correct the issue with forced update, #15
  • Loading branch information
ST-Apps authored Aug 1, 2016
2 parents 5d08f8b + e6a3749 commit 1823d25
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions PokemonGo-UWP/ViewModels/GameManagerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Template10.Mvvm;
using Template10.Services.NavigationService;
using Universal_Authenticator_v2.Views;
using System.Threading;

namespace PokemonGo_UWP.ViewModels
{
Expand Down Expand Up @@ -166,6 +167,16 @@ public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
/// </summary>
private ThreadPoolTimer _updateDataTimer;

/// <summary>
/// Mutex to secure the parallel access to the data update
/// </summary>
private Mutex _updateDataMutex = new Mutex();

/// <summary>
/// If a forced refresh caused an update we should skip the next update
/// </summary>
private bool _skipNextUpdate = false;

#endregion

#region Bindable Game Vars
Expand Down Expand Up @@ -340,8 +351,20 @@ public async Task InitGame(bool hadAuthTokenStored = false)
_updateDataTimer = ThreadPoolTimer.CreatePeriodicTimer(t =>
{
if (_stopUpdatingMap) return;
Logger.Write("Updating map");
UpdateMapData(false);
if (_updateDataMutex.WaitOne(0))
{
if (_skipNextUpdate)
{
_skipNextUpdate = false;
}
else
{
Logger.Write("Updating map");
UpdateMapData(false);
}

_updateDataMutex.ReleaseMutex();
}
}, TimeSpan.FromSeconds(15));
}
}
Expand Down Expand Up @@ -450,6 +473,16 @@ private async void HandleException()
});
}

private void ForcedUpdateMapData()
{
if (_updateDataMutex.WaitOne(0))
{
_skipNextUpdate = true;
UpdateMapData(true);
_updateDataMutex.ReleaseMutex();
}
}

/// <summary>
/// Retrieves data for the current position
/// </summary>
Expand Down Expand Up @@ -605,7 +638,7 @@ await Dispatcher.DispatchAsync(() =>
{
// Encounter failed, probably the Pokemon ran away
await new MessageDialog("Pokemon ran away, sorry :(").ShowAsyncQueue();
UpdateMapData();
ForcedUpdateMapData();
}
}, pokemon => true)
);
Expand Down Expand Up @@ -658,24 +691,23 @@ private async Task ThrowPokeball()
CurrentCaptureScore = caughtPokemonResponse.Scores;
Logger.Write($"We caught {CurrentPokemon.PokemonId}");
CatchSuccess?.Invoke(this, null);
UpdateMapData();
ForcedUpdateMapData();
UpdateInventory();
UpdatePlayerData();
break;
case CatchPokemonResponse.Types.CatchStatus.CatchEscape:
CurrentCaptureScore = caughtPokemonResponse.Scores;
Logger.Write($"{CurrentPokemon.PokemonId} escaped");
CatchEscape?.Invoke(this, null);
await new MessageDialog($"{CurrentPokemon.PokemonId} escaped").ShowAsyncQueue();
UpdateMapData();
ForcedUpdateMapData();
UpdateInventory();
UpdatePlayerData();
break;
case CatchPokemonResponse.Types.CatchStatus.CatchFlee:
Logger.Write($"{CurrentPokemon.PokemonId} fleed");
CatchEscape?.Invoke(this, null);
await new MessageDialog($"{CurrentPokemon.PokemonId} fleed").ShowAsyncQueue();
UpdateMapData();
ForcedUpdateMapData();
UpdateInventory();
ReturnToGameScreen.Execute();
break;
Expand Down

0 comments on commit 1823d25

Please sign in to comment.