ChatLogic.lua 10 KB

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