ChatLogic.lua 7.9 KB

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