GoldTask.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by zhangkai.
  4. --- DateTime: 2024/8/20 上午10:02
  5. ---
  6. GoldTask = {}
  7. local this = {}
  8. GoldTask.Var = {
  9. GOLD_TASK_INFO = "T$goldtaskinfo"
  10. }
  11. GoldTask.Const = {
  12. ---任务状态
  13. TASK_STATUS = {
  14. ---已接取
  15. ACCEPT = 1,
  16. ---已完成
  17. FINISH = 2,
  18. ---已提交
  19. SUBMIT = 3
  20. },
  21. ---任务星级
  22. TASK_STAR = {
  23. ---一星
  24. ONE = 1,
  25. ---二星
  26. TWO = 2,
  27. ---三星
  28. THREE = 3
  29. }
  30. }
  31. ---任务池大小
  32. local TASK_POOL_SIZE = 4
  33. --function goldtasktest(actor,msgID, msgData)
  34. -- jprint("黄金任务测试")
  35. -- handlerequest(actor, 0,tonumber(msgID), msgData)
  36. --end
  37. function this.GetNewTaskRecord()
  38. ---玩家任务记录
  39. local taskrecord = {
  40. taskid = 0,
  41. star = 1,
  42. progress = 0,
  43. status = GoldTask.Const.TASK_STATUS.ACCEPT
  44. }
  45. return taskrecord
  46. end
  47. function this.GetNewGoldTask()
  48. ---玩家黄金任务信息任务
  49. local goldtaskinfo = {
  50. ---当前任务池
  51. nowtaskpool = {},
  52. ---当日刷新次数
  53. flushcount = 0,
  54. ---当前正在进行的任务
  55. nowtask = nil,
  56. ---是否首次开启任务页面
  57. firstopen = true,
  58. ---当日已完成的任务记录
  59. dailyrecord = {},
  60. --重置时间
  61. lastresettime = ""
  62. }
  63. return goldtaskinfo
  64. end
  65. function GoldTask.GetPayerTaskInfo(actor)
  66. local taskInfo = getplaydef(actor, GoldTask.Var.GOLD_TASK_INFO)
  67. if not taskInfo then
  68. taskInfo = this.GetNewGoldTask()
  69. end
  70. return taskInfo
  71. end
  72. function GoldTask.Login(actor)
  73. GoldTask.SendTaskInfo(actor)
  74. end
  75. ---刷新玩家任务池
  76. function GoldTask.FlushGoldTaskPool(actor)
  77. local playerLevel = tonumber(getbaseinfo(actor, "level"))
  78. local levelLimit =
  79. tonumber(ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.GOLD_TASK_LEVEL_LIMIT))
  80. if playerLevel < levelLimit then
  81. noticeTip.noticeinfo(actor, StringIdConst.LEVEL_INSUFFICIENT)
  82. return
  83. end
  84. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  85. if taskInfo.nowtask ~= nil then
  86. noticeTip.noticeinfo(actor, StringIdConst.TEXT349)
  87. return
  88. end
  89. local flushCount = taskInfo.flushcount
  90. local pay = false
  91. local freeLimit =
  92. tonumber(ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.GOLD_TASK_FREE_LIMIT))
  93. local itemCfg = 0
  94. local needCount = 0
  95. if flushCount >= freeLimit then
  96. --尝试付费刷新
  97. itemCfg, needCount = GoldTask.CheckConst(actor)
  98. if itemCfg == nil or needCount == nil then
  99. return
  100. end
  101. end
  102. local taskCfgList = ConfigDataManager.getList("cfg_task_gold")
  103. local taskList = GoldTask.RandomTask(actor, playerLevel, taskCfgList)
  104. if not taskList or #taskList == 0 then
  105. print("刷新数据异常, playerLevel: ", tostring(playerLevel), "配置空")
  106. return
  107. end
  108. --扣消耗
  109. if itemCfg ~= nil and needCount > 0 then
  110. local ret = removeitemfrombag(actor, itemCfg, needCount, 0, 9999, "黄金任务")
  111. if not ret then
  112. noticeTip.noticeinfo(actor, StringIdConst.TEXT350)
  113. return
  114. end
  115. pay = true
  116. end
  117. taskInfo.nowtaskpool = GoldTask.BuildNewTaskPool(actor, taskList, pay, false)
  118. --jprint(taskInfo.nowtaskpool)
  119. taskInfo.flushcount = flushCount + 1
  120. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  121. --通知客户端
  122. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  123. --jprint("刷新黄金任务池",taskInfo)
  124. end
  125. ---付费刷新
  126. function GoldTask.CheckConst(actor)
  127. local flushCost =
  128. string.split(
  129. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.GOLD_TASK_FLUSH_COST),
  130. "#"
  131. )
  132. local itemCfg = tonumber(flushCost[1])
  133. local needCount = tonumber(flushCost[2])
  134. local ownCount = tonumber(getbagitemcountbyid(actor, itemCfg))
  135. if ownCount == nil or needCount > ownCount then
  136. noticeTip.noticeinfo(actor, StringIdConst.TEXT351)
  137. return nil
  138. end
  139. return itemCfg, needCount
  140. end
  141. function GoldTask.BuildNewTaskPool(actor, taskList, pay, first)
  142. local taskPool = {}
  143. for _, taskId in pairs(taskList) do
  144. local star = this.RandomStar(taskId, pay)
  145. taskPool[taskId] = star
  146. end
  147. return taskPool
  148. end
  149. function this.RandomStar(taskId, pay)
  150. local taskCfgList = ConfigDataManager.getTable("cfg_task_gold", "id", taskId)
  151. local star = GoldTask.Const.TASK_STAR.ONE
  152. if table.isNullOrEmpty(taskCfgList) then
  153. return 0
  154. end
  155. local taskCfg = taskCfgList[1]
  156. local weightCfg = string.split(taskCfg["probability"], "#")
  157. if pay then
  158. weightCfg = string.split(taskCfg["specialprobability"], "#")
  159. end
  160. retList = randombyweight(weightCfg, 1)
  161. if not table.isNullOrEmpty(retList) then
  162. star = retList[1]
  163. end
  164. return star
  165. end
  166. function GoldTask.RandomTask(actor, level, taskCfgList)
  167. local weightMap = {}
  168. for _, taskCfg in pairs(taskCfgList) do
  169. local taskId = tonumber(taskCfg["id"])
  170. local pass = GoldTask.CheckLv(level, taskCfg["level"])
  171. if pass then
  172. weightMap[taskId] = tonumber(taskCfg["weight"])
  173. end
  174. end
  175. return randombyweight(actor, weightMap, TASK_POOL_SIZE)
  176. end
  177. function GoldTask.CheckLv(level, taskLvCfgLimit)
  178. if string.isNullOrEmpty(taskLvCfgLimit) then
  179. return true
  180. end
  181. local lvLimit = string.split(taskLvCfgLimit, "#")
  182. if #lvLimit < 0 then
  183. return true
  184. end
  185. if #lvLimit >= 1 and level < tonumber(lvLimit[1]) then
  186. return false
  187. end
  188. if #lvLimit >= 2 and level > tonumber(lvLimit[2]) then
  189. return false
  190. end
  191. return true
  192. end
  193. ---接取任务
  194. function GoldTask.AcceptTask(actor, msgData)
  195. jprint("请求接取黄金任务", msgData)
  196. local taskId = GoldTask.GetTaskId(msgData)
  197. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  198. local myTaskPool = taskInfo.nowtaskpool
  199. if not myTaskPool then
  200. noticeTip.noticeinfo(actor, StringIdConst.TEXT352)
  201. return
  202. end
  203. local myTask = taskInfo.nowtask
  204. if myTask or myTask ~= nil then
  205. noticeTip.noticeinfo(actor, StringIdConst.TEXT349)
  206. return
  207. end
  208. local finishCount = 0
  209. local taskRecord = taskInfo.dailyrecord
  210. if taskRecord then
  211. finishCount = table.count(taskRecord)
  212. end
  213. local countLimit =
  214. tonumber(ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.GOLD_TASK_COUNT))
  215. if finishCount >= countLimit then
  216. noticeTip.noticeinfo(actor, StringIdConst.TEXT354)
  217. return
  218. end
  219. local taskStar = myTaskPool[taskId]
  220. if taskStar == nil then
  221. noticeTip.noticeinfo(actor, StringIdConst.TEXT355)
  222. return
  223. end
  224. local record = GoldTask.BuildTaskRecord(taskId, taskStar)
  225. taskInfo.nowtask = record
  226. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  227. --通知客户端
  228. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  229. --jprint("接取黄金任务",taskInfo)
  230. end
  231. function GoldTask.BuildTaskRecord(taskId, taskStar)
  232. local record = this.GetNewTaskRecord()
  233. record.taskid = taskId
  234. record.star = taskStar
  235. return record
  236. end
  237. function GoldTask.UpdateTaskProgress(actor, monsterId)
  238. local success, errorInfo = xpcall(this.UpdateTaskProgress, debug.traceback, actor, monsterId)
  239. gameDebug.assertPrint(success, "怪物死亡更新黄金任务进度异常:", actor, monsterId, errorInfo)
  240. end
  241. ---更新任务进度
  242. function this.UpdateTaskProgress(actor, monsterId)
  243. if actor == nil then
  244. return
  245. end
  246. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  247. local myTask = taskInfo.nowtask
  248. if table.isNullOrEmpty(myTask) then
  249. return
  250. end
  251. local taskCfgList = ConfigDataManager.getTable("cfg_task_gold", "id", myTask.taskid)
  252. if table.isNullOrEmpty(taskCfgList) then
  253. return
  254. end
  255. local taskCfg = taskCfgList[1]
  256. local monsterCfg = tonumber(taskCfg["monsterid"])
  257. if monsterId ~= monsterCfg then
  258. return
  259. end
  260. if myTask.status > GoldTask.Const.TASK_STATUS.ACCEPT then
  261. return
  262. end
  263. local myProgress = myTask.progress
  264. local taskGoalNum = string.split(taskCfg["taskgoalnum"], "#")
  265. local needNum = tonumber(taskGoalNum[myTask.star])
  266. if myProgress + 1 >= needNum then
  267. myTask.status = GoldTask.Const.TASK_STATUS.FINISH
  268. end
  269. myTask.progress = myProgress + 1
  270. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  271. --通知客户端
  272. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  273. end
  274. ---放弃任务
  275. function GoldTask.GiveUpTask(actor, msgData)
  276. jprint("请求放弃黄金任务", msgData)
  277. local taskId = GoldTask.GetTaskId(msgData)
  278. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  279. local myTask = taskInfo.nowtask
  280. if table.isNullOrEmpty(myTask) or myTask.taskid ~= tonumber(taskId) then
  281. noticeTip.noticeinfo(actor, StringIdConst.TEXT355)
  282. return
  283. end
  284. if myTask.status > GoldTask.Const.TASK_STATUS.ACCEPT then
  285. tipinfo(actor, "任务已完成")
  286. return
  287. end
  288. taskInfo.nowtask = nil
  289. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  290. --通知客户端
  291. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  292. --jprint("放弃黄金任务",taskId,taskInfo)
  293. end
  294. ---提交任务
  295. function GoldTask.SubmitTask(actor, msgData)
  296. jprint("请求提交黄金任务", msgData)
  297. local taskId = GoldTask.GetTaskId(msgData)
  298. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  299. local myTask = taskInfo.nowtask
  300. if table.isNullOrEmpty(myTask) or myTask.taskid ~= taskId then
  301. noticeTip.noticeinfo(actor, StringIdConst.TEXT355)
  302. return
  303. end
  304. if myTask.status ~= GoldTask.Const.TASK_STATUS.FINISH then
  305. tipinfo(actor, "任务未完成")
  306. return
  307. end
  308. local taskStar = myTask.star
  309. local taskCfgList = ConfigDataManager.getTable("cfg_task_gold", "id", taskId)
  310. if not taskCfgList then
  311. return
  312. end
  313. local ret = GoldTask.GetTaskReward(taskStar, taskCfgList[1])
  314. local exp = ret.exp
  315. local itemBind = ConfigDataManager.getTableValue("cfg_bind", "bind", "id", 16)
  316. --发奖励
  317. if exp > 0 then
  318. -- additemtobag(actor, ItemConfigId.EXP, exp, itemBind, 9999, "黄金任务")
  319. -- 增加加成逻辑
  320. _, exp = Bag.addItemToBag(actor, ItemConfigId.EXP, exp, itemBind, 9999, "黄金任务")
  321. end
  322. local itemAward = ret.item
  323. for itemCfgId, count in pairs(itemAward) do
  324. Bag.addItemToBag(actor, itemCfgId, count, 0, 9999, "黄金任务")
  325. end
  326. --奖励面板
  327. if exp > 0 then
  328. itemAward[ItemConfigId.EXP] = exp
  329. end
  330. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, itemAward)
  331. --提交任务
  332. taskInfo.nowtask = nil
  333. myTask.status = GoldTask.Const.TASK_STATUS.SUBMIT
  334. table.insert(taskInfo.dailyrecord, myTask)
  335. this.SubmitFlushTaskPool(actor, taskInfo.nowtaskpool, taskId)
  336. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  337. --通知客户端
  338. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  339. jprint("提交黄金任务", taskId, taskInfo)
  340. --更新任务进度
  341. TaskHandler.TriggerTaskGoal(actor, TaskTargetType.FINISH_GOLD_TASK, taskId)
  342. end
  343. function this.SubmitFlushTaskPool(actor, nowTaskPool, submitTask)
  344. local playerLevel = tonumber(getbaseinfo(actor, "level"))
  345. local taskCfgList = ConfigDataManager.getList("cfg_task_gold")
  346. local weightMap = {}
  347. for _, taskCfg in pairs(taskCfgList) do
  348. local taskId = tonumber(taskCfg["id"])
  349. local pass = GoldTask.CheckLv(playerLevel, taskCfg["level"])
  350. local taskStar = nowTaskPool[taskId]
  351. if pass and taskStar == nil then
  352. weightMap[taskId] = tonumber(taskCfg["weight"])
  353. end
  354. end
  355. local randomTask = submitTask
  356. if not table.isNullOrEmpty(weightMap) then
  357. --随机一个新任务
  358. local randomTaskList = randombyweight(actor, weightMap, 1)
  359. if not table.isNullOrEmpty(randomTaskList) then
  360. randomTask = randomTaskList[1]
  361. end
  362. end
  363. --随机一个星级
  364. local star = this.RandomStar(randomTask, false)
  365. if star == 0 then
  366. return
  367. end
  368. nowTaskPool[submitTask] = nil
  369. nowTaskPool[randomTask] = star
  370. --jprint("黄金任务池",nowTaskPool)
  371. end
  372. function GoldTask.GetTaskReward(taskStar, taskCfg)
  373. local rewardCfg = ""
  374. local exp = 0
  375. local reward = {
  376. exp = exp,
  377. item = {}
  378. }
  379. if taskStar == GoldTask.Const.TASK_STAR.ONE then
  380. rewardCfg = taskCfg["rewarditem1"]
  381. exp = tonumber(taskCfg["rewardexpnum1"])
  382. end
  383. if taskStar == GoldTask.Const.TASK_STAR.TWO then
  384. rewardCfg = taskCfg["rewarditem2"]
  385. exp = tonumber(taskCfg["rewardexpnum2"])
  386. end
  387. if taskStar == GoldTask.Const.TASK_STAR.THREE then
  388. rewardCfg = taskCfg["rewarditem3"]
  389. exp = tonumber(taskCfg["rewardexpnum3"])
  390. end
  391. if exp ~= nil then
  392. reward.exp = exp
  393. end
  394. if string.isNullOrEmpty(rewardCfg) then
  395. return reward
  396. end
  397. for _, itemCfg in pairs(string.split(rewardCfg, "|")) do
  398. local itemCfgArray = string.split(itemCfg, "#")
  399. local itemId = tonumber(itemCfgArray[1])
  400. local itemNum = tonumber(itemCfgArray[2])
  401. reward.item[itemId] = itemNum
  402. end
  403. return reward
  404. end
  405. ---发送玩家黄金任务信息
  406. function GoldTask.SendTaskInfo(actor)
  407. --jprint("发送玩家黄金任务信息")
  408. local playerLevel = tonumber(getbaseinfo(actor, "level"))
  409. local levelLimit =
  410. tonumber(ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.GOLD_TASK_LEVEL_LIMIT))
  411. if playerLevel < levelLimit then
  412. return
  413. end
  414. ---检测重置
  415. GoldTask.TryResetTaskRecord(actor)
  416. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  417. if taskInfo.firstopen or table.isNullOrEmpty(taskInfo.nowtaskpool) then
  418. GoldTask.FirstFlushTaskPool(actor, playerLevel, taskInfo)
  419. end
  420. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  421. --jprint(taskInfo)
  422. end
  423. ---首次打开任务池
  424. function GoldTask.FirstFlushTaskPool(actor, playerLevel, taskInfo)
  425. local taskCfgList = ConfigDataManager.getList("cfg_task_gold")
  426. local taskList = GoldTask.RandomTask(actor, playerLevel, taskCfgList)
  427. if not taskList or #taskList == 0 then
  428. print("刷新数据异常, playerLevel: ", tostring(playerLevel), "配置空")
  429. return
  430. end
  431. taskInfo.nowtaskpool = GoldTask.BuildNewTaskPool(actor, taskList, false, taskInfo.firstopen)
  432. taskInfo.flushcount = 1
  433. taskInfo.firstopen = false
  434. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  435. end
  436. ---重置任务信息
  437. function GoldTask.TryResetTaskRecord(actor)
  438. local nowTime = tonumber((getbaseinfo("nowsec")))
  439. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  440. local lastResetTime = 0
  441. if taskInfo.lastresettime ~= nil and #taskInfo.lastresettime > 0 then
  442. lastResetTime = tonumber(taskInfo.lastresettime)
  443. end
  444. local sameDay = TimeUtil.isSameDay(lastResetTime, nowTime)
  445. if sameDay then
  446. return
  447. end
  448. local firstOpen = taskInfo.firstopen
  449. taskInfo = this.GetNewGoldTask()
  450. if not firstOpen then
  451. taskInfo.firstopen = false
  452. end
  453. taskInfo.lastresettime = tostring(nowTime)
  454. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  455. --通知客户端
  456. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  457. end
  458. function GoldTask.GetTaskId(msgData)
  459. if type(msgData) == "number" then
  460. return msgData
  461. end
  462. if msgData == nil or #msgData <= 2 then
  463. return 0
  464. end
  465. --移除首尾
  466. local taskId = string.sub(msgData, 2, -2)
  467. return tonumber(taskId)
  468. end
  469. ---完成任务GM
  470. function finishgoldtasktest(actor)
  471. local taskInfo = GoldTask.GetPayerTaskInfo(actor)
  472. local myTask = taskInfo.nowtask
  473. if not myTask or myTask == nil then
  474. return
  475. end
  476. local taskCfgList = ConfigDataManager.getTable("cfg_task_gold", "id", myTask.taskid)
  477. if table.isNullOrEmpty(taskCfgList) then
  478. return
  479. end
  480. local taskCfg = taskCfgList[1]
  481. if myTask.status > GoldTask.Const.TASK_STATUS.ACCEPT then
  482. return
  483. end
  484. local taskGoalNum = string.split(taskCfg["taskgoalnum"], "#")
  485. local needNum = tonumber(taskGoalNum[myTask.star])
  486. myTask.progress = needNum
  487. myTask.status = GoldTask.Const.TASK_STATUS.FINISH
  488. setplaydef(actor, GoldTask.Var.GOLD_TASK_INFO, taskInfo)
  489. --通知客户端
  490. sendluamsg(actor, LuaMessageIdToClient.RES_GOLD_TASK_INFO, taskInfo)
  491. end