You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mastermind/Sources/MauiSpark/Pages/ClassementPage.xaml.cs

137 lines
4.8 KiB

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<int> Statistiques => Enum.GetValues<Statistique>()
.Select(statistique => Joueur.Statistique(classement.Regles, statistique));
public IEnumerable<string> Objets => new List<string>([$"{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<Statistique>().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<string> Titres => new List<string>(["Place", "Nom"]).Concat(
Enum.GetValues<Statistique>().Select(
statistique => string.Concat((Enum.GetName(typeof(Statistique), statistique) ?? "").Select(
(lettre, indice) => indice > 0 && char.IsUpper(lettre) ? $" {lettre}" : $"{lettre}")
)
)
);
public IEnumerable<Enfant> 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<IRegles> 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<Statistique>(((Label)sender).Text.Replace(" ", ""));
Classement classement = (Classement)BindingContext;
if (classement.Statistique == statistique)
{
classement.Inverser = !classement.Inverser;
return;
}
classement.Inverser = true;
classement.Statistique = statistique;
}
}
}