internal static bool?QueryAccountIdExists(string accountId)
        {
            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_GetUserAccountFromIdString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?id", accountId));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    return(true);
                                }
                            }
                        }

                        MySqlConnector.CloseConnection(mySqlConnection);
                        return(false);
                    }

                    return(null); //Unable To Open Database Connection
                }
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); return(null); }
        }
        internal static void CreateAccount(string clientId, string accountId, string username, string password, string email)
        {
            string passwordSalt = GenerateHashSalt(m_HashSize);
            string passwordHash = GenerateHash(password + passwordSalt);

            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_CreateAccountString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?id", accountId));
                            c.Parameters.Add(new MySqlParameter("?account", username));
                            c.Parameters.Add(new MySqlParameter("?passHash", passwordHash));
                            c.Parameters.Add(new MySqlParameter("?passSalt", passwordSalt));
                            c.Parameters.Add(new MySqlParameter("?email", email));

                            int i = c.ExecuteNonQuery();

                            if (i > 0)
                            {
                                if (clientId != string.Empty)
                                {
                                    NetworkEventDispatcher.InvokeAccountCreationSuccessEvent
                                        (new AccountCreationSuccessArgs(clientId));
                                }

                                Console.WriteLine("Account Successfully Created For: {0}", username);
                            }

                            else
                            {
                                if (clientId != string.Empty)
                                {
                                    NetworkEventDispatcher.InvokeAccountCreationFailedEvent
                                        (new AccountCreationFailedArgs(clientId, AccountCreationFailType.Unknown));
                                }

                                Console.WriteLine("Failed To Create New Account For: {0}", username);
                            }

                            MySqlConnector.CloseConnection(mySqlConnection);
                        }
                    }

                    else
                    {
                        Console.WriteLine("[Error]: Unable To Contact Database During: (CreateAccount).");
                    }
                }
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); }
        }
        internal static bool?UserRelationshipExists(string userId, string friendId)
        {
            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_QueryFriendIdentityExistsString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?user", userId));
                            c.Parameters.Add(new MySqlParameter("?friend", friendId));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    if (ServerCore.DebugMode)
                                    {
                                        Console.WriteLine("User Relationship [{1}] Does Not Exist For: {0}",
                                                          AccountHandler.GetAccountById(userId), AccountHandler.GetAccountById(friendId));
                                    }
                                    return(true);
                                }

                                else
                                if (ServerCore.DebugMode)
                                {
                                    Console.WriteLine("User Relationship [{1}] Already Exists For: {0}",
                                                      AccountHandler.GetAccountById(userId), AccountHandler.GetAccountById(friendId));
                                }
                            }
                        }

                        MySqlConnector.CloseConnection(mySqlConnection);
                    }

                    else
                    {
                        Console.WriteLine("[Error]: Unable To Contact Database During: (GetUserFriends).");
                        return(null);
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }

            return(false);
        }
        internal static string[] GetAccountDetails(string username)
        {
            string[] details = new string[5];

            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_GetAccountDetailsString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?account", username));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    details[0] = reader["account_email"].ToString();
                                    details[1] = reader["user_rating"].ToString();
                                    details[2] = reader["user_wins"].ToString();
                                    details[3] = reader["user_losses"].ToString();
                                    details[4] = reader["id"].ToString();
                                }
                            }
                        }

                        MySqlConnector.CloseConnection(mySqlConnection);
                    }

                    else
                    {
                        Console.WriteLine("[Error]: Unable To Contact Database During: (GetAccountDetails).");
                        return(null);
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }

            if (ServerCore.DebugMode)
            {
                Console.WriteLine("Details For [{0}]: {1}, {2}, {3}, {4}, {5}",
                                  username, details[0], details[1], details[2], details[3], details[4]);
            }

            return(details);
        }
        internal static string[] GetUserFriends(string id)
        {
            List <string> friendIds = new List <string>();

            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_GetUserFriendsString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?id", id));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    friendIds.Add((string)reader["friendId"]);
                                }
                            }
                        }

                        MySqlConnector.CloseConnection(mySqlConnection);
                    }

                    else
                    {
                        Console.WriteLine("[Error]: Unable To Contact Database During: (GetUserFriends).");
                        return(null);
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }

            if (ServerCore.DebugMode)
            {
                Console.WriteLine("Gathered [{0}] Friend Entries..", friendIds.Count);
            }

            return(friendIds.ToArray());
        }
