AbsDiscountStoreLogic.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. --新商业化活动——特价商店
  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.absAct").AbsDiscountStore
  18. local CycleActivityLogic = require("yunying.CycleActivity")
  19. local LOGTAG = "AbsDiscountStore" --日志标识
  20. function isOpen(human, YYInfo, funcConfig)
  21. -- local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
  22. local state, endTime, startTime = CycleActivityLogic.isStarted(human, funcConfig.funcID)
  23. return state, endTime, startTime
  24. end
  25. function isActive(human, YYInfo, funcConfig)
  26. local state = isOpen(human, YYInfo, funcConfig)
  27. return not state
  28. end
  29. --查询
  30. function Query(human, id, isBuy)
  31. -- local state = AbsActLogic.isStarted(human, id)
  32. local state = CycleActivityLogic.isStarted(human, id)
  33. if not state then
  34. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  35. end
  36. local actData = human.db.absAct[id]
  37. if not actData then
  38. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  39. end
  40. local buyRecord = actData.buyRecord
  41. local msgRet = Msg.gc.GC_DISCOUNTSTORE_QUERY
  42. msgRet.isBuy = isBuy and 1 or 0
  43. local itemVec = msgRet.itemVec
  44. itemVec[0] = #Config
  45. for k,v in ipairs(Config) do
  46. local singleItem = itemVec[k]
  47. singleItem.idx = k
  48. singleItem.maxBuyCnt = v.maxBuyCnt
  49. singleItem.nowBuyCnt = buyRecord and buyRecord[k] or 0
  50. Grid.makeItem(singleItem.item, v.item[1], v.item[2])
  51. -- singleItem.currencyId = v.currencyInfo[1]
  52. -- singleItem.currencyCnt = v.currencyInfo[2]
  53. Grid.makeItem(singleItem.currencyInfo, v.currencyInfo[1], v.currencyInfo[2])
  54. end
  55. Msg.send(msgRet, human.fd)
  56. end
  57. --购买商品
  58. function BuyItem(human, id, itemIdx, buyCnt)
  59. -- local state = AbsActLogic.isStarted(human, id)
  60. local state = CycleActivityLogic.isStarted(human, id)
  61. if not state then
  62. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  63. end
  64. local actData = human.db.absAct[id]
  65. if not actData then
  66. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  67. end
  68. local itemCfg = Config[itemIdx]
  69. if not itemCfg or buyCnt <= 0 then
  70. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  71. end
  72. local buyRecord = actData.buyRecord
  73. if buyRecord and buyRecord[itemIdx] then
  74. if buyRecord[itemIdx] + buyCnt > itemCfg.maxBuyCnt then
  75. return Broadcast.sendErr(human, Lang.COMMON_BUY_NUM_NOT_ENOUGH)
  76. end
  77. end
  78. local currencyInfo = itemCfg.currencyInfo
  79. local cnt = currencyInfo[2] * buyCnt
  80. if BagLogic.getItemCnt(human, currencyInfo[1]) < cnt then
  81. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  82. end
  83. BagLogic.delItem(human, currencyInfo[1], cnt, LOGTAG)
  84. --更新数据
  85. buyRecord = buyRecord or {}
  86. buyRecord[itemIdx] = (buyRecord[itemIdx] or 0) + buyCnt
  87. actData.buyRecord = buyRecord
  88. --发放奖励
  89. local itemInfo = itemCfg.item
  90. BagLogic.addItemList(human, {{itemInfo[1], itemInfo[2] * buyCnt}}, LOGTAG)
  91. --下发数据
  92. -- local msgRet = Msg.gc.GC_DISCOUNTSTORE_BUY
  93. -- msgRet.idx = itemIdx
  94. -- msgRet.nowBuyCnt = buyRecord[itemIdx]
  95. -- Msg.send(msgRet, human.fd)
  96. Query(human, id, true)
  97. end