예제 #1
0
        public void Text(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            bool        flag = false;
            var         msg  = ChatFilter.RemoveBadWords(message, out flag);
            ChatMessage msgChat;
            var         account = Lddb.Instance.GetAccountInfo(AccountSession.AccountID);
            int         result  = GameManager.CheckEnableChat(account.AccountID, account.DisplayName, AccountSession.UserType, msg, out msgChat);

            NLogManager.LogMessage($"{account.AccountID}|{ account.DisplayName}|{AccountSession.UserType} => {JsonConvert.SerializeObject(msgChat)}");
            if (result == -1)
            {
                msgChat = new ChatMessage
                {
                    T = 1,
                    //M = "Tổng đặt trong 5 ngày ít nhất 50.000 vàng mới có thể chat", Chanh
                    M = "Tổng tiền đặt trong 5 ngày của bạn phải tối thiểu 20.000 vàng chat",
                    U = "System Admin"
                };
                Clients.Caller.Msg(msgChat);
            }
            else if (result == -2)
            {
                msgChat = new ChatMessage
                {
                    T = 1,
                    M = "Bạn đã bị chặn chat, vui lòng liên hệ admin",
                    U = "System Admin"
                };
                Clients.Caller.Msg(msgChat);
            }
            else if (result == 0)
            {
                Lddb.Instance._userNameChatReal[account.AccountID] = account.DisplayName;
                Clients.All.Msg(msgChat);
            }
        }
