From 919a29e3768e7721171f15865ce3a815370ccf24 Mon Sep 17 00:00:00 2001 From: p2111528 Date: Wed, 8 Jun 2022 07:57:23 +0200 Subject: [PATCH] Sort scoreboard by points and update when game over --- Tetris/ViewsModels/GameViewModel.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Tetris/ViewsModels/GameViewModel.cs b/Tetris/ViewsModels/GameViewModel.cs index 9403ff4..0105249 100644 --- a/Tetris/ViewsModels/GameViewModel.cs +++ b/Tetris/ViewsModels/GameViewModel.cs @@ -33,7 +33,10 @@ public class GameViewModel : INotifyPropertyChanged private string _scoreText = ""; private string _userNameText = ""; - public GameViewModel() { + public GameViewModel() + { + fetchScoreBoard(); + _writeableBitmap = BitmapFactory.New(_width, _height); var dispatcherRenderTimer = new DispatcherTimer @@ -65,10 +68,15 @@ public class GameViewModel : INotifyPropertyChanged } } + private List _scoreBoard; + public List ScoreBoard { get { - using var db = new ScoreContext(); - return db.Scores.ToList(); + return _scoreBoard; + } + set { + _scoreBoard = value; + OnPropertyChanged("ScoreBoard"); } } @@ -82,6 +90,14 @@ public class GameViewModel : INotifyPropertyChanged } } + private void fetchScoreBoard() + { + using var db = new ScoreContext(); + List scores = db.Scores.ToList(); + scores.Sort((x, y) => y.Points.CompareTo(x.Points)); + ScoreBoard = scores; + } + private void Render(object? sender, EventArgs eventArgs) { _writeableBitmap.Lock(); @@ -154,6 +170,7 @@ public class GameViewModel : INotifyPropertyChanged Game.Playing = false; Game.PrintTetrominoe(); Game.SaveGame(); + fetchScoreBoard(); } } } \ No newline at end of file