SanctuaryBoss.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. SanctuaryBoss = {}
  2. SanctuaryBoss.__index = SanctuaryBoss
  3. local this = {}
  4. local function __serverType()
  5. return 2
  6. end
  7. local function __bossType()
  8. return 5
  9. end
  10. local function __globalKey()
  11. return "R$_SanctuaryBossGlobalKey"
  12. end
  13. function SanctuaryBoss.getDbKey()
  14. return __globalKey()
  15. end
  16. function SanctuaryBoss.get()
  17. local data = getsysvar(__globalKey(), 1)
  18. return setmetatable(data or {}, SanctuaryBoss)
  19. end
  20. function SanctuaryBoss:save()
  21. setsysvar(__globalKey(), self, 1)
  22. -- 同步数据到所有主机
  23. local hosts = gethosts()
  24. for _, host in ipairs(hosts) do
  25. setsysvar(host, __globalKey(), self)
  26. end
  27. end
  28. function SanctuaryBoss.combine()
  29. -- 发起合服是清理缓存
  30. this.clearCache()
  31. end
  32. function SanctuaryBoss.initBossState()
  33. this.initBossState()
  34. end
  35. function SanctuaryBoss.getMonsterCount(map_id, line, monster_id, state)
  36. return this.getMonsterCount(map_id, line, monster_id, state)
  37. end
  38. function SanctuaryBoss.monsterDie(actor)
  39. local success, ret = xpcall(this.monsterStateChange, debug.traceback, actor)
  40. gameDebug.assertTrue(success, "圣域BOSS怪物死亡同步数据异常! error", ret)
  41. end
  42. function SanctuaryBoss.monsterRelive(actor)
  43. local success, ret = xpcall(this.monsterStateChange, debug.traceback, actor)
  44. gameDebug.assertTrue(success, "圣域BOSS怪物复活同步数据异常! error", ret)
  45. end
  46. --- 请求进入圣域地图
  47. function SanctuaryBoss.enterMap(actor, msgData)
  48. this.enterMap(actor, msgData)
  49. end
  50. function this.enterMap(actor, move_id)
  51. this.assertT(string.tonumber(move_id) > 0, "参数错误", move_id)
  52. local map_id = getbaseinfo(actor, "mapid")
  53. this.assertT(string.tonumber(map_id) == CrossMap.CROSS_MAP_ID, "必须通过跨服主城进入地图", map_id, CrossMap.CROSS_MAP_ID)
  54. -- 执行传送
  55. MapMoveTransfer.mapMove(actor, move_id)
  56. end
  57. function this.initBossState()
  58. if not this.is_cross() then
  59. return
  60. end
  61. -- 清理缓存
  62. this.clearCache()
  63. local config_list = ConfigDataManager.getTable("cfg_BOSS_challenge", "monsterType", __bossType())
  64. this.assertT(table.notEmpty(config_list), "cfg_BOSS_challenge配置错误", __bossType())
  65. -- 开始缓存数据
  66. local data = SanctuaryBoss.get()
  67. for _, config in ipairs(config_list) do
  68. local map_data = config.mapid
  69. for _, v in pairs(string.split(map_data, "|")) do
  70. local mapInfo = string.split(v, "#")
  71. local map_cfg_id = mapInfo[1]
  72. local line = mapInfo[2]
  73. local map_id = gamemap.getMapKey(map_cfg_id, line)
  74. if table.isEmpty(data.map[map_id]) then
  75. local map = {}
  76. local info_list = mapbossinfo(map_id)
  77. for _, v in pairs(info_list) do
  78. map[v.id] = v
  79. end
  80. data.map[map_id] = map
  81. end
  82. end
  83. end
  84. data:save()
  85. this.debug("---------- finish init boss state cache. ----------")
  86. end
  87. function this.clearCache()
  88. local data = SanctuaryBoss.get()
  89. data.map = {}
  90. data:save()
  91. end
  92. function this.getMonsterCount(map_id, line, monster_id, state)
  93. local data = SanctuaryBoss.get()
  94. local key = gamemap.getMapKey(map_id, line)
  95. local map_data = data.map[key]
  96. this.assertT(table.notEmpty(map_data))
  97. local count = 0
  98. for _, info in pairs(map_data) do
  99. local is_dead = info.isdead and 0 or 1
  100. if tonumber(state) ~= is_dead then
  101. goto continue
  102. end
  103. if tonumber(info.cfgid) ~= tonumber(monster_id) then
  104. goto continue
  105. end
  106. count = count + 1
  107. ::continue::
  108. end
  109. return count
  110. end
  111. function this.monsterStateChange(actor)
  112. if not this.is_cross() then
  113. return
  114. end
  115. local map_id = getbaseinfo(actor, "unimapid")
  116. local data = SanctuaryBoss.get()
  117. if table.isEmpty(data.map) then
  118. return
  119. end
  120. local cache_info = data.map[map_id] or {}
  121. local mon_info = getmonsterinfo(actor)
  122. if table.isEmpty(mon_info) then
  123. return
  124. end
  125. cache_info[mon_info.id] = mon_info
  126. data:save()
  127. end
  128. function this.is_cross()
  129. local server_type = getbaseinfo("servertype")
  130. return tonumber(server_type) == __serverType()
  131. end
  132. -- ----------------------------------------------------------------- --
  133. this.log_open = false
  134. function this.debug(...)
  135. if not this.log_open then
  136. return
  137. end
  138. gameDebug.print(...)
  139. end
  140. function this.assertT(condition, ...)
  141. gameDebug.assertTrue(condition, ...)
  142. end