VoucherSpecialOffer.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 giftConfig = require("excel.VoucherSpecialOffer").gift
  13. local LOGTYPE = "VoucherSpecialOffer"
  14. local VOUCHERSHOP_ACTID = 27
  15. local function getCfg(buyId)
  16. for _, v in ipairs(giftConfig) do
  17. if buyId == v.buyId then
  18. return v
  19. end
  20. end
  21. end
  22. local function updateIcon(human)
  23. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  24. end
  25. function isOpen(human, YYInfo, funcConfig)
  26. return VoucherShopLogic.isOpen(human, YYInfo, funcConfig)
  27. end
  28. function isRed(human)
  29. return false
  30. end
  31. function updateDaily(human)
  32. human.db.voucherSpecialOfferData = nil
  33. if human.fd then
  34. VoucherSpecialOffer_Query(human)
  35. end
  36. end
  37. function VoucherSpecialOffer_Query(human)
  38. if not isOpen(human) then
  39. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  40. end
  41. local buyData = human.db.voucherSpecialOfferData
  42. local msgRet = Msg.gc.GC_VOUCHER_SPECIAL_OFFER_QUERY
  43. local giftArr = msgRet.giftArr
  44. giftArr[0] = 0
  45. for k, v in ipairs(giftConfig) do
  46. giftArr[0] = k
  47. giftArr[k].nowBuyCnt = buyData and buyData[v.buyId] or 0
  48. giftArr[k].maxBuyCnt = v.amount
  49. BuyLogic.fontBuyItem(human, giftArr[k].buyItem, v.buyId)
  50. giftArr[k].giftItem[0] = #v.rewards
  51. for idx, itemCfg in ipairs(v.rewards) do
  52. Grid.makeItem(giftArr[k].giftItem[idx], itemCfg[1], itemCfg[2])
  53. end
  54. end
  55. Msg.send(msgRet, human.fd)
  56. end
  57. function onCharge(human, buyId, buyNum)
  58. if not isOpen(human) then
  59. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  60. end
  61. local giftCfg = getCfg(buyId)
  62. if not giftCfg then
  63. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  64. end
  65. buyNum = buyNum or 1
  66. local buyData = human.db.voucherSpecialOfferData
  67. local nowBuyCnt = buyData and buyData[buyId] or 0
  68. if nowBuyCnt + buyNum > giftCfg.amount then
  69. return Broadcast.sendErr(human, Lang.ABS_GIFT_BUY_LIMIT)
  70. end
  71. local itemArr = {}
  72. for idx, itemCfg in ipairs(giftCfg.rewards) do
  73. itemArr[idx] = {itemCfg[1], itemCfg[2] * buyNum}
  74. end
  75. BagLogic.addItemList(human, itemArr, LOGTYPE)
  76. buyData = buyData or {}
  77. buyData[buyId] = nowBuyCnt + buyNum
  78. human.db.voucherSpecialOfferData = buyData
  79. updateIcon(human)
  80. VoucherSpecialOffer_Query(human)
  81. end
  82. function GetRemainNum(human, nBuyID)
  83. local giftCfg = getCfg(nBuyID)
  84. if not giftCfg then
  85. return 0
  86. end
  87. local buyData = human.db.voucherSpecialOfferData
  88. if not buyData or not buyData[nBuyID] then
  89. return giftCfg.amount
  90. end
  91. local leftCnt = giftCfg.amount - buyData[nBuyID]
  92. return leftCnt > 0 and leftCnt or 0
  93. end