コード例 #1
0
ファイル: Chats.cs プロジェクト: mizzouse/FreeInfantry
        static public void Handle_CS_JoinChat(CS_JoinChat <Zone> pkt, Zone zone)
        {
            DBServer server = zone._server;

            Zone.Player   player   = server.getPlayer(pkt.from);
            char[]        splitArr = { ',' };
            List <string> chats    = new List <string>();

            //Hey, how'd you get here?!
            if (player == null)
            {
                Log.write(TLog.Error, "Handle_CS_JoinChat(): Unable to find player.");
                return;
            }

            //He wants to see the player list of each chat..
            if (String.IsNullOrWhiteSpace(pkt.chat))
            {
                if (player.chats.Count > 0)
                {
                    foreach (var chat in player.chats)
                    {
                        if (String.IsNullOrEmpty(chat))
                        {
                            continue;
                        }

                        Chat _chat = server.getChat(chat);
                        if (_chat != null && _chat.hasPlayer(player))
                        {
                            server.sendMessage(zone, pkt.from, String.Format("{0}: {1}", _chat._name, _chat.List()));
                        }
                    }
                }
                return;
            }

            //Split and trim our chats
            chats = pkt.chat.Split(splitArr, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList();

            foreach (string chat in chats)
            {
                if (String.IsNullOrEmpty(chat))
                {
                    continue;
                }

                string name = chat.ToLower();

                //Remove him from everything..
                if (name == "off")
                {
                    foreach (var c in server._chats.Values.ToList())
                    {
                        if (c == null)
                        {
                            continue;
                        }

                        if (c.hasPlayer(player))
                        {
                            c.lostPlayer(player);
                        }
                    }
                    server.sendMessage(zone, pkt.from, "No Chat Channels Defined");
                    return;
                }

                Chat _chat = server.getChat(chat);

                //New chat
                if (_chat == null)
                {
                    Log.write(TLog.Normal, "Opened chat: '{0}'", chat);
                    _chat = new Chat(server, chat);
                }

                //Add him
                if (!_chat.hasPlayer(player))
                {
                    _chat.newPlayer(player);
                }


                //Send him the updated list..
                server.sendMessage(zone, pkt.from, String.Format("{0}: {1}", chat, _chat.List()));
            }

            //Remove him from any chats that didn't come over in the packet.
            //Rewrite me! Just make sure I'm case-insensitive
            foreach (Chat c in server._chats.Values.ToList())
            {
                if (c == null)
                {
                    continue;
                }

                if (!chats.Contains(c._name, StringComparer.OrdinalIgnoreCase))
                {
                    if (c.hasPlayer(player))
                    {
                        c.lostPlayer(player);
                    }
                }
            }
        }