private void submitAnswer(int answer)
        {
            thr.Abort();
            disableAllButtons();
            SubmitQuestionRequest submitQuestionRequest = new SubmitQuestionRequest();

            submitQuestionRequest.answerId = answer;
            app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(submitQuestionRequest, Constants.SUBMIT_ANSWER_REQUEST)).ContinueWith(task =>
            {
                ResponseInfo response = task.Result;
                SubmitQuestionResponse submitQuestionResponse = JsonDeserializer.deserializeResponse <SubmitQuestionResponse>(response.buffer);

                switch (submitQuestionResponse.status)
                {
                case Constants.SUBMIT_ANSWER_SUCCESS:
                    if (answer == submitQuestionResponse.correctAnswerId)
                    {
                        score++;
                    }
                    this.Dispatcher.Invoke(() =>
                    {
                        ScoreTXT.Text = score.ToString();
                        showAnswers(submitQuestionResponse.correctAnswerId, answer);
                    });
                    break;
                }
            });
        }
        private void StartBTN_Click(object sender, RoutedEventArgs e)
        {
            app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(new StartGameRequest(), Constants.START_GAME_REQUEST)).ContinueWith(task =>
            {
                ResponseInfo response = task.Result;
                StartGameResponse startGameResponse = JsonDeserializer.deserializeResponse <StartGameResponse>(response.buffer);
                switch (startGameResponse.status)
                {
                case Constants.START_GAME_SUCCESS:
                    thr.Abort();
                    MyMessageQueue.Enqueue("Room started successfuly.");
                    this.Dispatcher.Invoke(() =>
                    {
                        NavigationService ns = NavigationService.GetNavigationService(this);
                        ns.Navigate(new Uri("QuestionScreen.xaml", UriKind.Relative));
                    });
                    break;

                case Constants.START_GAME_NOT_EXIST:
                    thr.Abort();
                    MyMessageQueue.Enqueue("Room not exist anymore.");
                    this.Dispatcher.Invoke(() =>
                    {
                        NavigationService ns = NavigationService.GetNavigationService(this);
                        ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                    });
                    break;
                }
            });
        }
        private void loadQuestion()
        {
            GetQuestionRequest getQuestionRequest = new GetQuestionRequest();

            app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getQuestionRequest, Constants.GET_QUESTION_REQUEST)).ContinueWith(task =>
            {
                ResponseInfo response = task.Result;
                GetQuestionResponse getQuestionResponse = JsonDeserializer.deserializeResponse <GetQuestionResponse>(response.buffer);

                switch (getQuestionResponse.status)
                {
                case Constants.GET_QUESTION_SUCCESS:

                    this.Dispatcher.Invoke(() =>
                    {
                        LoadQuestionToUI(getQuestionResponse);
                    });
                    break;

                case Constants.GET_QUESTION_FINISHED:
                    thr.Abort();
                    MyMessageQueue.Enqueue("You finished!");
                    this.Dispatcher.Invoke(() =>
                    {
                        NavigationService ns = NavigationService.GetNavigationService(this);
                        ns.Navigate(new Uri("LeaderboardScreen.xaml", UriKind.Relative));
                    });
                    break;
                }
            });
        }
Beispiel #4
0
        private void SignoutBTN_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);
            disableAllButtons();
            app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(new SignoutRequest(), Constants.SIGNOUT_REQUEST_CODE)).ContinueWith(task =>
            {
                ResponseInfo response       = task.Result;
                LoginResponse loginResponse = JsonDeserializer.deserializeResponse <LoginResponse>(response.buffer);
                switch (loginResponse.status)
                {
                case Constants.LOGOUT_SUCCESS:
                    MyMessageQueue.Enqueue("Sign out Successfully!");
                    this.Dispatcher.Invoke(() =>
                    {
                        NavigationService ns = NavigationService.GetNavigationService(this);
                        ns.Navigate(new Uri("Signin.xaml", UriKind.Relative));
                    });
                    break;
                }
            });
        }
