using CoreLibrary.Joueurs; using CoreLibrary.Statistiques; using CoreLibrary.Regles; using System.ComponentModel; using System.Runtime.CompilerServices; namespace MauiSpark.Pages { partial class Enfant { private readonly Classement classement; public Joueur Joueur { get; set; } public int Place => classement.Enfants.ToList().IndexOf(this) + 1; public IEnumerable Statistiques => Enum.GetValues() .Select(statistique => Joueur.Statistique(classement.Regles, statistique)); public IEnumerable Objets => new List([$"{Place}", Joueur.Nom]) .Concat(Statistiques.Select(statistique => $"{statistique}")); public Enfant(Joueur joueur, Classement classement) { this.classement = classement; Joueur = joueur; } public override bool Equals(object? obj) { if (obj == null || obj is not Enfant) return false; return Joueur == ((Enfant)obj).Joueur; } public override int GetHashCode() => Joueur.GetHashCode(); } partial class Classement : INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; public void QuandProprieteChangee([CallerMemberName] string? propriete = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propriete)); private int indiceRegles = 0; public IRegles Regles => ClassementPage.ToutesRegles.ElementAt(indiceRegles); private Statistique statistique = Enum.GetValues().First(); public Statistique Statistique { get => statistique; set { statistique = value; QuandProprieteChangee(nameof(Enfants)); } } private bool inverser = true; public bool Inverser { get => inverser; set { inverser = value; QuandProprieteChangee(nameof(Enfants)); } } public IEnumerable Titres => new List(["Place", "Nom"]).Concat( Enum.GetValues().Select( statistique => string.Concat((Enum.GetName(typeof(Statistique), statistique) ?? "").Select( (lettre, indice) => indice > 0 && char.IsUpper(lettre) ? $" {lettre}" : $"{lettre}") ) ) ); public IEnumerable Enfants => Inverser ? MauiProgram.Manageur.Joueurs .OrderBy(joueur => joueur.Statistique(Regles, Statistique)) .Select(joueur => new Enfant(joueur, this)).Reverse() : MauiProgram.Manageur.Joueurs .OrderBy(joueur => joueur.Statistique(Regles, Statistique)) .Select(joueur => new Enfant(joueur, this)); public void IncrementerRegles(int valeur) { if ((indiceRegles += valeur) < 0) indiceRegles = ClassementPage.ToutesRegles.Count() - 1; else if (indiceRegles >= ClassementPage.ToutesRegles.Count()) indiceRegles = 0; QuandProprieteChangee(nameof(Regles)); QuandProprieteChangee(nameof(Enfants)); } } public partial class ClassementPage : ContentPage { public static IEnumerable ToutesRegles => typeof(IRegles).Assembly.GetTypes() .Where(type => typeof(IRegles).IsAssignableFrom(type) && type.IsClass) .Select(type => (Activator.CreateInstance(type) as IRegles)!) .OrderBy(regles => regles.Indice); public ClassementPage() { InitializeComponent(); NavigationPage.SetHasNavigationBar(this, false); BindingContext = new Classement(); } public void ChangerReglesPresse(object sender, EventArgs e) { ((Classement)BindingContext).IncrementerRegles(sender == DiminuerRegles ? -1 : 1); } public void StatistiquePressee(object sender, EventArgs e) { if (sender is not Label || !Enum.IsDefined(typeof(Statistique), ((Label)sender).Text.Replace(" ", ""))) return; Statistique statistique = Enum.Parse(((Label)sender).Text.Replace(" ", "")); Classement classement = (Classement)BindingContext; if (classement.Statistique == statistique) { classement.Inverser = !classement.Inverser; return; } classement.Inverser = true; classement.Statistique = statistique; } } }