SkinGiftLogic.lua 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. -- 华丽皮肤礼包:仅直购,配置读取 excel.skin.skinGift
  2. local Msg = require("core.Msg")
  3. local Grid = require("bag.Grid")
  4. local BagLogic = require("bag.BagLogic")
  5. local BuyLogic = require("topup.BuyLogic")
  6. local Lang = require("common.Lang")
  7. local Broadcast = require("broadcast.Broadcast")
  8. local SkinGiftExcel = require("excel.skin").skinGift
  9. local LOGTAG = "skinGift"
  10. local GIFT_CFG_ID = 1
  11. local function getCfg()
  12. return SkinGiftExcel[GIFT_CFG_ID]
  13. end
  14. local function getCfgByBuyId(buyId)
  15. local cfg = getCfg()
  16. if cfg and cfg.buyID == buyId then
  17. return cfg
  18. end
  19. end
  20. local function getBuyCnt(human, buyId)
  21. local skinGiftData = human.db.skinGiftData
  22. return skinGiftData and skinGiftData[buyId] or 0
  23. end
  24. function Query(human)
  25. local cfg = getCfg()
  26. if not cfg then
  27. return
  28. end
  29. local msgRet = Msg.gc.GC_SKIN_GIFT_QUERY
  30. BuyLogic.fontBuyItem(human, msgRet.buyItem, cfg.buyID)
  31. msgRet.nowBuyCnt = getBuyCnt(human, cfg.buyID)
  32. msgRet.maxBuyCnt = cfg.amount
  33. msgRet.giftItem[0] = #cfg.rewards
  34. for i, item in ipairs(cfg.rewards) do
  35. Grid.makeItem(msgRet.giftItem[i], item[1], item[2])
  36. end
  37. Msg.send(msgRet, human.fd)
  38. end
  39. function onCharge(human, buyId, buyNum)
  40. local cfg = getCfgByBuyId(buyId)
  41. if not cfg then
  42. return
  43. end
  44. buyNum = buyNum or 1
  45. local nowBuyCnt = getBuyCnt(human, buyId)
  46. if cfg.amount > 0 and buyNum + nowBuyCnt > cfg.amount then
  47. return Broadcast.sendErr(human, Lang.ABS_GIFT_BUY_LIMIT)
  48. end
  49. local itemArr = {}
  50. for i, item in ipairs(cfg.rewards) do
  51. itemArr[i] = {item[1], item[2] * buyNum}
  52. end
  53. BagLogic.addItemList(human, itemArr, LOGTAG)
  54. local skinGiftData = human.db.skinGiftData or {}
  55. skinGiftData[buyId] = nowBuyCnt + buyNum
  56. human.db.skinGiftData = skinGiftData
  57. Query(human)
  58. end
  59. function GetRemainNum(human, buyId)
  60. local cfg = getCfgByBuyId(buyId)
  61. if not cfg then
  62. return 0
  63. end
  64. if cfg.amount == 0 then
  65. return 0
  66. end
  67. local nowBuyCnt = getBuyCnt(human, buyId)
  68. if cfg.amount > nowBuyCnt then
  69. return cfg.amount - nowBuyCnt
  70. end
  71. return 0
  72. end