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/PlateauPage.xaml.cs

109 lines
3.0 KiB

using CoreLibrary.Core;
using CoreLibrary.Evenements;
using CoreLibrary.Exceptions;
namespace MauiSpark.Pages;
internal class Tour
{
public IEnumerable<(IEnumerable<Jeton>, IEnumerable<Indicateur>)> Plateau { get; private init; }
public string Joueur { get; private init; }
public string Numero { get; private init; }
public Code Code { get; private init; }
public bool EstJoueur { get; private init; }
public Tour(PartieNouveauTourEventArgs e)
{
Numero = $"Tour {e.Tour}";
Joueur = e.Nom;
Code = e.Code;
EstJoueur = e.EstJoueur;
(IReadOnlyList<IReadOnlyList<Jeton>> jetons, IReadOnlyList < IReadOnlyList < Indicateur >> indicateurs) = e.Plateau.Grille;
List<(IEnumerable<Jeton>, IEnumerable<Indicateur>)> plateau = new List<(IEnumerable<Jeton>, IEnumerable<Indicateur>)>();
for (int i = 0; i < e.Plateau.TailleMax; ++i)
{
if(i >= jetons.Count)
{
plateau.Add(([], []));
continue;
}
plateau.Add((jetons.ElementAt(i), indicateurs.ElementAt(i)));
}
Plateau = plateau;
}
}
public partial class PlateauPage : ContentPage
{
private Code? code;
private Plateau? plateau;
private bool? estJoueur;
public PlateauPage()
{
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
}
public async void QuandNouveauTour(object? sender, PartieNouveauTourEventArgs e)
{
if (Application.Current == null || Application.Current.MainPage == null)
return;
if (((NavigationPage)Application.Current!.MainPage).CurrentPage != this)
await Application.Current.MainPage.Navigation.PushAsync(this);
IEnumerable<Page> pages = Application.Current.MainPage.Navigation.NavigationStack.Reverse().Skip(1);
foreach (Page page in pages)
{
if (page is AccueilPage)
break;
Application.Current.MainPage.Navigation.RemovePage(page);
}
code = e.Code;
plateau = e.Plateau;
estJoueur = e.EstJoueur;
BindingContext = new Tour(e);
}
private void SupprimerDernierJeton(Object sender, EventArgs e)
{
try
{
if(code != null && estJoueur.HasValue && estJoueur.Value)
code.SupprimerDernierJeton();
}
catch(CodeVideException)
{
DisplayAlert("Attention", "La code est vide", "OK");
}
}
private void ValiderCode(Object sender, EventArgs e)
{
try
{
if (plateau != null && code != null)
plateau.AjouterCode(code);
}
catch (CodeIncompletException)
{
DisplayAlert("Attention", "La code n'est pas complet", "OK");
}
}
private async void QuandReglesClique(object sender, EventArgs e)
{
await Navigation.PushAsync(new ReglesPage());
}
}