CommonActMoneyTree.lua 8.6 KB

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