ChatLogic.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. local Msg = require("core.Msg")
  2. local ChatHandler = require("chat.Handler")
  3. local VipLogic = require("vip.VipLogic")
  4. local Log = require("common.Log")
  5. local FilterUtil = require("common.FilterUtil")
  6. local Gm = require("chat.Gm")
  7. local ChatRecord = require("chat.ChatRecord")
  8. local Broadcast = require("broadcast.Broadcast")
  9. local UnionDBLogic = require("union.UnionDBLogic")
  10. local UnionLogic = require("union.UnionLogic")
  11. local Timer = require("core.Timer")
  12. local Lang = require("common.Lang")
  13. local Util = require("common.Util")
  14. local RoleLogic = require("role.RoleLogic")
  15. local FriendDBLogic = require("friend.FriendDBLogic")
  16. local Config = require("Config")
  17. local RoleDBLogic = require("role.RoleDBLogic")
  18. local SettingLogic = require("setting.SettingLogic")
  19. local ObjHuman = require("core.ObjHuman")
  20. local InnerMsg = require("core.InnerMsg")
  21. local CommonDB = require("common.CommonDB")
  22. local YunYingLogic = require("yunying.YunYingLogic")
  23. local TriggerDefine = require("trigger.TriggerDefine")
  24. local TriggerLogic = require("trigger.TriggerLogic")
  25. local CHAT_SENDDOWN_RESON_LV = 1
  26. local CHAT_SENDDOWN_RESON_DAY = 2
  27. local function ChatLogic_GetLimit(nChatType)
  28. local nLimitLv, nLimitOpenDay = 0, 0
  29. if ChatHandler.CHAT_TYPE_MIDDLE == nChatType then
  30. nLimitLv = ChatHandler.CHAT_LV_NEED_MIDDLE
  31. nLimitOpenDay = ChatHandler.CHAT_OPEN_DAY_MIDDLE
  32. elseif ChatHandler.CHAT_TYPE_WARZONE == nChatType then
  33. nLimitLv = ChatHandler.CHAT_LV_NEED_WARZONE
  34. nLimitOpenDay = ChatHandler.CHAT_OPEN_DAY_WARZONE
  35. elseif ChatHandler.CHAT_TYPE_WORLD == nChatType then
  36. nLimitLv = ChatHandler.CHAT_LV_NEED_WORLD
  37. elseif ChatHandler.CHAT_TYPE_FRIEND == nChatType then
  38. nLimitLv = ChatHandler.CHAT_LV_NEED_FRIEND
  39. end
  40. return nLimitLv, nLimitOpenDay
  41. end
  42. local function ChatLogic_SendDown(human, nChatType, nReson)
  43. local str
  44. if ChatHandler.CHAT_TYPE_MIDDLE == nChatType then
  45. str = Util.format(Lang.CHAT_MIDDLE_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_MIDDLE)
  46. elseif ChatHandler.CHAT_TYPE_WARZONE == nChatType then
  47. str = Util.format(Lang.CHAT_WARZONE_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_WARZONE)
  48. elseif ChatHandler.CHAT_TYPE_WORLD == nChatType then
  49. str = Util.format(Lang.CHAT_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_WORLD)
  50. elseif ChatHandler.CHAT_TYPE_FRIEND == nChatType then
  51. str = Util.format(Lang.CHAT_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_FRIEND)
  52. end
  53. Broadcast.sendDown(human, str)
  54. end
  55. function chat(human, recvMsg, isJson)
  56. if Gm.checkGm(human, recvMsg.msg) then
  57. return
  58. end
  59. isJson = isJson or ChatHandler.CHAT_NORMAL
  60. local nOpenServerDay = CommonDB.getServerOpenDay()
  61. local nLimitLv, nLimitOpenDay = ChatLogic_GetLimit(recvMsg.msgType)
  62. if human.db.lv < nLimitLv and VipLogic.getVipLv(human) == 0 then
  63. ChatLogic_SendDown(human, recvMsg.msgType, CHAT_SENDDOWN_RESON_LV)
  64. return
  65. end
  66. if nOpenServerDay < nLimitOpenDay then
  67. ChatLogic_SendDown(human, recvMsg.msgType, CHAT_SENDDOWN_RESON_DAY)
  68. return
  69. end
  70. --世界聊天限制等级
  71. -- if recvMsg.msgType == ChatHandler.CHAT_TYPE_WORLD then
  72. -- if human.db.lv < ChatHandler.CHAT_LV_NEED_WORLD and VipLogic.getVipLv(human) == 0 then
  73. -- local str = Util.format(Lang.CHAT_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_WORLD)
  74. -- Broadcast.sendDown(human, str)
  75. -- return
  76. -- end
  77. -- end
  78. -- 跨服聊天限制等级
  79. -- if recvMsg.msgType == ChatHandler.CHAT_TYPE_MIDDLE then
  80. -- local nOpenServerDay = CommonDB.getServerOpenDay()
  81. -- if nOpenServerDay < 15 then
  82. -- local str = Util.format(Lang.CHAT_MIDDLE_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_MIDDLE)
  83. -- Broadcast.sendDown(human, str)
  84. -- return
  85. -- end
  86. -- if human.db.lv < ChatHandler.CHAT_LV_NEED_MIDDLE and VipLogic.getVipLv(human) == 0 then
  87. -- local str = Util.format(Lang.CHAT_MIDDLE_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_MIDDLE)
  88. -- Broadcast.sendDown(human, str)
  89. -- return
  90. -- end
  91. -- end
  92. -- 好友私聊
  93. if recvMsg.msgType == ChatHandler.CHAT_TYPE_FRIEND then
  94. -- 聊天权限判断
  95. -- if human.db.lv < ChatHandler.CHAT_LV_NEED_FRIEND and VipLogic.getVipLv(human) == 0 then
  96. -- local str = Util.format(Lang.CHAT_NEED_LV_ARGS, ChatHandler.CHAT_LV_NEED_FRIEND)
  97. -- Broadcast.sendDown(human, str)
  98. -- return
  99. -- end
  100. -- 是否互为好友
  101. local isFriend = FriendDBLogic.isFriend(human.db._id, recvMsg.fUuid)
  102. if isFriend ~= true then
  103. return Broadcast.sendDown(human, Lang.CHAT_ERR_NOT_FIREND)
  104. end
  105. local db = RoleDBLogic.getDb(recvMsg.fUuid, fields)
  106. local tempHuman = {}
  107. tempHuman.db = db
  108. local cantPrivate = SettingLogic.isNoPrivateChat(tempHuman)
  109. if cantPrivate == true then
  110. return Broadcast.sendDown(human, Lang.CHAT_ERR_CNT_FIREND)
  111. end
  112. end
  113. --判定是否被禁言
  114. if human.db.banSayTime and (os.time() < human.db.banSayTime or human.db.banSayTime == -1) then
  115. local leftMin = math.ceil((human.db.banSayTime - os.time())/60)
  116. local content = Util.format(human.db.banSayReason,leftMin)
  117. Broadcast.sendErr(human, content)
  118. return
  119. end
  120. human.db.banSay = nil
  121. human.db.banSayTime = nil
  122. --Log.write(Log.LOGID_OSS_CHAT, human.db._id, human.db.account, human.db.name, human.db.ip, recvMsg.msgType, recvMsg.msg)
  123. Log.write(Log.LOGID_OSS_CHAT, human.db._id, human.db.newUniqueTag, human.db.name, human.db.ip, recvMsg.msgType, recvMsg.msg)
  124. -- 发送聊天数据
  125. local msgRet = Msg.gc.GC_CHAT
  126. if SettingLogic.isNoShowVip(human) == true then
  127. msgRet.item.vipLv = VipLogic.getVipLv(human)
  128. if msgRet.item.vipLv > 0 and msgRet.item.vipLv < 14 then
  129. msgRet.item.vipLv = -1
  130. end
  131. else
  132. msgRet.item.vipLv = VipLogic.getVipLv(human)
  133. end
  134. -- 获取服务器名
  135. msgRet.item.svrName = Config.SVR_NAME
  136. -- 角色信息
  137. RoleLogic.getRoleBase(human,msgRet.item.roleBase)
  138. -- 称号
  139. msgRet.item.label = human.db.chenghao or 0
  140. -- 聊天内容
  141. if isJson > ChatHandler.CHAT_NORMAL then
  142. msgRet.item.msg = recvMsg.msg
  143. else
  144. msgRet.item.msg = FilterUtil.filter(recvMsg.msg)
  145. end
  146. msgRet.item.msgType = recvMsg.msgType
  147. msgRet.item.isJson = isJson
  148. msgRet.item.sendTime = os.time()
  149. msgRet.item.videoUuid = recvMsg.videoUuid or ""
  150. -- 频道上次发言时间
  151. human.chatTime = human.chatTime or {}
  152. local chatTime = human.chatTime[recvMsg.msgType] or 0
  153. if recvMsg.msgType == ChatHandler.CHAT_TYPE_WORLD or --世界聊天
  154. recvMsg.msgType == ChatHandler.CHAT_TYPE_MIDDLE or -- 全服
  155. ChatHandler.CHAT_TYPE_WARZONE == recvMsg.msgType then -- 战区
  156. --世界聊天
  157. -- 判断上次发言时间
  158. if chatTime and chatTime + ChatHandler.CHAT_CD > Timer.now then
  159. local cdLeftSec = math.ceil((ChatHandler.CHAT_CD - (Timer.now - chatTime))/1000)
  160. Broadcast.sendDown(human, Util.format(Lang.CHAT_TIME_SHORT, cdLeftSec))
  161. return
  162. end
  163. -- 不是战斗记录的情况下,判断是否重复发消息
  164. if (recvMsg.videoUuid == nil or
  165. recvMsg.videoUuid == "") and
  166. human.worldChatLast and
  167. human.worldChatLast[recvMsg.msgType] and
  168. human.worldChatLast[recvMsg.msgType] == msgRet.item.msg then
  169. Broadcast.sendDown(human, Lang.CHAT_TIME_DUPLICATE)
  170. return
  171. end
  172. human.worldChatLast = human.worldChatLast or {}
  173. human.worldChatLast[recvMsg.msgType] = msgRet.item.msg
  174. human.chatTime[recvMsg.msgType] = Timer.now
  175. -- 跨服
  176. if recvMsg.msgType == ChatHandler.CHAT_TYPE_MIDDLE
  177. or ChatHandler.CHAT_TYPE_WARZONE == recvMsg.msgType then
  178. -- 发送数据到中心服
  179. local lwMsgRet = InnerMsg.lw.LW_MIDDLE_CHAT
  180. lwMsgRet.svrIndex = Config.SVR_INDEX
  181. lwMsgRet.tChatMsg = msgRet
  182. InnerMsg.sendMsg(0, lwMsgRet)
  183. print("[chat] 发送跨服消息开始")
  184. else
  185. Msg.sendWorld(msgRet)
  186. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType)
  187. end
  188. elseif recvMsg.msgType == ChatHandler.CHAT_TYPE_UNION then
  189. --公会聊天
  190. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  191. if not union then
  192. Broadcast.sendDown(human, Lang.CHAT_NEED_JOIN_UNION)
  193. return
  194. end
  195. -- if msgRet.item.isJson > ChatHandler.CHAT_NORMAL then
  196. -- Broadcast.sendDown(human, Lang.SHARE_SUCCESS)
  197. -- end
  198. UnionLogic.send2Union(msgRet, union)
  199. --ChatRecord.addUnionRecord(msgRet.item, human.db.unionUuid)
  200. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType)
  201. else
  202. -- 其他聊天
  203. if chatTime and chatTime + ChatHandler.CHAT_CD > Timer.now then
  204. local cdLeftSec = math.ceil((ChatHandler.CHAT_CD - (Timer.now - chatTime))/1000)
  205. Broadcast.sendDown(human, Util.format(Lang.CHAT_TIME_SHORT, cdLeftSec))
  206. return
  207. end
  208. human.chatTime[recvMsg.msgType] = Timer.now
  209. -- 好友特殊发送
  210. if recvMsg.msgType == ChatHandler.CHAT_TYPE_FRIEND then
  211. local fHuman = ObjHuman.onlineUuid[recvMsg.fUuid]
  212. if fHuman and fHuman.fd then
  213. Msg.send(msgRet, fHuman.fd)
  214. end
  215. Msg.send(msgRet, human.fd)
  216. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType,recvMsg.fUuid)
  217. else
  218. Msg.sendWorld(msgRet)
  219. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType)
  220. end
  221. end
  222. YunYingLogic.onCallBack(human, "ChatTimes", 1)
  223. TriggerLogic.PublishEvent(TriggerDefine.CHAT_TIMES, human.db._id, 1)
  224. -- 上报消息到后台
  225. ChatHandler.chatReport(human,recvMsg.msg)
  226. return true
  227. end
  228. function sendChatByTestGm(human)
  229. local str = "test chat okokok"
  230. chat(human, {msgType = ChatHandler.CHAT_TYPE_WORLD,msg = str},false, false)
  231. end
  232. function getCombatName(combatType, name)
  233. return Util.format(Lang.COMBAT_CHAT_MSG, name)
  234. end
  235. function isDot(human)
  236. if ChatRecord.CHAT_RECORD_FRIEND[human.db._id] and ChatRecord.CHAT_RECORD_FRIEND[human.db._id].count ~= 0 then
  237. return true
  238. end
  239. end
  240. -- 跨服消息到达
  241. function WL_MIDDLE_CHAT(fd, msg)
  242. Msg.sendWorld(msg.tChatMsg)
  243. ChatRecord.addMiddleRecord(msg.tChatMsg.item, msg.tChatMsg.item.msgType)
  244. end