AbsDiscountStoreLogic.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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)
  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. local itemVec = msgRet.itemVec
  40. itemVec[0] = #Config
  41. for k,v in ipairs(Config) do
  42. local singleItem = itemVec[k]
  43. singleItem.idx = k
  44. singleItem.maxBuyCnt = v.maxBuyCnt
  45. singleItem.nowBuyCnt = buyRecord and buyRecord[k] or 0
  46. Grid.makeItem(singleItem.item, v.item[1], v.item[2])
  47. -- singleItem.currencyId = v.currencyInfo[1]
  48. -- singleItem.currencyCnt = v.currencyInfo[2]
  49. Grid.makeItem(singleItem.currency, v.currencyInfo[1], v.currencyInfo[2])
  50. end
  51. Msg.send(msgRet, human.fd)
  52. end
  53. --购买商品
  54. function BuyItem(human, id, itemIdx, buyCnt)
  55. local state = AbsActLogic.isStarted(human, id)
  56. if not state then
  57. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  58. end
  59. local actData = human.db.absAct[id]
  60. if not actData then
  61. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  62. end
  63. local itemCfg = Config[itemIdx]
  64. if not itemCfg then
  65. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  66. end
  67. local buyRecord = actData.buyRecord
  68. if buyRecord and buyRecord[itemIdx] then
  69. if buyRecord[itemIdx] + buyCnt > itemCfg.maxBuyCnt then
  70. return Broadcast.sendErr(human, Lang.COMMON_BUY_NUM_NOT_ENOUGH)
  71. end
  72. end
  73. local currencyInfo = itemCfg.currencyInfo
  74. if BagLogic.getItemCnt(human, currencyInfo[1]) < currencyInfo[2] then
  75. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  76. end
  77. BagLogic.delItem(human, currencyInfo[1], currencyInfo[2], LOGTAG)
  78. --更新数据
  79. buyRecord = buyRecord or {}
  80. buyRecord[itemIdx] = (buyRecord[itemIdx] or 0) + buyCnt
  81. actData.buyRecord = buyRecord
  82. --发放奖励
  83. local itemInfo = itemCfg.item
  84. BagLogic.addItemList(human, {{itemInfo[1], itemInfo[2] * buyCnt}}, LOGTAG)
  85. --下发数据
  86. local msgRet = Msg.gc.GC_DISCOUNTSTORE_BUY
  87. msgRet.idx = itemIdx
  88. msgRet.nowBuyCnt = itemCfg.buyRecord[itemIdx]
  89. Msg.send(msgRet, human.fd)
  90. end