Beispiel #5
0
        private void joinRoom(int id)
        {
            JoinRoomRequest joinRoomRequest = new JoinRoomRequest();

            joinRoomRequest.roomId = id;
            app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(joinRoomRequest, Constants.JOIN_ROOM_REQUEST)).ContinueWith(task =>
            {
                ResponseInfo response             = task.Result;
                JoinRoomResponse joinRoomResponse = JsonDeserializer.deserializeResponse <JoinRoomResponse>(response.buffer);
                switch (joinRoomResponse.status)
                {
                case Constants.JOIN_ROOM_SUCCESS:
                    MyMessageQueue.Enqueue("Joined Successfully");
                    this.Dispatcher.Invoke(() =>
                    {
                        thr.Abort();
                        app.admin            = false;
                        NavigationService ns = NavigationService.GetNavigationService(this);
                        ns.Navigate(new Uri("RoomLobby.xaml", UriKind.Relative));
                    });
                    break;

                case Constants.JOIN_ROOM_NOT_EXIST:
                    MyMessageQueue.Enqueue("Room not exist any more.");
                    break;

                case Constants.JOIN_ROOM_MAXIMUM_USERS_IN_ROOM:
                    MyMessageQueue.Enqueue("Room is full.");
                    break;
                }
            });
        }
        private void BackBTN_Click(object sender, RoutedEventArgs e)
        {
            if (app.admin)
            {
                CloseRoomRequest closeRoomRequest = new CloseRoomRequest();

                app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(closeRoomRequest, Constants.CLOSE_ROOM_REQUEST)).ContinueWith(task =>
                {
                    ResponseInfo response = task.Result;
                    CloseRoomResponse closeRoomResponse = JsonDeserializer.deserializeResponse <CloseRoomResponse>(response.buffer);
                    switch (closeRoomResponse.status)
                    {
                    case Constants.CLOSE_ROOM_SUCCESS:
                        thr.Abort();
                        MyMessageQueue.Enqueue("You leaved the room successfuly.");
                        this.Dispatcher.Invoke(() =>
                        {
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                        });

                        break;
                    }
                });
            }
            else
            {
                LeaveRoomRequest leaveRoomRequest = new LeaveRoomRequest();

                app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(leaveRoomRequest, Constants.LEAVE_ROOM_REQUEST)).ContinueWith(task =>
                {
                    ResponseInfo response = task.Result;
                    LeaveRoomResponse leaveRoomResponse = JsonDeserializer.deserializeResponse <LeaveRoomResponse>(response.buffer);
                    switch (leaveRoomResponse.status)
                    {
                    case Constants.LEAVE_ROOM_SUCCESS:
                        thr.Abort();
                        MyMessageQueue.Enqueue("You left the room successfuly.");
                        this.Dispatcher.Invoke(() =>
                        {
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                        });

                        break;
                    }
                });
            }
        }
Beispiel #7
0
 private void updateRooms()
 {
     while (true)
     {
         GetRoomsIdRequest  getRoomsIdRequest  = new GetRoomsIdRequest();
         ResponseInfo       response           = app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getRoomsIdRequest, Constants.GET_ROOMS_ID_REQUEST_CODE)).Result;
         GetRoomsIdResponse getRoomsIdResponse = JsonDeserializer.deserializeResponse <GetRoomsIdResponse>(response.buffer);
         removeRooms(getRoomsIdResponse.ids);
         for (int i = 0; i < getRoomsIdResponse.ids.Length; i++)
         {
             GetRoomDetailsRequest getRoomDetailsRequest = new GetRoomDetailsRequest();
             getRoomDetailsRequest.roomId = getRoomsIdResponse.ids[i];
             ResponseInfo           response1 = app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getRoomDetailsRequest, Constants.GET_ROOM_DETAILS_REQUEST)).Result;
             GetRoomDetailsResponse getRoomDetailsResponse = JsonDeserializer.deserializeResponse <GetRoomDetailsResponse>(response1.buffer);
             addOrUpdateRoom(getRoomDetailsResponse);
         }
         System.Threading.Thread.Sleep(500);
     }
 }
