Exemple #1
0
        private void SignupBTN(object sender, RoutedEventArgs e)
        {
            bool EverythingFine = true;

            if (String.IsNullOrEmpty(EmailBox.Text))
            {
                EmailBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), EmailBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    EmailBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else if (!IsValidEmail(EmailBox.Text))
            {
                EmailBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), EmailBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Must be a valid email.";

                Validation.MarkInvalid(
                    EmailBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(UsernameBox.GetBindingExpression(TextBox.TextProperty));
            }
            if (String.IsNullOrEmpty(UsernameBox.Text))
            {
                UsernameBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), UsernameBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    UsernameBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(UsernameBox.GetBindingExpression(TextBox.TextProperty));
            }
            if (String.IsNullOrEmpty(PasswordBox.Password))
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else if (PasswordBox.Password.Length < 8)
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "At least 8 characters.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
            }
            if (EverythingFine)
            {
                SignupRequest signupRequest = new SignupRequest();
                signupRequest.username = UsernameBox.Text;
                signupRequest.password = PasswordBox.Password;
                signupRequest.email    = EmailBox.Text;

                app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(signupRequest, Constants.SIGNUP_REQUEST_CODE)).ContinueWith((task) =>
                {
                    ResponseInfo response         = task.Result;
                    SignupResponse signupResponse = JsonDeserializer.deserializeResponse <SignupResponse>(response.buffer);
                    switch (signupResponse.status)
                    {
                    case Constants.SIGNUP_SUCCESS:
                        MyMessageQueue.Enqueue("Sign up Successfully!");
                        this.Dispatcher.Invoke(() =>
                        {
                            app.username         = UsernameBox.Text;
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                        });
                        break;

                    case Constants.SIGNUP_EMAIL_OR_USERNAME_EXIST:
                        MyMessageQueue.Enqueue("Username or email already exist.");
                        break;

                    case Constants.SIGNUP_UNEXPECTED_ERR:
                        MyMessageQueue.Enqueue("There was an unexpected error.");
                        break;
                    }
                });
            }
        }
Exemple #2
0
        private void CreateBTN_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndeterminate(btn, true);
            MaterialDesignThemes.Wpf.ButtonProgressAssist.SetValue(btn, -1);
            MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndicatorVisible(btn, true);
            btn.IsEnabled = false;
            bool allFine = true;

            MaxPlayersBox.Text = new String(MaxPlayersBox.Text.Where(Char.IsDigit).ToArray());
            if (MaxPlayersBox.Text.Length > 3)
            {
                MaxPlayersBox.Text = MaxPlayersBox.Text.Substring(0, 3);
            }
            if (String.IsNullOrEmpty(MaxPlayersBox.Text))
            {
                setError(MaxPlayersBox, "Field is required.");
                allFine = false;
            }
            else
            {
                int count = int.Parse(MaxPlayersBox.Text);
                if (count > 15)
                {
                    setError(MaxPlayersBox, "Can't be more than 15 players.");
                    allFine = false;
                }
                else if (count < 2)
                {
                    setError(MaxPlayersBox, "Can't be less than 2 players.");
                    allFine = false;
                }
                else
                {
                    Validation.ClearInvalid(MaxPlayersBox.GetBindingExpression(TextBox.TextProperty));
                }
            }
            QuestionsBox.Text = new String(QuestionsBox.Text.Where(Char.IsDigit).ToArray());
            if (QuestionsBox.Text.Length > 3)
            {
                QuestionsBox.Text = QuestionsBox.Text.Substring(0, 3);
            }
            if (String.IsNullOrEmpty(QuestionsBox.Text))
            {
                setError(QuestionsBox, "Field is required.");
                allFine = false;
            }
            else
            {
                int count = int.Parse(QuestionsBox.Text);
                if (count > 20)
                {
                    setError(QuestionsBox, "Can't be more than 20 questions.");
                    allFine = false;
                }
                else if (count < 2)
                {
                    setError(QuestionsBox, "Can't be less than 2 questions.");
                    allFine = false;
                }
                else
                {
                    Validation.ClearInvalid(QuestionsBox.GetBindingExpression(TextBox.TextProperty));
                }
            }
            SecondsBox.Text = new String(SecondsBox.Text.Where(Char.IsDigit).ToArray());
            if (SecondsBox.Text.Length > 3)
            {
                SecondsBox.Text = SecondsBox.Text.Substring(0, 3);
            }
            if (String.IsNullOrEmpty(SecondsBox.Text))
            {
                setError(SecondsBox, "Field is required.");
                allFine = false;
            }
            else
            {
                int count = int.Parse(SecondsBox.Text);
                if (count > 100)
                {
                    setError(SecondsBox, "Can't be more than 100 seconds.");
                    allFine = false;
                }
                else if (count < 10)
                {
                    setError(SecondsBox, "Can't be less than 10 seconds.");
                    allFine = false;
                }
                else
                {
                    Validation.ClearInvalid(SecondsBox.GetBindingExpression(TextBox.TextProperty));
                }
            }
            if (String.IsNullOrEmpty(RoomNameBox.Text))
            {
                setError(RoomNameBox, "Field is required.");
                allFine = false;
            }
            else
            {
                Validation.ClearInvalid(RoomNameBox.GetBindingExpression(TextBox.TextProperty));
            }
            if (allFine)
            {
                CreateRoomRequest createRoomRequest = new CreateRoomRequest();
                createRoomRequest.answerTimeout = int.Parse(SecondsBox.Text);
                createRoomRequest.maxUsers      = int.Parse(MaxPlayersBox.Text);
                createRoomRequest.questionCount = int.Parse(QuestionsBox.Text);
                createRoomRequest.roomName      = RoomNameBox.Text;
                app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(createRoomRequest, Constants.CREATE_ROOM_REQUEST_CODE)).ContinueWith(task =>
                {
                    ResponseInfo response = task.Result;
                    CreateRoomResponse createRoomResponse = JsonDeserializer.deserializeResponse <CreateRoomResponse>(response.buffer);
                    switch (createRoomResponse.status)
                    {
                    case Constants.CREATE_ROOM_SUCCESS:
                        MyMessageQueue.Enqueue("Room Created Successfully!");
                        this.Dispatcher.Invoke(() =>
                        {
                            app.admin            = true;
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("RoomLobby.xaml", UriKind.Relative));
                        });
                        break;

                    case Constants.CREATE_ROOM_MAXIMUM_ROOMS_IN_SERVER:
                        MyMessageQueue.Enqueue("Max rooms reached.\nTry again later.");
                        break;
                    }
                    this.Dispatcher.Invoke(() =>
                    {
                        ButtonProgressAssist.SetIsIndeterminate(btn, false);
                        ButtonProgressAssist.SetIsIndicatorVisible(btn, false);
                        btn.IsEnabled = true;
                    });
                });
            }
            else
            {
                ButtonProgressAssist.SetIsIndeterminate(btn, false);
                ButtonProgressAssist.SetIsIndicatorVisible(btn, false);
                btn.IsEnabled = true;
            }
        }