/// <summary> /// When a Client signs-in /// </summary> /// <param name="newCoach">Data of the newly created Coach</param> private void CoachCreate(CoachWithPassword newCoach) { // We convert the Coach (with a password) into a regular Coach Coach coach = new Coach(newCoach); // Adding the new objects to the database representation Database.coaches.Add(coach); // Creating an JSON file in the database Database.COACH.Write(newCoach); }
/// <summary> /// Writes a Coach structure into a JSON file (more precisely, both a Coach and its password, contained in the according Credentials instance /// </summary> /// <param name="coach">Coach to transcribe into a JSON file</param> public static bool Write(Coach coach) { try { // Get the folder's path string pathFolder = pathCoach(coach); // Determine whether the directory exists : if not, we create it. if (!Directory.Exists(pathFolder)) { Directory.CreateDirectory(pathFolder); } // We also need the password ! // Hence, we need to get the credentials Credentials credentials = CREDENTIALS.GetById(coach.id); // We put all the data into a single instance : a Coach With a Password CoachWithPassword coachWithPassword = new CoachWithPassword(coach, credentials); // Get the JSON file's path string pathJson = pathCoachData(coach); // Convert the instance into a string string json = coachWithPassword.Serialize(); // Write the JSON into the file System.IO.File.WriteAllText(pathJson, json); // It worked ! return(true); } catch (Exception) { } // It didn't work. return(false); }
/// <summary> /// Server-side verification for the Sign-in /// Returns an instance of the newly created Coach (if it didn't work, its ID = -1) /// </summary> /// <param name="coachReceived">Coach's data of the user trying to Sign in</param> /// <returns> The newly created Coach (with password), if successful; otherwise, a default one</returns> public CoachWithPassword SignIn(CoachWithPassword coachReceived) { // We extract the profile's data string name = coachReceived.name; string password = coachReceived.password; string email = coachReceived.email; // We create a bool repertoring whether or not a Profile already has the same data as the new user // By default, this is false bool isTaken = false; // We iterate through all the Database's Profiles foreach (Coach coach in Database.coaches) { // If we found a match : ERROR ! if (name == coach.name || email == coach.email) { isTaken = true; break; } } // If the Credentials don't exist : we can create a new Coach if (!isTaken) { // We create a new Coach representing the user's data userCoach = new Coach(name, email); } // We send the Coach (Empty id = SignIn failed) Net.COACH.Send(comm.GetStream(), userCoach); // We return the profile created return(new CoachWithPassword(userCoach, password)); }
/// <summary> /// Server's HUB : it does stuff. /// </summary> public void DoOperation() { while (true) { // We receive a Communication from the client Communication communication = Net.COMMUNICATION.Receive(comm.GetStream()); // We extract the data Instructions instruction = communication.instruction; Object content = communication.content; // We display the instruction CONSOLE.WriteLine(ConsoleColor.Cyan, "\nINSTRUCTION : " + instruction); // According to the instruction, we'll do a specific action switch (instruction) { case Instructions.Exit_Software: // We raise the event : a user left the software When_Server_Exit?.Invoke(comm); break; // CREDENTIALS case Instructions.LogIn: if (LogIn((Credentials)content)) { // We raise the event : a user has logged in When_Server_LogIn?.Invoke(comm); } break; case Instructions.SignIn: // We get the profile Signed In CoachWithPassword newCoach = SignIn((CoachWithPassword)content); // If the Profile's ID is not Empty (means that the Sign In was successful) if (newCoach.id != Guid.Empty) { // We raise the event : a user has logged in When_Server_LogIn?.Invoke(comm); // We raise the event : a Profile has been created When_Coach_Create(newCoach); } break; case Instructions.LogOut: LogOut(); // We raise the event : a user has logged off When_Server_LogOff?.Invoke(comm); break; // COACH case Instructions.Coach_GetById: GetCoachById((Guid)content); break; // TEAM case Instructions.Team_New: // We get the newly created Team Team newTeam = NewTeam((Team)content); // If the Team can be created (ID is not empty) if (newTeam.IsComplete) { // We raise the event : a Team has been created When_Team_Create?.Invoke(newTeam); } break; case Instructions.Team_AddPlayer: // We get the newly created Player Player newPlayer = NewPlayer((Player)content); // If the Player can be created (ID is not empty) if (newPlayer.IsComplete) { // We raise the event : a Player has been created When_Player_Create?.Invoke(newPlayer); } break; case Instructions.Team_RemovePlayer: // We get the Player to remove Player playerToRemove = (Player)content; // We remove him from the database playerToRemove = RemovePlayer(playerToRemove); // If the removal was successful) if (playerToRemove.IsComplete) { // We raise the event : a Player has been removed When_Player_Remove?.Invoke(playerToRemove); } break; // PLAYER case Instructions.Player_LevelUp: // We get the Player to remove Player player = (Player)content; // We add a new Effect to the player Effect?newEffect = PlayerLevelsUp(player); // If a new effect was chosen if (newEffect != null) { // We add the effect to the Player player.effects.Add((Effect)newEffect); // We raise the event : an Effect has been added When_Player_LevelsUp?.Invoke(player); } break; // otherwise : Error (should not occur, but we're not taking any chance) default: CONSOLE.WriteLine(ConsoleColor.Red, "Message instruction not understood : " + instruction); break; } } }
/// <summary> /// Gets the newly created Coach's data of the user, and connects him / makes him retry accordingly /// </summary> private void SignIn() { bool continueSignin = true; string errorMessage = ""; while (continueSignin) { // Displaying some messages Console.Clear(); CONSOLE.WriteLine(ConsoleColor.Blue, "\n Enter empty fields to leave the SIGN-IN"); // Displays a message if the credentials are incorrect CONSOLE.WriteLine(ConsoleColor.Red, errorMessage); // USERNAME Console.Write("\n Please enter your username : "******"\n Please enter your password : "******"\n Please verify your password : "******"\n Please enter your email : "); string email = Console.ReadLine(); // All the fields are empty : go back to the menu if (name.Length == 0 && password.Length == 0 && passwordVerif.Length == 0 && email.Length == 0) { continueSignin = false; } // If at least one field is empty : ERROR else if (name.Length == 0 || password.Length == 0 || passwordVerif.Length == 0 || email.Length == 0) { errorMessage = PrefabMessages.INCOMPLETE_FIELDS; } // The password and its verification do not match : ERROR else if (password != passwordVerif) { errorMessage = PrefabMessages.SIGNIN_PASSWORD_DONT_MATCH; } // NOW, we don't have to verify the PasswordVerif field anymore ! // If at least one field is too long : ERROR else if (name.Length > PrefabMessages.INPUT_MAXSIZE_COACH_NAME || password.Length > PrefabMessages.INPUT_MAXSIZE_COACH_PASSWORD || email.Length > PrefabMessages.INPUT_MAXSIZE_COACH_EMAIL) { errorMessage = PrefabMessages.INCORRECT_INPUT_SIZE; } // If at least one field has one incorrect character : ERROR else if (!Util.CorrectInput(name) || !Util.CorrectInput(password) || !Util.CorrectInput(email)) { errorMessage = PrefabMessages.INCORRECT_INPUT_CHARACTER; } // Otherwise : verify with the server else { // Sending the new Coach (with password !) Instructions instruction = Instructions.SignIn; Object content = new CoachWithPassword(name, password, email); Net.COMMUNICATION.Send(comm.GetStream(), new Communication(instruction, content)); // We get the Coach data back // CORRECT : regular data // INCORRECT : default data userData = Net.COACH.Receive(comm.GetStream()); // the data is complete : successful Sign in if (userData.IsComplete) { continueSignin = false; } // If the data is incomplete : the profile's name and/or email is already taken, we reset the sign-in { errorMessage = PrefabMessages.SIGNIN_FAILURE; } } } }