Esempio n. 1
0
        /// <summary>
        /// Case when a client try to connect using his credentials
        /// </summary>
        /// <param name="msg">A client message</param>
        /// <returns></returns>
        public static Client ClientConnect(ReceivedMessage msg)
        {
            // TODO demock ip address

            // Get user credentials
            string[]    credentials     = msg.data;
            Credentials userCredentials = new Credentials(credentials[0], credentials[1], new IPAddress(new byte[] { 10, 10, 10, 10 }));

            // Call API to try log in to the game
            string apiUrl = "http://51.91.156.75:5000/api/Connexion/Connexion";
            string ouput  = userCredentials.ToString();
            HttpResponseMessage response = CallApiAsync(apiUrl, ouput).Result;

            // If we get a good response we send SUCCESS to the user, else we send FAIL ...
            if (response.IsSuccessStatusCode)
            {
                Client client;
                // Map the response from HttpResponseMessage to Client
                try
                {
                    client = ClientMapper.mapAsync(response).Result;
                } catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    client = null;
                }

                if (client == null)
                {
                    return(null);
                }

                // Set the ip of the client to the object client
                client.idAddress = userCredentials.ip;

                // Prepare the return message of the server to the client
                SendMessage succes = new SendMessage();
                succes.action = ConstsActions.CONNECTION;
                succes.data   = new string[] { "SUCCESS", client.idUser.ToString(), client.idAccount.ToString(), client.nicknameUser, client.firstNameUser, client.lastNameUser, client.birthdayUser.ToString(), client.genderUser, client.statusUser };

                // Send the message
                SendMessage(succes);

                // Return the client to add him in the client list
                return(client);
            }
            else
            {
                // Prepare the return message of the server to the client
                SendMessage fail = new SendMessage();
                fail.action = ConstsActions.CONNECTION;
                fail.data   = new string[] { "FAIL", response.StatusCode.ToString() };

                // Send the message
                SendMessage(fail);

                return(null);
            }
        }