CommonActMoneyTree.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. -- 通用节日活动 - 摇钱树
  2. -- db
  3. --[=[
  4. human.db.absAct[actId] = {
  5. basketItemArr = nil, --篮子里的道具idx列表
  6. lotteryTimes = nil, -- 抽奖次数
  7. }
  8. ]=]--
  9. local Msg = require("core.Msg")
  10. local Grid = require("bag.Grid")
  11. local BagLogic = require("bag.BagLogic")
  12. local AbsActLogic = require("absAct.AbsActLogic")
  13. local Broadcast = require("broadcast.Broadcast")
  14. local Lang = require("common.Lang")
  15. local MoneyTreeConfig = require("excel.commonact").treereward
  16. local YunYingLogic = require("yunying.YunYingLogic")
  17. local AbsActExcel = require("excel.absAct")
  18. local LOGTYPE = "CommonActMoneyTree" -- 日志标识
  19. local COMMONACT_MONEYTREE_ID = 7506 -- 活动Id
  20. local BASKET_WEIGHT = 100 -- 篮子可承受的总重量
  21. local function getData(human)
  22. return human.db.absAct[COMMONACT_MONEYTREE_ID]
  23. end
  24. local function updateLotteryTimes(human, val)
  25. local actData = getData(human)
  26. actData.lotteryTimes = (actData.lotteryTimes or 0) + val
  27. end
  28. local function insertBasketItemArr(human, itemIdx)
  29. local actData = getData(human)
  30. actData.basketItemArr = actData.basketItemArr or {}
  31. table.insert(actData.basketItemArr, itemIdx)
  32. end
  33. local function updateBasketItemArr(human, newBasketItemArr)
  34. local actData = getData(human)
  35. actData.basketItemArr = newBasketItemArr
  36. end
  37. local function resetBasketArr(human)
  38. local actData = getData(human)
  39. actData.basketItemArr = nil
  40. end
  41. local function isOpenAct(human, funcID)
  42. return AbsActLogic.isStarted(human, funcID)
  43. end
  44. -- 计算篮子中所有道具的总重量
  45. local function calcBasketWeight(basketItemArr)
  46. local weightSum = 0
  47. for _, cfgIdx in ipairs(basketItemArr or {}) do
  48. local itemCfg = MoneyTreeConfig[cfgIdx]
  49. weightSum = weightSum + itemCfg.value
  50. end
  51. return weightSum
  52. end
  53. -- 抽奖
  54. local function lottery()
  55. local totalWeight = 0
  56. for _, itemCfg in ipairs(MoneyTreeConfig) do
  57. totalWeight = totalWeight + itemCfg.nWeight
  58. end
  59. local finalIdx = 0
  60. local weight = 0
  61. local randWeight = math.random(0, totalWeight)
  62. for i, itemCfg in ipairs(MoneyTreeConfig) do
  63. weight = weight + itemCfg.nWeight
  64. if randWeight <= weight then
  65. finalIdx = i
  66. break
  67. end
  68. end
  69. return finalIdx
  70. end
  71. -- 将篮子中每个道具都随机一次, 获得最新的篮子道具
  72. local function randBasketItem(basketItemArr)
  73. local val = 50
  74. local newBasketItemArr = {}
  75. for _,itemCfgIdx in ipairs(basketItemArr) do
  76. local randVal = math.random(1, 100)
  77. if randVal >= val then
  78. newBasketItemArr[#newBasketItemArr+1] = itemCfgIdx
  79. end
  80. end
  81. return newBasketItemArr
  82. end
  83. -- 主动刷新一次红点
  84. local function updateRedDot(human)
  85. YunYingLogic.sendBanner(human)
  86. local config = AbsActExcel.absActivity[COMMONACT_MONEYTREE_ID]
  87. YunYingLogic.sendGroupUpdate(YYInfo[COMMONACT_MONEYTREE_ID], human, config.panelID)
  88. end
  89. function isRed(human, funcConfig)
  90. local state = isOpenAct(human, funcConfig and funcConfig.funcID)
  91. if not state then
  92. return false
  93. end
  94. local actData = getData(human)
  95. if (actData.lotteryTimes or 0) > 0 then
  96. return true
  97. end
  98. return false
  99. end
  100. function isOpen(human, YYInfo, funcConfig)
  101. return isOpenAct(human, funcConfig and funcConfig.funcID)
  102. end
  103. function isActive(human, YYInfo, funcConfig)
  104. return not isOpen(human, YYInfo, funcConfig)
  105. end
  106. -- 日常任务活跃值达标
  107. function CompleteHuoYueTask(human)
  108. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  109. return
  110. end
  111. updateLotteryTimes(human, 1)
  112. updateRedDot(human)
  113. end
  114. function updateDaily(human, funcID)
  115. if not isOpenAct(human, funcID) then
  116. return
  117. end
  118. updateLotteryTimes(human, 1)
  119. updateRedDot(human)
  120. end
  121. -- 查询
  122. function CommonActMoneyTree_Query(human)
  123. local msgRet = Msg.gc.GC_ABS_COMONACT_MONEYTREE_QUERY
  124. msgRet.rewardBasket[0] = 0
  125. msgRet.weighMax = BASKET_WEIGHT
  126. msgRet.weighNow = 0
  127. msgRet.lotteryTimes = 0
  128. msgRet.isStart = 1
  129. msgRet.isEnd = 0
  130. local actData = getData(human)
  131. if actData.lotteryTimes then
  132. msgRet.lotteryTimes = actData.lotteryTimes
  133. end
  134. if actData.basketItemArr then
  135. msgRet.weighNow = calcBasketWeight(actData.basketItemArr)
  136. end
  137. local itemNum = actData.basketItemArr and #actData.basketItemArr or 0
  138. if itemNum == 0 then
  139. return Msg.send(msgRet, human.fd)
  140. end
  141. local len, msgOneceMaxLen = 0, 20
  142. for _, itemCfgIdx in ipairs(actData.basketItemArr) do
  143. len = len + 1
  144. msgRet.rewardBasket[0] = len
  145. local itemCfg = MoneyTreeConfig[itemCfgIdx].reward
  146. Grid.makeItem(msgRet.rewardBasket[len], itemCfg[1], itemCfg[2])
  147. if len >= msgOneceMaxLen then
  148. itemNum = itemNum - len
  149. if itemNum <= 0 then
  150. msgRet.isEnd = 1
  151. return Msg.send(msgRet, human.fd)
  152. end
  153. Msg.send(msgRet, human.fd)
  154. len = 0
  155. msgRet.isStart = 0
  156. end
  157. end
  158. if len > 0 then
  159. msgRet.isEnd = 1
  160. Msg.send(msgRet, human.fd)
  161. end
  162. end
  163. -- 抽奖
  164. function CommonActMoneyTree_Lottery(human)
  165. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  166. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  167. end
  168. local actData = getData(human)
  169. if (actData.lotteryTimes or 0) <= 0 then
  170. return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
  171. end
  172. if actData.basketItemArr then
  173. local weightNow = calcBasketWeight(actData.basketItemArr)
  174. if weightNow > BASKET_WEIGHT then
  175. return Broadcast.sendErr(human, Lang.MONEYTREE_IS_MAX)
  176. end
  177. end
  178. -- 扣除次数
  179. updateLotteryTimes(human, -1)
  180. local finalIdx = lottery()
  181. if finalIdx == 0 then
  182. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  183. end
  184. -- 检查当前篮子中道具重量是否超过限定重量, 如果超过则当前篮子中每个道具都有50%几率失去
  185. insertBasketItemArr(human, finalIdx)
  186. local weightNow = calcBasketWeight(actData.basketItemArr)
  187. if weightNow > BASKET_WEIGHT then
  188. local newBasketItemArr = randBasketItem(actData.basketItemArr)
  189. updateBasketItemArr(human, newBasketItemArr)
  190. CommonActMoneyTree_Query(human)
  191. end
  192. local msgRet = Msg.gc.GC_ABS_COMONACT_MONEYTREE_LOTTERY
  193. local itemCfg = MoneyTreeConfig[finalIdx]
  194. Grid.makeItem(msgRet.reward, itemCfg.reward[1], itemCfg.reward[2])
  195. Msg.send(msgRet, human.fd)
  196. end
  197. -- 领取奖励
  198. function CommonActMoneyTree_GetReward(human)
  199. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  200. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  201. end
  202. local actData = getData(human)
  203. if not actData.basketItemArr or not next(actData.basketItemArr) then
  204. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  205. end
  206. local itemList = {}
  207. for _, itemCfgIdx in ipairs(actData.basketItemArr) do
  208. local itemCfg = MoneyTreeConfig[itemCfgIdx].reward
  209. local itemId, itemNum = itemCfg[1], itemCfg[2]
  210. itemList[itemId] = (itemList[itemId] or 0) + itemNum
  211. end
  212. -- 重置篮子中道具数据
  213. resetBasketArr(human)
  214. -- 发放道具
  215. BagLogic.addItemList(human, itemList, LOGTYPE)
  216. CommonActMoneyTree_Query(human)
  217. end