From b555f8b849e9a424eb660b9ed9abda8d76d52af1 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Thu, 6 Jun 2024 16:45:50 +0200 Subject: [PATCH] Game save + Leaderboard save + Leaderboard load --- .../Players/Leaderboard.cs | 10 ++--- Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs | 38 ++++++++----------- Qwirkle/QwirkleViews/App.xaml.cs | 9 ++++- Qwirkle/QwirkleViews/MainPage.xaml | 4 +- Qwirkle/QwirkleViews/MainPage.xaml.cs | 3 +- Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs | 15 +++++++- Qwirkle/QwirkleViews/Pages/Leaderboard.xaml | 34 +++++++++-------- .../QwirkleViews/Pages/Leaderboard.xaml.cs | 2 +- Qwirkle/TestBase/TestLeaderboard.cs | 4 +- 9 files changed, 64 insertions(+), 55 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs index 8d40223..d85d4ee 100644 --- a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs +++ b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs @@ -16,7 +16,7 @@ namespace QwirkleClassLibrary.Players public class Leaderboard : INotifyPropertyChanged { [DataMember] - private readonly ObservableCollection leaderboard = new(); + private ObservableCollection leaderboard = new(); public event PropertyChangedEventHandler? PropertyChanged; @@ -26,12 +26,7 @@ namespace QwirkleClassLibrary.Players } public ReadOnlyObservableCollection Lb => new(leaderboard); - - - - - - + /// /// Returns the index of the player in the leaderboard, -1 if the player is not in the leaderboard /// @@ -96,6 +91,7 @@ namespace QwirkleClassLibrary.Players first = false; } + leaderboard = new ObservableCollection(leaderboard.OrderByDescending(x => x.Points).ThenBy(x => x.Victories)); } } } diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs index 8f096dc..05d5c92 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs @@ -15,7 +15,7 @@ namespace QwirkleClassLibrary.Tiles private readonly List tiles = []; public ReadOnlyCollection? TilesBag { get; private set; } - + /// /// This is the constructor for the TileBag. It will create a tile of each of the possibilities among the Color and Shape Enums. @@ -24,29 +24,23 @@ namespace QwirkleClassLibrary.Tiles /// Throw an exception if the number of copies is negative (impossible) or superior to 3 (contradiction with the rules). public TileBag(int nbSet) { - /* if (nbSet < 0 || nbSet > 3) - { - throw new ArgumentException(nbSet.ToString()); - } - - for (int i = 0; i < nbSet; i++) - { - foreach (Shape s in Enum.GetValues(typeof(Shape))) - { - foreach (Color c in Enum.GetValues(typeof(Color))) - { - Tile t = new(s, c); - tiles.Add(t); - } - } - }*/ + if (nbSet < 0 || nbSet > 3) + { + throw new ArgumentException(nbSet.ToString()); + } - Tile t1 = new Tile(Shape.Club, Color.Yellow); - Tile t2 = new Tile(Shape.Round, Color.Orange); + for (int i = 0; i < nbSet; i++) + { + foreach (Shape s in Enum.GetValues(typeof(Shape))) + { + foreach (Color c in Enum.GetValues(typeof(Color))) + { + Tile t = new(s, c); + tiles.Add(t); + } + } + } - tiles.Add(t1); - tiles.Add(t2); - Init(); } diff --git a/Qwirkle/QwirkleViews/App.xaml.cs b/Qwirkle/QwirkleViews/App.xaml.cs index 12606b5..5a8252b 100644 --- a/Qwirkle/QwirkleViews/App.xaml.cs +++ b/Qwirkle/QwirkleViews/App.xaml.cs @@ -2,7 +2,9 @@ using Microsoft.Maui.Controls; using Qwirkle.Pages; using QwirkleClassLibrary.Games; +using QwirkleClassLibrary.Persistences; using QwirkleClassLibrary.Players; +using Leaderboard = QwirkleClassLibrary.Players.Leaderboard; namespace Qwirkle { @@ -10,6 +12,7 @@ namespace Qwirkle { public App() { + Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\Files")); InitializeComponent(); MainPage = new AppShell(); @@ -20,10 +23,12 @@ namespace Qwirkle Routing.RegisterRoute(nameof(MainPage), typeof(MainPage)); Routing.RegisterRoute(nameof(Qwirkle.Pages.Leaderboard), typeof(Qwirkle.Pages.Leaderboard)); + ILeaderboardPersistence leaderboardLoad = new LeaderboardPersistenceJson(); + Ld = leaderboardLoad.LoadLeaderboard(); } + public Game Game { get; set; } = new(); - public QwirkleClassLibrary.Players.Leaderboard LD { get; set; } = new(); - + public Leaderboard Ld { get; set; } } } diff --git a/Qwirkle/QwirkleViews/MainPage.xaml b/Qwirkle/QwirkleViews/MainPage.xaml index 607867c..c8eb72a 100644 --- a/Qwirkle/QwirkleViews/MainPage.xaml +++ b/Qwirkle/QwirkleViews/MainPage.xaml @@ -6,7 +6,7 @@ - + \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/Pages/Leaderboard.xaml.cs b/Qwirkle/QwirkleViews/Pages/Leaderboard.xaml.cs index 85be440..83f3139 100644 --- a/Qwirkle/QwirkleViews/Pages/Leaderboard.xaml.cs +++ b/Qwirkle/QwirkleViews/Pages/Leaderboard.xaml.cs @@ -5,6 +5,6 @@ public partial class Leaderboard : ContentPage public Leaderboard() { InitializeComponent(); - BindingContext = ((App)Application.Current!).LD; + BindingContext = ((App)Application.Current!).Ld; } } \ No newline at end of file diff --git a/Qwirkle/TestBase/TestLeaderboard.cs b/Qwirkle/TestBase/TestLeaderboard.cs index ee860c5..6c0d9fe 100644 --- a/Qwirkle/TestBase/TestLeaderboard.cs +++ b/Qwirkle/TestBase/TestLeaderboard.cs @@ -50,8 +50,6 @@ public class TestLeaderboard leaderboard.AddScoreInLead(game.ScoreBoard); - var game2 = new Game(); - game.AddPlayerInGame(playerstest); game.StartGame(); @@ -61,7 +59,7 @@ public class TestLeaderboard leaderboard.AddScoreInLead(game.ScoreBoard); - Assert.Equal(2, leaderboard.Lb[0].Victories); + Assert.Equal(2, leaderboard.Lb[1].Victories); }