NoticeTip.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. -- 提示公告功能
  2. noticeTip = {}
  3. local this = {}
  4. ---提示公告通用方法
  5. ---@param actor 玩家对象
  6. ---@param stringId cfg_string表id
  7. ---@param ... 替换的参数
  8. function noticeTip.noticeinfo(actor, stringId, ...)
  9. local text = ConfigDataManager.getTableValue("cfg_string", "text", "id", stringId)
  10. if string.isNullOrEmpty(text) then
  11. gameDebug.print("noticeinfo ConfigDataManager.getTableValue text is nil")
  12. return
  13. end
  14. local receiver = ConfigDataManager.getTableValue("cfg_string", "receiver", "id", stringId)
  15. if string.isNullOrEmpty(receiver) then
  16. gameDebug.print("noticeinfo ConfigDataManager.getTableValue receiver is nil")
  17. return
  18. end
  19. local messageType = ConfigDataManager.getTableValue("cfg_string", "messageType", "id", stringId)
  20. if string.isNullOrEmpty(messageType) then
  21. gameDebug.print("noticeinfo ConfigDataManager.getTableValue messageType error")
  22. return
  23. end
  24. local condition = ConfigDataManager.getTableValue("cfg_string", "condition", "id", stringId)
  25. if string.isNullOrEmpty(condition) then
  26. gameDebug.print("noticeinfo ConfigDataManager.getTableValue condition error")
  27. return
  28. end
  29. local splitStr = string.split(condition, "|")
  30. local levelLimit = 0
  31. local mapLimit = 0
  32. for i = 1, #splitStr do
  33. local split = string.split(splitStr[i], "#")
  34. if tonumber(split[1]) == 1 then
  35. levelLimit = tonumber(split[2])
  36. else
  37. mapLimit = tonumber(split[2])
  38. end
  39. end
  40. if string.find(text, "%%") then
  41. local content = noticeTip.strreplace(text)
  42. local args = { ... }
  43. if #args <= 0 then
  44. gameDebug.print("noticeinfo params error", stringId, args)
  45. return
  46. end
  47. text = string.format(content, ...)
  48. end
  49. -- 敏感词替换成星号
  50. --text = replacesensitiveword(actor, text)
  51. this.sendNoticeInfo(actor, tonumber(messageType), stringId, text, receiver, levelLimit, mapLimit)
  52. end
  53. --- 发送提示公告
  54. ---@param actor 玩家对象
  55. ---@param messageType 消息类型
  56. ---@param stringId cfg_string表id
  57. ---@param text 消息内容
  58. ---@param receiver 接收者类型
  59. ---@param levelLimit 等级限制
  60. ---@param mapLimit 地图限制
  61. function this.sendNoticeInfo(actor, messageType, stringId, text, receiver, levelLimit, mapLimit)
  62. if StringReceiverType.SYSTEM_SELF == tonumber(receiver) then
  63. if this.checkCondition(actor, levelLimit, mapLimit) then
  64. this.sendMessage(actor, messageType, stringId, text, NoticeChannel.SYSTEM)
  65. end
  66. elseif StringReceiverType.SYSTEM_CURRENT_MAP == tonumber(receiver) then
  67. local playerIds = getmapplayerids(actor)
  68. for i = 1, #playerIds do
  69. local player = getactor(actor, playerIds[i])
  70. if this.checkCondition(player, levelLimit, 0) then
  71. this.sendMessage(player, messageType, stringId, text, NoticeChannel.SYSTEM)
  72. end
  73. end
  74. elseif StringReceiverType.SYSTEM_SAME_UNION == tonumber(receiver) then
  75. local unionInfo = getunioninfo(actor)
  76. if not table.isNullOrEmpty(unionInfo) then
  77. local members = unionInfo.memberinfos
  78. if not table.isNullOrEmpty(members) then
  79. for key, _ in pairs(members) do
  80. local player = getactor(actor, key)
  81. if this.checkCondition(player, levelLimit, mapLimit) then
  82. this.sendMessage(player, messageType, stringId, text, NoticeChannel.SYSTEM)
  83. end
  84. end
  85. end
  86. end
  87. elseif StringReceiverType.UNION_SELF == tonumber(receiver) then
  88. if this.checkCondition(actor, levelLimit, mapLimit) then
  89. this.sendMessage(actor, messageType, stringId, text, NoticeChannel.UNION)
  90. end
  91. elseif StringReceiverType.UNION_ALL == tonumber(receiver) then
  92. local unionInfo = getunioninfo(actor)
  93. if not table.isNullOrEmpty(unionInfo) then
  94. local members = unionInfo.memberinfos
  95. if not table.isNullOrEmpty(members) then
  96. for key, _ in pairs(members) do
  97. local player = getactor(actor, key)
  98. if this.checkCondition(player, levelLimit, mapLimit) then
  99. this.sendMessage(player, messageType, stringId, text, NoticeChannel.UNION)
  100. end
  101. end
  102. end
  103. end
  104. else
  105. local allPlayer = getallplayer(actor)
  106. if allPlayer ~= nil then
  107. for i = 1, #allPlayer do
  108. local player = getactor(actor, allPlayer[i])
  109. if this.checkCondition(player, levelLimit, mapLimit) then
  110. this.sendMessage(player, messageType, stringId, text, NoticeChannel.SYSTEM)
  111. end
  112. end
  113. end
  114. end
  115. end
  116. --- 根据条件发送对应的lua消息给客户端
  117. ---@param actor 玩家对象
  118. ---@param messageType 消息类型
  119. ---@param stringId cfg_string表id
  120. ---@param content 消息内容
  121. ---@param channel 聊天通道类型
  122. function this.sendMessage(actor, messageType, stringId, content, channel)
  123. if messageType >= 1 and messageType <= 5 then
  124. -- 发送聊天消息与跑马灯
  125. sendluamsg(actor, LuaMessageIdToClient.CHAT, {
  126. channel = channel,
  127. content = content
  128. })
  129. sendluamsg(actor, LuaMessageIdToClient.HORSE_LAMP, {
  130. cfgId = stringId,
  131. content = content
  132. })
  133. elseif messageType == 7 then
  134. -- 发送聊天消息
  135. sendluamsg(actor, LuaMessageIdToClient.CHAT, {
  136. channel = channel,
  137. content = content
  138. })
  139. else
  140. -- 发送其他类型的消息
  141. sendluamsg(actor, LuaMessageIdToClient.OTHER_NOTICE, {
  142. messageType = messageType,
  143. cfgId = stringId,
  144. content = content
  145. })
  146. if messageType == 15 then
  147. -- 发送聊天消息
  148. sendluamsg(actor, LuaMessageIdToClient.CHAT, {
  149. channel = channel,
  150. content = content
  151. })
  152. end
  153. end
  154. end
  155. --- 替换字符串中的占位符
  156. ---@param str 目标字符串
  157. function noticeTip.strreplace(str)
  158. -- %s:玩家名
  159. -- %p:充值人民币数
  160. -- %i:道具名
  161. -- %g:工会名
  162. -- %m:地图名
  163. -- %x:x坐标
  164. -- %y:y坐标
  165. -- %d:替换数字
  166. -- %a:伙伴名称
  167. -- %b:boss名称
  168. -- %c:战阶名称
  169. -- %e:转职名称
  170. -- %w:翅膀名称
  171. -- %f :翅膀品质
  172. str = string.gsub(str, "%%p", "%%s")
  173. str = string.gsub(str, "%%i", "%%s")
  174. str = string.gsub(str, "%%g", "%%s")
  175. str = string.gsub(str, "%%m", "%%s")
  176. str = string.gsub(str, "%%x", "%%s")
  177. str = string.gsub(str, "%%y", "%%s")
  178. str = string.gsub(str, "%%d", "%%s")
  179. str = string.gsub(str, "%%a", "%%s")
  180. str = string.gsub(str, "%%b", "%%s")
  181. str = string.gsub(str, "%%c", "%%s")
  182. str = string.gsub(str, "%%e", "%%s")
  183. str = string.gsub(str, "%%w", "%%s")
  184. str = string.gsub(str, "%%f", "%%s")
  185. return str
  186. end
  187. --- 检查等级与地图条件是否满足
  188. ---@param player 玩家对象
  189. ---@param levelLimit 等级限制
  190. ---@param mapLimit 地图限制
  191. function this.checkCondition(player, levelLimit, mapLimit)
  192. local actorLevel = getbaseinfo(player, "level")
  193. if levelLimit > 0 and mapLimit > 0 then
  194. return actorLevel >= levelLimit and isonmap(player, mapLimit)
  195. end
  196. if levelLimit > 0 then
  197. return actorLevel >= levelLimit
  198. end
  199. if mapLimit > 0 then
  200. return isonmap(player, mapLimit)
  201. end
  202. end
  203. --- 测试公告信息
  204. function testnotice(actor, stringId, ...)
  205. noticeTip.noticeinfo(actor, stringId, ...)
  206. end