WingsTask.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. Wings = {}
  2. local this = {}
  3. ---章节状态
  4. local ChapterState = {
  5. ---已解锁
  6. ACCEPT = 1,
  7. ---已完成
  8. FINISH = 2,
  9. ---已领取
  10. REWARD = 3
  11. }
  12. local WingsTask = "T$wings_task"
  13. --全部章节完成
  14. local ALL_FINISH = -1
  15. function this.GetNewWingsTask()
  16. local WingsTaskInfo = {
  17. ---当前章节
  18. now_chapter = 0,
  19. ---章节任务信息
  20. chapter_task_info = {}
  21. }
  22. return WingsTaskInfo
  23. end
  24. function Wings.Login(actor)
  25. Wings.TryInitWingsTask(actor)
  26. Wings.SendWingsTask(actor)
  27. end
  28. function Wings.TryInitWingsTask(actor)
  29. if this.InitNewChapter(actor) then
  30. Wings.SendWingsTask(actor)
  31. end
  32. end
  33. function this.SaveWingsTask(actor, wingsTaskInfo)
  34. setplaydef(actor, WingsTask, wingsTaskInfo)
  35. end
  36. function this.GetWingsTask(actor)
  37. local wingsTaskInfo = getplaydef(actor, WingsTask)
  38. return wingsTaskInfo or this.GetNewWingsTask()
  39. end
  40. function this.CheckAllFinish(wingsTask)
  41. local nextChapter = wingsTask.now_chapter + 1
  42. local chapterCfgList = ConfigDataManager.getTable("cfg_wings", "groupid", nextChapter)
  43. if table.isNullOrEmpty(chapterCfgList) then
  44. local nowChapter = wingsTask.chapter_task_info[wingsTask.now_chapter] or {}
  45. local state = nowChapter.state or 0
  46. return state == ChapterState.REWARD
  47. end
  48. return false
  49. end
  50. function Wings.SendWingsTask(actor)
  51. local wingsTask = this.GetWingsTask(actor)
  52. if this.CheckAllFinish(wingsTask) then
  53. wingsTask.now_chapter = ALL_FINISH
  54. end
  55. sendluamsg(actor, LuaMessageIdToClient.RES_WINGS_TASK_INFO, wingsTask)
  56. end
  57. function this.WingsChange(actor, changeTask, chapter, chapterStatus)
  58. local chapterTask = {
  59. now_chapter = chapter,
  60. chapter_state = chapterStatus,
  61. change_task = changeTask
  62. }
  63. sendluamsg(actor, LuaMessageIdToClient.RES_WINGS_TASK_CHANGE, chapterTask)
  64. end
  65. function this.InitNewChapter(actor)
  66. local wingsTask = this.GetWingsTask(actor)
  67. local chapterTaskInfo = wingsTask.chapter_task_info
  68. local nowChapterTask = chapterTaskInfo[wingsTask.now_chapter]
  69. -- lg("初始化章节任务:", wingsTask)
  70. if not table.isNullOrEmpty(nowChapterTask) and nowChapterTask.state ~= ChapterState.REWARD then
  71. return false
  72. end
  73. local nextChapter = wingsTask.now_chapter + 1
  74. local chapterCfgList = ConfigDataManager.getTable("cfg_wings", "groupid", nextChapter)
  75. -- lg("章节任务配置:", chapterCfgList,nextChapter)
  76. if table.isNullOrEmpty(chapterCfgList) then
  77. return false
  78. end
  79. local nextChapterTask = this.InitChapterTask(actor, nextChapter)
  80. -- lg("章节任务:", nextChapterTask)
  81. if table.isNullOrEmpty(nextChapterTask) then
  82. return false
  83. end
  84. chapterTaskInfo[nextChapter] = nextChapterTask
  85. wingsTask.now_chapter = nextChapter
  86. wingsTask.chapter_task_info = chapterTaskInfo
  87. this.SaveWingsTask(actor, wingsTask)
  88. return true
  89. end
  90. ---初始化章节任务池
  91. function this.InitChapterTask(actor, group)
  92. local taskCfgList = ConfigDataManager.getTable("cfg_wingstask", "group", group)
  93. if table.isNullOrEmpty(taskCfgList) then
  94. return nil
  95. end
  96. local chapterTask = {
  97. state = ChapterState.ACCEPT,
  98. task_info = {}
  99. }
  100. for _, taskCfg in pairs(taskCfgList) do
  101. -- lg("章节任务配置:", taskCfg)
  102. local taskId = tonumber(taskCfg["id"])
  103. local taskTargetId = tonumber(taskCfg["tasktargetid"])
  104. local careerLimit = taskCfg["careerlimit"]
  105. if this.CheckCareer(actor, careerLimit) then
  106. local taskRecord = TaskHandler.BuildTaskRecord(actor, taskId, taskTargetId)
  107. if taskRecord ~= nil then
  108. chapterTask.task_info[taskId] = taskRecord
  109. end
  110. end
  111. end
  112. return next(chapterTask.task_info) ~= nil and chapterTask or nil
  113. end
  114. function this.CheckCareer(actor, careerLimit)
  115. if not careerLimit or #careerLimit == 0 or (tonumber(careerLimit) == 0) then
  116. return true
  117. end
  118. local baseCareer = tonumber(getbaseinfo(actor, "getbasecareer"))
  119. return baseCareer == tonumber(careerLimit)
  120. end
  121. function Wings.FlushWingsTask(actor, taskTargetType, param)
  122. local success, errorInfo = xpcall(this.FlushWingsTask, debug.traceback, actor, taskTargetType, param)
  123. gameDebug.assertPrint(success, "翅膀任务目标异常:", actor, taskTargetType, param, errorInfo)
  124. end
  125. ---刷新任务
  126. function this.FlushWingsTask(actor, taskTargetType, param)
  127. -- lg("刷新任务:", taskTargetType, param)
  128. local wingsTask = this.GetWingsTask(actor)
  129. -- lg("刷新任务:",wingsTask)
  130. local nowChapter = wingsTask.now_chapter
  131. if nowChapter == 0 then
  132. return
  133. end
  134. local chapterTaskInfo = wingsTask.chapter_task_info
  135. if table.isNullOrEmpty(chapterTaskInfo) then
  136. return
  137. end
  138. -- 当前章节
  139. local chapterTask = chapterTaskInfo[nowChapter]
  140. if table.isNullOrEmpty(chapterTask) or chapterTask.state >= ChapterState.FINISH then
  141. return
  142. end
  143. local taskList = chapterTask.task_info
  144. local changeTask = this.CheckTaskRecord(actor, taskList, taskTargetType, param)
  145. -- lg("刷新任务:", changeTask)
  146. if table.isNullOrEmpty(changeTask) then
  147. return
  148. end
  149. chapterTask.task_info = taskList
  150. chapterTaskInfo[nowChapter] = chapterTask
  151. wingsTask.chapter_task_info = chapterTaskInfo
  152. this.SaveWingsTask(actor, wingsTask)
  153. this.WingsChange(actor, changeTask, nowChapter, chapterTask.state)
  154. end
  155. ---检查任务记录
  156. function this.CheckTaskRecord(actor, taskList, taskTargetType, param)
  157. local changeTask = {}
  158. for taskId, taskRecord in pairs(taskList) do
  159. local taskGoal = taskRecord.task_goal
  160. local target_id = taskRecord.target_id
  161. local goal_count = taskRecord.goal_count
  162. local needCount = taskRecord.max_count
  163. if taskTargetType == taskGoal and taskRecord.state == TaskHandler.Status.ACCEPT then
  164. local count = TaskGoal.FlushTaskCount(actor, taskGoal, target_id, goal_count, param)
  165. if count >= needCount then
  166. taskRecord.state = TaskHandler.Status.FINISH
  167. end
  168. if count > goal_count then
  169. taskRecord.goal_count = math.min(count, needCount)
  170. changeTask[taskId] = taskRecord
  171. end
  172. end
  173. end
  174. return changeTask
  175. end
  176. ---提交任务
  177. function Wings.SubmitChapterTask(actor, taskId)
  178. -- lg("提交任务:", taskId)
  179. local wingsTask = this.GetWingsTask(actor)
  180. local nowChapter = wingsTask.now_chapter
  181. local chapterTaskInfo = wingsTask.chapter_task_info
  182. if table.isNullOrEmpty(chapterTaskInfo) then
  183. noticeTip.noticeinfo(actor, StringIdConst.TEXT405)
  184. return
  185. end
  186. local chapterTask = chapterTaskInfo[nowChapter]
  187. if chapterTask.state >= ChapterState.FINISH then
  188. tipinfo(actor, "任务已提交")
  189. return
  190. end
  191. local taskList = chapterTask.task_info
  192. local taskRecord = taskList[taskId]
  193. if taskRecord == nil then
  194. noticeTip.noticeinfo(actor, StringIdConst.TEXT355)
  195. return
  196. end
  197. if taskRecord.state == TaskHandler.Status.SUBMIT then
  198. tipinfo(actor, "任务已提交")
  199. return
  200. end
  201. if taskRecord.state == TaskHandler.Status.ACCEPT then
  202. tipinfo(actor, "任务未完成")
  203. return
  204. end
  205. --发奖励
  206. local rewardStr = ConfigDataManager.getTableValue("cfg_wingstask", "rewarditem", "id", taskRecord.task_id)
  207. this.SendTaskReward(actor, rewardStr)
  208. taskList[taskId].state = TaskHandler.Status.SUBMIT
  209. chapterTask.task_info = taskList
  210. --检查章节完成
  211. local chapterFinish = this.CheckChapterFinish(taskList)
  212. if chapterFinish then
  213. chapterTask.state = ChapterState.FINISH
  214. end
  215. chapterTaskInfo[nowChapter] = chapterTask
  216. wingsTask.chapter_task_info = chapterTaskInfo
  217. this.SaveWingsTask(actor, wingsTask)
  218. local changeTask = {}
  219. changeTask[taskId] = taskRecord
  220. this.WingsChange(actor, changeTask, nowChapter, chapterTask.state)
  221. end
  222. function this.SendTaskReward(actor, rewarStr)
  223. if string.isNullOrEmpty(rewarStr) then
  224. return
  225. end
  226. Bag.sendRewards4StringByBind(actor, rewarStr,"3","翅膀任务")
  227. -- local awardMap = {}
  228. -- string.putIntIntMap(awardMap, rewarStr, "#", "|")
  229. -- for k, v in pairs(awardMap) do
  230. -- local itemBing = ConfigDataManager.getTableValue("cfg_bind", "bind", "id", 3)
  231. -- additemtobag(actor, k, v, itemBing)
  232. -- end
  233. -- --奖励弹框
  234. -- sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, awardMap)
  235. end
  236. function this.CheckChapterFinish(taskList)
  237. for _, taskRecord in pairs(taskList) do
  238. if taskRecord.state ~= TaskHandler.Status.SUBMIT then
  239. return false
  240. end
  241. end
  242. return true
  243. end
  244. function Wings.ReceiveChapterAward(actor)
  245. -- lg("领取章节奖励")
  246. local wingsTask = this.GetWingsTask(actor)
  247. local nowChapter = wingsTask.now_chapter
  248. if nowChapter == 0 then
  249. tipinfo(actor, "未解锁")
  250. return
  251. end
  252. local chapterTaskInfo = wingsTask.chapter_task_info
  253. local chapterTask = chapterTaskInfo[nowChapter]
  254. -- lg("领取奖励:",chapterTask)
  255. if table.isNullOrEmpty(chapterTask) or chapterTask.state ~= ChapterState.FINISH then
  256. tipinfo(actor, "无奖励可领取")
  257. return
  258. end
  259. --发奖励
  260. local rewardStr = ConfigDataManager.getTableValue("cfg_wings", "rewarditem", "groupid", nowChapter)
  261. this.SendTaskReward(actor, rewardStr)
  262. chapterTask.state = ChapterState.REWARD
  263. chapterTaskInfo[nowChapter] = chapterTask
  264. wingsTask.chapter_task_info = chapterTaskInfo
  265. this.SaveWingsTask(actor, wingsTask)
  266. --尝试初始化下一章
  267. this.InitNewChapter(actor)
  268. Wings.SendWingsTask(actor)
  269. end
  270. --GM一键完成章节所有任务
  271. function finishchapteralltask(actor)
  272. local wingsTask = this.GetWingsTask(actor)
  273. local nowChapter = wingsTask.now_chapter
  274. if nowChapter == 0 then
  275. return
  276. end
  277. local chapterTaskInfo = wingsTask.chapter_task_info
  278. local chapterTask = chapterTaskInfo[nowChapter]
  279. if chapterTask.state >= ChapterState.FINISH then
  280. return
  281. end
  282. local taskInfo = chapterTask.task_info
  283. for _, taskRecord in pairs(taskInfo) do
  284. if taskRecord.state == TaskHandler.Status.ACCEPT then
  285. taskRecord.goal_count = taskRecord.max_count
  286. taskRecord.state = TaskHandler.Status.FINISH
  287. end
  288. end
  289. chapterTask.task_info = taskInfo
  290. chapterTaskInfo[nowChapter] = chapterTask
  291. wingsTask.chapter_task_info = chapterTaskInfo
  292. this.SaveWingsTask(actor, wingsTask)
  293. Wings.SendWingsTask(actor)
  294. end