Beispiel #6
0
        private static void InitializeModules()
        {
            RsaKeysLoaded = RSAModule.QueryRsa();

            m_AesModule = new AesModule();
            m_AesModule.InitializeProviderFromFile();

            MySqlConnector.InitializeMySqlPasswordFromEncrypted();
            MySqlConnection SqlConnection = MySqlConnector.InitializeMySqlConnection();

            if (SqlConnection != null)
            {
                SqlConnection.Dispose();
                Console.WriteLine
                    ("Mysql Database Connection Sucessful..");
            }
        }
        internal static void RemoveUserRelationships(string user, string friend)
        {
            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_DeleteUserRelationshipString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?user", user));
                            c.Parameters.Add(new MySqlParameter("?friend", friend));

                            int i = c.ExecuteNonQuery();

                            if (i > 0)
                            {
                                if (ServerCore.DebugMode)
                                {
                                    Console.WriteLine("Sucessfully Removed User Relationship: {0} <> {1}", user, friend);
                                }
                            }

                            else
                            {
                                if (ServerCore.DebugMode)
                                {
                                    Console.WriteLine("Failed To Remove User Relationship: {0} <> {1}", user, friend);
                                }
                            }

                            MySqlConnector.CloseConnection(mySqlConnection);
                        }
                    }

                    else
                    {
                        Console.WriteLine("[Error]: Unable To Contact Database During: (CreateAccount).");
                    }
                }
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); }
        }
        internal static void AddFriendRelationship(string userId, string friendId)
        {
            bool?relationshipExists = UserRelationshipExists(userId, friendId);

            if (relationshipExists != null && relationshipExists.Value == false)
            {
                try
                {
                    using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                    {
                        if (MySqlConnector.OpenConnection(mySqlConnection))
                        {
                            using (MySqlCommand c = new MySqlCommand(m_AddUserFriendRelationshipString, mySqlConnection))
                            {
                                c.Parameters.Add(new MySqlParameter("?userId", userId));
                                c.Parameters.Add(new MySqlParameter("?friendId", friendId));

                                int i = c.ExecuteNonQuery();

                                if (i > 0)
                                {
                                    Console.WriteLine("User Relationship Successfully Created For: {0}", AccountHandler.GetAccountById(userId).Username);
                                }

                                else
                                {
                                    Console.WriteLine("Failed To Create New User Relationship For: {0}", AccountHandler.GetAccountById(userId).Username);
                                }

                                MySqlConnector.CloseConnection(mySqlConnection);
                            }
                        }

                        else
                        {
                            Console.WriteLine("[Error]: Unable To Contact Database During: (CreateAccount).");
                        }
                    }
                }

                catch (Exception e) { Console.WriteLine(e.ToString()); }
            }
        }
        internal static string GetAccountNameFromId(string id)
        {
            string username = null;

            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_GetUserAccountFromIdString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?id", id));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    username = reader["account_name"].ToString();
                                }
                            }
                        }

                        MySqlConnector.CloseConnection(mySqlConnection);
                    }

                    else
                    {
                        Console.WriteLine("[Error]: Unable To Contact Database During: (GetAccounFromId).");
                        return(null);
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }

            return(username);
        }
        internal static string GetUserPassSalt(string username)
        {
            try
            {
                using (MySqlConnection connection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(connection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_GetPasswordSaltString, connection))
                        {
                            c.Parameters.Add(new MySqlParameter("?account", username));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    return(reader.GetString(0));
                                }

                                else
                                {
                                    Console.WriteLine("Unable To Obtain Hash Salt For Account: {0}", username);
                                    return(string.Empty);
                                }
                            }
                        }
                    }

                    else
                    {
                        Console.WriteLine("[Error]: Unable To Contact Database During: (GetUserPassSalt).");
                        return(string.Empty);
                    }
                }
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); return(string.Empty); }
        }
        internal static bool?QueryAccountExists(string clientId, string username, string email)
        {
            try
            {
                using (MySqlConnection mySqlConnection = MySqlConnector.InitializeMySqlConnection())
                {
                    if (MySqlConnector.OpenConnection(mySqlConnection))
                    {
                        using (MySqlCommand c = new MySqlCommand(m_QueryAccountNameExistsString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?account", username));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    if (clientId != string.Empty)
                                    {
                                        NetworkEventDispatcher. //Notify Client Account Name Exists
                                        InvokeAccountCreationFailedEvent
                                            (new AccountCreationFailedArgs(clientId, AccountCreationFailType.UsernameExists));
                                    }

                                    Console.WriteLine
                                        ("Failed To Create New Account For: {0} (Username [{1}] Already Exists)",
                                        username, reader["account_name"].ToString());

                                    return(true);
                                }
                            }
                        }

                        using (MySqlCommand c = new MySqlCommand(m_QueryAccountEmailExistsString, mySqlConnection))
                        {
                            c.Parameters.Add(new MySqlParameter("?email", email));

                            using (MySqlDataReader reader = c.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    if (clientId != string.Empty)
                                    {
                                        NetworkEventDispatcher. //Notify Client Email Already In Use
                                        InvokeAccountCreationFailedEvent
                                            (new AccountCreationFailedArgs(clientId, AccountCreationFailType.EmailExists));
                                    }

                                    Console.WriteLine("Failed To Create New Account For: {0} (Email Already Exists)", username);

                                    return(true);
                                }
                            }
                        }

                        MySqlConnector.CloseConnection(mySqlConnection);
                        return(false);
                    }

                    return(null); //Unable To Open Database Connection
                }
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); return(null); }
        }