From 54ce10925faedfdf253429aac9c913665c250cc1 Mon Sep 17 00:00:00 2001 From: Nicolas BLONDEAU Date: Thu, 27 Apr 2023 12:15:41 +0200 Subject: [PATCH] =?UTF-8?q?=C3=A7a=20marche=20ENFIN,=20affichage=20du=20me?= =?UTF-8?q?nu,=20partie=20connexion=20fonctionnelle,=20mot=20de=20passe=20?= =?UTF-8?q?cach=C3=A9,=20cr=C3=A9ation=20du=20stub,=20ajout=20de=20la=20Us?= =?UTF-8?q?erBase=20et=20tout=20est=20fonctionnelle=20YESSSS=20:sparkles:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Console/Program.cs | 113 ++++++++++++++++++++++++++++++++++---------- Modèle/Stub.cs | 23 +++++++++ Modèle/User.cs | 24 ---------- Modèle/UserBase.cs | 42 ++++++++++++++++ 4 files changed, 154 insertions(+), 48 deletions(-) create mode 100644 Modèle/Stub.cs create mode 100644 Modèle/UserBase.cs diff --git a/Console/Program.cs b/Console/Program.cs index 7283ba7..6785ad4 100644 --- a/Console/Program.cs +++ b/Console/Program.cs @@ -3,6 +3,11 @@ using Modèle; using System; using System.Reflection.PortableExecutable; + +///Déclaration du STUB + +UserBase ub = new UserBase(); +/* void testMonstre() { //Création non permanente d'une liste de caractéristiques (pour tests) -> PEUT-ÊTRE EN FAIRE UNE CLASSE PLUS TARD??? @@ -26,37 +31,97 @@ void testMonstre() Console.WriteLine(); Monstre monstre2 = new Monstre(423, "Mouton", "Je suis un animal présent dans la campagne.", charact, appear); Console.WriteLine(monstre2.IntroduceTest()); -} +}*/ -void testUser(){ - // User - string pseudo; - string mdp; - User Dede = new User("DedeDu42", "dede", "dodo", "mdp"); - Dede.ListUsers.Add(Dede); - Console.WriteLine("Veuillez Saisir votre pseudo"); - pseudo = Console.ReadLine(); - Console.WriteLine("Veuillez Saisir votre mdp"); - mdp = Console.ReadLine(); - if (Dede.testConnexion(pseudo, mdp) == 0) - { - Console.WriteLine("Welcome home"); - } - //FAILLE DE SECU -> Ne pas dire si c'est le pseudo ou le mot de passe - if (Dede.testConnexion(pseudo, mdp) == 1) //Ne passe jamais dans cette condition -> Directement dans le cas ou testConnexion = -1 +int menuPrincipal(){ + string? choix; + Console.WriteLine("Menu - Connexion / Inscription"); + Console.WriteLine("\t1 - Connexion\n\t2 - Inscription\n\t3 - Connexion plus tard\n"); + choix = Console.ReadLine(); + if ( choix == "1") + { + Console.Clear(); + Console.WriteLine("Choix 1"); + menuConnexion(); + return 1; + } else if ( choix == "2") + { + Console.Clear(); + Console.WriteLine("Choix 2"); + return 2; + } else if (choix == "3") { - Console.WriteLine("Pseudo incorrect"); + Console.Clear(); + Console.WriteLine("Choix 3"); + return 3; + } else + { + Console.WriteLine("Écris un nombre compris entre 1 et 3"); } - if (Dede.testConnexion(pseudo, mdp) == 2) + return 0; +} + +string ReadPassword() +{ + string psswrd = ""; + ConsoleKeyInfo info = Console.ReadKey(true); + while (info.Key != ConsoleKey.Enter) { - Console.WriteLine("Mot de passe incorrect"); + if (info.Key != ConsoleKey.Backspace) + { + Console.Write("*"); + psswrd += info.KeyChar; + } + else if (info.Key == ConsoleKey.Backspace) + { + if (!string.IsNullOrEmpty(psswrd)) + { + // Supprime un élément de la liste de char de psswrd + psswrd = psswrd.Substring(0, psswrd.Length - 1); + // get the location of the cursor + int pos = Console.CursorLeft; + // move the cursor to the left by one character + Console.SetCursorPosition(pos - 1, Console.CursorTop); + // replace it with space + Console.Write(" "); + // move the cursor to the left by one character again + Console.SetCursorPosition(pos - 1, Console.CursorTop); + } + } + info = Console.ReadKey(true); } - if (Dede.testConnexion(pseudo, mdp) == -1) + // Ajoute un alinéa parce que l'utlisateur a validé + Console.WriteLine(); + return psswrd; +} +int menuConnexion() +{ + string? id; + string? psswd; + Console.WriteLine("Identifiant : "); + id = Console.ReadLine(); + Console.WriteLine("Mot de passe : "); + psswd = ReadPassword(); + int nbRetries = 0; + int exists = ub.checkIfExists(id, psswd); + while (exists == 0) { - Console.WriteLine("..."); + if(nbRetries >= 3) + { + return -1; + } + Console.Clear(); + Console.WriteLine("Erreur, identifiant ou mot de passe incorrect."); + nbRetries++; + Console.WriteLine(""); + Console.WriteLine("Identifiant : "); + id = Console.ReadLine(); + Console.WriteLine("Mot de passe : "); + psswd = ReadPassword(); + exists = ub.checkIfExists(id, psswd); } + return 0; } -testMonstre(); -testUser(); \ No newline at end of file +int codeRetour = menuPrincipal(); \ No newline at end of file diff --git a/Modèle/Stub.cs b/Modèle/Stub.cs new file mode 100644 index 0000000..44babf0 --- /dev/null +++ b/Modèle/Stub.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace Modèle +{ + public class Stub + { + public Stub() { } + public List loadUsers() ///CHANGER VISIBILITEE, CAR PAS BIEN DE LAISSER A TOUT LE MONDE + { + List lu = new List(); + lu.Add(new User("DedeDu42", "dede", "dodo", "mdp")); + lu.Add(new User("Moi", "Monchanin", "Liam", "feur")); + lu.Add(new User("Nikoala", "Blondeau", "Nicolas", "niblondeau")); + lu.Add(new User("Yadoumir", "Doumir", "Yannis", "mdp")); + return lu; + } + } +} \ No newline at end of file diff --git a/Modèle/User.cs b/Modèle/User.cs index fef63e4..f6c38ea 100644 --- a/Modèle/User.cs +++ b/Modèle/User.cs @@ -12,7 +12,6 @@ namespace Modèle private string Nom { get; } private string Prenom { get; } public string Mdp { get; private set; } - public List ListUsers { get; private set; } = new List(); public User(string pseudo, string nom, string prenom, string mdp) @@ -22,28 +21,5 @@ namespace Modèle Prenom = prenom; Mdp = mdp; } - - public int testConnexion(string pseudo, string mdp) - { - foreach (User i in ListUsers) - { - if (i.Pseudo.Equals(pseudo) && i.Mdp.Equals(mdp)) - { - return 0; - } - else - { - if (i.Pseudo != pseudo && i.Mdp.Equals(mdp)) - { - return 1; - } - if (i.Pseudo.Equals(pseudo) && i.Mdp != mdp) - { - return 2; - } - } - } - return -1; - } } } diff --git a/Modèle/UserBase.cs b/Modèle/UserBase.cs new file mode 100644 index 0000000..bc0829d --- /dev/null +++ b/Modèle/UserBase.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modèle +{ + public class UserBase + { + private List users; + public List ListUsers + { + get + { + return users; + } + private set + { + users = value; + } + } + + public UserBase() + { + ListUsers = new Stub().loadUsers(); + } + + public int checkIfExists(string username, string password) + { + foreach (User u in ListUsers) + { + if(username.Equals(u.Pseudo) && password.Equals(u.Mdp)) + { + Console.WriteLine($"Bienvenue, {u.Pseudo}, vous venez de vous connecter!"); + return 1; + } + } + return 0; + } + } +} \ No newline at end of file