AbsDiscountStoreLogic.lua 3.3 KB

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