From abda958d9e5b0c3ae5f8b0f7b3b8d8ab14691b39 Mon Sep 17 00:00:00 2001 From: Nikolay Kostov Date: Thu, 7 Apr 2016 11:35:16 +0300 Subject: [PATCH] Total game result tracking --- .../MainPage.xaml.cs | 29 +++++---- .../Santase.UI.WindowsUniversal.csproj | 1 + .../TotalResultPersister.cs | 62 +++++++++++++++++++ 3 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 Source/UI/Santase.UI.WindowsUniversal/TotalResultPersister.cs diff --git a/Source/UI/Santase.UI.WindowsUniversal/MainPage.xaml.cs b/Source/UI/Santase.UI.WindowsUniversal/MainPage.xaml.cs index 153176b..daa0009 100644 --- a/Source/UI/Santase.UI.WindowsUniversal/MainPage.xaml.cs +++ b/Source/UI/Santase.UI.WindowsUniversal/MainPage.xaml.cs @@ -21,24 +21,17 @@ public sealed partial class MainPage private readonly SantaseGame game; + private readonly TotalResultPersister resultPersister; + private readonly CardControl[] playerCardControls; public MainPage() { this.InitializeComponent(); - if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop") - { - this.AdRow.Height = new GridLength(90); - this.AdMediator_2D55AF.Height = 90; - this.AdMediator_2D55AF.Width = 768; - } - else - { - this.AdRow.Height = new GridLength(80); - this.AdMediator_2D55AF.Height = 80; - this.AdMediator_2D55AF.Width = 480; - } + this.resultPersister = new TotalResultPersister(); + this.TotalResult.Text = + $"{this.resultPersister.PlayerScore}-{this.resultPersister.OtherPlayerScore}"; this.playerCardControls = new[] { @@ -247,7 +240,17 @@ private void UiPlayerOnGameClosed(object sender, EventArgs eventArgs) private void UiPlayerOnGameEnded(object sender, bool amIWinner) { - // TODO: Inform player for the game result + this.resultPersister.Update(amIWinner); +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + this.Dispatcher.RunAsync( +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + CoreDispatcherPriority.Normal, + () => + { + this.TotalResult.Text = + $"{this.resultPersister.PlayerScore}-{this.resultPersister.OtherPlayerScore}"; + }); + Task.Run(() => this.game.Start()); } diff --git a/Source/UI/Santase.UI.WindowsUniversal/Santase.UI.WindowsUniversal.csproj b/Source/UI/Santase.UI.WindowsUniversal/Santase.UI.WindowsUniversal.csproj index e7244b3..02fb2fe 100644 --- a/Source/UI/Santase.UI.WindowsUniversal/Santase.UI.WindowsUniversal.csproj +++ b/Source/UI/Santase.UI.WindowsUniversal/Santase.UI.WindowsUniversal.csproj @@ -115,6 +115,7 @@ MainPage.xaml + diff --git a/Source/UI/Santase.UI.WindowsUniversal/TotalResultPersister.cs b/Source/UI/Santase.UI.WindowsUniversal/TotalResultPersister.cs new file mode 100644 index 0000000..1e904a4 --- /dev/null +++ b/Source/UI/Santase.UI.WindowsUniversal/TotalResultPersister.cs @@ -0,0 +1,62 @@ +namespace Santase.UI.WindowsUniversal +{ + using Windows.Storage; + + public class TotalResultPersister + { + private const string PlayerValueName = "PlayerScore"; + private const string OtherPlayerValueName = "OtherPlayerScore"; + + private int playerScore; + private int otherPlayerScore; + + public TotalResultPersister() + { + try + { + if (!ApplicationData.Current.LocalSettings.Values.ContainsKey(PlayerValueName)) + { + ApplicationData.Current.LocalSettings.Values.Add(PlayerValueName, 0); + } + + if (!ApplicationData.Current.LocalSettings.Values.ContainsKey(OtherPlayerValueName)) + { + ApplicationData.Current.LocalSettings.Values.Add(OtherPlayerValueName, 0); + } + + int.TryParse(ApplicationData.Current.LocalSettings.Values[PlayerValueName].ToString(), out this.playerScore); + int.TryParse(ApplicationData.Current.LocalSettings.Values[OtherPlayerValueName].ToString(), out this.otherPlayerScore); + } + catch + { + this.playerScore = 0; + this.otherPlayerScore = 0; + } + } + + public int PlayerScore => this.playerScore; + + public int OtherPlayerScore => this.otherPlayerScore; + + public void Update(bool playerWins) + { + if (playerWins) + { + this.playerScore++; + } + else + { + this.otherPlayerScore++; + } + + try + { + ApplicationData.Current.LocalSettings.Values[PlayerValueName] = this.playerScore.ToString(); + ApplicationData.Current.LocalSettings.Values[OtherPlayerValueName] = this.otherPlayerScore.ToString(); + } + catch + { + } + } + } +}