CommonActShop.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Util = require("common.Util")
  12. local Msg = require("core.Msg")
  13. local Grid = require("bag.Grid")
  14. local BagLogic = require("bag.BagLogic")
  15. local AbsActLogic = require("absAct.AbsActLogic")
  16. local Broadcast = require("broadcast.Broadcast")
  17. local Lang = require("common.Lang")
  18. local Config = require("excel.commonact").store
  19. local LOGTAG = "CommonActShop" --日志标识
  20. local COMMONACTSHOPABSID = 7502 -- 对应ABS活动ID
  21. local function CreateDB(human)
  22. if not human.db.absAct[COMMONACTSHOPABSID] then
  23. human.db.absAct[COMMONACTSHOPABSID] = {}
  24. end
  25. if not human.db.absAct[COMMONACTSHOPABSID].buyRecord then
  26. human.db.absAct[COMMONACTSHOPABSID].buyRecord = {}
  27. end
  28. for index, v in ipairs(Config) do
  29. human.db.absAct[COMMONACTSHOPABSID].buyRecord[index] = 0
  30. end
  31. end
  32. local function CheckAndCreateDB(human)
  33. if not human.db.absAct[COMMONACTSHOPABSID] or not human.db.absAct[COMMONACTSHOPABSID].buyRecord then
  34. CreateDB(human)
  35. end
  36. end
  37. function isOpen(human, YYInfo, funcConfig)
  38. local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID and funcConfig.funcID or COMMONACTSHOPABSID)
  39. if not state then
  40. print("[CommonActShop_isOpen] 当前活动未开启")
  41. return
  42. end
  43. print("[CommonActShop_isOpen] 进入判断 endTime = "..endTime.." startTime = "..startTime)
  44. return state, endTime, startTime
  45. end
  46. function isActive(human, YYInfo, funcConfig)
  47. local state = isOpen(human, YYInfo, funcConfig)
  48. return not state
  49. end
  50. --查询
  51. function CommonActShop_Query(human)
  52. CheckAndCreateDB(human)
  53. local state = AbsActLogic.isStarted(human, COMMONACTSHOPABSID)
  54. if not state then
  55. print("[CommonActShop_Query] 当前活动未开启")
  56. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  57. end
  58. local actData = human.db.absAct[COMMONACTSHOPABSID]
  59. if not actData then
  60. print("[CommonActShop_Query] 不存在对应的活动数据")
  61. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  62. end
  63. print("[CommonActShop_Query] 正常进入")
  64. local buyRecord = actData.buyRecord
  65. local msgRet = Msg.gc.GC_ABS_FESTIVAL_SHOP_QUERY
  66. local itemVec = msgRet.tGoodInfo
  67. itemVec[0] = #Config
  68. for k,v in ipairs(Config) do
  69. local singleItem = itemVec[k]
  70. local tempConfig = v
  71. Grid.makeItem(singleItem.itemData, tempConfig.item[1], tempConfig.item[2])
  72. singleItem.itemIndex = k
  73. Grid.makeItem(singleItem.needItem, tempConfig.currencyInfo[1], tempConfig.currencyInfo[2])
  74. singleItem.maxCanBuy = tempConfig.maxBuyCnt
  75. singleItem.nowBuy = buyRecord and buyRecord[k] or 0
  76. singleItem.zhekou = 0
  77. singleItem.order = 0
  78. singleItem.needVipLv = 0
  79. singleItem.rare = 0
  80. singleItem.limitType = 1
  81. singleItem.bCanChose = 0
  82. singleItem.bChose = 0
  83. end
  84. Msg.send(msgRet, human.fd)
  85. end
  86. --购买商品
  87. function CommonActShop_Buy(human, itemIdx, buyCnt)
  88. local state = AbsActLogic.isStarted(human, COMMONACTSHOPABSID)
  89. if not state then
  90. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  91. end
  92. local actData = human.db.absAct[COMMONACTSHOPABSID]
  93. if not actData then
  94. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  95. end
  96. local itemCfg = Config[itemIdx]
  97. if not itemCfg or buyCnt <= 0 then
  98. print("[CommonActShop_Buy] 不正确的数据 itemIdx = "..itemIdx.." buyCnt = "..buyCnt)
  99. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  100. end
  101. local buyRecord = actData.buyRecord
  102. if buyRecord and buyRecord[itemIdx] then
  103. if buyRecord[itemIdx] + buyCnt > itemCfg.maxBuyCnt then
  104. return Broadcast.sendErr(human, Lang.COMMON_BUY_NUM_NOT_ENOUGH)
  105. end
  106. end
  107. local currencyInfo = itemCfg.currencyInfo
  108. local cnt = currencyInfo[2] * buyCnt
  109. if BagLogic.getItemCnt(human, currencyInfo[1]) < cnt then
  110. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  111. end
  112. BagLogic.delItem(human, currencyInfo[1], cnt, LOGTAG)
  113. --更新数据
  114. buyRecord = buyRecord or {}
  115. buyRecord[itemIdx] = (buyRecord[itemIdx] or 0) + buyCnt
  116. actData.buyRecord = buyRecord
  117. --发放奖励
  118. local itemInfo = itemCfg.item
  119. BagLogic.addItemList(human, {{itemInfo[1], itemInfo[2] * buyCnt}}, LOGTAG)
  120. --下发数据
  121. CommonActShop_Query(human)
  122. end