예제 #2
0
        /// <summary>
        /// Gửi message chat
        /// </summary>
        /// <param name="hubCallerContext"></param>
        /// <param name="message"></param>
        /// <param name="channelId"></param>
        /// <returns></returns>
        public bool SendMessage(HubCallerContext hubCallerContext, string message, string channelId)
        {
            NLogManager.LogMessage(">>Start SendMessage");
            try
            {
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["CloseChatServer"].ToString()))
                {
                    return(false);
                }

                if (MAX_MESSAGE_LENGTH < message.Length)
                {
                    broadcastMessage(hubCallerContext.ConnectionId, string.Format("Nội dung chát không quá {0} kí tự!", MAX_MESSAGE_LENGTH));
                    return(false);
                }

                //NLogManager.LogMessage(string.Format(">>Statr chat:{0}", channelId));
                LoadListAdmin();
                LoadTimeSendMessage();
                long accountId = GetAccountId(hubCallerContext);
                if (accountId < 1)
                {
                    NLogManager.LogMessage(string.Format("Sending message: not authenticated accountId: {0} - channel: {1} - content={2}", accountId, channelId, message));
                    return(false);
                }
                ChatChannel chatChannel = GetChannel(channelId);
                if (chatChannel == null)
                {
                    NLogManager.LogMessage(string.Format("Sending message: accountId: {0} - not has channel: {1} - content={2}", accountId, channelId, message));
                    return(false);
                }
                if (message.IndexOf('/') == 0)
                {
                    string notice;
                    bool   ret = RunCommand(message, out notice);

                    if (!string.IsNullOrEmpty(notice))
                    {
                        Instance.ClientSystemMessage(channelId, notice, 0);
                    }

                    if (ret)
                    {
                        NLogManager.LogMessage(string.Format("Account: {0} - run command: {1}", accountId, message));
                        return(true);
                    }
                }
                ChatUser chatUser = GetUser(accountId);
                if (chatUser == null)
                {
                    NLogManager.LogMessage(string.Format("Sending message: not chat user: {0} in channel={1} - content={2}", accountId, channelId, message));
                    return(false);
                }
                if (CMSAllGame != "1")
                {
                    if (!ADMINS.Contains(chatUser.UserName))
                    {
                        try
                        {
                            object level = CacheHandler.Get(chatUser.UserName + "_" + channelId);
                            if (level == null)
                            {
                                SetCacheLevel(chatUser.UserName, (int)chatUser.AccountID, channelId);
                            }
                            else if (int.Parse(level.ToString()) == 0)
                            {
                                NLogManager.LogMessage(string.Format("Tổng đặt trong 7 ngày của bạn chưa đủ 2.000.000 Sao để tham gia chat!: {0} ", chatUser.UserName));
                                broadcastMessage(hubCallerContext.ConnectionId, string.Format("Tổng đặt trong 7 ngày của bạn chưa đủ 2.000.000 Sao để tham gia chat!"));
                                return(false);
                            }
                        }
                        catch (Exception ex)
                        {
                            NLogManager.LogMessage(string.Format("{0} Null Cache level", chatUser.UserName));
                            SetCacheLevel(chatUser.UserName, (int)chatUser.AccountID, channelId);
                        }
                    }
                }

                string filteredMessage     = message;
                string tempFilteredMessage = message;
                bool   flag = false;

                //WaitCallback callBack = new WaitCallback(InsertChat);
                //var chatchat = new Insert_Chat();
                //chatchat.AccountID = int.Parse(chatUser.AccountID.ToString());
                //chatchat.UserName = string.Format("{0}({1})", chatUser.UserName, chatUser.NickName);
                //chatchat.channelId = channelId;
                //chatchat.filteredMessage = filteredMessage;
                //ThreadPool.QueueUserWorkItem(callBack, chatchat);

                //khong phai admin thi loc bad link
                if (!ADMINS.Contains(chatUser.UserName))
                {
                    ChatFilter.RemoveBadLinks(tempFilteredMessage, out flag);
                }
                //Check thời gian chát theo quy định

                if (!ADMINS.Contains(chatUser.UserName))
                {
                    try
                    {
                        object value = CacheHandler.Get(chatUser.UserName);
                        if (value == null)
                        {
                            CacheHandler.Add(chatUser.UserName, DateTime.Now.ToString());
                        }
                        else
                        {
                            if (DateTime.Now != DateTime.Parse(value.ToString()))
                            {
                                var lastTime = (DateTime.Now - DateTime.Parse(value.ToString())).TotalSeconds;
                                var a        = MIN_MESSAGE_TIME_SECOND;
                                if (lastTime < int.Parse(MIN_MESSAGE_TIME_SECOND))
                                {
                                    broadcastMessage(hubCallerContext.ConnectionId, string.Format("Chưa hết thời gian chờ {0}s, t/g chờ còn {1}s", MIN_MESSAGE_TIME_SECOND, System.Math.Round((int.Parse(MIN_MESSAGE_TIME_SECOND) - lastTime), 2)));
                                    return(false);
                                }
                            }
                            CacheHandler.Add(chatUser.UserName, DateTime.Now.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        NLogManager.LogMessage(string.Format("{0} Null Cache TimeChat", chatUser.UserName));
                        CacheHandler.Add(chatUser.UserName, DateTime.Now.ToString());
                    }
                }



                if (CheckBanUsers(chatUser.UserName))
                {
                    NLogManager.LogMessage(string.Format(">> {0} - Tài khoản đang bị Block! ", chatUser.UserName));
                    broadcastMessage(hubCallerContext.ConnectionId, ReturnCheckBanUsers(chatUser.UserName));
                    return(false);
                }

                //nếu nội dung chat có bad links thì block gói tin và ghi log
                if (flag)
                {
                    chatUser.CountBadLinks += 1;
                    filteredMessage         = "***";
                    if (chatUser.CountBadLinks > 5)
                    {
                        ChatFilter.BanUser(chatUser.UserName);
                    }

                    NLogManager.LogMessage(string.Format("User sent bad link: accountId={0}, username={1} - channelId={2} - content={3} - \n\rAgent: {4}", chatUser.AccountID, chatUser.UserName, channelId, message, chatUser.UserAgent));
                }
                else
                {
                    //khong phai admin thi loc bad word
                    if (!ADMINS.Contains(chatUser.UserName))
                    {
                        filteredMessage = ChatFilter.RemoveBadWords(message, out flag);
                    }

                    NLogManager.LogMessage(flag
                        ? string.Format(
                                               "User sent bad word: accountId={0}, username={1} - channelId={2} - content={3}",
                                               chatUser.AccountID, chatUser.UserName, channelId, message)
                        : string.Format("User sent message: accountId={0}, username={1} - channelId={2} - content={3}",
                                        chatUser.AccountID, chatUser.UserName, channelId, message));
                }

                //khong phai admin thi loc bad word
                if (!ADMINS.Contains(chatUser.UserName))
                {
                    filteredMessage = RemoveBadWords(message, out flag);
                }

                //Thay thế từ khóa
                if (!ADMINS.Contains(chatUser.UserName))
                {
                    filteredMessage = ReplaceKeyword(filteredMessage);
                }


                //admin thì text chat màu đỏ
                ChatMessage chatMessage = new ChatMessage(channelId, chatUser.AccountID, !ADMINS.Contains(chatUser.UserName) ? chatUser.NickName : string.Format("<span style='color:red; font-weight:bold'>ADMIN ({0})</span>", chatUser.NickName), !ADMINS.Contains(chatUser.UserName) ? filteredMessage : string.Format("{0}", filteredMessage));
                bool        canSend;
                if (!ADMINS.Contains(chatUser.UserName))
                {
                    canSend = chatChannel.AddMessage(chatMessage);
                }
                else
                {
                    canSend = chatChannel.AddMessageAdmin(chatMessage);
                }

                if (canSend)
                {
                    canSend = chatUser.AddMessage(channelId, chatMessage);
                }

                if (canSend)
                {
                    ClientReceiveMessage(chatMessage);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
            }

            NLogManager.LogMessage(string.Format(">>Gửi tin nhắn không thành công! "));
            return(false);
        }