コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: grahfft/CS513
        private void OnLoginPress(object sender, RoutedEventArgs e)
        {
            //purposely using variable capturing here
            string loginName = this.localName.Text;

            this.Name = loginName;

            //get sending task off the GUI thread
            Task.Run(() =>
            {
                try
                {
                    IResponseManager responseManager = this.services.ResponseManager;
                    responseManager.Start();

                    IMessageHandler messageHandler = this.services.MessageHandler;
                    IMessage request = messageHandler.GetMessage(loginName, "server", loginName,
                                                                 MessageCommand.LoginRequest);

                    IClientHandler clientHandler = this.services.ClientHandler;
                    clientHandler.SendMessage(request);
                }
                catch (Exception exception)
                {
                    this.services.ChatLog.LogMessage(string.Format("Unable to send log in : {0}", exception.Message));
                }
            });
        }
コード例 #2
0
 /// <summary>
 /// Executes the command.
 /// </summary>
 /// <param name="args">the arguments for the command.</param>
 /// <param name="client">the client that gave the command</param>
 /// <returns></returns>
 public TaskResult Execute(string[] args, TcpClient client = null)
 {
     // needs to be only one argument.
     if (args.Length != 1)
     {
         return(new TaskResult("bad args", false));
     }
     try
     {
         string name = args[0];
         // find the game to close.
         MultiPlayerGame game = model.GetGame(client);
         // get the client we need to send him the close msg.
         TcpClient opp = game.GetOpponent(client);
         // error - eather no opp or no game.
         if (game == null || opp == null)
         {
             return(new TaskResult(null, false));
         }
         // send the opp the game is over.
         ch.SendMessage(opp, "game over");
         // disconnect the current client.
         return(new TaskResult("disconnect", false));
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(new TaskResult("error occured", false));
     }
 }
コード例 #3
0
ファイル: MainWindow.xaml.cs プロジェクト: grahfft/CS513
        private void OnSendMessagePress(object sender, RoutedEventArgs e)
        {
            //Capture message text
            string message       = this.messageText.Text;
            string messageSender = this.Name;

            //send message
            Task.Run(() =>
            {
                try
                {
                    IMessageHandler messageHandler = this.services.MessageHandler;
                    IMessage request = messageHandler.GetMessage(messageSender, "server", message,
                                                                 MessageCommand.ChatMessage);

                    IClientHandler clientHandler = this.services.ClientHandler;
                    clientHandler.SendMessage(request);
                }
                catch (Exception exception)
                {
                    this.services.ChatLog.LogMessage(string.Format("Unable to send message : {0}", exception.Message));
                }
            });

            //Clear message text from box
            this.messageText.Clear();
        }
コード例 #4
0
ファイル: MainWindow.xaml.cs プロジェクト: grahfft/CS513
        private void OnUpdateNamePress(object sender, RoutedEventArgs e)
        {
            //purposely using variable capturing here
            string newName = this.localName.Text;
            string oldName = this.Name;

            this.Name = newName;

            //get sending task off the GUI thread
            Task.Run(() =>
            {
                try
                {
                    IMessageHandler messageHandler = this.services.MessageHandler;
                    IMessage request = messageHandler.GetMessage(oldName, "server", newName, MessageCommand.UpdateName);

                    IClientHandler clientHandler = this.services.ClientHandler;
                    clientHandler.SendMessage(request);
                }
                catch (Exception exception)
                {
                    this.services.ChatLog.LogMessage(string.Format("Unable to send name update : {0}", exception.Message));
                }
            });
        }
コード例 #5
0
ファイル: MainWindow.xaml.cs プロジェクト: grahfft/CS513
        private void SendDisconnect(object sender, EventArgs e)
        {
            try
            {
                IMessageHandler messageHandler = this.services.MessageHandler;
                IMessage        request        = messageHandler.GetMessage(this.Name, "server", this.Name,
                                                                           MessageCommand.DisconnectRequest);

                IClientHandler clientHandler = this.services.ClientHandler;
                clientHandler.SendMessage(request);
            }
            catch (Exception exception)
            {
                this.services.ChatLog.LogMessage(string.Format("Unable to send disconnect : {0}", exception.Message));
            }
        }
コード例 #6
0
 /// <summary>
 /// Executes the command.
 /// </summary>
 /// <param name="args">the arguments for the command.</param>
 /// <param name="client">the client that gave the command</param>
 /// <returns></returns>
 public TaskResult Execute(string[] args, TcpClient client = null)
 {
     // needs to be only one argument.
     if (args.Length != 1)
     {
         return(new TaskResult("bad args", false));
     }
     try
     {
         string move = args[0];
         // find the playing game.
         MultiPlayerGame game = model.GetGame(client);
         // get the client we need to send him the close msg.
         TcpClient opp = game.GetOpponent(client);
         if (game == null || opp == null)
         {
             return(new TaskResult(null, false));
         }
         // json solution.
         JObject sol = new JObject
         {
             { "Name", game.Name },
             { "Direction", move }
         };
         string Jsol = JsonConvert.SerializeObject(sol);
         // send the opp the players move.
         ch.SendMessage(opp, Jsol);
         // stay connected.
         return(new TaskResult("", true));
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(new TaskResult("error occured", false));
     }
 }
コード例 #7
0
        private void OnSendMessagePress(object sender, RoutedEventArgs e)
        {
            string sendTo  = this.receiver.Text;
            string message = this.chatBox.Text;
            string from    = this.sender;

            Task.Run(() =>
            {
                try
                {
                    IMessageHandler messageHandler = this.services.MessageHandler;
                    IMessage request = messageHandler.GetMessage(from, sendTo, message, MessageCommand.WhisperMessage);

                    IClientHandler clientHandler = this.services.ClientHandler;
                    clientHandler.SendMessage(request);
                }
                catch (Exception exception)
                {
                    this.services.ChatLog.LogMessage(string.Format("Unable to send whisper : {0}", exception.Message));
                }
            });

            this.Close();
        }