Implémentation de l'interface INotifyPropertyChanged et des ObservableCollection pour l'actualisation des vues et l'encapsulation. Ajout d'une méthode de recherche et suppression d'un profil

pull/97/head
Rémi LAVERGNE 11 months ago
parent b84558e54e
commit b06f729ffd
No known key found for this signature in database
GPG Key ID: 7BCBAE9031E39160

@ -1,7 +1,10 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow; using System.Threading.Tasks.Dataflow;
@ -16,16 +19,59 @@ namespace Models.Game
/// The Game class represents a game session in the application. /// 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. /// It contains all the necessary properties and methods to manage a game, including the game loop, dice rolling, and use of the game rules.
/// </summary> /// </summary>
public class Game public class Game : INotifyPropertyChanged
{ {
/* Persistence */ /* Persistence */
public IPersistence PersistenceManager { get; set; } public IPersistence PersistenceManager { get; set; }
/* List for the game and persistence */ /* List for the game and persistence */
public List<Player> Players { get; set; } private ObservableCollection<Player> _players;
public List<Game> Games { get; set; }
public List<Map> Maps { get; set; } public ObservableCollection<Player> Players
public List<BestScore> BestScores { get; set; } {
get => _players;
set
{
_players = value;
OnPropertyChanged(nameof(Players));
}
}
private ObservableCollection<Game> _games;
public ObservableCollection<Game> Games
{
get => _games;
set
{
_games = value;
OnPropertyChanged(nameof(Games));
}
}
private ObservableCollection<Map> _maps;
public ObservableCollection<Map> Maps
{
get => _maps;
set
{
_maps = value;
OnPropertyChanged(nameof(Maps));
}
}
private ObservableCollection<BestScore> _bestScores;
public ObservableCollection<BestScore> BestScores
{
get => _bestScores;
set
{
_bestScores = value;
OnPropertyChanged(nameof(BestScores));
}
}
private bool _isRunning; private bool _isRunning;
public bool IsRunning public bool IsRunning
@ -74,6 +120,17 @@ namespace Models.Game
BestScores.Add(bestScore); 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() public void LoadData()
{ {
var data = PersistenceManager.LoadData(); var data = PersistenceManager.LoadData();
@ -101,10 +158,10 @@ namespace Models.Game
{ {
PersistenceManager = persistenceManager; PersistenceManager = persistenceManager;
Players = new List<Player>(); Players = new ObservableCollection<Player>();
Games = new List<Game>(); Games = new ObservableCollection<Game>();
Maps = new List<Map>(); Maps = new ObservableCollection<Map>();
BestScores = new List<BestScore>(); BestScores = new ObservableCollection<BestScore>();
GameRules = new Rules.Rules(); GameRules = new Rules.Rules();
@ -113,8 +170,10 @@ namespace Models.Game
public Game() public Game()
{ {
UsedMap = new Map("Map1"); Players = new ObservableCollection<Player>();
BestScores = new List<BestScore>(); Games = new ObservableCollection<Game>();
Maps = new ObservableCollection<Map>();
BestScores = new ObservableCollection<BestScore>();
GameRules = new Rules.Rules(); GameRules = new Rules.Rules();
@ -351,5 +410,19 @@ namespace Models.Game
return true; return true;
//BoardUpdated?.Invoke(this, EventArgs.Empty); //BoardUpdated?.Invoke(this, EventArgs.Empty);
} }
/// <summary>
/// Event raised when a property is changed to notify the view.
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Trigger the PropertyChanged event for a specific property.
/// </summary>
/// <param name="propertyName">Name of the property that changed.</param>
public virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} }
} }

Loading…
Cancel
Save