BraveTest.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. --===========================================================================================
  2. --勇者试炼
  3. --===========================================================================================
  4. BraveTest = {}
  5. local this = {}
  6. ---红点id
  7. this.ReadId = 100
  8. local MONSTER_ID_KEY = "CURRENT_MONSTERS"
  9. local REP_ID_KEY = "REP_ID"
  10. local CURR_LEVEL = "T$_BRAVE_TEST_CURR_LEVEL"
  11. local COMPLETE_INFO = "T$_BRAVE_TEST_COMPLETE_INFO"
  12. local STAGE_REWARD_KEY = "T$_STAGE_REWARD"
  13. local FIGHT_TIME_KEY = "T$_BRAVE_TEST_TIME"
  14. --region 战斗时间数据访问相关
  15. function BraveTest.getFightTime(actor)
  16. local data = getplaydef(actor, FIGHT_TIME_KEY)
  17. if table.isNullOrEmpty(data) then
  18. return {}
  19. end
  20. return data
  21. end
  22. function BraveTest.setFightTime(actor, data)
  23. setplaydef(actor, FIGHT_TIME_KEY, data)
  24. end
  25. function BraveTest.getFightTimeByLevel(actor, level)
  26. local data = BraveTest.getFightTime(actor)
  27. local result = table.getValue(data, tostring(level))
  28. if result == nil then
  29. return nil
  30. end
  31. return tonumber(result)
  32. end
  33. function BraveTest.setFightTimeByLevel(actor, level, timestampMills)
  34. local data = BraveTest.getFightTime(actor)
  35. data[tostring(level)] = timestampMills
  36. BraveTest.setFightTime(actor, data)
  37. end
  38. --endregion
  39. function BraveTest.CheckEnter(actor)
  40. local currLevel = getplaydef(actor, CURR_LEVEL)
  41. if string.isNullOrEmpty(currLevel) then
  42. currLevel = 1
  43. else
  44. currLevel = tonumber(currLevel) + 1
  45. end
  46. if currLevel > this.GetMaxLevel() then
  47. return false
  48. end
  49. local id = BraveTest.level2ConfigId(currLevel)
  50. id = tonumber(id)
  51. if id == nil or id <= 0 then
  52. gameDebug.assertPrintTrace(false, actor:toString() .. "勇者试炼检测进入失败,副本配置未找到!currLevel:" .. currLevel)
  53. return false
  54. end
  55. return DuplicateCommon.CheckEnterConditonCommon(actor, id) == EnterLimitResultConst.ALLOW
  56. end
  57. function this.GetMaxLevel()
  58. local minAndMaxId = ConfigDataManager.getTableValue("cfg_repGlobal", "value", "id", RepGlobalConfig.BRAVE_TEST_LEVEL)
  59. local maxId = string.split(minAndMaxId, "#")[2]
  60. local maxLevel = tonumber(ConfigDataManager.getTableValue("cfg_rep", "replevel", "id", maxId))
  61. return tonumber(maxLevel)
  62. end
  63. function BraveTest.ReqEnterBraveTest(actor, configId)
  64. --个人进入
  65. if DuplicateCommon.CheckEnterConditonCommon(actor, configId) ~= EnterLimitResultConst.ALLOW then
  66. return
  67. end
  68. local maxHP = getattrinfo(actor, "maxHP")
  69. local maxMP = getattrinfo(actor, "maxMP")
  70. sethp(actor, maxHP)
  71. setmp(actor, maxMP)
  72. --寻找是否有可进入的副本,如果没有创建副本
  73. local mapId = DuplicateCommon.CreateDupMapCommon(actor, configId, true)
  74. local x, y = DuplicateCommon.GetEnterPointXYCommon(configId)
  75. --回蓝回血
  76. DuplicateCommon.RecoverHPMP(actor)
  77. --进入副本
  78. --print("【勇者试炼】进入副本",actor:toString() ,mapId, x, y)
  79. enterduplicate(actor, mapId, x, y)
  80. --BraveTest.resCurrencyStateInfo(DuplicateState.PREPARE,mapId,actor)
  81. --上一关奖励未领取则执行领取操作
  82. local prevCfgId = BraveTest.getPrevCfgId(configId)
  83. if prevCfgId ~= nil and BraveTest.rewardable(actor, prevCfgId) then
  84. local level = BraveTest.config2Level(prevCfgId)
  85. BraveTest.rewardByLevel(actor, level)
  86. end
  87. end
  88. function BraveTest.BraveTestStateUpdate(system, id, state, nextStateStartTime, configId)
  89. local currLevel = BraveTest.config2Level(configId)
  90. local now = getbaseinfo("now")
  91. local dupInfo = getduplicate(id)
  92. local players = table.getValue(dupInfo, "players")
  93. if state == DuplicateState.PREPARE then
  94. --jprint("【勇者试炼】副本开始",configId,id,state)
  95. elseif state == DuplicateState.FIGHT then
  96. --战斗阶段,刷怪
  97. local monsterId = ConfigDataManager.getTableValue("cfg_rep", "monster", "id", configId)
  98. --jprint("【勇者试炼】战斗阶段开始刷怪",configId,id,state)
  99. local monsterActors = DuplicateCommon.DupGenMonsterCommon(id, monsterId)
  100. local monsterActorIDs = {}
  101. for _, id in pairs(monsterActors) do
  102. table.insert(monsterActorIDs, id:toString())
  103. end
  104. --jprint("monsterActorIDs", monsterActorIDs)
  105. --把刷出来的怪保存起来
  106. setenvirvar(id, MONSTER_ID_KEY, monsterActorIDs)
  107. --把副本的配置ID保存起来
  108. setenvirvar(id, REP_ID_KEY, configId)
  109. BraveTest.resCurrencyStateInfo(state, id)
  110. --记录开始时间
  111. if players ~= nil then
  112. for _, actor in pairs(players) do
  113. BraveTest.setFightTimeByLevel(actor, currLevel, now)
  114. end
  115. end
  116. elseif state == DuplicateState.FINISH then
  117. --jprint("【勇者试炼】副本结束",configId,id,state)
  118. BraveTest.resCurrencyStateInfo(state, id)
  119. --计算战斗时间
  120. if players ~= nil then
  121. for _, actor in pairs(players) do
  122. local startTime = BraveTest.getFightTimeByLevel(actor, currLevel);
  123. if startTime ~= nil then
  124. local diffMills = now - startTime
  125. jprint("勇者试炼完成时间", actor, currLevel, startTime, now, diffMills)
  126. RankScript.updateBraveTestInfos(actor, currLevel, diffMills)
  127. AngelBenefit.UpdateTaskProgress(actor, AngelBenefit.TASK_TYPE.BRAVE, currLevel)
  128. end
  129. end
  130. end
  131. end
  132. end
  133. function BraveTest.refreshRedDot(actor)
  134. RedPoint.sendOneRedPoint(actor, this.ReadId, BraveTest.CheckEnter(actor) or false)
  135. end
  136. ---登录触发红点刷新
  137. function BraveTest.loginRed(red_data, actor)
  138. if BraveTest.CheckEnter(actor) then
  139. ---登陆的时候判定 false 可以不发送的
  140. red_data[this.ReadId] = true
  141. end
  142. end
  143. -- @description 响应给客户端当前阶段
  144. -- @param 玩家对象;地图id
  145. -- @return
  146. function BraveTest.resCurrencyStateInfo(state, mapId, actor)
  147. local dupInfo = getduplicate(mapId)
  148. local players = table.getValue(dupInfo, "players")
  149. -- local state = table.getValue(dupInfo,"state")
  150. local configId = table.getValue(dupInfo, "dupcfgid")
  151. local nextStateStartTime = table.getValue(dupInfo, "nextstatetime")
  152. local ok = false
  153. local monsterActorIDs = getenvirvar(mapId, MONSTER_ID_KEY)
  154. if table.isNullOrEmpty(monsterActorIDs) and state == DuplicateState.FINISH then
  155. ok = true
  156. end
  157. local res = { state, tostring(nextStateStartTime), configId, ok }
  158. if actor ~= nil then
  159. sendluamsg(actor, LuaMessageIdToClient.BRAVE_TEST_STATE, res)
  160. else
  161. for _, _actor in ipairs(players) do
  162. sendluamsg(_actor, LuaMessageIdToClient.BRAVE_TEST_STATE, res)
  163. end
  164. end
  165. end
  166. function BraveTest.BraveTestEnter(actor, mapId, state, nextStateStartTime, configId)
  167. BraveTest.resCurrencyStateInfo(state, mapId, actor)
  168. end
  169. function BraveTest.BraveTestMonsterDie(mapId, killer, monCfgId, monActor)
  170. local monsterActorIDs = getenvirvar(mapId, MONSTER_ID_KEY)
  171. --jprint("monsterActorIDs", monsterActorIDs)
  172. if table.isNullOrEmpty(monsterActorIDs) then
  173. return
  174. end
  175. local strId = monActor:toString()
  176. if array.contains(monsterActorIDs, strId) then
  177. table.removeByValue(monsterActorIDs, strId)
  178. end
  179. --jprint("monsterActorIDs2", monsterActorIDs)
  180. setenvirvar(mapId, MONSTER_ID_KEY, monsterActorIDs)
  181. if table.isNullOrEmpty(monsterActorIDs) then
  182. --领取奖励
  183. local cfgId = getenvirvar(mapId, REP_ID_KEY)
  184. --记录下当前的关卡层级
  185. local level = BraveTest.config2Level(cfgId)
  186. setplaydef(killer, CURR_LEVEL, level)
  187. OpenServerCompetition.updateRankData(killer, CompetitionType.BRAVE_CHALLENGE, level)
  188. --BraveTest.resCurrencyStateInfo(DuplicateState.FINISH, mapId)
  189. setduplicatestate(mapId, SetDuplicateStateConst.TO_FINISH)
  190. end
  191. end
  192. function BraveTest.ReqBraveTestPanelInfo(actor)
  193. local currLevel = getplaydef(actor, CURR_LEVEL)
  194. if string.isNullOrEmpty(currLevel) then
  195. currLevel = 1
  196. else
  197. currLevel = tonumber(currLevel) + 1
  198. end
  199. local rewardInfo = BraveTest.getStageRewardInfo(actor)
  200. local stageReward = not table.isNullOrEmpty(rewardInfo)
  201. local id = BraveTest.level2ConfigId(currLevel)
  202. local res = {
  203. id = id,
  204. level = currLevel,
  205. stageReward = stageReward
  206. }
  207. sendluamsg(actor, LuaMessageIdToClient.BRAVE_TEST_PANEL_INFO, res)
  208. end
  209. function BraveTest.level2ConfigId(level)
  210. local cfgId = ConfigDataManager.getTableValue("cfg_rep", "id", "type", DuplicateType.BRAVE_TEST, "replevel", level)
  211. return cfgId
  212. end
  213. function BraveTest.config2Level(cfgId)
  214. local level = ConfigDataManager.getTableValue("cfg_rep", "replevel", "id", cfgId)
  215. if level == nil then
  216. return nil
  217. end
  218. return tonumber(level)
  219. end
  220. function BraveTest.getPrevCfgId(cfgId)
  221. local currLevel = BraveTest.config2Level(cfgId)
  222. if currLevel ~= nil and currLevel > 1 then
  223. local prevLevel = currLevel - 1
  224. return BraveTest.level2ConfigId(prevLevel)
  225. else
  226. return nil
  227. end
  228. end
  229. function BraveTest.onQuitDup(actor, cfgId)
  230. if BraveTest.rewardable(actor, cfgId) then
  231. local level = BraveTest.config2Level(cfgId)
  232. BraveTest.rewardByLevel(actor, level)
  233. end
  234. end
  235. function BraveTest.getStageRewardInfo(actor)
  236. local data = getplaydef(actor, STAGE_REWARD_KEY)
  237. if data == nil then
  238. return {}
  239. end
  240. return data
  241. end
  242. function BraveTest.setStageRewardInfo(actor, data)
  243. setplaydef(actor, STAGE_REWARD_KEY, data)
  244. end
  245. --领取阶阶段奖励
  246. function BraveTest.ReqBraveTestStageReward(actor)
  247. local rewardConfigStr = ConfigDataManager.getTableValue("cfg_repGlobal", "value", "id", 12001002)
  248. if string.isNullOrEmpty(rewardConfigStr) then
  249. return
  250. end
  251. local items = string.split(rewardConfigStr, "#")
  252. if array.length(items) ~= 3 then
  253. return
  254. end
  255. local level = items[1]
  256. local itemId = items[2]
  257. local itemCount = items[3]
  258. local numLevel = tonumber(level)
  259. local currLevel = getplaydef(actor, CURR_LEVEL)
  260. if tonumber(currLevel) >= numLevel then
  261. --可以领奖
  262. local rewardInfo = BraveTest.getStageRewardInfo(actor)
  263. if table.contains(rewardInfo, numLevel) then
  264. noticeTip.noticeinfo(actor, StringIdConst.TEXT385)
  265. return
  266. end
  267. additemtobag(actor, itemId, itemCount, 0, 9999, '勇气试炼')
  268. table.insert(rewardInfo, numLevel)
  269. BraveTest.setStageRewardInfo(actor, rewardInfo)
  270. local res = {}
  271. res[itemId] = itemCount
  272. sendluamsg(actor, LuaMessageIdToClient.BRAVE_TEST_STAGE_REWARD, res)
  273. else
  274. noticeTip.noticeinfo(actor, StringIdConst.TEXT386)
  275. end
  276. BraveTest.refreshRedDot(actor)
  277. end
  278. -- 领取奖励
  279. function BraveTest.ReqBraveTestReward(actor)
  280. local currLevel = getplaydef(actor, CURR_LEVEL)
  281. if currLevel == nil and currLevel <= 0 then
  282. return
  283. end
  284. local cfgId = BraveTest.level2ConfigId(currLevel)
  285. if BraveTest.rewardable(actor, cfgId) then
  286. BraveTest.rewardByLevel(actor, currLevel)
  287. jprint("领取勇者试炼奖励", actor, currLevel, cfgId)
  288. end
  289. end
  290. --执行领奖
  291. function BraveTest.rewardByLevel(actor, currLevel)
  292. local cfgId = BraveTest.level2ConfigId(currLevel)
  293. local activityId = ConfigDataManager.getTableValue("cfg_rep", "type", "id", cfgId)
  294. reduceactivitytimes(actor, activityId, 1)
  295. local completeInfo = getplaydef(actor, COMPLETE_INFO)
  296. if completeInfo == nil then
  297. completeInfo = {}
  298. end
  299. local levelKey = tostring(currLevel)
  300. local completeInfoItem = table.getValue(completeInfo, levelKey)
  301. if completeInfoItem == nil then
  302. completeInfoItem = {}
  303. completeInfo[levelKey] = completeInfoItem
  304. end
  305. local isreward = table.getValue(completeInfoItem, "isreward");
  306. if isreward == 1 then
  307. return
  308. end
  309. --local cfgId = BraveTest.level2ConfigId(currLevel)
  310. if string.isNullOrEmpty(cfgId) then
  311. noticeTip.noticeinfo(actor, StringIdConst.TEXT387)
  312. return
  313. end
  314. local reward = ConfigDataManager.getTableValue("cfg_rep", "reward", "id", cfgId)
  315. if string.isNullOrEmpty(reward) then
  316. noticeTip.noticeinfo(actor, StringIdConst.TEXT388)
  317. return
  318. end
  319. local rewardMap = string.toIntIntMap(reward, "#", "|")
  320. if table.isNullOrEmpty(rewardMap) then
  321. noticeTip.noticeinfo(actor, StringIdConst.TEXT389)
  322. return
  323. end
  324. --jprint("领取奖励",rewardMap)
  325. local itemBing = ConfigDataManager.getTableValue("cfg_bind", "bind", "id", 11)
  326. additemmaptobag(actor, rewardMap, itemBing, 9999, '勇气试炼')
  327. sendluamsg(actor, LuaMessageIdToClient.BRAVE_TEST_REWARD, rewardMap)
  328. completeInfoItem["isreward"] = 1
  329. setplaydef(actor, COMPLETE_INFO, completeInfo)
  330. DuplicateCommon.FinishDupActivity(actor, cfgId)
  331. BraveTest.refreshRedDot(actor)
  332. --勇气试炼任务
  333. TaskHandler.TriggerTaskGoal(actor, TaskTargetType.FLUSH_BRAVE_TEST_LV)
  334. end
  335. --是否领取了奖励
  336. function BraveTest.rewardable(actor, cfgId)
  337. local currlevel = getplaydef(actor, CURR_LEVEL)
  338. if currlevel ~= BraveTest.config2Level(cfgId) then
  339. return false
  340. end
  341. local completeInfo = getplaydef(actor, COMPLETE_INFO)
  342. if completeInfo == nil then
  343. return true
  344. end
  345. local currLevel = BraveTest.config2Level(cfgId)
  346. local levelKey = tostring(currLevel)
  347. local completeInfoItem = table.getValue(completeInfo, levelKey)
  348. if completeInfoItem == nil then
  349. return true
  350. end
  351. local isreward = table.getValue(completeInfoItem, "isreward");
  352. return isreward == 0
  353. end
  354. function BraveTest.playerDie(actor)
  355. local mapId = getbaseinfo(actor, "unimapid")
  356. --print("mapid", mapId)
  357. local dupInfo = getduplicate(mapId)
  358. if dupInfo == nil then
  359. return
  360. end
  361. local type = dupInfo["type"]
  362. if type ~= DuplicateType.BRAVE_TEST then
  363. return
  364. end
  365. --print("dupinfo", dupInfo)
  366. local players = table.getValue(dupInfo, "players")
  367. for _, v in pairs(players) do
  368. if tostring(v) == actor:toString() then
  369. intervalcalldelay(actor, 4000, 1000, 1, "bravetestquitdump", actor)
  370. break
  371. end
  372. end
  373. end
  374. function BraveTest.getBraveTestLv(actor)
  375. return getplaydef(actor, CURR_LEVEL) or 0
  376. end
  377. function bravetestquitdump(actor)
  378. --print("bravetestquitdump", actor:toString())
  379. quitduplicate(actor)
  380. end
  381. function addbravetestcount(actor, count)
  382. jprint(actor, "增加勇者试炼次数", count)
  383. addleftcountofactivity(actor, DuplicateType.BRAVE_TEST, count)
  384. end
  385. RedPointEventListerTable:eventLister("0", "勇者试炼红点", BraveTest.loginRed)