Beispiel #1
0
 /// <summary>
 ///  this function will called when client want exit the "FourRowGameWindow".
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void FourRowGameWindow_OnClosing(object sender, CancelEventArgs e)
 {
     if (!_endGame)
     {
         var result = MessageBox.Show(_userName + " Are You Sure You Want To Exit The Game?",
                                      "Exit!", MessageBoxButton.YesNo, MessageBoxImage.Question);
         if (result == MessageBoxResult.Yes)
         {
             if (_fourRowGame.IsMyTurn)
             {
                 if (WaitingRoomWindow.CheckPingToServer(Client))
                 {
                     ExitMiddleLiveGame();
                 }
                 else
                 {
                     MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                 }
             }
         }
         else
         {
             e.Cancel = true;
             return;
         }
     }
     ShowWaitingRoomAfterEndGame(_opponentUserName);
 }
 /// <summary>
 /// this function called when the window is loaded.
 /// </summary>
 private void SearchTwoPlayersGameWindow_OnLoaded(object sender, RoutedEventArgs e)
 {
     try
     {
         if (WaitingRoomWindow.CheckPingToServer(Client))
         {
             LbAllClients.ItemsSource = Client.GetAllRegisterClients(); // ask & get all register users for the server.
         }
         else
         {
             MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
     catch (FaultException <DbFault> fault) //DB exception has happened at the server.
     {
         if (fault.InnerException != null)
         {
             MessageBox.Show(
                 fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
         }
     }
     catch (Exception ex) // all un know exceptions.
     {
         MessageBox.Show(
             ex.Message + " ##\n##\n" + ex.InnerException.Message);
     }
 }
Beispiel #3
0
 /// <summary>
 /// this function called when window is loaded.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     if (!_isTop10) // regular mod.
     {
         var results = Client.SortAllRegisterUsers(Username, "UserName");
         results = results.OrderByDescending(r => r.UserName).ToList();
         LbRegisterUsers.ItemsSource = results;
         MessageBox.Show("Please Select Option to Sort using - Sort By", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
     }
     else // top10 mode
     {
         DockPanel.Visibility = Visibility.Hidden;
         if (WaitingRoomWindow.CheckPingToServer(Client))
         {
             try
             {
                 var results = Client.SortAllRegisterUsers(Username, "NumberOfVictory");
                 results = results.OrderByDescending(r => r.NumberOfVictory).Take(10).ToList();
                 LbRegisterUsers.ItemsSource = results;
                 if (results.Count == 0)
                 {
                     NoValues();
                 }
             }
             catch (FaultException <DbFault> fault) //DB exception has happened at the server.
             {
                 if (fault.InnerException != null)
                 {
                     MessageBox.Show(
                         fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
                 }
             }
             catch (TimeoutException) // timeout exceptions.
             {
                 MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
             }
             catch (Exception ex) // all un know exceptions.
             {
                 if (ex.InnerException != null && ex.InnerException.Message.Contains("Unable to connect to the remote server"))
                 {
                     MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                 }
                 else
                 {
                     MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                 }
             }
         }
         else
         {
             MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
 }
        }                                                // client ref.

        /// <summary>
        /// this function for search two selected player for the list box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchButton_OnClick(object sender, RoutedEventArgs e)
        {
            var tempUserName = new List <string>(); // selected two users name..

            LbGames.ItemsSource = null;
            if (LbAllClients.SelectedItems.Count < 2)
            {
                MessageBox.Show("Please Select Two Players!",
                                "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            foreach (var selectedItem in LbAllClients.SelectedItems) // save all the users.
            {
                var userName = selectedItem as string;
                if (userName != null)
                {
                    tempUserName.Add(userName);
                }
            }

            if (WaitingRoomWindow.CheckPingToServer(Client))
            {
                var results = Client.ShowAllHistoryGamesTwoPlayers(tempUserName); // ask for records of the two players.
                LbGames.ItemsSource = results;
                if (results == null)                                              // check if records has found.
                {
                    TbUsersWinnerPercent.Text       = "No Much Has Found!";
                    TbUsersWinnerPercent.Foreground = new SolidColorBrush(Colors.Red);
                }
                else // parse the found records to view.
                {
                    var totalGame  = results.Count;
                    var winUserOne = results.Where(h => h.WinUserName == tempUserName[0]).ToList().Count;
                    var winUserTwo = results.Where(h => h.WinUserName == tempUserName[1]).ToList().Count;
                    TbUsersWinnerPercent.Foreground = new SolidColorBrush(Colors.Black);

                    var ratioUserOne = ((double)winUserOne / totalGame) * 100;
                    var strRatioOne  = $"{ratioUserOne:0.00}";
                    var ratioUserTwo = ((double)winUserTwo / totalGame) * 100;
                    var strRatioTwo  = $"{ratioUserTwo:0.00}";
                    TbUsersWinnerPercent.Text = $"{tempUserName[0]} Win %: {strRatioOne}, {tempUserName[1]} Win %: {strRatioTwo}";
                }
            }
        }
Beispiel #5
0
        }                                    // username

        /// <summary>
        /// this function will happened when window is loaded.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SummeryUserGameWindows_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (WaitingRoomWindow.CheckPingToServer(Client))
            {
                try
                { //ask for server records about the player.
                    var result    = Client.ShowAllPlayerHistoryGames(Username);
                    var totalGame = result.Item1.Count;
                    var winGame   = result.Item2.NumberOfVictory;
                    if (totalGame > 0) // no game founds
                    {
                        var ratio    = ((double)winGame / totalGame) * 100;
                        var strRatio = $"{ratio:0.00}";
                        TbSummeryDetails.Text = $"{Username} Win: {strRatio}%, Number Of Games: {result.Item2.NumberOfGames} \n" +
                                                $"Number Of Victory: {result.Item2.NumberOfVictory}, Number Of Losses: {result.Item2.NumberOfLosses} \n" +
                                                $"Number Of Points: {result.Item2.NumberOfPoints}";
                    }
                    else // history games has found!
                    {
                        TbSummeryDetails.Text = $"{Username} Win: 0%, Number Of Games: {result.Item2.NumberOfGames} \n" +
                                                $"Number Of Victory: {result.Item2.NumberOfVictory}, Number Of Losses: {result.Item2.NumberOfLosses} \n" +
                                                $"Number Of Points: {result.Item2.NumberOfPoints}";
                    }
                    LbGames.ItemsSource = result.Item1;
                }
                catch (FaultException <DbFault> fault) //DB exception has happened at the server.
                {
                    if (fault.InnerException != null)
                    {
                        MessageBox.Show(
                            fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
                    }
                }
                catch (Exception ex) // all un know exceptions.
                {
                    MessageBox.Show(
                        ex.Message + " ##\n##\n" + ex.InnerException.Message);
                }
            }
            else
            {
                MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #6
0
 /// <summary>
 /// sort all result by sum of points of clients.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void SortByNumberOfPointsMenu_OnClick(object sender, RoutedEventArgs e)
 {
     if (WaitingRoomWindow.CheckPingToServer(Client))
     {
         try
         {
             var results = Client.SortAllRegisterUsers(Username, "NumberOfPoints");
             LbRegisterUsers.ItemsSource = results;
             if (results.Count == 0)
             {
                 NoValues();
             }
         }
         catch (FaultException <DbFault> fault)
         {
             if (fault.InnerException != null)
             {
                 MessageBox.Show(
                     fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
             }
         }
         catch (TimeoutException)
         {
             MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         }
         catch (Exception ex)
         {
             if (ex.InnerException != null && ex.InnerException.Message.Contains("Unable to connect to the remote server"))
             {
                 MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
             }
             else
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
             }
         }
     }
     else
     {
         MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        }                                                // client ref.

        /// <summary>
        /// this function is start when window is loaded.
        /// </summary>
        private void SearchGameWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (WaitingRoomWindow.CheckPingToServer(Client))
                {
                    if (_isHistoryGame) // is history mode, ask the server for history records.
                    {
                        LbGames.ItemsSource = Client.ShowAllHistoryGames();
                    }
                    else  // is live mode, ask the server for live records.
                    {
                        LbGames.ItemsSource = Client.ShowAllLiveGames();
                    }
                }
                else
                {
                    MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (FaultException <DbFault> fault) //DB exception has happened at the server.
            {
                if (fault.InnerException != null)
                {
                    MessageBox.Show(
                        fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
                }
            }
            catch (Exception ex) // all un know exceptions.
            {
                if (ex.InnerException != null && ex.InnerException.Message.Contains("Unable to connect to the remote server"))
                {
                    MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
Beispiel #8
0
        public Action <string> ShowWaitingRoomAfterEndGame; // event that called after game end (good or bad).


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (WaitingRoomWindow.CheckPingToServer(Client)) // show clients images
            {
                var imageClients = Client.GetImageClients(_userName, _opponentUserName);
                LoadImage(imageClients[0], true);
                LoadImage(imageClients[1], false);
            }
            else
            {
                MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            TbUserNameOne.Text = "(You) " + _userName + ":";
            TbUserNameTwo.Text = _opponentUserName + ":";

            Title = $"{_userName} Board Game";

            CurrentPlayer.Fill = _isStartFirst ? _myColorBrush : _opponentColorBrush; // starting disk color...

            StartTimerTick();                                                         // start timer.
        }
Beispiel #9
0
        }                                    // user name.

        /// <summary>
        /// sort all result by username.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SortByUserNameMenu_OnClick(object sender, RoutedEventArgs e)
        {
            if (WaitingRoomWindow.CheckPingToServer(Client))
            {
                try
                {
                    var results = Client.SortAllRegisterUsers(Username, "UserName"); // ask form server for all user result games info.
                    LbRegisterUsers.ItemsSource = results;
                    if (results.Count == 0)                                          // no result found.
                    {
                        NoValues();
                    }
                }
                catch (FaultException <DbFault> fault) //DB exception has happened at the server.
                {
                    if (fault.InnerException != null)
                    {
                        MessageBox.Show(
                            fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
                    }
                }
                catch (Exception ex) // all un know exceptions.
                {
                    if (ex.InnerException != null && ex.InnerException.Message.Contains("Unable to connect to the remote server"))
                    {
                        MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #10
0
        /// <summary>
        /// this function called  when click the register button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RegisterButton_OnClick(object sender, RoutedEventArgs e)
        {
            bool hasChangeDb = false; // when DB has update. at the end of register.

            string password = TbPassword.Password.Trim();

            if (!string.IsNullOrEmpty(TbUsername.Text) &&
                !string.IsNullOrEmpty(TbPassword.Password) &&
                !string.IsNullOrEmpty(TbEmojiName.Text))
            {
                var clientCallBack = new ClientCallBack();                                          // temp callback obj.
                var client         = new FourRowServiceClient(new InstanceContext(clientCallBack)); // client for this moment.
                var userName       = TbUsername.Text.Trim();
                var emojiName      = TbEmojiName.Text;
                try
                {
                    if (WaitingRoomWindow.CheckPingToServer(client)) // try to register at the server.
                    {
                        hasChangeDb = client.Register(userName, LoginWindow.HashValue(password), emojiName);
                    }
                }
                catch (FaultException <UserExistsFault> fault)  // user exist at the DB (key-username).
                {
                    MessageBox.Show(fault.Detail.Details, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (FaultException <DbFault> fault) // DB exception has happened at the server.
                {
                    if (fault.InnerException != null)
                    {
                        MessageBox.Show(
                            fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
                    }
                }
                catch (TimeoutException) // timeout exceptions.
                {
                    MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (Exception ex) // all un know exceptions.
                {
                    if (ex.InnerException != null && ex.InnerException.Message.Contains("Unable to connect to the remote server"))
                    {
                        MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }

                if (hasChangeDb) // notify about the DB update.
                {
                    MessageBox.Show("Register Success ✔️, Database UpDate!",
                                    "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    _manualClosing = true; // for moving the waiting room without ask if want the exit.
                    Close();
                }
            }
            else // no all field are fill.
            {
                if (!string.IsNullOrEmpty(TbUsername.Text))
                {
                    MessageBox.Show("Please Fill Username!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else if (!string.IsNullOrEmpty(TbPassword.Password))
                {
                    MessageBox.Show("Please Fill Password!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show("Please Browser a Icon!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        /// <summary>
        /// this function is when click at the connect button.
        /// check at the DB and create a client.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConnectButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(TbUsername.Text) &&
                !string.IsNullOrEmpty(TbPassword.Password))
            {
                ConnectButton.Cursor = Cursors.Wait;
                var clientCallBack = new ClientCallBack();                                          // create a client callback.
                var client         = new FourRowServiceClient(new InstanceContext(clientCallBack)); // create the client.

                string username = TbUsername.Text.Trim();
                var    name     = username;
                string password = TbPassword.Password.Trim();
                try
                {
                    if (WaitingRoomWindow.CheckPingToServer(client))
                    {
                        client.Connect(username, HashValue(password));                // try to connect.
                        var waitingRoomWindow = new WaitingRoomWindow(clientCallBack) // create a waiting window..
                        {
                            Client   = client,
                            Username = username,
                            Title    = username + " Waiting Room"
                        };

                        _manualClosing = true; // manual closing is happened.
                        Close();
                        ConnectButton.Cursor = Cursors.Arrow;
                        waitingRoomWindow.Show();
                    }
                    else
                    {
                        MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                catch (FaultException <UserExistsFault> fault) // user found at the DB at the server.
                {
                    MessageBox.Show(fault.Detail.Details, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (FaultException <UserNotExistsFault> fault) // user not found at the DB at the server.
                {
                    MessageBox.Show(fault.Detail.Details, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (FaultException <WrongPasswordFault> fault) // user wrong password error.
                {
                    MessageBox.Show(fault.Detail.Details, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (TimeoutException) // timeout exceptions.
                {
                    MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (Exception ex) // all un know exceptions.
                {
                    if (ex.InnerException != null && ex.InnerException.Message.Contains("Unable to connect to the remote server"))
                    {
                        MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else
            {
                if (string.IsNullOrEmpty(TbUsername.Text))
                {
                    MessageBox.Show("Please Fill Username!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show("Please Fill Password!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// play the played move, graphic & logic
        /// </summary>
        private void MovePlayed(Move move, Ball newBall)
        {
            if (_fourRowGame.Move(move.Column)) // make his move at the game model/logic.
            {
                try
                {
                    if (_fourRowGame.IsWin) // check win & report accordingly.
                    {
                        _endGame             = true;
                        move.WinningRowDisks = _fourRowGame.WinningRowDisks; // set the winning row, for the nice display.
                        if (WaitingRoomWindow.CheckPingToServer(Client))     // report client move to the server.
                        {
                            Client.ReportMove(_userName, move, _opponentUserName, MoveResult.YouWon, _fourRowGame.Score);
                        }
                        else
                        {
                            MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            _endGame = true;
                        }
                    }
                    else if (_fourRowGame.IsTie) // only the invited client can call this, because hi played last and the board is full now.
                    {
                        _endGame = true;
                        if (WaitingRoomWindow.CheckPingToServer(Client))
                        {
                            Client.ReportMove(_userName, move, _opponentUserName, MoveResult.Draw, _fourRowGame.Score);
                        }
                        else
                        {
                            MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            _endGame = true;
                        }
                    }
                    else
                    {
                        if (WaitingRoomWindow.CheckPingToServer(Client)) // report the move, game still playing.
                        {
                            Client.ReportMove(_userName, move, _opponentUserName, MoveResult.NotYourTurn, _fourRowGame.Score);
                        }
                        else
                        {
                            MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            _endGame = true;
                        }
                    }

                    RegisterMouseToBall(move.Column, newBall); // fill Ellipse can be "clicked".
                    SmoothBallOnTheBoard(newBall);             // show graphic at the screen.
                    if (_fourRowGame.IsWin)                    // show winning graphic at the screen.
                    {
                        ShowWinningRowDisks(_fourRowGame.WinningRowDisks, true);
                    }
                    TbUserOneScore.Text = "Score: " + _fourRowGame.Score.ToString(); // update my score.

                    if (!_endGame)                                                   // game didn't end yet.
                    {
                        CurrentPlayer.Fill = _opponentColorBrush;                    // set the opponent color disk/ball.
                        NotMyTurn();                                                 // update turn info.
                    }

                    else // game ended (win, tie)
                    {
                        if (WaitingRoomWindow.CheckPingToServer(Client))
                        {
                            Client.EndGame(_userName, _fourRowGame.Score, _opponentUserName, _opponentScore);
                        }
                        else
                        {
                            MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            _endGame = true;
                        }

                        TbTurn.Text       = "The Game Ended, Please Exit!";
                        TbTurn.Foreground = new SolidColorBrush(Colors.DarkBlue);
                        _timer.Stop();          // stop game timer.

                        if (_fourRowGame.IsWin) // display winning message.
                        {
                            MessageBox.Show(_userName + " Win The Game!!", "Winner!!!", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                }
                catch (FaultException <OpponentDisconnectedFault> fault) // when opponent disconnect while playing.
                {
                    EndGame(fault.Detail.Details);                       // end game.
                    TbUserOneScore.Text = "Score: 1000";
                    TbUserTwoScore.Text = "Score: 0";
                }
                catch (FaultException <DbFault> fault) // DB exception that happened at the server while try to update DB.
                {
                    if (fault.InnerException != null)
                    {
                        MessageBox.Show(
                            fault.Detail.Details + " ##\n##\n" + fault.InnerException.Message);
                    }
                }
                catch (TimeoutException) // Timeout..
                {
                    MessageBox.Show("The Server Has Disconnected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    _endGame = true;
                }
                catch (Exception ex) // all un know exceptions.
                {
                    if (ex.InnerException != null && ex.InnerException.Message.Contains("Unable to connect to the remote server"))
                    {
                        MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }