Exemple #1
0
        /// <summary>
        /// Tries to execute the query to create an account.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="password">The password.</param>
        /// <param name="email">The email.</param>
        /// <param name="ip">The IP address.</param>
        /// <returns>True if the account was successfully created; otherwise false.</returns>
        public bool TryExecute(string name, string password, string email, uint ip)
        {
            if (!GameData.AccountName.IsValid(name))
            {
                return(false);
            }
            if (!GameData.AccountPassword.IsValid(password))
            {
                return(false);
            }
            if (!GameData.AccountEmail.IsValid(email))
            {
                return(false);
            }

            bool success;

            password = UserAccountManager.EncodePassword(password);
            var queryArgs = new QueryArgs(name, password, email, ip);

            try
            {
                using (var r = ExecuteReader(queryArgs))
                {
                    switch (r.RecordsAffected)
                    {
                    case 0:
                        success = false;
                        break;

                    case 1:
                        success = true;
                        break;

                    default:
                        success = true;
                        Debug.Fail("How was there more than one affected row!?");
                        break;
                    }
                }
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                case 1062:
                    // Duplicate key
                    break;

                default:
                    const string errmsg = "Failed to execute query. Exception: {0}";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, ex);
                    }
                    Debug.Fail(string.Format(errmsg, ex));
                    break;
                }

                success = false;
            }

            return(success);
        }