Example #1
0
        /// <summary>
        /// Connect to game method - configures the endpoint details then activates the remote object
        /// Enables and disables tools depending on if the user is spectating or not
        /// Paints the arena gray
        /// </summary>
        public void connectToGame()
        {
            try
            {
                // Configure the Endpoint details
                DuplexChannelFactory<IGame> channel = new DuplexChannelFactory<IGame>(this, "Game");

                // Activate a remote game object
                gameInstance = channel.CreateChannel();
                if(callbackId == 0)
                    callbackId = gameInstance.CallbackRegister();

                if (!spectating)
                {
                    _player = gameInstance.createPlayer(txtName.Text);
                    if (_player == 0)
                    {
                        gameInstance.Spectator++;
                        spectating = true;
                        txtName.Text = "";
                        txtName.IsEnabled = true;
                        cmdConnect.IsEnabled = false;
                        cmdSpectate.IsEnabled = false;
                        lblColorText.Visibility = Visibility.Hidden;
                        lblColor.Visibility = Visibility.Hidden;
                    }
                    else
                        this.KeyDown += new KeyEventHandler(buttonDown);

                    lblColorText.Visibility = Visibility.Visible;
                    lblColor.Visibility = Visibility.Visible;

                    colorCode++;
                }
                else
                {
                    gameInstance.Spectator++;
                    lblColorText.Visibility = Visibility.Hidden;
                    lblColor.Visibility = Visibility.Hidden;
                }

                gameBoard.Background = new SolidColorBrush(Colors.Gray);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error communicating with server: " + ex.Message);
            }
        }