ChatLogic.lua 10 KB

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