Beispiel #8
0
        public void getStats()
        {
            GetMyStatisticsRequest getMyStatisticsRequest = new GetMyStatisticsRequest();


            app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getMyStatisticsRequest, Constants.GET_MY_STATISTICS_REQUEST)).ContinueWith(task =>
            {
                ResponseInfo response = task.Result;
                GetMyStatisticsResponse getMyStatisticsResponse = JsonDeserializer.deserializeResponse <GetMyStatisticsResponse>(response.buffer);
                switch (getMyStatisticsResponse.status)
                {
                case Constants.GET_MY_STATISTICS_SUCCESS:
                    MyMessageQueue.Enqueue("Load Stats Successfully!");
                    this.Dispatcher.Invoke(() =>
                    {
                        loadStatsToUI(getMyStatisticsResponse);
                    });
                    break;
                }
            });
        }
        public void updateLeaderboard()
        {
            while (true)
            {
                GetResultsRequest getResultsRequest = new GetResultsRequest();

                ResponseInfo response = app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getResultsRequest, Constants.GET_RESULTS_REQUEST)).Result;

                GetResultsResponse getResultsResponse = JsonDeserializer.deserializeResponse <GetResultsResponse>(response.buffer);

                switch (getResultsResponse.status)
                {
                case Constants.GET_RESULTS_SUCCESS:
                    this.Dispatcher.Invoke(() =>
                    {
                        UpdateLeaderboardUI(getResultsResponse.results);
                    });
                    break;
                }

                System.Threading.Thread.Sleep(100);
            }
        }
Beispiel #10
0
        private void updateRoom()
        {
            while (true)
            {
                GetRoomDetailsRequest getRoomDetailsRequest = new GetRoomDetailsRequest();
                getRoomDetailsRequest.roomId = 0;
                ResponseInfo           response = app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getRoomDetailsRequest, Constants.GET_ROOM_DETAILS_REQUEST)).Result;
                GetRoomDetailsResponse getRoomDetailsResponse = JsonDeserializer.deserializeResponse <GetRoomDetailsResponse>(response.buffer);

                switch (getRoomDetailsResponse.status)
                {
                case Constants.GET_ROOM_DETAILS_SUCCESS:
                    if (getRoomDetailsResponse.isActive == true)
                    {
                        MyMessageQueue.Enqueue("The admin started the game.");
                        this.Dispatcher.Invoke(() =>
                        {
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("QuestionScreen.xaml", UriKind.Relative));
                        });
                        thr.Abort();
                        break;
                    }
                    else if (!app.admin && getRoomDetailsResponse.admin == app.username)
                    {
                        MyMessageQueue.Enqueue("You are admin now.");
                        app.admin = true;
                        NewAdminRequest newAdminRequest = new NewAdminRequest();

                        ResponseInfo response1 = app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(newAdminRequest, Constants.NEW_ADMIN_REQUEST)).Result;


                        NewAdminResponse newAdminResponse = JsonDeserializer.deserializeResponse <NewAdminResponse>(response.buffer);
                        switch (newAdminResponse.status)
                        {
                        case Constants.NEW_ADMIN_SUCCESS:
                            this.Dispatcher.Invoke(() =>
                            {
                                NavigationService ns = NavigationService.GetNavigationService(this);
                                ns.Refresh();
                            });
                            thr.Abort();
                            break;
                        }
                    }
                    else
                    {
                        updateRoomUI(getRoomDetailsResponse);
                    }

                    break;

                case Constants.GET_ROOM_DETAILS_NOT_EXIST:

                    MyMessageQueue.Enqueue("The admin closed the room.");
                    this.Dispatcher.Invoke(() =>
                    {
                        NavigationService ns = NavigationService.GetNavigationService(this);
                        ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                    });
                    thr.Abort();
                    break;
                }
                System.Threading.Thread.Sleep(100);
            }
        }
