From b06f729ffd9056abe99eb86dd8b10c44a2e4837c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Mon, 3 Jun 2024 21:55:01 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9D=97=20Impl=C3=A9mentation=20de=20l'interf?= =?UTF-8?q?ace=20INotifyPropertyChanged=20et=20des=20ObservableCollection?= =?UTF-8?q?=20pour=20l'actualisation=20des=20vues=20et=20l'encapsulation.?= =?UTF-8?q?=20Ajout=20d'une=20m=C3=A9thode=20de=20recherche=20et=20suppres?= =?UTF-8?q?sion=20d'un=20profil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Game/Game.cs | 95 ++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 11 deletions(-) diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs index e80f6bc..9be37a2 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -1,7 +1,10 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; @@ -16,16 +19,59 @@ namespace Models.Game /// The Game class represents a game session in the application. /// It contains all the necessary properties and methods to manage a game, including the game loop, dice rolling, and use of the game rules. /// - public class Game + public class Game : INotifyPropertyChanged { /* Persistence */ public IPersistence PersistenceManager { get; set; } /* List for the game and persistence */ - public List Players { get; set; } - public List Games { get; set; } - public List Maps { get; set; } - public List BestScores { get; set; } + private ObservableCollection _players; + + public ObservableCollection Players + { + get => _players; + set + { + _players = value; + OnPropertyChanged(nameof(Players)); + } + } + + private ObservableCollection _games; + + public ObservableCollection Games + { + get => _games; + set + { + _games = value; + OnPropertyChanged(nameof(Games)); + } + } + + private ObservableCollection _maps; + + public ObservableCollection Maps + { + get => _maps; + set + { + _maps = value; + OnPropertyChanged(nameof(Maps)); + } + } + + private ObservableCollection _bestScores; + + public ObservableCollection BestScores + { + get => _bestScores; + set + { + _bestScores = value; + OnPropertyChanged(nameof(BestScores)); + } + } private bool _isRunning; public bool IsRunning @@ -74,6 +120,17 @@ namespace Models.Game BestScores.Add(bestScore); } + public bool RemovePlayer(string playerName) + { + Player player = Players.FirstOrDefault(p => p.Pseudo == playerName); + if (player == null) + { + return false; + } + Players.Remove(player); + return true; + } + public void LoadData() { var data = PersistenceManager.LoadData(); @@ -101,10 +158,10 @@ namespace Models.Game { PersistenceManager = persistenceManager; - Players = new List(); - Games = new List(); - Maps = new List(); - BestScores = new List(); + Players = new ObservableCollection(); + Games = new ObservableCollection(); + Maps = new ObservableCollection(); + BestScores = new ObservableCollection(); GameRules = new Rules.Rules(); @@ -113,8 +170,10 @@ namespace Models.Game public Game() { - UsedMap = new Map("Map1"); - BestScores = new List(); + Players = new ObservableCollection(); + Games = new ObservableCollection(); + Maps = new ObservableCollection(); + BestScores = new ObservableCollection(); GameRules = new Rules.Rules(); @@ -351,5 +410,19 @@ namespace Models.Game return true; //BoardUpdated?.Invoke(this, EventArgs.Empty); } + + /// + /// Event raised when a property is changed to notify the view. + /// + public event PropertyChangedEventHandler? PropertyChanged; + + /// + /// Trigger the PropertyChanged event for a specific property. + /// + /// Name of the property that changed. + public virtual void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } }