From a69d8bb899119ced68328e1d84ae8c3e506a72c9 Mon Sep 17 00:00:00 2001 From: Nicolas BLONDEAU Date: Mon, 15 May 2023 18:47:11 +0200 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20Serialisation/Deserialisa?= =?UTF-8?q?tion=20XML=20de=20User=20et=20correction=20bug=20Program.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Console/Program.cs | 23 ++--- Sources/Persistance/IUserDataManager.cs | 5 +- Sources/Persistance/LoaderXML.cs | 110 ++++++++++++++++++++++-- Sources/Persistance/UserManager.cs | 6 +- 4 files changed, 123 insertions(+), 21 deletions(-) diff --git a/Sources/Console/Program.cs b/Sources/Console/Program.cs index 6393c67..521f937 100644 --- a/Sources/Console/Program.cs +++ b/Sources/Console/Program.cs @@ -8,8 +8,8 @@ using System.Reflection.Metadata.Ecma335; using System.Reflection.PortableExecutable; // Déclaration des Managers (et de leur méthode de sauvegarde) -UserManager userMngr = new UserManager(new LoaderStub()); -MonsterManager monsterMngr = new MonsterManager(new LoaderStub()); +UserManager userMngr = new UserManager(new LoaderXML()); +//MonsterManager ub = new MonsterManager(new LoaderStub()); //======================================= Fonctions d'affichage ============================================// @@ -49,7 +49,7 @@ void menuAccueil(){ do { choix = ConsoleHelper.MultipleChoice("Menu principal", true, - "Connexion", "Inscription", "Continuer en tant qu'invité", "Quitter l'application"); + "Connexion", "Inscription", "Continuer en tant qu'invité", "Quitter l'application", "[TEST] - LoadUsers", "[TEST] - LoadUsers"); //Traitement du choix de l'utilisateur switch (choix) @@ -98,11 +98,11 @@ void menuAccueil(){ int menuConnexion() { - string? id, psswd; + string id, psswd; int nbTries = 1; // Initialise le nombre d'essais à 1 bool exists = false; - while (exists!) + while (!exists) { if (nbTries > 3) // Si il y a eu plus de 3 essais effectués { @@ -136,7 +136,7 @@ int menuConnexion() continue; } - exists = ub.checkIfExists(id, psswd); + exists = userMngr.checkIfExists(id, psswd); if ( !exists ) // Si le nom d'utilisateur ou le mot de passe ne correspondent pas, // ou s'ils ne sont pas présent dans la base de données. { @@ -208,7 +208,7 @@ int menuInscription() continue; } - if(ub.checkIfPseudoExists(pseudo)) + if(userMngr.checkIfPseudoExists(pseudo)) { Console.Clear(); ConsoleHelper.displayTitle("Inscription", true); @@ -217,7 +217,7 @@ int menuInscription() System.Threading.Thread.Sleep(1500); n++; } - else if (ub.addUser(pseudo, nom, prenom, mdp)) + else if (userMngr.addUser(pseudo, nom, prenom, mdp)) { break; } @@ -279,6 +279,7 @@ void menuMontres() //======================================= Fonctions d'affichage ============================================// void displayAllMonsters() { + /* ConsoleHelper.displayTitle("Index des monstres - Affichage des monstres", true); displayAllMonstersLegend(); Console.WriteLine(); @@ -303,7 +304,7 @@ void displayAllMonsters() Console.ResetColor(); continue; } - } + }*/ } void displayAllMonstersLegend() @@ -324,7 +325,7 @@ void displayAllMonstersLegend() // Fonction de recherche de monstre, mise à jour de la liste à chaque touche appuyée void rechercheMonstre() -{ +{/* List m; Console.Clear(); ConsoleKeyInfo carac; @@ -358,7 +359,7 @@ void rechercheMonstre() carac = Console.ReadKey(true); } writeLineError("Retour à la page précédente..."); - Thread.Sleep(1000); + Thread.Sleep(1000);*/ } diff --git a/Sources/Persistance/IUserDataManager.cs b/Sources/Persistance/IUserDataManager.cs index f9daf57..b9c552f 100644 --- a/Sources/Persistance/IUserDataManager.cs +++ b/Sources/Persistance/IUserDataManager.cs @@ -9,7 +9,8 @@ namespace Persistance { public interface IUserDataManager { - void saveUsers(List users); - List loadUsers(); + //A CHANGER VISIBILITEE -> pour pas que tout le monde puisse load et save :) + public void saveUsers(List users); + public List loadUsers(); } } diff --git a/Sources/Persistance/LoaderXML.cs b/Sources/Persistance/LoaderXML.cs index 69ebe0a..e430ac1 100644 --- a/Sources/Persistance/LoaderXML.cs +++ b/Sources/Persistance/LoaderXML.cs @@ -4,29 +4,129 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Runtime.Serialization; +using System.Xml; +using System.IO; +using System.Collections.ObjectModel; +using static System.Net.Mime.MediaTypeNames; namespace Persistance { + public class LoaderXML : IUserDataManager, IMonsterDataManager { - List IMonsterDataManager.loadMonsters() + static string fichierXML = "users.xml"; + /* + #region SauvegardeUser + public static void sauvegardeUserXML(List) //Fonction de sauvegarde fonctionnelle si on enlève loadUsers car conflit en les deux { - throw new NotImplementedException(); + User test = new User("DedeDu42", "dede", "dodo", "mdp", new List { }); + + + Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), "../../../../Persistance/saves/")); // Setup le chemin d'accès + + var serialiserXML = new DataContractSerializer(typeof(User)); + + #region Serialisation + + XmlWriterSettings xmlSettings = new XmlWriterSettings() { Indent = true }; // Pour avoir le format xml dans le fichier ( indentation etc... ) + + using (TextWriter tw = File.CreateText(fichierXML)) + { + using (XmlWriter writer = XmlWriter.Create(tw, xmlSettings)) + { + serialiserXML.WriteObject(writer, test); + } + } + + #endregion + + + #region Deserialisation + + User user2; + using (Stream s = File.OpenRead(fichierXML)) + { + user2 = serialiserXML.ReadObject(s) as User; + } + + #endregion + + #region AffichageTest + + Console.WriteLine(user2.Prenom); + Console.WriteLine(user2.Nom); + Console.WriteLine(user2.Pseudo); + Console.WriteLine(user2.monstresDejaVu); + + + #endregion + #endregion } + */ + + + public static void sauvegarderListUsers() + { - List IUserDataManager.loadUsers() + } + List IMonsterDataManager.loadMonsters() { throw new NotImplementedException(); } + /// + /// Enregistre les données dans un fichier XML + /// CORECTION: à adapter en fonction de IUserDataManager mais sinon fonctionnelle + /// + + void IMonsterDataManager.saveMonsters(List monstres) { throw new NotImplementedException(); } + + void IUserDataManager.saveUsers(List users)// Serialise correctement juste voir comment l'appelé en fonction de IUserDataManager + { + Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), "../../../../Persistance/saves/")); // Setup le chemin d'accès + var serialiserXML = new DataContractSerializer(typeof(List)); + + #region Serialisatin + + XmlWriterSettings xmlSettings = new XmlWriterSettings() { Indent = true }; // Pour avoir le format xml dans le fichier ( indentation etc... ) + using (TextWriter tw = File.CreateText(fichierXML)) + { + using (XmlWriter writer = XmlWriter.Create(tw, xmlSettings)) + { + serialiserXML.WriteObject(writer, users); + } + } + + #endregion + //throw new NotImplementedException(); + } - void IUserDataManager.saveUsers(List users) + public List loadUsers() { - throw new NotImplementedException(); + #region Deserialisation + var serialiserXML = new DataContractSerializer(typeof(List)); + List users; + using (Stream s = File.OpenRead(fichierXML)) + { + users = serialiserXML.ReadObject(s) as List; + } + + #endregion + #region AffichageTest + + foreach (User u in users) + { + Console.WriteLine(u.Pseudo); + Console.WriteLine(u.Prenom); + } + return users; + #endregion } } } + diff --git a/Sources/Persistance/UserManager.cs b/Sources/Persistance/UserManager.cs index eabfe8c..2637027 100644 --- a/Sources/Persistance/UserManager.cs +++ b/Sources/Persistance/UserManager.cs @@ -32,15 +32,15 @@ namespace Persistance { } - + //CHANGER VISIBILITE CAR ATTENTION void IUserDataManager.saveUsers(List users) { - + Pers.saveUsers(users); } List IUserDataManager.loadUsers() { - throw new NotImplementedException(); + return Pers.loadUsers(); } ///