GoldTask.lua 16 KB

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