CycleSevenDayTask.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. -- 新商业化活动2 —— 7日任务
  2. -- 逻辑:每天任务虽然不同,但是只要在活动结束前完成就可获得奖励,同类型任务共享进度。完成任务数量达到一定程度,可以获得额外奖励。
  3. -- db
  4. --[=[
  5. human.db.absAct[id] =
  6. {
  7. taskList = {
  8. [taskId] = { --有进度的任务才存db
  9. progress = 0,
  10. isGet = nil,
  11. },
  12. },
  13. extraRewardRecord = {
  14. [1] = '1', -- 领取了的额外奖励才存db
  15. },
  16. }
  17. ]=]--
  18. local Msg = require("core.Msg")
  19. local Grid = require("bag.Grid")
  20. local Util = require("common.Util")
  21. local Lang = require("common.Lang")
  22. local BagLogic = require("bag.BagLogic")
  23. local ObjHuman = require("core.ObjHuman")
  24. local AbsActExcel = require("excel.absAct")
  25. local Broadcast = require("broadcast.Broadcast")
  26. local YunYingLogic = require("yunying.YunYingLogic")
  27. local TriggerLogic = require("trigger.TriggerLogic")
  28. local CycleActivityLogic = require("yunying.CycleActivity")
  29. local actVariableCfg = require("excel.commercializationActivity").variable[1]
  30. local sevenDayTaskCfg = require("excel.commercializationActivity").sevenDayTask
  31. -- 本活动日志标识
  32. local LOGTAG = "cycleAct_sevenDayTask"
  33. -- 活动ID
  34. local ACTID = 7301
  35. local function getActBaseInfo(human, actId)
  36. local state, endTime, startTime = CycleActivityLogic.isStarted(human, actId)
  37. return state, endTime, startTime
  38. end
  39. local function getActData(human, actId)
  40. return human.db.absAct[actId]
  41. end
  42. local function updateTaskData(human, actId, taskId, progress, isGet)
  43. local actData = getActData(human, actId)
  44. actData.taskList = actData.taskList or {}
  45. local taskList = actData.taskList
  46. taskList[taskId] = taskList[taskId] or {}
  47. if progress then
  48. taskList[taskId].progress = (taskList[taskId].progress or 0) + progress
  49. end
  50. if isGet then
  51. taskList[taskId].isGet = isGet
  52. end
  53. end
  54. local function updateExtraRewardData(human, actId, extraIdx)
  55. local actData = getActData(human, actId)
  56. actData.extraRewardRecord = actData.extraRewardRecord or {}
  57. actData.extraRewardRecord[extraIdx] = '1'
  58. end
  59. -- 计算当前是活动开启第几天
  60. local function getActOPenDayIdx(human)
  61. local state, _, startTime = getActBaseInfo(human, ACTID)
  62. if not state then
  63. return 0
  64. end
  65. local dayStartTime = Util.getDayStartTime(startTime)
  66. local now = os.time()
  67. return math.ceil((now - dayStartTime + 1) / 24 / 3600)
  68. end
  69. -- 更新红点
  70. local function updateRedDot(human)
  71. YunYingLogic.sendBanner(human)
  72. local otherConfig = AbsActExcel.absActivity[ACTID]
  73. YunYingLogic.sendGroupUpdate(YYInfo[ACTID], human, otherConfig.panelID)
  74. end
  75. -- 1 ~ 7天的红点标识数组
  76. local function getRedDotArray(human)
  77. local redDotArray = {0, 0, 0, 0, 0, 0, 0}
  78. local actOpenDayIdx = getActOPenDayIdx(human)
  79. if actOpenDayIdx <= 0 then
  80. return redDotArray
  81. end
  82. local actData = getActData(human, ACTID)
  83. local taskListData = actData and actData.taskList
  84. if not taskListData then
  85. return redDotArray
  86. end
  87. for taskId, taskData in pairs(taskListData) do
  88. local taskCfg = sevenDayTaskCfg[taskId]
  89. if (not taskData.isGet and taskData.progress >= taskCfg.condValue) and taskCfg.dayIndx <= actOpenDayIdx then
  90. redDotArray[taskCfg.dayIndx] = 1
  91. end
  92. end
  93. return redDotArray
  94. end
  95. -- 是否完成所有任务
  96. local function isCompleteAllTask(human, actId)
  97. local actData = getActData(human, actId)
  98. if not actData then
  99. return false
  100. end
  101. local taskListData = actData.taskList
  102. if not taskListData then
  103. return false
  104. end
  105. for taskId, taskCfg in ipairs(sevenDayTaskCfg) do
  106. if not taskListData[taskId] or taskListData[taskId].progress < taskCfg.condValue then
  107. return false
  108. end
  109. end
  110. return true
  111. end
  112. -- 统计已领取奖励的任务数量
  113. local function calcGetRewardTaskNum(human)
  114. local actData = getActData(human, ACTID)
  115. if not actData then
  116. return 0
  117. end
  118. local taskListData = actData.taskList
  119. if not taskListData then
  120. return 0
  121. end
  122. local cnt = 0
  123. for taskId, taskData in pairs(taskListData) do
  124. if taskData.isGet then
  125. cnt = cnt + 1
  126. end
  127. end
  128. return cnt
  129. end
  130. --订阅事件
  131. local function subscribeEvents(human, actId)
  132. local actData = getActData(human, actId)
  133. local taskList = actData.taskList or {}
  134. local registerTypeList = {}
  135. for taskId, taskCfg in ipairs(sevenDayTaskCfg) do
  136. if not registerTypeList[taskCfg.eventType] and (not taskList[taskId] or taskList[taskId].progress < taskCfg.condValue) then
  137. TriggerLogic.SubscribeEvent(taskCfg.eventType, human.db._id, EventCBFunc)
  138. registerTypeList[taskCfg.eventType] = 1
  139. end
  140. end
  141. end
  142. -- 领取任务奖励
  143. local function getTaskReward(human, dayIdx)
  144. local actOpenDayIdx = getActOPenDayIdx(human)
  145. if dayIdx <= 0 or dayIdx > actOpenDayIdx then
  146. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  147. end
  148. local actData = getActData(human, ACTID)
  149. local taskListData = actData.taskList
  150. if not taskListData then
  151. return Broadcast.sendErr(human, Lang.ABS_ANSWER_ITEM_IS_GET)
  152. end
  153. local len = 0
  154. local rewardList
  155. for taskId, taskData in pairs(taskListData) do
  156. local taskCfg = sevenDayTaskCfg[taskId]
  157. if taskCfg.dayIndx == dayIdx and taskData.progress >= taskCfg.condValue and not taskData.isGet then
  158. rewardList = rewardList or {}
  159. for _, itemCfg in ipairs(taskCfg.reward) do
  160. len = len + 1
  161. rewardList[len] = {itemCfg[1], itemCfg[2]}
  162. updateTaskData(human, ACTID, taskId, nil, true)
  163. end
  164. end
  165. end
  166. return rewardList
  167. end
  168. -- 领取额外奖励
  169. local function getExtraReward(human)
  170. local actData = getActData(human, ACTID)
  171. if not actData.taskList then
  172. return Broadcast.sendErr(human, Lang.ABS_ANSWER_ITEM_IS_GET)
  173. end
  174. local completeTaskCnt = calcGetRewardTaskNum(human)
  175. if completeTaskCnt <= 0 then
  176. return Broadcast.sendErr(human, Lang.ABS_ANSWER_ITEM_IS_GET)
  177. end
  178. local len = 0
  179. local rewardList
  180. local extraRewardRecord = actData.extraRewardRecord
  181. for idx, targetTaskCnt in ipairs(actVariableCfg.va1) do
  182. if targetTaskCnt > completeTaskCnt then
  183. break
  184. end
  185. if not extraRewardRecord or not extraRewardRecord[idx] then
  186. rewardList = rewardList or {}
  187. local itemArrCfg = actVariableCfg.va2[idx]
  188. for _,itemCfg in pairs(itemArrCfg) do
  189. len = len + 1
  190. rewardList[len] = {itemCfg[1], itemCfg[2]}
  191. end
  192. updateExtraRewardData(human, ACTID, idx)
  193. end
  194. end
  195. return rewardList
  196. end
  197. --事件处理函数
  198. function EventCBFunc(eventType, uuid, nValue1, nValue2)
  199. local human = ObjHuman.onlineUuid[uuid]
  200. if not human then
  201. return
  202. end
  203. local dayList
  204. local actOpenDayIdx = getActOPenDayIdx(human)
  205. local oldRedDotTag = isRed(human)
  206. local actData = getActData(human, ACTID)
  207. actData.taskList = actData.taskList or {}
  208. local taskList = actData.taskList
  209. for taskId, taskCfg in ipairs(sevenDayTaskCfg) do
  210. if taskCfg.eventType == eventType and (not taskList[taskId] or taskList[taskId].progress < taskCfg.condValue) then
  211. if taskCfg.condValueExtra <= 0 or (nValue2 and nValue2 >= taskCfg.condValueExtra) then
  212. updateTaskData(human, ACTID, taskId, nValue1)
  213. local dayIndx = taskCfg.dayIndx
  214. if dayIndx <= actOpenDayIdx and (not dayList or not dayList[dayIndx]) then
  215. dayList = dayList or {}
  216. dayList[dayIndx] = true
  217. end
  218. end
  219. end
  220. end
  221. --推数据给客户端更新
  222. if dayList then
  223. for dayIdx in pairs(dayList) do
  224. Query(human, dayIdx)
  225. end
  226. if oldRedDotTag ~= isRed(human) then
  227. updateRedDot(human)
  228. end
  229. end
  230. end
  231. -- 红点检测
  232. function isRed(human, YYInfo, funcConfig)
  233. local actData = getActData(human, ACTID)
  234. if not actData or not actData.taskList then
  235. return false
  236. end
  237. local completeTaskNum = 0
  238. local taskListData = actData.taskList
  239. local actOpenDayIdx = getActOPenDayIdx(human)
  240. for taskId, taskData in pairs(taskListData) do
  241. local taskCfg = sevenDayTaskCfg[taskId]
  242. if taskCfg.condValue <= taskData.progress then
  243. if not taskData.isGet and actOpenDayIdx >= taskCfg.dayIndx then
  244. return true
  245. end
  246. if taskData.isGet then
  247. completeTaskNum = completeTaskNum + 1
  248. end
  249. end
  250. end
  251. local extraRewardRecord = actData.extraRewardRecord
  252. if completeTaskNum > 0 then
  253. local taskNumArr = actVariableCfg.va1
  254. for idx, needTaskNum in ipairs(taskNumArr) do
  255. if needTaskNum > completeTaskNum then
  256. break
  257. end
  258. if not extraRewardRecord or not extraRewardRecord[idx] then
  259. return true
  260. end
  261. end
  262. end
  263. return false
  264. end
  265. function isOpen(human, YYInfo, funcConfig)
  266. local state, endTime, startTime = getActBaseInfo(human, ACTID)
  267. if not state then return end
  268. return true, endTime, startTime
  269. end
  270. function isActive(human, YYInfo, funcConfig)
  271. return not isOpen(human, YYInfo, funcConfig)
  272. end
  273. function onLogin(human, funcID)
  274. if not isOpen(human) then
  275. return
  276. end
  277. if isCompleteAllTask(human, funcID) then
  278. return
  279. end
  280. subscribeEvents(human, funcID)
  281. end
  282. function updateDaily(human, funcID)
  283. if not isOpen(human) then
  284. return
  285. end
  286. local actData = getActData(human, ACTID)
  287. if not actData then
  288. return
  289. end
  290. local actOpenDayIdx = getActOPenDayIdx(human)
  291. Query(human, actOpenDayIdx)
  292. end
  293. function Query(human, dayIdx)
  294. if not isOpen(human) then
  295. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  296. end
  297. local actOpenDayIdx = getActOPenDayIdx(human)
  298. if dayIdx <= 0 or dayIdx > actOpenDayIdx then
  299. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  300. end
  301. local actData = getActData(human, ACTID)
  302. local completeTaskCnt = calcGetRewardTaskNum(human)
  303. local msgRet = Msg.gc.GC_CYCLESEVENDAYTASK_QUERY
  304. msgRet.dayIdx = dayIdx
  305. msgRet.completeTaskNum = completeTaskCnt
  306. local len = 0
  307. local taskList = msgRet.taskList
  308. local taskListData = actData.taskList
  309. for taskIdx, taskCfg in ipairs(sevenDayTaskCfg) do
  310. if dayIdx == taskCfg.dayIndx then
  311. len = len + 1
  312. taskList[len].taskIdx = taskIdx
  313. taskList[len].taskDesc = taskCfg.taskDesc
  314. taskList[len].taskState = 0
  315. taskList[len].taskNowProgress = 0
  316. taskList[len].taskCondProgress = taskCfg.condValue
  317. if taskListData and taskListData[taskIdx] then
  318. taskList[len].taskNowProgress = taskListData[taskIdx].progress
  319. if taskListData[taskIdx].progress >= taskCfg.condValue then
  320. if taskListData[taskIdx].isGet then
  321. taskList[len].taskState = 2
  322. else
  323. taskList[len].taskState = 1
  324. end
  325. end
  326. end
  327. local taskRewardList = taskList[len].taskRewardList
  328. for idx, itemCfg in ipairs(taskCfg.reward) do
  329. taskRewardList[0] = idx
  330. Grid.makeItem(taskRewardList[idx], itemCfg[1], itemCfg[2])
  331. end
  332. end
  333. end
  334. taskList[0] = len
  335. -- 额外奖励
  336. local extraRewardList = msgRet.extraRewardList
  337. local extraRewardRecord = actData.extraRewardRecord
  338. for idx, targetTaskCnt in ipairs(actVariableCfg.va1) do
  339. extraRewardList[0] = idx
  340. extraRewardList[idx].condProgress = targetTaskCnt
  341. extraRewardList[idx].state = 0
  342. if completeTaskCnt >= targetTaskCnt then
  343. if extraRewardRecord and extraRewardRecord[idx] then
  344. extraRewardList[idx].state = 2
  345. else
  346. extraRewardList[idx].state = 1
  347. end
  348. end
  349. local itemCfg = actVariableCfg.va2[idx]
  350. Grid.makeItem(extraRewardList[idx].itemInfo, itemCfg[1][1], itemCfg[1][2])
  351. end
  352. local redDotArray = getRedDotArray(human)
  353. for idx, redTag in ipairs(redDotArray or {}) do
  354. msgRet.redDotArray[0] = idx
  355. msgRet.redDotArray[idx] = redTag
  356. end
  357. msgRet.openDay = getActOPenDayIdx(human)
  358. Msg.send(msgRet, human.fd)
  359. human.sevenDayTaskDayIdx = dayIdx
  360. end
  361. function GetReward(human, rewardType, idx)
  362. if not isOpen(human) then
  363. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  364. end
  365. local rewardList
  366. if rewardType == 1 then
  367. rewardList = getTaskReward(human, idx)
  368. elseif rewardType == 2 then
  369. rewardList = getExtraReward(human)
  370. else
  371. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  372. end
  373. if not rewardList then
  374. return Broadcast.sendErr(human, Lang.ABS_ANSWER_ITEM_IS_GET)
  375. end
  376. BagLogic.addItemList(human, rewardList, LOGTAG)
  377. Query(human, human.sevenDayTaskDayIdx)
  378. if not isRed(human) then
  379. updateRedDot(human)
  380. end
  381. end