Ejemplo n.º 1
0
        private void BtnDone_Click(object sender, EventArgs e)
        {
            try
            {
                //Si el nombre ingresado no está vacío ni sobrepasa una longitud mayor a 20 caracteres
                if (!txtUsername.Text.Trim().Equals("") && !(txtUsername.Text.Length > 20))
                {
                    Player currentPlayer = PlayerDAO.CurrentPlayer(txtUsername.Text.ToUpper().Trim());
                    //Si el nickname no fue encontrado en la base de datos, se registra
                    if (currentPlayer.PlayerId == 0)
                    {
                        MessageBox.Show("User registered successfully!", "ARKANOID", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                        currentPlayer.PlayerId = PlayerDAO.GetId(currentPlayer.Name);
                    }
                    else
                    {
                        MessageBox.Show("Welcome back!", "ARKANOID", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }

                    Hide();

                    Skin skin = new Skin(currentPlayer);
                    skin.Dock = DockStyle.Fill;
                    Parent.Controls.Add(skin);
                    Parent.Controls.Remove(this);
                }
                else
                {
                    if (txtUsername.Text.Trim().Length != 0)
                    {
                        throw new UsernameLengthException("Nickname too long.\nMax length: 20 characters.");
                    }
                    throw new EmptyNameException("Write a nickname.");
                }
            }
            catch (EmptyNameException ex)
            {
                MessageBox.Show(ex.Message, "ARKANOID", MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            catch (UsernameLengthException ex)
            {
                MessageBox.Show(ex.Message, "ARKANOID", MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            finally
            {
                // Limpia el textBox para quitar los espacios.
                txtUsername.Clear();
            }
        }