Skip to content

Commit

Permalink
Treat Forced Update Slightly Differently, ST-Apps#15
Browse files Browse the repository at this point in the history
Only allow a timed update if we have not just executed a forced update.
Added a mutex as well that will bail out of either update if we are
currently running an update.
  • Loading branch information
JFouts committed Jul 31, 2016
1 parent 699ce35 commit 096b4d2
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 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,7 +691,7 @@ private async Task ThrowPokeball()
CurrentCaptureScore = caughtPokemonResponse.Scores;
Logger.Write($"We caught {CurrentPokemon.PokemonId}");
CatchSuccess?.Invoke(this, null);
UpdateMapData();
ForcedUpdateMapData();
UpdateInventory();
UpdatePlayerData();
break;
Expand All @@ -667,15 +700,15 @@ private async Task ThrowPokeball()
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 096b4d2

Please sign in to comment.