コード例 #1
0
        private void TopicMenu(string TopicName)
        {
            if (this._client.User == null)
            {
                HomeMenu();
                return;
            }

            List <string> choices = new List <string>();

            if (this._client.Topics[TopicName].Topic.Owner.Username == this._client.User.Username)
            {
                choices.Add("Delete the Topic");
            }
            else
            {
                choices.Add("Leave the Topic");
            }

            choices.Add("Post a message in the Topic");

            choices.Add("<- Go back to the Select Topic Menu");

            ChoiceSelection.Choice choice = new ChoiceSelection.Choice("What do you want to do in the topic :", choices);

            int menu = ChoiceSelection.GetChoice(choice, (this._client.User == null) ? null : this._client.User.Username, TopicName);



            switch (menu)
            {
            case 0:

                if (this._client.Topics[TopicName].Topic.Owner.Username == this._client.User.Username)
                {
                    Console.Clear();
                    ConsoleManager.PrintTrack();

                    string password = ConsoleManager.Read("Please enter the password of the Topic : ");

                    //We tried to delete the Topic
                    this._client.Topics[TopicName].DeleteTopic(password, (response) =>
                    {
                        //On écoute la réponse

                        switch (response)
                        {
                        case Success s:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Green, s.ToString());

                            break;


                        case CommunicationException error:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, error.Message);

                            break;


                        default:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, "Error while deleting Topic : " + response);

                            break;
                        }

                        HomeMenu();
                    });
                }
                else
                {
                    //We tried to leave the Topic
                    this._client.Topics[TopicName].LeaveTopic((response) =>
                    {
                        //On écoute la réponse

                        switch (response)
                        {
                        case Success s:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Green, s.ToString());

                            break;


                        case CommunicationException error:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, error.Message);

                            break;


                        default:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, "Error while leaving Topic : " + response);

                            break;
                        }

                        HomeMenu();
                    });
                }

                break;


            case 1:

                //On envoie un message au ServerTopicListener

                string message = ConsoleManager.Read("\nPlease, you can now enter messages to be sent to the Topic `" + TopicName + "` :");

                ConsoleManager.TrackWriteLine(ConsoleColor.White, "Sending message to the server...\n");
                _client.Topics[TopicName].SendingMessage(message, (response) =>
                {
                    ConsoleManager.TrackWriteLine(ConsoleColor.Cyan, "MESSAGE RESPONSE :");
                    switch (response)
                    {
                    case Success s:
                        ConsoleManager.TrackWriteLine(ConsoleColor.Gray, "I received my own message from the server");

                        break;


                    case CommunicationException ce:
                        ConsoleManager.TrackWriteLine(ConsoleColor.Red, ce.Message);

                        break;
                    }

                    TopicMenu(TopicName);
                });


                break;



            case 2:

                SelectTopicMenu();

                break;
            }
        }
