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