CycleSevenDayTask.lua 14 KB

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