ChatLogic.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. -- 发送聊天数据
  122. local msgRet = Msg.gc.GC_CHAT
  123. if SettingLogic.isNoShowVip(human) == true then
  124. msgRet.item.vipLv = VipLogic.getVipLv(human)
  125. if msgRet.item.vipLv > 0 and msgRet.item.vipLv < 14 then
  126. msgRet.item.vipLv = -1
  127. end
  128. else
  129. msgRet.item.vipLv = VipLogic.getVipLv(human)
  130. end
  131. -- 获取服务器名
  132. msgRet.item.svrName = Config.SVR_NAME
  133. -- 角色信息
  134. RoleLogic.getRoleBase(human,msgRet.item.roleBase)
  135. -- 称号
  136. msgRet.item.label = human.db.chenghao or 0
  137. -- 聊天内容
  138. if isJson > ChatHandler.CHAT_NORMAL then
  139. msgRet.item.msg = recvMsg.msg
  140. else
  141. msgRet.item.msg = FilterUtil.filter(recvMsg.msg)
  142. end
  143. msgRet.item.msgType = recvMsg.msgType
  144. msgRet.item.isJson = isJson
  145. msgRet.item.sendTime = os.time()
  146. msgRet.item.videoUuid = recvMsg.videoUuid or ""
  147. -- 频道上次发言时间
  148. human.chatTime = human.chatTime or {}
  149. local chatTime = human.chatTime[recvMsg.msgType] or 0
  150. if recvMsg.msgType == ChatHandler.CHAT_TYPE_WORLD or --世界聊天
  151. recvMsg.msgType == ChatHandler.CHAT_TYPE_MIDDLE or -- 全服
  152. ChatHandler.CHAT_TYPE_WARZONE == recvMsg.msgType then -- 战区
  153. --世界聊天
  154. -- 判断上次发言时间
  155. if chatTime and chatTime + ChatHandler.CHAT_CD > Timer.now then
  156. local cdLeftSec = math.ceil((ChatHandler.CHAT_CD - (Timer.now - chatTime))/1000)
  157. Broadcast.sendDown(human, Util.format(Lang.CHAT_TIME_SHORT, cdLeftSec))
  158. return
  159. end
  160. -- 不是战斗记录的情况下,判断是否重复发消息
  161. if (recvMsg.videoUuid == nil or
  162. recvMsg.videoUuid == "") and
  163. human.worldChatLast and
  164. human.worldChatLast[recvMsg.msgType] and
  165. human.worldChatLast[recvMsg.msgType] == msgRet.item.msg then
  166. Broadcast.sendDown(human, Lang.CHAT_TIME_DUPLICATE)
  167. return
  168. end
  169. human.worldChatLast = human.worldChatLast or {}
  170. human.worldChatLast[recvMsg.msgType] = msgRet.item.msg
  171. human.chatTime[recvMsg.msgType] = Timer.now
  172. -- 跨服
  173. if recvMsg.msgType == ChatHandler.CHAT_TYPE_MIDDLE
  174. or ChatHandler.CHAT_TYPE_WARZONE == recvMsg.msgType then
  175. -- 发送数据到中心服
  176. local lwMsgRet = InnerMsg.lw.LW_MIDDLE_CHAT
  177. lwMsgRet.svrIndex = Config.SVR_INDEX
  178. lwMsgRet.tChatMsg = msgRet
  179. InnerMsg.sendMsg(0, lwMsgRet)
  180. print("[chat] 发送跨服消息开始")
  181. else
  182. Msg.sendWorld(msgRet)
  183. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType)
  184. end
  185. elseif recvMsg.msgType == ChatHandler.CHAT_TYPE_UNION then
  186. --公会聊天
  187. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  188. if not union then
  189. Broadcast.sendDown(human, Lang.CHAT_NEED_JOIN_UNION)
  190. return
  191. end
  192. -- if msgRet.item.isJson > ChatHandler.CHAT_NORMAL then
  193. -- Broadcast.sendDown(human, Lang.SHARE_SUCCESS)
  194. -- end
  195. UnionLogic.send2Union(msgRet, union)
  196. --ChatRecord.addUnionRecord(msgRet.item, human.db.unionUuid)
  197. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType)
  198. else
  199. -- 其他聊天
  200. if chatTime and chatTime + ChatHandler.CHAT_CD > Timer.now then
  201. local cdLeftSec = math.ceil((ChatHandler.CHAT_CD - (Timer.now - chatTime))/1000)
  202. Broadcast.sendDown(human, Util.format(Lang.CHAT_TIME_SHORT, cdLeftSec))
  203. return
  204. end
  205. human.chatTime[recvMsg.msgType] = Timer.now
  206. -- 好友特殊发送
  207. if recvMsg.msgType == ChatHandler.CHAT_TYPE_FRIEND then
  208. local fHuman = ObjHuman.onlineUuid[recvMsg.fUuid]
  209. if fHuman and fHuman.fd then
  210. Msg.send(msgRet, fHuman.fd)
  211. end
  212. Msg.send(msgRet, human.fd)
  213. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType,recvMsg.fUuid)
  214. else
  215. Msg.sendWorld(msgRet)
  216. ChatRecord.addRecord(human,msgRet.item, recvMsg.msgType)
  217. end
  218. end
  219. YunYingLogic.onCallBack(human, "ChatTimes", 1)
  220. -- 上报消息到后台
  221. ChatHandler.chatReport(human,recvMsg.msg)
  222. return true
  223. end
  224. function sendChatByTestGm(human)
  225. local str = "test chat okokok"
  226. chat(human, {msgType = ChatHandler.CHAT_TYPE_WORLD,msg = str},false, false)
  227. end
  228. function getCombatName(combatType, name)
  229. return Util.format(Lang.COMBAT_CHAT_MSG, name)
  230. end
  231. function isDot(human)
  232. if ChatRecord.CHAT_RECORD_FRIEND[human.db._id] and ChatRecord.CHAT_RECORD_FRIEND[human.db._id].count ~= 0 then
  233. return true
  234. end
  235. end
  236. -- 跨服消息到达
  237. function WL_MIDDLE_CHAT(fd, msg)
  238. Msg.sendWorld(msg.tChatMsg)
  239. ChatRecord.addMiddleRecord(msg.tChatMsg.item, msg.tChatMsg.item.msgType)
  240. end