Exemple #1
0
        private ClientCallBack _clientCallBack;               // client callBack ref.

        // c'tor.
        public FourRowGameWindow(ClientCallBack clientCallBack, string userName, string opponentUserName, bool isStatFirst)
        {
            InitializeComponent();
            _isStartFirst     = isStatFirst;
            _userName         = userName;
            _opponentUserName = opponentUserName;

            _clientCallBack                    = clientCallBack;
            _clientCallBack.UpdateGame         = OpponentMoveUpdate;
            _clientCallBack.EndGame            = EndGame;
            _clientCallBack.OpponentDisconnect = NotifyOpponentDisconnect;

            if (_isStartFirst) // first play.
            {
                _fourRowGame        = new Game('R', 'Y', _isStartFirst);
                _myColorBrush       = new SolidColorBrush(Colors.DarkRed);
                _opponentColorBrush = new SolidColorBrush(Colors.DarkGoldenrod);
                MyTurn();
            }
            else // second play.
            {
                _fourRowGame        = new Game('Y', 'R', _isStartFirst);
                _myColorBrush       = new SolidColorBrush(Colors.DarkGoldenrod);
                _opponentColorBrush = new SolidColorBrush(Colors.DarkRed);
                NotMyTurn();
            }
        }
        private ClientCallBack _clientCallBack;                            // callback ref.

        // c'tor.
        public WaitingRoomWindow(ClientCallBack clientCallBack)
        {
            InitializeComponent();
            _clientCallBack = clientCallBack;
            _clientCallBack.InviteToPlay      = InvitedToPlay;
            _clientCallBack.ReplyInviteToPlay = ReplyInvitedToPlay;
            InviteUserToPlayButton.IsEnabled  = false;
        }
Exemple #3
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);
                }
            }
        }