Beispiel #11
0
        private void SaveBTN_Click(object sender, RoutedEventArgs e)
        {
            disableAllButtonsAndText();
            startLoadingAnim();
            bool allFine = true;

            if (String.IsNullOrEmpty(RoomNameBox.Text))
            {
                setError(RoomNameBox, "Field is required.");
                allFine = false;
            }
            else
            {
                Validation.ClearInvalid(RoomNameBox.GetBindingExpression(TextBox.TextProperty));
            }
            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));
                }
            }
            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));
                }
            }
            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));
                }
            }

            if (allFine)
            {
                EditRoomRequest editRoomRequest = new EditRoomRequest();
                editRoomRequest.answerTimeout = int.Parse(SecondsBox.Text);
                editRoomRequest.questionCount = int.Parse(QuestionsBox.Text);
                editRoomRequest.maxUsers      = int.Parse(MaxPlayersBox.Text);
                editRoomRequest.roomName      = RoomNameBox.Text;
                app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(editRoomRequest, Constants.EDIT_ROOM_REQUEST)).ContinueWith(task =>
                {
                    ResponseInfo response             = task.Result;
                    EditRoomResponse editRoomResponse = JsonDeserializer.deserializeResponse <EditRoomResponse>(response.buffer);
                    switch (editRoomResponse.status)
                    {
                    case Constants.EDIT_ROOM_SUCCESS:
                        GetRoomDetailsRequest getRoomDetailsRequest = new GetRoomDetailsRequest();
                        getRoomDetailsRequest.roomId = 0;
                        app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getRoomDetailsRequest, Constants.GET_ROOM_DETAILS_REQUEST)).ContinueWith(task1 =>
                        {
                            ResponseInfo response1 = task1.Result;
                            GetRoomDetailsResponse getRoomDetailsResponse = JsonDeserializer.deserializeResponse <GetRoomDetailsResponse>(response1.buffer);
                            updateRoomTexts(getRoomDetailsResponse);
                            MyMessageQueue.Enqueue("Saved successfully!");
                            enableAllButtonsAndText();
                            stopLoadingAnim();
                        });
                        break;

                    case Constants.EDIT_ROOM_NOT_EXIST:
                        thr.Abort();
                        MyMessageQueue.Enqueue("Room not exist anymore.");
                        this.Dispatcher.Invoke(() =>
                        {
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                        });
                        break;
                    }
                });
            }
            else
            {
                enableAllButtonsAndText();
                stopLoadingAnim();
            }
        }
Beispiel #12
0
        private void LoginBTN_Clicked(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;
            SignupBTN.IsEnabled = false;
            bool EverythingFine = true;

            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 < 1)
            {
                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)
            {
                LoginRequest loginRequest = new LoginRequest();
                loginRequest.username = UsernameBox.Text;
                loginRequest.password = PasswordBox.Password;

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

                    case Constants.LOGIN_INCORRECT_PASSWORD:
                        MyMessageQueue.Enqueue("Incorrect password.");
                        break;

                    case Constants.LOGIN_USERNAME_NOT_EXIST:
                        MyMessageQueue.Enqueue("Username not exist.");
                        break;

                    case Constants.LOGIN_UNEXPECTED_ERR:
                        MyMessageQueue.Enqueue("There was an unexpected error.");
                        break;

                    case Constants.LOGIN_ALREADY_ONLINE:
                        MyMessageQueue.Enqueue("This Username is already online.");
                        break;
                    }
                    this.Dispatcher.Invoke(() =>
                    {
                        ButtonProgressAssist.SetIsIndeterminate(btn, false);
                        ButtonProgressAssist.SetIsIndicatorVisible(btn, false);
                        btn.IsEnabled       = true;
                        SignupBTN.IsEnabled = true;
                    });
                });
            }
            else
            {
                MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndeterminate(btn, false);
                MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndicatorVisible(btn, false);
                btn.IsEnabled       = true;
                SignupBTN.IsEnabled = true;
            }
        }
Beispiel #13
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;
                    }
                });
            }
        }
Beispiel #14
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;
            }
        }