WelfareGiftLogic.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ---------------
  2. -- 福利礼包
  3. -- type 当前类型
  4. -- period 当前阶段
  5. -- cnt 剩余次数
  6. local Util = require("common.Util")
  7. local Log = require("common.Log")
  8. local Lang = require("common.Lang")
  9. local Msg = require("core.Msg")
  10. local ObjHuman = require("core.ObjHuman")
  11. local WelfareGiftExcel = require("excel.present").welfareGift
  12. local BuyExcel = require("excel.buy")
  13. local BuyLogic = require("topup.BuyLogic")
  14. local HeroLogic = require("hero.HeroLogic")
  15. local BagLogic = require("bag.BagLogic")
  16. local Grid = require("bag.Grid")
  17. local Broadcast = require("broadcast.Broadcast")
  18. local UnionDBLogic = require("union.UnionDBLogic")
  19. local MailManager = require("mail.MailManager")
  20. local MailExcel = require("excel.mail")
  21. local WELFACE_GIFT_EXCEL = {}
  22. local WELFACE_GIFT_LEN = 0
  23. function initAfterHot()
  24. for k, v in ipairs(WelfareGiftExcel) do
  25. local type = v.type
  26. if not WELFACE_GIFT_EXCEL[type] then
  27. WELFACE_GIFT_LEN = WELFACE_GIFT_LEN + 1
  28. end
  29. WELFACE_GIFT_EXCEL[type] = WELFACE_GIFT_EXCEL[type] or {}
  30. WELFACE_GIFT_EXCEL[type][v.period] = k
  31. end
  32. end
  33. function query(human)
  34. local welfareGift = human.db.welfareGift
  35. local giftIndex = welfareGift.giftIndex
  36. if not giftIndex then return end
  37. local msgRet = Msg.gc.GC_WELFARE_GIFT_QUERY
  38. local now = os.time()
  39. local diffTime = Util.getNextDayTime(now, 1)
  40. msgRet.endTime = diffTime - now
  41. local len = 0
  42. for k, v in pairs(WELFACE_GIFT_EXCEL) do
  43. welfareGift[k] = welfareGift[k] or {}
  44. local welfare = welfareGift[k]
  45. local startIndex = 1
  46. local period = welfare.period or startIndex
  47. local config = WelfareGiftExcel[v[period]]
  48. if giftIndex == config.type then
  49. period = welfare.period or 0
  50. config = WelfareGiftExcel[v[period]]
  51. end
  52. local cnt = welfare.cnt or config.cnt
  53. len = len + 1
  54. local net = msgRet.list[len]
  55. net.id = v[period]
  56. net.name = config.name
  57. net.cnt = cnt
  58. net.cntMax = config.cnt
  59. net.price = config.price
  60. net.oldPrice = config.oldPrice
  61. net.desc = config.desc
  62. local rewardLen = 0
  63. for _id, data in ipairs(config.reward) do
  64. rewardLen = rewardLen + 1
  65. Grid.makeItem(net.items[rewardLen], data[1], data[2])
  66. end
  67. net.items[0] = rewardLen
  68. end
  69. msgRet.list[0] = len
  70. Msg.send(msgRet,human.fd)
  71. end
  72. function buy(human, id)
  73. local config = WelfareGiftExcel[id]
  74. if not config then return end
  75. local welfareGift = human.db.welfareGift
  76. local welfare = welfareGift[config.type]
  77. if not welfare then return end
  78. local giftIndex = welfareGift.giftIndex
  79. if not giftIndex then return end
  80. local startIndex = 1
  81. if giftIndex == config.type then
  82. startIndex = 0
  83. end
  84. -- 不是同一级礼包
  85. local period = welfare.period or startIndex
  86. if period ~= config.period then return end
  87. local cnt = welfare.cnt or config.cnt
  88. if cnt <= 0 then return end
  89. -- 钻石判断
  90. local needZuanshi = config.price
  91. if human.db.zuanshi < needZuanshi then
  92. return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
  93. end
  94. -- 扣钻石
  95. ObjHuman.decZuanshi(human, - needZuanshi, "buySpecific")
  96. BagLogic.cleanMomentItemList()
  97. for _id, data in ipairs(config.reward) do
  98. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, data[1], data[2])
  99. end
  100. cnt = cnt - 1
  101. welfare.cnt = cnt
  102. if welfare.cnt <= 0 then
  103. period = period + 1
  104. if not WELFACE_GIFT_EXCEL[config.type][period] then
  105. query(human)
  106. return
  107. end
  108. welfare.period = period
  109. end
  110. local items = {}
  111. local len = 0
  112. for k, v in ipairs(config.unionReward) do
  113. len = len + 1
  114. local itemID = v[1]
  115. local itemCnt = v[2]
  116. items[len] = { itemID, itemCnt }
  117. end
  118. for k,v in pairs(items) do
  119. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, v[1],v[2])
  120. end
  121. BagLogic.addMomentItemList(human, "buySpecific")
  122. query(human)
  123. end
  124. function updateDaily(human)
  125. local ramdomIndex = math.random(1, WELFACE_GIFT_LEN)
  126. human.db.welfareGift = {giftIndex = ramdomIndex}
  127. end