ChatLogic.lua 11 KB

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