コード例 #2
0
        public void HomeMenu()
        {
            List <string> choices = new List <string>();

            if (_client.User == null)
            {
                choices.Add("Log In");
                choices.Add("Sign in");
            }
            else
            {
                choices.Add("Disconnect");
                choices.Add("Join a new Topic");
                choices.Add("Create a new Topic");

                choices.Add("Select a Topic" + " ->");

                choices.Add("Select a User" + " ->");
            }

            choices.Add("<- Exit");

            ChoiceSelection.Choice choice = new ChoiceSelection.Choice("Welcome to the Home Menu, what do you want to do ?", choices);

            int menu = ChoiceSelection.GetChoice(choice, (this._client.User == null) ? null : this._client.User.Username, null);

            if (_client.User == null)
            {
                switch (menu)
                {
                case 0:

                    Console.Clear();
                    ConsoleManager.PrintTrack();

                    string username = ConsoleManager.Read("Please enter your username : "******"Username = "******"\nPlease enter your password : "******"I'm connected as " + username);
                            }

                            break;

                        case CommunicationException ce:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, ce.Message);

                            break;
                        }

                        HomeMenu();
                    }).OnResponse;


                    break;


                case 1:

                    Console.Clear();
                    ConsoleManager.PrintTrack();

                    username = ConsoleManager.Read("Please enter a username : "******"Username = "******"\nPlease enter a password : "******"Username = "******"\nPassword = "******"\nPlease enter a email : ");


                    //On cherche à créer un User
                    request = new SignIn(username, password, email);
                    Net.SendClientCommunication(this._comm.GetStream(), request);

                    ResponseEvent.MyResponseEvent += new ResponseEvent(request, (response) =>
                    {
                        switch (response)
                        {
                        case Success success:
                            //Si la réponse est Success, alors la création s'est bien effectué
                            ConsoleManager.TrackWriteLine(ConsoleColor.Green, success.ToString());
                            break;

                        case CommunicationException error:
                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, error.Message);
                            break;

                        default:
                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, "Error while creating new User : "******"Please enter the name of the Topic : ");


                    Console.Clear();
                    ConsoleManager.PrintTrack();

                    string password = ConsoleManager.Read("Topic name = " + topic_name + "\nPlease enter the password of the Topic : ");


                    //We tried to join the first Topic found
                    ClientCommunication request = new Join(_client.User, topic_name, password);
                    Net.SendClientCommunication(_comm.GetStream(), request);
                    ResponseEvent.MyResponseEvent += new ResponseEvent(request, (response) =>
                    {
                        //On écoute la réponse

                        switch (response)
                        {
                        case Topic t:

                            //Si la réponse est bien un Topic, alors le join s'est bien effectué
                            ConsoleManager.TrackWriteLine(ConsoleColor.Green, "Connection to the Topic `" + t.Topic_name + "` ! ");

                            break;


                        case CommunicationException error:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, error.Message);

                            break;


                        default:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, "Error while joining Topic : " + response);

                            break;
                        }

                        HomeMenu();
                    }).OnResponse;


                    break;


                case 2:

                    Console.Clear();
                    ConsoleManager.PrintTrack();

                    topic_name = ConsoleManager.Read("Please enter the name of the new Topic : ");


                    Console.Clear();
                    ConsoleManager.PrintTrack();

                    password = ConsoleManager.Read("Topic name = " + topic_name + "\nPlease enter the password of the new Topic : ");


                    //We tried to create the Topic
                    request = new Creation(topic_name, _client.User, password);
                    Net.SendClientCommunication(_comm.GetStream(), request);
                    ResponseEvent.MyResponseEvent += new ResponseEvent(request, (response) =>
                    {
                        //On écoute la réponse

                        switch (response)
                        {
                        case Topic topic:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Green, "New Topic `" + topic.Topic_name + "` created !");

                            break;


                        case CommunicationException error:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, error.Message);

                            break;


                        default:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, "Error while creating Topic : " + response);

                            break;
                        }

                        HomeMenu();
                    }).OnResponse;

                    break;


                case 3:

                    SelectTopicMenu();

                    break;


                case 4:

                    request = new AskConnectUser(this._client.User);
                    Net.SendClientCommunication(this._comm.GetStream(), request);

                    ResponseEvent.MyResponseEvent += new ResponseEvent(request, (response) =>
                    {
                        switch (response)
                        {
                        case List <string> users_names:

                            SelectClientMenu(users_names);

                            break;


                        case NoUserConnectedException e:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, e.Message);

                            HomeMenu();

                            break;


                        default:

                            ConsoleManager.TrackWriteLine(ConsoleColor.Red, "Error while acquiring all connected Users " + response);

                            break;
                        }
                    }).OnResponse;

                    break;


                case 5:

                    this._client.KillClient();

                    break;
                }
            }
        }
コード例 #3
0
        public static int GetChoice(Choice choice, string username, string topic)
        {
            // We initialize our variables
            ConsoleKeyInfo input;
            int            value = 0;

            MAX = choice.choices.Count - 1;



            // While the user doesn't press Enter, the loop continues
            do
            {
                //We lock the Console until we display the menu
                Console.Clear();

                ConsoleManager.PrintTrack();
                Console.WriteLine();

                //We display the username of the client
                ConsoleManager.Write(ConsoleColor.Gray, "[" + Thread.CurrentThread.Name + "] Connected as : ");

                if (username == null)
                {
                    ConsoleManager.WriteLine(ConsoleColor.Red, "NOT CONNECTED");
                }
                else
                {
                    ConsoleManager.WriteLine(ConsoleColor.Green, username);
                }

                //We display the topic of the client
                ConsoleManager.Write(ConsoleColor.Gray, "[" + Thread.CurrentThread.Name + "] Focus on the topic : ");

                if (topic == null)
                {
                    ConsoleManager.WriteLine(ConsoleColor.Red, "NOT CONNECTED");
                }
                else
                {
                    ConsoleManager.WriteLine(ConsoleColor.Green, topic);
                }


                // We display a given message
                ConsoleManager.WriteLine(ConsoleColor.Gray, "[" + Thread.CurrentThread.Name + "] " + choice.message + "\n\n");


                // We display all our choices (and we highlight the current choice)
                int          index = 0;
                ConsoleColor color = ConsoleColor.White;

                foreach (string s in choice.choices)
                {
                    if (index == value)
                    {
                        Console.Write("     --> ");
                    }
                    else
                    {
                        Console.Write("         ");
                    }

                    // We change the color to Blue if we reach the final statement (usually one saying "Go back" or "Log out")
                    if (index == MAX)
                    {
                        color = ConsoleColor.Blue;
                    }

                    ConsoleManager.WriteLine(color, choice.choices[index++] + "\n");
                }


                // We read the input
                input = Console.ReadKey();


                // If it is a key (Up or Down), we modify our choice accordingly
                if (input.Key == ConsoleKey.UpArrow)
                {
                    value--;
                }
                if (input.Key == ConsoleKey.DownArrow)
                {
                    value++;
                }
                if (input.Key == ConsoleKey.LeftArrow)
                {
                    value = 0;
                }
                if (input.Key == ConsoleKey.RightArrow)
                {
                    value = MAX;
                }


                // If the value goes too low / too high, it goes to the other extreme
                if (value < MIN)
                {
                    value = MAX;
                }
                if (value > MAX)
                {
                    value = MIN;
                }
            }while (input.Key != ConsoleKey.Enter);


            // We return the value
            return(value);
        }