Пример #1
0
        public string ParseMessage(string Message)
        {
            int    UserID;
            String Type;
            String Contents;

            try
            {
                Type = Message.Substring(0, 4);
                Console.WriteLine("Message type " + Type);
                if (Message.Substring(4, 4) == "0000")
                {
                    UserID = 0;
                }
                else
                {
                    UserID = int.Parse((Message.Substring(4, 4)).TrimStart('0'));
                }
                Contents = Message.Substring(8);
                Console.WriteLine("User Id: " + UserID.ToString());
                Console.WriteLine("Message: " + Contents);
            }
            catch
            {
                return("Message Not Accepted");
            }

            if (Type != "PING" && UserID != 0 && !UserDict.ContainsKey(UserID))
            {
                return("User ID not accepted, message not processed");
            }

            switch (Type)
            {
            case "NWID":
                Console.WriteLine("New ID");
                if (UserID != 0 && UserDict.ContainsKey(UserID))
                {
                    return(UserID.ToString());
                }
                else
                {
                    for (int i = 1; i < 1000; i++)
                    {
                        if (!UserDict.ContainsKey(i))
                        {
                            UserDict.Add(i, new UserData(i, Contents, new List <String>()));
                            return(i.ToString());
                        }
                    }
                }
                break;

            case "CHLS":
                Console.WriteLine("List Channels");
                string output = "";
                foreach (String Name in ChannelDict.Keys)
                {
                    output = output + Name + ",";
                }
                output.Trim(',');
                return(output);

            case "CRCH":
                Console.WriteLine("Create Channel");
                if (ChannelDict.ContainsKey(Contents.Trim()))
                {
                    return("Already Exists");
                }
                else
                {
                    ChannelDict.Add(Contents.Trim(),
                                    new ChannelData(Contents.Trim(),
                                                    "Channel " + Contents.Trim() + " Started by " + UserDict[UserID].Alias + "\n",
                                                    new List <int>()));
                    ChannelDict[Contents.Trim()].Members.Add(UserID);
                    return("Channel Created");
                }

            case "JNCH":
                Console.WriteLine("Join Channel");
                if (ChannelDict.ContainsKey(Contents.Trim()))
                {
                    if (!ChannelDict[Contents.Trim()].Members.Contains(UserID))
                    {
                        ChannelDict[Contents.Trim()].Members.Add(UserID);
                    }
                    if (!UserDict[UserID].Channels.Contains(Contents.Trim()))
                    {
                        UserDict[UserID].Channels.Add(Contents.Trim());
                    }

                    return(ChannelDict[Contents.Trim()].Messages);
                }
                else
                {
                    return("Channel Does Not Exist");
                }

            case "LVCH":
                Console.WriteLine("Leave Channel");
                if (!ChannelDict.ContainsKey(Contents.Trim()))
                {
                    return("Channel Does Not Exist");
                }
                if (UserDict[UserID].Channels.Contains(Contents.Trim()))
                {
                    UserDict[UserID].Channels.Remove(Contents.Trim());
                }
                if (ChannelDict[Contents.Trim()].Members.Contains(UserID))
                {
                    ChannelDict[Contents.Trim()].Members.Remove(UserID);
                }
                return("User is no longer in channel " + Contents.Trim());

            case "CHCH":
                Console.WriteLine("Channel History");
                if (ChannelDict.ContainsKey(Contents.Trim()))
                {
                    if (ChannelDict[Contents.Trim()].Members.Contains(UserID))
                    {
                        return(ChannelDict[Contents.Trim()].Messages);
                    }
                    else
                    {
                        return("User is not a member of this channel");
                    }
                }
                else
                {
                    return("Channel Does not exist");
                }

            case "MEMB":
                Console.WriteLine("Member List");
                if (ChannelDict.ContainsKey(Contents.Trim()))
                {
                    if (ChannelDict[Contents.Trim()].Members.Contains(UserID))
                    {
                        string members = "";
                        foreach (int UserInt in ChannelDict[Contents.Trim()].Members)
                        {
                            members = members + UserDict[UserInt].Alias + ", ";
                        }
                        members.Trim();
                        members.Trim(',');
                        return(members);
                    }
                    else
                    {
                        return("User is not a member of this channel");
                    }
                }
                else
                {
                    return("Channel Does not exist");
                }

            case "PING":
                Console.WriteLine("Ping");
                return("Hello Client!");

            case "MECH":
                Console.WriteLine("Message Channel");
                String ChatMessage;
                String ChannelName;
                ChannelName = Contents.Substring(0, Contents.IndexOf(','));
                ChatMessage = Contents.Substring(Contents.IndexOf(',') + 1);
                if (!ChannelDict.ContainsKey(ChannelName))
                {
                    return("Channel dosen't exist");
                }
                ChannelDict[ChannelName].Messages = ChannelDict[ChannelName].Messages + UserDict[UserID].Alias + ": " + ChatMessage + "\n";
                return(ChannelDict[ChannelName.Trim()].Messages);

            case "GBYE":
                Console.WriteLine("Disconnect");
                foreach (ChannelData Channel in ChannelDict.Values)
                {
                    if (Channel.Members.Contains(UserID))
                    {
                        Channel.Members.Remove(UserID);
                    }
                }
                UserDict.Remove(UserID);
                return("Bye!");

            default:
                Console.WriteLine("No Command");
                return("Message Type not Recognised");
            }
            return("");
        }