BusThreeActDiscountStore.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. --新商业化活动3—天选兑换
  2. --db
  3. --[=[
  4. human.db.absAct[id] = {
  5. buyRecord = {
  6. [1] = 2, --key是道具在配置中的id, value是已经购买次数
  7. [2] = 1,
  8. }
  9. }
  10. ]=]--
  11. local Msg = require("core.Msg")
  12. local Grid = require("bag.Grid")
  13. local BagLogic = require("bag.BagLogic")
  14. local AbsActLogic = require("absAct.AbsActLogic")
  15. local Broadcast = require("broadcast.Broadcast")
  16. local Lang = require("common.Lang")
  17. local Config = require("excel.BusThreeActivity").DiscountStore
  18. local LOGTAG = "BusThreeDiscountStore" --日志标识
  19. local BUSTHREDISCOUNTSTOREABSID = 7403 -- 对应ABS活动ID
  20. local function CreateDB(human)
  21. if not human.db.absAct[BUSTHREDISCOUNTSTOREABSID] then
  22. human.db.absAct[BUSTHREDISCOUNTSTOREABSID] = {}
  23. end
  24. if not human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord then
  25. human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord = {}
  26. end
  27. for index, v in ipairs(Config) do
  28. human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord[index] = 0
  29. end
  30. end
  31. local function CheckAndCreateDB(human)
  32. if not human.db.absAct[BUSTHREDISCOUNTSTOREABSID] or not human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord then
  33. CreateDB(human)
  34. end
  35. end
  36. function isOpen(human, YYInfo, funcConfig)
  37. local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID and funcConfig.funcID or BUSTHREDISCOUNTSTOREABSID)
  38. return state, endTime, startTime
  39. end
  40. function isActive(human, YYInfo, funcConfig)
  41. local state = isOpen(human, YYInfo, funcConfig)
  42. return not state
  43. end
  44. -- function isRed(human, YYInfo, absActConfig)
  45. -- local state = AbsActLogic.isStarted(human, BUSTHREDISCOUNTSTOREABSID)
  46. -- if not state then
  47. -- return false
  48. -- end
  49. -- CheckAndCreateDB(human)
  50. -- for nID, v in ipairs(Config) do
  51. -- return true
  52. -- end
  53. -- return false
  54. -- end
  55. --查询
  56. function Query(human, id, isBuy)
  57. CheckAndCreateDB(human)
  58. local state = AbsActLogic.isStarted(human, BUSTHREDISCOUNTSTOREABSID)
  59. if not state then
  60. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  61. end
  62. local actData = human.db.absAct[BUSTHREDISCOUNTSTOREABSID]
  63. if not actData then
  64. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  65. end
  66. local buyRecord = actData.buyRecord
  67. local msgRet = Msg.gc.GC_NEW_BUSTHREEACT_DISQUERY
  68. local itemVec = msgRet.titemData
  69. itemVec[0] = #Config
  70. for k,v in ipairs(Config) do
  71. local singleItem = itemVec[k]
  72. singleItem.idx = k
  73. singleItem.maxBuyCnt = v.maxBuyCnt
  74. singleItem.nowBuyCnt = buyRecord and buyRecord[k] or 0
  75. Grid.makeItem(singleItem.item, v.item[1], v.item[2])
  76. Grid.makeItem(singleItem.currencyInfo, v.currencyInfo[1], v.currencyInfo[2])
  77. end
  78. Msg.send(msgRet, human.fd)
  79. end
  80. --购买商品
  81. function BuyItem(human, id, itemIdx, buyCnt)
  82. local state = AbsActLogic.isStarted(human, id)
  83. if not state then
  84. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  85. end
  86. local actData = human.db.absAct[BUSTHREDISCOUNTSTOREABSID]
  87. if not actData then
  88. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  89. end
  90. local itemCfg = Config[itemIdx]
  91. if not itemCfg or buyCnt <= 0 then
  92. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  93. end
  94. local buyRecord = actData.buyRecord
  95. if buyRecord and buyRecord[itemIdx] then
  96. if buyRecord[itemIdx] + buyCnt > itemCfg.maxBuyCnt then
  97. return Broadcast.sendErr(human, Lang.COMMON_BUY_NUM_NOT_ENOUGH)
  98. end
  99. end
  100. local currencyInfo = itemCfg.currencyInfo
  101. local cnt = currencyInfo[2] * buyCnt
  102. if BagLogic.getItemCnt(human, currencyInfo[1]) < cnt then
  103. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  104. end
  105. BagLogic.delItem(human, currencyInfo[1], cnt, LOGTAG)
  106. --更新数据
  107. buyRecord = buyRecord or {}
  108. buyRecord[itemIdx] = (buyRecord[itemIdx] or 0) + buyCnt
  109. actData.buyRecord = buyRecord
  110. --发放奖励
  111. local itemInfo = itemCfg.item
  112. BagLogic.addItemList(human, {{itemInfo[1], itemInfo[2] * buyCnt}}, LOGTAG)
  113. --下发数据
  114. Query(human, id, true)
  115. end