CycleSevenDayTask.lua 14 KB

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