ChatLogic.lua 7.3 KB

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