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.
112 lines
3.4 KiB
112 lines
3.4 KiB
using CoreLibrary.Persistance;
|
|
using CoreLibrary.Joueurs;
|
|
using CoreLibrary.Regles;
|
|
using CoreLibrary.Statistiques;
|
|
|
|
namespace CoreLibrary.Manageurs
|
|
{
|
|
public class Manageur
|
|
{
|
|
private readonly IPersistance persistance;
|
|
private readonly List<Joueur> joueurs;
|
|
private readonly List<Partie> parties;
|
|
|
|
public IReadOnlyList<Joueur> Joueurs => joueurs;
|
|
public IReadOnlyList<Partie> Parties => parties;
|
|
public IReadOnlyList<Partie> PartiesNonTerminees => parties.Where(partie => !partie.Termine).Reverse().ToList();
|
|
|
|
public Manageur(IPersistance persistance)
|
|
{
|
|
this.persistance = persistance;
|
|
|
|
joueurs = persistance.Charger<Joueur>().ToList();
|
|
parties = persistance.Charger<Partie>().ToList();
|
|
}
|
|
|
|
private void Sauvegarder()
|
|
{
|
|
persistance.Enregistrer(joueurs.ToArray());
|
|
persistance.Enregistrer(parties.ToArray());
|
|
}
|
|
|
|
public Partie ChargerPartie(Partie partie)
|
|
{
|
|
parties.Remove(partie);
|
|
|
|
Partie nouvellePartie = new Partie(partie);
|
|
parties.Add(nouvellePartie);
|
|
|
|
EcouterPartie(nouvellePartie);
|
|
|
|
return nouvellePartie;
|
|
}
|
|
|
|
public Partie NouvellePartie(IRegles regles)
|
|
{
|
|
Partie partie = new Partie(regles);
|
|
parties.Add(partie);
|
|
|
|
EcouterPartie(partie);
|
|
|
|
return partie;
|
|
}
|
|
|
|
private void EcouterPartie(Partie partie)
|
|
{
|
|
partie.PartieDemanderJoueur += (sender, e) => Sauvegarder();
|
|
partie.PartieDebutPartie += (sender, e) => Sauvegarder();
|
|
partie.PartieDemanderJoueurJouer += (sender, e) => Sauvegarder();
|
|
|
|
partie.PartiePasserLaMain += (sender, e) =>
|
|
{
|
|
DemanderJoueurExistant(e.Joueur)?.IncrementerStatistique(partie.Regles, Statistique.CoupJoue);
|
|
Sauvegarder();
|
|
};
|
|
|
|
partie.PartiePartieTerminee += (sender, e) =>
|
|
{
|
|
if (e.Gagnants.Count == 1)
|
|
{
|
|
DemanderJoueurExistant(e.Gagnants[0])?.IncrementerStatistique(partie.Regles, Statistique.PartieGagnee);
|
|
}
|
|
else
|
|
{
|
|
foreach (string gagnant in e.Gagnants)
|
|
DemanderJoueurExistant(gagnant)?.IncrementerStatistique(partie.Regles, Statistique.PartieEgalite);
|
|
}
|
|
|
|
foreach (string perdant in e.Perdants)
|
|
{
|
|
DemanderJoueurExistant(perdant)?.IncrementerStatistique(partie.Regles, Statistique.PartiePerdue);
|
|
}
|
|
Sauvegarder();
|
|
};
|
|
}
|
|
|
|
private Joueur? DemanderJoueurExistant(string nom)
|
|
{
|
|
foreach (Joueur joueur in joueurs)
|
|
{
|
|
if (joueur.Nom == nom)
|
|
{
|
|
return joueur;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public Joueur DemanderJoueur(string nom)
|
|
{
|
|
Joueur? joueur = DemanderJoueurExistant(nom);
|
|
if (joueur == null)
|
|
{
|
|
joueur = new Joueur(nom);
|
|
joueurs.Add(joueur);
|
|
}
|
|
|
|
return joueur;
|
|
}
|
|
}
|
|
}
|