Skip to content

Commit

Permalink
Total game result tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayIT committed Apr 7, 2016
1 parent 08ab930 commit abda958
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
29 changes: 16 additions & 13 deletions Source/UI/Santase.UI.WindowsUniversal/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
{
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TotalResultPersister.cs" />
<Compile Include="UiPlayer.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
62 changes: 62 additions & 0 deletions Source/UI/Santase.UI.WindowsUniversal/TotalResultPersister.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
}
}
}

0 comments on commit abda958

Please sign in to comment.