LimitGiftBagLogic.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. --- 限时礼包
  2. --[[
  3. db.limitGiftBag = {
  4. actTime = {} -- 激活方式 的激活时间 [1] = time
  5. actNum = {} -- 激活方式 的激活次数 [1] = 10
  6. bagBuy = {} -- 礼包购买状态 [1] = 1 重新激活时 清除
  7. }
  8. ]]
  9. local Msg = require("core.Msg")
  10. local BagLogic = require("bag.BagLogic")
  11. local Grid = require("bag.Grid")
  12. local BuyLogic = require("topup.BuyLogic")
  13. local LimitGiftBagExcel = require("excel.limitBuy").limitGiftBag
  14. LIMITGIFTBAG_TYPE_1 = 1 -- 获得X星英雄
  15. LIMITGIFTBAG_TYPE_2 = 2 -- 达到X级
  16. LIMITGIFTBAG_TYPE_3 = 3 -- 爬塔X层
  17. -- 激活方式 (同种激活方式激活 一起激活 策划需求 这里定义是为了防止 策划忘记 )
  18. local LIMITGIFTBAG_ACT_TYPE_1 = 1 -- 获得5X英雄
  19. local LIMITGIFTBAG_ACT_TYPE_2 = 2 -- 获得6X英雄
  20. local LIMITGIFTBAG_ACT_TYPE_3 = 3 -- 达到20级
  21. local LIMITGIFTBAG_ACT_TYPE_4 = 4 -- 达到30级
  22. local LIMITGIFTBAG_ACT_TYPE_5 = 5 -- 爬塔100层
  23. function inintDB(human)
  24. if not human.db.limitGiftBag then
  25. human.db.limitGiftBag = {}
  26. human.db.limitGiftBag.actTime = {}
  27. human.db.limitGiftBag.actNum = {}
  28. human.db.limitGiftBag.bagBuy = {}
  29. end
  30. end
  31. local PERSON_LIMITGIFTBAG_MAX_TIME = nil -- actType 最大持续时间
  32. local PERSON_LIMITGIFTBAG_ID_ACTTYPE = nil -- ID 所对应的 actType
  33. local PERSON_LIMITGIFTBAG_ACTTPE_ID = nil -- 同激活方式的 ID
  34. local function updateExcel()
  35. if PERSON_LIMITGIFTBAG_MAX_TIME then return end
  36. PERSON_LIMITGIFTBAG_MAX_TIME = {}
  37. PERSON_LIMITGIFTBAG_ID_ACTTYPE = {}
  38. PERSON_LIMITGIFTBAG_ACTTPE_ID = {}
  39. for k, v in ipairs(LimitGiftBagExcel) do
  40. PERSON_LIMITGIFTBAG_ID_ACTTYPE[k] = v.actType
  41. PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType] = PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType] or {}
  42. PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType][#PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType] + 1] = k
  43. if not PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] then
  44. PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] = v.endTime
  45. elseif PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] and PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] < v.endTime then
  46. PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] = v.endTime
  47. end
  48. end
  49. end
  50. -- 获取最老结束时间
  51. local function getMaxEndTime(actType)
  52. updateExcel()
  53. return PERSON_LIMITGIFTBAG_MAX_TIME[actType]
  54. end
  55. -- 获取该激活类型 是否已经激活
  56. local function getActTypeState(human, actType, now)
  57. now = now or os.time()
  58. local maxEndTime = getMaxEndTime(actType)
  59. if maxEndTime <= 0 then return assert(nil, "actType endTime is not find : " .. actType .. " .") end
  60. local lastTime = human.db.actTime[actType] or 0
  61. return now < lastTime + maxEndTime or false
  62. end
  63. local function getIdState(time, id, now)
  64. local config = LimitGiftBagExcel[id]
  65. local endTime = time + config.endTime
  66. return endTime - now > 0 and true or false
  67. end
  68. -- 清理掉上次 激活时 购买次数
  69. local function cleanGiftBagBuy(human, actType)
  70. for _, id in ipairs(PERSON_LIMITGIFTBAG_ACTTPE_ID[actType]) do
  71. human.db.limitGiftBag.bagBuy[id] = nil
  72. end
  73. end
  74. -- 获取 限时礼包是否能购买
  75. local function getGiftBagBuyState(human, config)
  76. if not human.db.limitGiftBag.bagBuy[config.id] then return true end
  77. if config.canBuy > 0 and human.db.limitGiftBag.bagBuy[config.id] >= config.canBuy then
  78. return false
  79. end
  80. return true
  81. end
  82. function fontLimitBagNet(human, msgRet, id, actTime, now)
  83. local config = LimitGiftBagExcel[id]
  84. local state = getIdState(actTime, id, os.time())
  85. if not state then return end
  86. msgRet.list[0] = msgRet.list[0] + 1
  87. local net = msgRet.list[msgRet.list[0]]
  88. net.id = id
  89. net.name = config.name
  90. net.buy = human.db.limitGiftBag.bagBuy[id] and human.db.limitGiftBag.bagBuy[id] or 0
  91. net.maxBuy = config.canBuy
  92. net.endTime = actTime + config.endTime - now
  93. net.endTime = net.endTime > 0 and net.endTime or 0
  94. BuyLogic.fontBuyItem(human,net.buyItem, config.buyID)
  95. for k, v in ipairs(config.item) do
  96. Grid.makeItem(net.reward[k], v[1], v[2])
  97. end
  98. net.reward[0] = #config.item
  99. end
  100. -- 查询
  101. function query(human)
  102. local msgRet = Msg.gc.GC_PRESENT_LIMITGIFT_BAG_QUERY
  103. if not human.db.limitGiftBag then
  104. msgRet.list[0] = 0
  105. Msg.send(msgRet, human.fd)
  106. end
  107. local now = os.time()
  108. for k, v in pairs(human.db.limitGiftBag.actTime) do
  109. if getActTypeState(human, k, now) then
  110. for _,id in ipairs(PERSON_LIMITGIFTBAG_ACTTPE_ID[k]) do
  111. fontLimitBagNet(human, msgRet, id, v, now)
  112. end
  113. end
  114. end
  115. Msg.send(msgRet, human.fd)
  116. end
  117. -- 激活
  118. local function active(human, actType)
  119. if true then
  120. return
  121. end
  122. inintDB(human)
  123. if not getActTypeState(human, actType, os.time()) then
  124. human.db.limitGiftBag.actTime[actType] = os.time()
  125. cleanGiftBagBuy(human, actType)
  126. end
  127. end
  128. -- 尝试激活
  129. function touchActive(human, type, cnt)
  130. updateExcel()
  131. local activeList = {}
  132. for k, v in ipairs(LimitGiftBagExcel) do
  133. if v.type == type and v.need <= cnt then
  134. activeList[v.actType] = 1
  135. end
  136. end
  137. for actType in pairs(activeList) do
  138. active(human, actType)
  139. end
  140. end
  141. -- 购买 后台触发 发送
  142. function buy(human, id)
  143. if true then
  144. return
  145. end
  146. local config = LimitGiftBagExcel[id]
  147. if not config then return end
  148. local state = getGiftBagBuyState(human, config)
  149. if not state then return end
  150. BagLogic.addItemList(human, config.item, "limitBag")
  151. end