using CoreLibrary.Persistance; using CoreLibrary.Core; using CoreLibrary.Evenements; using CoreLibrary.Joueurs; using CoreLibrary.Regles; using System.Runtime.Serialization; namespace CoreLibrary { [DataContract] [KnownType(typeof(ReglesClassiques))] public class Partie : IEstPersistant { public event EventHandler? PartieDemanderJoueur; public event EventHandler? PartieDebutPartie; public event EventHandler? PartieNouveauTour; public event EventHandler? PartiePasserLaMain; public event EventHandler? PartiePartieTerminee; private void QuandPartieDemanderJoueur(Joueur joueurDemande) => PartieDemanderJoueur?.Invoke(this, new PartieDemanderJoueurEventArgs(joueurs.Count + 1, joueurDemande)); private void QuandPartieDebutPartie() => PartieDebutPartie?.Invoke(this, new PartieDebutPartieEventArgs()); private void QuandPartieNouveauTour() => PartieNouveauTour?.Invoke(this, new PartieNouveauTourEventArgs(plateaux.ElementAt(courant).Taille + 1, joueurs.ElementAt(courant), plateaux.ElementAt(courant), new Code(Regles.TailleCode))); private void QuandPartiePasserLaMain() => PartiePasserLaMain?.Invoke(this, new PartiePasserLaMainEventArgs(joueurs.ElementAt(courant))); private void QuandPartiePartieTerminee(IReadOnlyList gagnants, IReadOnlyList perdants) => PartiePartieTerminee?.Invoke(this, new PartiePartieTermineeEventArgs(gagnants, perdants)); [DataMember] private readonly List joueurs = new List(); [DataMember] private readonly List plateaux = new List(); [DataMember] private int courant = 0; [DataMember] public IRegles Regles { get; private init; } public Partie(IRegles regles) { Regles = regles; } public void Jouer() { DemanderJoueur(); } private void DemanderJoueur() { Joueur joueurDemande = new Joueur(); joueurDemande.JoueurSeConnecter += JoueurConnecte; QuandPartieDemanderJoueur(joueurDemande); } private void JoueurConnecte(object? sender, JoueurSeConnecterEventArgs e) { Plateau plateau = new Plateau(Regles.TailleCode, Regles.NbTour); plateau.PlateauAjouterCode += PlateauAjouterCode; joueurs.Add(e.JoueurConnecte); plateaux.Add(plateau); if (joueurs.Count < Regles.NbJoueurs) { DemanderJoueur(); } else { DebutPartie(); } } private void DebutPartie() { QuandPartieDebutPartie(); NouveauTour(); } private void NouveauTour() { QuandPartieNouveauTour(); } private void PlateauAjouterCode(object? sender, PlateauAjouterCodeEventArgs e) { QuandPartiePasserLaMain(); if (courant + 1 == joueurs.Count && (e.Plateau.Complet || plateaux.Any(plateau => plateau.Victoire))) { PartieTerminee(); } else { if (courant + 1 == joueurs.Count) courant = 0; else ++courant; NouveauTour(); } } private void PartieTerminee() { List gagnants = new List(); List perdants = new List(); for (int i = 0; i < joueurs.Count; ++i) { if (plateaux.ElementAt(i).Victoire) gagnants.Add(joueurs[i]); else perdants.Add(joueurs[i]); } QuandPartiePartieTerminee(gagnants, perdants); } } }