VoucherSpecialOffer.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. -- 代金券特惠
  2. -- 每日每档限购1次,跨天重置
  3. -- db.voucherSpecialOfferData = { [buyId] = buyCnt }
  4. local Lang = require("common.Lang")
  5. local Msg = require("core.Msg")
  6. local Broadcast = require("broadcast.Broadcast")
  7. local Grid = require("bag.Grid")
  8. local BagLogic = require("bag.BagLogic")
  9. local BuyLogic = require("topup.BuyLogic")
  10. local YunYingLogic = require("yunying.YunYingLogic")
  11. local VoucherShopLogic = require("voucher.VoucherShopLogic")
  12. local CommonDefine = require("common.CommonDefine")
  13. local giftConfig = require("excel.VoucherSpecialOffer").gift
  14. local LOGTYPE = "VoucherSpecialOffer"
  15. local VOUCHERSHOP_ACTID = 27
  16. local function isXiaoQiChannel(human)
  17. local channelID = human.db.phpChanelID or human.phpChanelID
  18. return channelID == CommonDefine.CHANNEL_TAG_XIAOQI
  19. or channelID == tostring(CommonDefine.CHANNEL_TAG_XIAOQI)
  20. end
  21. local function getCfg(buyId)
  22. for _, v in ipairs(giftConfig) do
  23. if buyId == v.buyId then
  24. return v
  25. end
  26. end
  27. end
  28. local function updateIcon(human)
  29. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  30. end
  31. function isOpen(human, YYInfo, funcConfig)
  32. if not isXiaoQiChannel(human) then
  33. return false
  34. end
  35. return VoucherShopLogic.isOpen(human, YYInfo, funcConfig)
  36. end
  37. function isRed(human)
  38. return false
  39. end
  40. function updateDaily(human)
  41. if not isOpen(human) then
  42. return
  43. end
  44. human.db.voucherSpecialOfferData = nil
  45. if human.fd then
  46. VoucherSpecialOffer_Query(human)
  47. end
  48. end
  49. function VoucherSpecialOffer_Query(human)
  50. if not isOpen(human) then
  51. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  52. end
  53. local buyData = human.db.voucherSpecialOfferData
  54. local msgRet = Msg.gc.GC_VOUCHER_SPECIAL_OFFER_QUERY
  55. local giftArr = msgRet.giftArr
  56. giftArr[0] = 0
  57. for k, v in ipairs(giftConfig) do
  58. giftArr[0] = k
  59. giftArr[k].nowBuyCnt = buyData and buyData[v.buyId] or 0
  60. giftArr[k].maxBuyCnt = v.amount
  61. BuyLogic.fontBuyItem(human, giftArr[k].buyItem, v.buyId)
  62. giftArr[k].giftItem[0] = #v.rewards
  63. for idx, itemCfg in ipairs(v.rewards) do
  64. Grid.makeItem(giftArr[k].giftItem[idx], itemCfg[1], itemCfg[2])
  65. end
  66. end
  67. Msg.send(msgRet, human.fd)
  68. end
  69. function onCharge(human, buyId, buyNum)
  70. if not isOpen(human) then
  71. return
  72. end
  73. local giftCfg = getCfg(buyId)
  74. if not giftCfg then
  75. return
  76. end
  77. buyNum = buyNum or 1
  78. local buyData = human.db.voucherSpecialOfferData
  79. local nowBuyCnt = buyData and buyData[buyId] or 0
  80. if nowBuyCnt + buyNum > giftCfg.amount then
  81. return Broadcast.sendErr(human, Lang.ABS_GIFT_BUY_LIMIT)
  82. end
  83. local itemArr = {}
  84. for idx, itemCfg in ipairs(giftCfg.rewards) do
  85. itemArr[idx] = {itemCfg[1], itemCfg[2] * buyNum}
  86. end
  87. BagLogic.addItemList(human, itemArr, LOGTYPE)
  88. buyData = buyData or {}
  89. buyData[buyId] = nowBuyCnt + buyNum
  90. human.db.voucherSpecialOfferData = buyData
  91. updateIcon(human)
  92. VoucherSpecialOffer_Query(human)
  93. end
  94. function GetRemainNum(human, nBuyID)
  95. if not isOpen(human) then
  96. return 0
  97. end
  98. local giftCfg = getCfg(nBuyID)
  99. if not giftCfg then
  100. return 0
  101. end
  102. local buyData = human.db.voucherSpecialOfferData
  103. if not buyData or not buyData[nBuyID] then
  104. return giftCfg.amount
  105. end
  106. local leftCnt = giftCfg.amount - buyData[nBuyID]
  107. return leftCnt > 0 and leftCnt or 0
  108. end