CommonActMoneyTree.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 = 150 -- 篮子可承受的总重量
  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 not actData then
  101. return false
  102. end
  103. if (actData.nowStage or 0) == 1 then
  104. return true
  105. end
  106. if (actData.lotteryTimes or 0) > 0 then
  107. return true
  108. end
  109. return false
  110. end
  111. function isOpen(human, YYInfo, funcConfig)
  112. return isOpenAct(human, funcConfig and funcConfig.funcID)
  113. end
  114. function isActive(human, YYInfo, funcConfig)
  115. return not isOpen(human, YYInfo, funcConfig)
  116. end
  117. -- 日常任务活跃值达标
  118. function CompleteHuoYueTask(human)
  119. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  120. return
  121. end
  122. updateLotteryTimes(human, 1)
  123. updateRedDot(human)
  124. end
  125. function genAbsActData()
  126. return {lotteryTimes = 1}
  127. end
  128. function updateDaily(human, funcID)
  129. if not isOpenAct(human, funcID) then
  130. return
  131. end
  132. local actData = getData(human)
  133. if not actData then
  134. return
  135. end
  136. updateLotteryTimes(human, 1)
  137. updateRedDot(human)
  138. end
  139. -- 查询
  140. function CommonActMoneyTree_Query(human)
  141. local msgRet = Msg.gc.GC_ABS_COMONACT_MONEYTREE_QUERY
  142. msgRet.rewardBasket[0] = 0
  143. msgRet.weighMax = BASKET_WEIGHT_MAX
  144. msgRet.weighNow = 0
  145. msgRet.lotteryTimes = 0
  146. msgRet.isStart = 1
  147. msgRet.isEnd = 0
  148. msgRet.nowStage = 0
  149. local actData = getData(human)
  150. if actData.lotteryTimes then
  151. msgRet.lotteryTimes = actData.lotteryTimes
  152. end
  153. if actData.basketItemArr then
  154. msgRet.weighNow = calcBasketWeight(actData.basketItemArr)
  155. end
  156. if actData.nowStage then
  157. msgRet.nowStage = actData.nowStage
  158. end
  159. local itemNum = actData.basketItemArr and #actData.basketItemArr or 0
  160. if itemNum == 0 then
  161. msgRet.isEnd = 1
  162. return Msg.send(msgRet, human.fd)
  163. end
  164. local len, msgOneceMaxLen = 0, 20
  165. for _, itemCfgIdx in ipairs(actData.basketItemArr) do
  166. len = len + 1
  167. msgRet.rewardBasket[0] = len
  168. local itemCfg = MoneyTreeConfig[itemCfgIdx].reward
  169. Grid.makeItem(msgRet.rewardBasket[len], itemCfg[1], itemCfg[2])
  170. if len >= msgOneceMaxLen then
  171. itemNum = itemNum - len
  172. if itemNum <= 0 then
  173. msgRet.isEnd = 1
  174. return Msg.send(msgRet, human.fd)
  175. end
  176. Msg.send(msgRet, human.fd)
  177. len = 0
  178. msgRet.isStart = 0
  179. end
  180. end
  181. if len > 0 then
  182. msgRet.isEnd = 1
  183. Msg.send(msgRet, human.fd)
  184. end
  185. end
  186. -- 进入抽奖阶段
  187. function CommonActMoneyTree_EnterLotteryStage(human)
  188. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  189. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  190. end
  191. local actData = getData(human)
  192. if (actData.lotteryTimes or 0) <= 0 then
  193. return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
  194. end
  195. -- 扣除次数
  196. updateLotteryTimes(human, -1)
  197. -- 更新阶段
  198. updateStage(human, 1)
  199. -- 推送最新数据给客户端
  200. CommonActMoneyTree_Query(human)
  201. end
  202. -- 抽一次奖
  203. function CommonActMoneyTree_Lottery(human)
  204. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  205. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  206. end
  207. local actData = getData(human)
  208. if (actData.nowStage or 0) ~= 1 then
  209. return Broadcast.sendErr(human, Lang.MONEYTREE_START)
  210. end
  211. if actData.basketItemArr then
  212. local weightNow = calcBasketWeight(actData.basketItemArr)
  213. if weightNow > BASKET_WEIGHT_MAX then
  214. return Broadcast.sendErr(human, Lang.MONEYTREE_IS_MAX)
  215. end
  216. end
  217. -- 扣除次数
  218. -- updateLotteryTimes(human, -1)
  219. local finalIdx = lottery()
  220. if finalIdx == 0 then
  221. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  222. end
  223. -- 把道具加入篮子
  224. insertBasketItemArr(human, finalIdx)
  225. local msgRet = Msg.gc.GC_ABS_COMONACT_MONEYTREE_LOTTERY
  226. local itemCfg = MoneyTreeConfig[finalIdx]
  227. Grid.makeItem(msgRet.reward, itemCfg.reward[1], itemCfg.reward[2])
  228. Msg.send(msgRet, human.fd)
  229. CommonActMoneyTree_Query(human)
  230. end
  231. -- 领取奖励
  232. function CommonActMoneyTree_GetReward(human)
  233. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  234. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  235. end
  236. local actData = getData(human)
  237. if (actData.nowStage or 0) ~= 1 then
  238. return Broadcast.sendErr(human, Lang.MONEYTREE_START)
  239. end
  240. if not actData.basketItemArr or not next(actData.basketItemArr) then
  241. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  242. end
  243. -- 检查当前篮子中道具重量是否超过限定重量, 如果超过则当前篮子中每个道具都有50%几率失去
  244. local itemArr = actData.basketItemArr
  245. local weightNow = calcBasketWeight(itemArr)
  246. if weightNow > BASKET_WEIGHT_MAX then
  247. itemArr = randBasketItem(itemArr)
  248. -- updateBasketItemArr(human, newBasketItemArr)
  249. -- CommonActMoneyTree_Query(human)
  250. end
  251. local itemList = {}
  252. for _, itemCfgIdx in ipairs(itemArr) do
  253. local itemCfg = MoneyTreeConfig[itemCfgIdx].reward
  254. local itemId, itemNum = itemCfg[1], itemCfg[2]
  255. itemList[itemId] = (itemList[itemId] or 0) + itemNum
  256. end
  257. -- 重置篮子中道具数据
  258. resetBasketArr(human)
  259. -- 更新阶段
  260. updateStage(human, 0)
  261. -- 发放道具
  262. BagLogic.addItemList(human, itemList, LOGTYPE)
  263. -- 推送最新数据给客户端
  264. CommonActMoneyTree_Query(human)
  265. -- 刷新红点
  266. updateRedDot(human)
  267. end