CommonActLoginGift.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. -- 通用节日活动 - 登录豪礼
  2. -- db
  3. --[=[
  4. human.db.absAct[actId] = {
  5. getRecordData = {
  6. [gitftType] = {
  7. [day1] = true,
  8. [day3] = true,
  9. },
  10. },
  11. buyGiftType = nil, -- 已经购买的礼包类型
  12. }
  13. ]=]--
  14. local Msg = require("core.Msg")
  15. local Grid = require("bag.Grid")
  16. local BagLogic = require("bag.BagLogic")
  17. local AbsActLogic = require("absAct.AbsActLogic")
  18. local Broadcast = require("broadcast.Broadcast")
  19. local Lang = require("common.Lang")
  20. local LoginGiftConfig = require("excel.commonact").loginreward
  21. local CommonDefine = require("common.CommonDefine")
  22. local Util = require("common.Util")
  23. local YunYingLogic = require("yunying.YunYingLogic")
  24. local AbsActExcel = require("excel.absAct")
  25. local BuyLogic = require("topup.BuyLogic")
  26. local LOGTYPE = "CommonActLoginGift"
  27. local COMMONACT_LOGINGIFT_ID = 7505
  28. local function getActData(human)
  29. return human.db.absAct[COMMONACT_LOGINGIFT_ID]
  30. end
  31. local function updateGetRecordData(human, giftType, dayIdx)
  32. human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData = human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData or {}
  33. human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData[giftType] = human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData[giftType] or {}
  34. human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData[giftType][dayIdx] = true
  35. end
  36. local function updateBuyGiftType(human, giftType)
  37. human.db.absAct[COMMONACT_LOGINGIFT_ID].buyGiftType = giftType
  38. end
  39. local function isOpenAct(human, funcID)
  40. return AbsActLogic.isStarted(human, funcID)
  41. end
  42. local function transformCfg()
  43. local tb = {}
  44. for _, giftCfg in ipairs(LoginGiftConfig) do
  45. if not tb[giftCfg.giftType] then
  46. tb[giftCfg.giftType] = {}
  47. end
  48. local t = tb[giftCfg.giftType]
  49. t[#t+1] = giftCfg
  50. end
  51. return tb
  52. end
  53. local function getAwardState(human, giftType, dayIdx)
  54. local actData = getActData(human)
  55. local diffDays = Util.diffDay(actData.realStartTime) + 1
  56. local getRecordData = actData and actData.getRecordData or {}
  57. local nowGiftTypeGetRecord = getRecordData[giftType] or {}
  58. local buyGiftType = actData and actData.buyGiftType
  59. local isBuy = true
  60. local state = CommonDefine.COMMON_PRIZE_STATE_CANGET
  61. if giftType ~= 1 and (not buyGiftType or buyGiftType ~= giftType) then -- 需要充值的礼包类型
  62. state = CommonDefine.COMMON_PRIZE_STATE_NOGET
  63. isBuy = false
  64. end
  65. if isBuy then
  66. if diffDays < dayIdx then
  67. state = CommonDefine.COMMON_PRIZE_STATE_NOGET
  68. else
  69. if nowGiftTypeGetRecord[dayIdx] then
  70. state = CommonDefine.COMMON_PRIZE_STATE_GET
  71. end
  72. end
  73. end
  74. return state
  75. end
  76. -- 检查是否有可领取的奖励
  77. local function isCanGet(human)
  78. local actData = getActData(human)
  79. if not actData then
  80. return false
  81. end
  82. local type_2_cfg = transformCfg()
  83. for giftType, giftCfgArr in pairs(type_2_cfg) do
  84. for _, giftCfg in ipairs(giftCfgArr) do
  85. local state = getAwardState(human, giftType, giftCfg.dayIdx)
  86. if state == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  87. return true
  88. end
  89. end
  90. end
  91. return false
  92. end
  93. -- 填充协议结构
  94. local function populateGiftMsg(human, net, giftCfg, nowGiftType)
  95. net.reward[0] = #giftCfg.reward
  96. for i, itemInfo in ipairs(giftCfg.reward) do
  97. Grid.makeItem(net.reward[i], itemInfo[1], itemInfo[2])
  98. end
  99. net.reallyDays = giftCfg.dayIdx
  100. net.rewardState = getAwardState(human, nowGiftType, giftCfg.dayIdx)
  101. BuyLogic.fontBuyItem(human, net.buyMsg, giftCfg.buyId)
  102. net.giftType = nowGiftType
  103. end
  104. -- 主动刷新一次红点
  105. local function updateRedDot(human)
  106. YunYingLogic.sendBanner(human)
  107. local config = AbsActExcel.absActivity[COMMONACT_LOGINGIFT_ID]
  108. YunYingLogic.sendGroupUpdate(YYInfo[COMMONACT_LOGINGIFT_ID], human, config.panelID)
  109. end
  110. function isRed(human, YYInfo, funcConfig)
  111. local state = isOpenAct(human, funcConfig and funcConfig.funcID)
  112. if not state then
  113. return false
  114. end
  115. return isCanGet(human)
  116. end
  117. function isOpen(human, YYInfo, funcConfig)
  118. return isOpenAct(human, funcConfig and funcConfig.funcID)
  119. end
  120. function isActive(human, YYInfo, funcConfig)
  121. return not isOpen(human, YYInfo, funcConfig)
  122. end
  123. function onCharge(human, nBuyID, buyNum)
  124. if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
  125. return
  126. end
  127. local actData = getActData(human)
  128. local buyGiftType = actData and actData.buyGiftType
  129. if buyGiftType then
  130. return
  131. end
  132. local type_2_cfg = transformCfg()
  133. for giftType, giftCfgArr in pairs(type_2_cfg) do
  134. local singleCfg = giftCfgArr[1]
  135. if singleCfg.buyId == nBuyID then
  136. updateBuyGiftType(human, giftType)
  137. updateRedDot(human)
  138. CommonActLoginGift_Query(human)
  139. break
  140. end
  141. end
  142. end
  143. function updateDaily(human, funcID)
  144. if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
  145. return
  146. end
  147. updateRedDot(human)
  148. end
  149. -- 查询
  150. function CommonActLoginGift_Query(human)
  151. if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
  152. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  153. end
  154. local msgRet = Msg.gc.GC_ABS_COMONACT_LOGINGIFT_QUERY
  155. msgRet.giftDatafree[0] = 0
  156. msgRet.giftData68[0] = 0
  157. msgRet.giftData128[0] = 0
  158. msgRet.giftData328[0] = 0
  159. msgRet.giftData648[0] = 0
  160. msgRet.isEnd = 0
  161. msgRet.isStart = 1
  162. msgRet.buyGiftType = 0
  163. local actData = getActData(human)
  164. msgRet.buyGiftType = actData and actData.buyGiftType or 0
  165. local len, msgOnceLen = 0, 5
  166. local type_2_cfg = transformCfg()
  167. local configNum = type_2_cfg[1] and #type_2_cfg[1] or 0
  168. local num = configNum
  169. for i=1, configNum do
  170. len = len + 1
  171. if type_2_cfg[1] then
  172. msgRet.giftDatafree[0] = len
  173. populateGiftMsg(human, msgRet.giftDatafree[len], type_2_cfg[1][i], 1)
  174. end
  175. if type_2_cfg[2] and type_2_cfg[2][i] then
  176. msgRet.giftData68[0] = len
  177. populateGiftMsg(human, msgRet.giftData68[len], type_2_cfg[2][i], 2)
  178. end
  179. if type_2_cfg[3] and type_2_cfg[3][i] then
  180. msgRet.giftData128[0] = len
  181. populateGiftMsg(human, msgRet.giftData128[len], type_2_cfg[3][i], 3)
  182. end
  183. if type_2_cfg[4] and type_2_cfg[4][i] then
  184. msgRet.giftData328[0] = len
  185. populateGiftMsg(human, msgRet.giftData328[len], type_2_cfg[4][i], 4)
  186. end
  187. if type_2_cfg[5] and type_2_cfg[5][i] then
  188. msgRet.giftData648[0] = len
  189. populateGiftMsg(human, msgRet.giftData648[len], type_2_cfg[5][i], 5)
  190. end
  191. if len >= msgOnceLen then
  192. num = num - len
  193. if num <= 0 then
  194. msgRet.isEnd = 1
  195. return Msg.send(msgRet, human.fd)
  196. end
  197. Msg.send(msgRet, human.fd)
  198. len = 0
  199. msgRet.isStart = 0
  200. end
  201. end
  202. if len > 0 then
  203. msgRet.isEnd = 1
  204. Msg.send(msgRet, human.fd)
  205. end
  206. end
  207. -- 领取奖励
  208. function CommonActLoginGift_GetReward(human, targetGiftType)
  209. if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
  210. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  211. end
  212. local type_2_cfg = transformCfg()
  213. local giftArr = type_2_cfg[targetGiftType]
  214. if not giftArr then
  215. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  216. end
  217. local actData = getActData(human)
  218. local buyGiftType = actData and actData.buyGiftType
  219. if targetGiftType ~= 1 and (not buyGiftType or buyGiftType ~= targetGiftType) then
  220. return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
  221. end
  222. local itemArr = {}
  223. for _, giftCfg in ipairs(giftArr) do
  224. local state = getAwardState(human, targetGiftType, giftCfg.dayIdx)
  225. if state == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  226. for _, itemInfo in ipairs(giftCfg.reward) do
  227. itemArr[#itemArr+1] = { itemInfo[1], itemInfo[2] }
  228. end
  229. -- 记录领取
  230. updateGetRecordData(human, targetGiftType, giftCfg.dayIdx)
  231. end
  232. end
  233. if #itemArr == 0 then
  234. return Broadcast.sendErr(human, Lang.HO_NO_CAN_GET)
  235. end
  236. -- 发放道具
  237. BagLogic.addItemList(human, itemArr, LOGTYPE)
  238. -- 更红点
  239. updateRedDot(human)
  240. -- 推送最新数给客户端
  241. CommonActLoginGift_Query(human)
  242. end