Example #1
0
        /// <summary>
        /// Receives the command and deals with it
        /// </summary>
        /// <param name="command">Command sent in</param>
        /// <param name="e">Exception</param>
        private void CommandReceived(string command, Exception e)
        {
            string[] scores = new string[2];

            // If an exception is given, displays a message and closes the client
            if (e != null)
            {
                MessageBox.Show("Server connection has closed.");
                clientModel.CloseClient();
                Switcher.Switch(new ChooseOpponent());
            }
            // If the command is not null, parses the command
            if (command != null)
            {
                //Update time
                if (Regex.IsMatch(command, @"^TIME"))
                {
                    string[] commandList = command.Split(' ');

                    timeRemaining.Text = commandList[1];
                }
                //Update score
                else if (Regex.IsMatch(command, @"^SCORE"))
                {
                    scores = command.Split(' ');

                    p1Score.Text = scores[1];
                    p2Score.Text = scores[2];
                }
                //Game ended
                else if (Regex.IsMatch(command, @"^STOP"))
                {
                    Switcher.Switch(new GameEnds(command, p1Score.Text, p2Score.Text, p1Name.Text, p2Name.Text));

                    clientModel.CloseClient();
                }
                //Ignore command
                else if (Regex.IsMatch(command, @"^IGNORING"))
                {
                    string[] ignoring = command.Split(' ');
                    MessageBox.Show("Invalid command: " + ignoring[1]);
                }
                //Game has been terminated
                else if (Regex.IsMatch(command, @"^TERMINATED"))
                {
                    MessageBox.Show("Opponent has closed connection.");
                    Switcher.Switch(new ChooseOpponent());
                }
            }
        }
 private void Window_Closed(object sender, EventArgs e)
 {
     if (Application.Current.Properties["clientModel"] != null)
     {
         BoggleClientModel model = (BoggleClientModel)Application.Current.Properties["clientModel"];
         model.CloseClient();
     }
 }
 /// <summary>
 /// Cancels the connection
 /// </summary>
 /// <param name="sender">sender</param>
 /// <param name="e">Exception</param>
 private void Cancel_Click(object sender, RoutedEventArgs e)
 {
     clientModel.CloseClient();
     clientModel.incomingWordEvent = null;
     Switcher.Switch(new ChooseOpponent());
 }