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