查看: 701|回复: 0

西游H5聊天修复

[复制链接]

18

主题

108

帖子

3465

积分

超级富翁

Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20

积分
3465
发表于 2019-5-24 00:00:00 | 显示全部楼层 |阅读模式
西游H5聊天修复:s1\server\lua\logic\modules\chat
ChatCenter.lua


  1. local server = require "server"
  2. local lua_app = require "lua_app"
  3. local lua_shield = require "lua_shield"
  4. local lua_util = require "lua_util"
  5. local ChatConfig = require "resource.ChatConfig"
  6. local RaidConfig = require "resource.RaidConfig"
  7. local utf8 = require("lua_utf8")
  8. local ChatCenter = {}

  9. local _ChatType = ChatConfig.ChatType
  10. local _LinkId = ChatConfig.LinkId
  11. local _MaxPrivateRecord = 20

  12. function ChatCenter:Init()
  13.         self.record = server.baseRecord:Ins(server.baseRecord.RecordType.WorldChat, server.configCenter.ChatConstConfig.saveChatListSize, true)
  14.         self.systemrecord = server.baseRecord:Ins(server.baseRecord.RecordType.System, server.configCenter.ChatConstConfig.saveSystemChatcount, true)
  15.         self.playerlist = {}
  16.         self.privateChatRecord = {}
  17.         self.offlineChat = {}
  18.         self.playershareinfo = {}
  19.         self.chatrecordOfbanned = {}
  20. end

  21. function ChatCenter:onDayTimer(player)
  22.         self.playerlist = {}
  23. end

  24. function ChatCenter:onInitClient(player)
  25.         local worlddatas = self.record:SendRecord(player)
  26.         local systemdatas = self.systemrecord:SendRecord(player)
  27.         server.sendReq(player, "sc_chat_init_msg", {
  28.                         chatDatas = self:FilteChat(worlddatas, systemdatas)
  29.                 })
  30.         self:PrivateOfflineChat(player)
  31. end

  32. --设置过滤名单
  33. function ChatCenter:SetFilter(flag, playerid)
  34.         if flag ~= 0 then
  35.                 server.broadcastReq("sc_chat_filte_list", {
  36.                                 filter = playerid,
  37.                         })
  38.         end
  39. end

  40. function ChatCenter:FilteChat(...)
  41.         local arg = {...}
  42.         local newchatdata = {}
  43.         for __, chatdatas in ipairs(arg) do
  44.                 for __, chat in ipairs(chatdatas) do
  45.                         if self:CheckFilteChat(chat.id) then
  46.                                 newchatdata[#newchatdata + 1] = chat
  47.                         end
  48.                 end
  49.         end
  50.         return newchatdata
  51. end

  52. function ChatCenter:CheckFilteChat(dbid)
  53.         if not dbid then return true end
  54.         local player = server.playerCenter:DoGetPlayerByDBID(dbid)
  55.         local status = player.cache.silent
  56.         return status ~= -1 and status <= lua_app.now()
  57. end

  58. function ChatCenter:GetRecord(chattype)
  59.         if chattype == server.baseRecord.RecordType.WorldChat or
  60.                 chattype == server.baseRecord.RecordType.WorldShare then
  61.                 return self.record
  62.         else
  63.                 return self.systemrecord
  64.         end
  65. end

  66. local function _CheckChatCount(sender, count)
  67.         local ChatLevelConfig = server.configCenter.ChatLevelConfig
  68.         local powerValue = sender.cache.totalpower
  69.         local index = #ChatLevelConfig
  70.         for i = #ChatLevelConfig, 1, -1 do
  71.                 local cfg = ChatLevelConfig[i]
  72.                 if powerValue >= cfg.power then
  73.                         index = i
  74.                         if count < cfg.chatSize then
  75.                                 return true
  76.                         elseif not ChatLevelConfig[i + 1] then
  77.                                 return false, cfg.errortips
  78.                         else
  79.                                 return false, string.format(cfg.errortips, ChatLevelConfig[i + 1].power)
  80.                         end
  81.                 end
  82.         end
  83.         return false, ChatLevelConfig[#ChatLevelConfig].errortips
  84. end

  85. function ChatCenter:CheckAddCount(sender)
  86.         local playerinfo = self.playerlist[sender.dbid]
  87.         if not playerinfo then
  88.                 playerinfo = {
  89.                         chatdaycount = 0,
  90.                 }
  91.                 self.playerlist[sender.dbid] = playerinfo
  92.         end
  93.         local nowtime = lua_app.now()
  94.         local ChatConstConfig = server.configCenter.ChatConstConfig
  95.         if not server.funcOpen:Check(sender, ChatConstConfig.openLevel) then
  96.                 server.sendErr(sender, "等级不足,不能发言")
  97.                 return false
  98.         end
  99.         local waittime = ChatConfig:GetWaitInterval("chat."..sender.dbid, ChatConstConfig.chatCd)
  100.         if waittime > 0 then
  101.                 server.sendErr(sender, "聊天cd中")
  102.                 return false
  103.         end
  104.         local checkcountret, errmsg = _CheckChatCount(sender, playerinfo.chatdaycount)
  105.         if not checkcountret then
  106.                 server.sendErr(sender, errmsg)
  107.                 return false
  108.         end
  109.         playerinfo.chatdaycount = playerinfo.chatdaycount + 1
  110.         return true
  111. end

  112. local _Chatting = {}
  113. _Chatting[_ChatType.WorldChat] = function(self, sender, str, recvId, pointId)        -- 世界聊天
  114.         if not self:CheckAddCount(sender) then
  115.                 return false
  116.         end
  117.         local data = {
  118.                         type = _ChatType.WorldChat,
  119.                         id = sender.dbid,
  120.                         name = sender.cache.name,
  121.                         vip = sender.cache.vip,
  122.                         str = str,
  123.                         job = sender.cache.job,
  124.                         sex = sender.cache.sex,
  125.                         headframe = sender.head:GetFrame(),
  126.                         time = lua_app.now(),
  127.                 }
  128.         --禁言玩家消息只有自己能看到
  129.         if not self:CheckFilteChat(sender.dbid) then
  130.                 server.sendReq(sender, "sc_chat_new_msg", { chatData = data })
  131.                 return true
  132.         end

  133.         self.record:AddRecord(data)
  134.         server.broadcastReq("sc_chat_new_msg", { chatData = data })
  135.         sender.task:onEventAdd(server.taskConfig.ConditionType.ChatWorld)
  136.         return true
  137. end

  138. _Chatting[_ChatType.PrivateChat] = function(self, sender, str, recvId)        -- 私聊
  139.         local ret = server.friendCenter:ChatCheck(sender, recvId)
  140.         if ret == 1 then
  141.                 server.sendErr(sender, "你屏蔽该玩家了")
  142.                 return
  143.         elseif ret == 2 then
  144.                 server.sendErr(sender, "你被该玩家屏蔽了")
  145.                 return
  146.         end
  147.         local data = {
  148.                         type = _ChatType.PrivateChat,
  149.                         id = sender.dbid,
  150.                         name = sender.cache.name,
  151.                         vip = sender.cache.vip,
  152.                         str = str,
  153.                         job = sender.cache.job,
  154.                         sex = sender.cache.sex,
  155.                         frame = sender.head:GetFrame(),
  156.                         time = lua_app.now(),
  157.                 }
  158.         --禁言玩家消息只有自己能看到
  159.         if not self:CheckFilteChat(sender.dbid) then
  160.                 server.sendReq(sender, "sc_chat_private_new_msg", {
  161.                         session = self:GetPrivateChatId(sender.dbid, recvId),
  162.                         chatData = data,
  163.                 })
  164.                 return true
  165.         end

  166.         local receiver = server.playerCenter:DoGetPlayerByDBID(recvId)
  167.         self:AddPrivateChatRecord(sender, receiver, data)
  168.         return true
  169. end

  170. function _StrRepeatCount(src, cond)
  171.         return select(2, string.gsub(src, string.format("%s[^|]*", cond), ""))
  172. end
  173. --禁言检测
  174. function ChatCenter:CheckBanned(dbid, chatstr)
  175.         local ChatshutupConfig = server.configCenter.ChatshutupConfig
  176.         local record = self.chatrecordOfbanned[dbid]
  177.         if not record then
  178.                 record = { nextindex = 1 }
  179.                 self.chatrecordOfbanned[dbid] = record
  180.         end
  181.         local text = table.concat(record, "|")
  182.         for __, str in ipairs(ChatshutupConfig.jlibrary) do
  183.                 if  _StrRepeatCount(text, str) > ChatshutupConfig.zicount then
  184.                         return true
  185.                 end
  186.         end
  187.         for __, str in ipairs(ChatshutupConfig.mlibrary) do
  188.                 if  _StrRepeatCount(text, str) > ChatshutupConfig.shutupcount then
  189.                         return true
  190.                 end
  191.         end
  192.         local count = 0
  193.         for w in string.gmatch(text, "[^|]+") do
  194.                 if w == chatstr then
  195.                         count = count + 1
  196.                 end
  197.                 if count > ChatshutupConfig.repeatcount then
  198.                         return true
  199.                 end
  200.         end
  201.         
  202.         local len = utf8.len(chatstr)
  203.         if len >= ChatshutupConfig.count then
  204.                 record[record.nextindex] = chatstr
  205.                 record.nextindex = record.nextindex % ChatshutupConfig.historycount + 1
  206.         end
  207.         return false
  208. end

  209. function ChatCenter:Chat(sender, type, str, recvId)
  210.         local status = sender.cache.silent
  211.         if status ~= -1 and self:CheckBanned(sender.dbid, str) then
  212.                 sender.cache.silent = -1
  213.                 self:SetFilter(-1, sender.dbid)
  214.                 lua_app.log_info("CheckBanned:Prohibit players from chatting.", sender.dbid, sender.cache.name)
  215.         end

  216.         if status > lua_app.now() then
  217.                 server.sendErr(sender, "您已经被禁言,剩余时间" .. (status - lua_app.now()) .. "秒")
  218.                 return false
  219.         end

  220.         if string.len(str) >= server.configCenter.ChatConstConfig.chatLen then
  221.                 server.sendErr(sender, "字节太长,超过160个字节")
  222.                 return false
  223.         end

  224.         str = lua_shield:string(str)
  225.         return _Chatting[type](self, sender, str, recvId)
  226. end

  227. local CheckLinkCondition = setmetatable({}, {__index = function()
  228.         return function() return true end
  229. end})

  230. CheckLinkCondition[_LinkId.AssistPkBoss] = function(sender)
  231.         local ChaptersCommonConfig = server.configCenter.ChaptersCommonConfig
  232.         local appealtime = sender.cache.chapter.appealtime
  233.         if appealtime >= ChaptersCommonConfig.appealtime then
  234.                 server.sendErr(sender, "当日求助次数达到上限")
  235.                 return false
  236.         end
  237.         return true
  238. end

  239. function ChatCenter:CheckChatLink(id, sender)
  240.         if not CheckLinkCondition[id](sender) then
  241.                 return false
  242.         end
  243.         local waittime = ChatConfig:GetWaitInterval("CheckChatLink."..sender.dbid)
  244.         if waittime > 0 then
  245.                 server.sendErr(sender, string.format("%d秒后才能发送", waittime))
  246.                 return false
  247.         end
  248.         local tips = ChatConfig.LinkTips[id] or "发送成功"
  249.         server.sendErr(sender, tips)
  250.         return true
  251. end

  252. --param1 分享id, param2为玩家的id列表
  253. function ChatCenter:ChatLink(shareid, sender, checkflag, ...)
  254.         if checkflag then
  255.                 if not self:CheckChatLink(shareid, sender) then
  256.                         return
  257.                 end
  258.         end
  259.         print("shareid", "--------------------------")
  260.         local chatData = ChatConfig:PackLinkData(shareid, sender, ...)
  261.         local record = self:GetRecord(chatData.type)
  262.         record:AddRecord(chatData)
  263.         server.broadcastReq("sc_chat_new_msg", {
  264.                 chatData = chatData
  265.                 })
  266.         return true
  267. end

  268. --战斗通知
  269. local _NoticeRaid = setmetatable({}, {__index = function() return function() end end})

  270. _NoticeRaid[RaidConfig.type.GuildFb] = function(dbid, level)
  271.         local GuildFubenConfig = server.configCenter.GuildFubenConfig[level]
  272.         local player = server.playerCenter:DoGetPlayerByDBID(dbid)
  273.         player.guild:ChatLink(11, 0, {ChatConfig.CollectType.Fb, level, RaidConfig.type.GuildFb}, player.cache.name, GuildFubenConfig.uititle)
  274. end

  275. _NoticeRaid[RaidConfig.type.CrossTeamFb] = function(dbid, level)
  276.         local player = server.playerCenter:DoGetPlayerByDBID(dbid)
  277.         server.chatCenter:ChatLink(14, player, nil, {ChatConfig.CollectType.Fb, level, RaidConfig.type.CrossTeamFb}, player.cache.name, level)
  278. end

  279. _NoticeRaid[RaidConfig.type.EightyOneHard] = function(dbid, level)
  280.         local player = server.playerCenter:DoGetPlayerByDBID(dbid)
  281.         local DisasterFbConfig = server.configCenter.DisasterFbConfig[level]
  282.         server.chatCenter:ChatLink(15, player, nil, {ChatConfig.CollectType.Fb, level, RaidConfig.type.EightyOneHard}, player.cache.name, DisasterFbConfig.name)
  283. end

  284. _NoticeRaid[RaidConfig.type.GuildBoss] = function(dbid, level, guildid)
  285.         local guild = server.guildCenter:GetGuild(guildid)
  286.         if not guild then
  287.                 return
  288.         end
  289.         local chatdata = server.chatConfig:PackLinkData(8)
  290.         guild.guildRecord:AddGuildChat(chatdata.str, nil, chatdata.share)
  291. end

  292. _NoticeRaid[RaidConfig.type.FieldBoss] = function(dbid, level)
  293.         server.chatCenter:ChatLink(13)
  294. end

  295. local _NoticeCheck = {}
  296. function ChatCenter:NoticeFb(raidtype, dbid, level, ...)
  297.         local recordkey = string.format("%s%s%s", raidtype, dbid, level)
  298.         local waittime = ChatConfig:GetWaitInterval(recordkey)
  299.         if waittime > 0 then return end
  300.         _NoticeRaid[raidtype](dbid, level, ...)
  301. end

  302. function ChatCenter:EmitTeamRecruit(dbid)
  303.          local waittime = ChatConfig:GetWaitInterval(dbid)
  304.          if waittime > 0 then
  305.                  server.sendErrByDBID(dbid, string.format("%d秒后才能招募", waittime))
  306.         return false
  307.     end
  308.     server.sendErrByDBID(dbid, "招募成功")
  309.     return true
  310. end

  311. function ChatCenter:AddPrivateChatRecord(sender, receiver, data)
  312.         local session = self:GetPrivateChatId(sender.dbid, receiver.dbid)
  313.         local records = self.privateChatRecord[session]
  314.         if not records then
  315.                 records = {}
  316.                 self.privateChatRecord[session] = records
  317.         end
  318.         if #records > _MaxPrivateRecord then
  319.                 table.remove(records, 1)
  320.         end
  321.         table.insert(records, data)
  322.         server.sendReq(sender, "sc_chat_private_new_msg", {
  323.                         session = session,
  324.                         chatData = data,
  325.                 })
  326.         if receiver.isLogin then
  327.                 server.sendReq(receiver, "sc_chat_private_new_msg", {
  328.                                 session = session,
  329.                                 chatData = data,
  330.                         })
  331.         else
  332.                 local offlineChat = self.offlineChat[receiver.dbid]
  333.                 if not offlineChat then
  334.                         offlineChat = {}
  335.                         self.offlineChat[receiver.dbid] = offlineChat
  336.                 end
  337.                 offlineChat[session] = true
  338.         end
  339. end

  340. function ChatCenter:PrivateOfflineChat(player)
  341.         local offlineChat = self.offlineChat[player.dbid]
  342.         if not offlineChat then
  343.                 return
  344.         end
  345.         for session,_ in pairs(offlineChat) do
  346.                 local records = self.privateChatRecord[session]
  347.                 server.sendReq(player, "sc_chat_private_init_msg", {
  348.                         session = session,
  349.                         chatData = records,
  350.                 })
  351.         end
  352.         self.offlineChat[player.dbid] = nil
  353. end

  354. function ChatCenter:PrivateChatStart(sender, recvId)
  355.         local session = self:GetPrivateChatId(sender.dbid, recvId)
  356.         local records = self.privateChatRecord[session]
  357.         if not records then
  358.                 records = {}
  359.                 self.privateChatRecord[session] = records
  360.         end
  361.         server.sendReq(sender, "sc_chat_private_init_msg", {
  362.                         session = session,
  363.                         chatData = records,
  364.                 })
  365. end

  366. function ChatCenter:GetPrivateChatId(senderId, receiverId)
  367.         return (senderId < receiverId and senderId ..":".. receiverId or receiverId ..":".. senderId)
  368. end

  369. function ChatCenter:ClearChat()
  370.         self.record:ClearRecord()
  371. end

  372. function ChatCenter:ChatSysInfo(dbid, str)
  373.         local player = server.playerCenter:DoGetPlayerByDBID(dbid)
  374.         if player then
  375.                 local data = {
  376.                                 type = _ChatType.SysChat,
  377.                                 id = dbid,
  378.                                 name = player.cache.name,
  379.                                 vip = player.cache.vip,
  380.                                 str = str,
  381.                                 job = player.cache.job,
  382.                                 sex = player.cache.sex,
  383.                                 time = lua_app.now(),
  384.                         }
  385.                 player:sendReq("sc_chat_new_msg", { chatData = data })
  386.         end
  387. end

  388. function ChatCenter:BroadcastSysInfo(str)
  389.         local data = {
  390.                                 type = _ChatType.SysChat,
  391.                                 id = 0,
  392.                                 name = player.cache.name,
  393.                                 vip = player.cache.vip,
  394.                                 str = str,
  395.                                 job = player.cache.job,
  396.                                 sex = player.cache.sex,
  397.                                 time = lua_app.now(),
  398.                         }
  399.         server.broadcastReq("sc_chat_new_msg", { chatData = data })
  400. end

  401. function ChatCenter:ResetServer()
  402.         self.record:ClearRecord()
  403.         self.systemrecord:ClearRecord()
  404.         self.playerlist = {}
  405.         self.privateChatRecord = {}
  406.         self.offlineChat = {}
  407. end

  408. server.SetCenter(ChatCenter, "chatCenter")
  409. return ChatCenter
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|手游资源 ( 蜀ICP备18005618号-1 )

GMT+8, 2024-4-19 03:22 , Processed in 0.161308 second(s), 32 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表
联系客服1
联系客服2