CommonActMoneyTree.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  142. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  143. end
  144. local msgRet = Msg.gc.GC_ABS_COMONACT_MONEYTREE_QUERY
  145. msgRet.rewardBasket[0] = 0
  146. msgRet.weighMax = BASKET_WEIGHT_MAX
  147. msgRet.weighNow = 0
  148. msgRet.lotteryTimes = 0
  149. msgRet.isStart = 1
  150. msgRet.isEnd = 0
  151. msgRet.nowStage = 0
  152. local actData = getData(human)
  153. if actData.lotteryTimes then
  154. msgRet.lotteryTimes = actData.lotteryTimes
  155. end
  156. if actData.basketItemArr then
  157. msgRet.weighNow = calcBasketWeight(actData.basketItemArr)
  158. end
  159. if actData.nowStage then
  160. msgRet.nowStage = actData.nowStage
  161. end
  162. local itemNum = actData.basketItemArr and #actData.basketItemArr or 0
  163. if itemNum == 0 then
  164. msgRet.isEnd = 1
  165. return Msg.send(msgRet, human.fd)
  166. end
  167. local len, msgOneceMaxLen = 0, 20
  168. for _, itemCfgIdx in ipairs(actData.basketItemArr) do
  169. len = len + 1
  170. msgRet.rewardBasket[0] = len
  171. local itemCfg = MoneyTreeConfig[itemCfgIdx].reward
  172. Grid.makeItem(msgRet.rewardBasket[len], itemCfg[1], itemCfg[2])
  173. if len >= msgOneceMaxLen then
  174. itemNum = itemNum - len
  175. if itemNum <= 0 then
  176. msgRet.isEnd = 1
  177. return Msg.send(msgRet, human.fd)
  178. end
  179. Msg.send(msgRet, human.fd)
  180. len = 0
  181. msgRet.isStart = 0
  182. end
  183. end
  184. if len > 0 then
  185. msgRet.isEnd = 1
  186. Msg.send(msgRet, human.fd)
  187. end
  188. end
  189. -- 进入抽奖阶段
  190. function CommonActMoneyTree_EnterLotteryStage(human)
  191. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  192. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  193. end
  194. local actData = getData(human)
  195. if (actData.lotteryTimes or 0) <= 0 then
  196. return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
  197. end
  198. -- 扣除次数
  199. updateLotteryTimes(human, -1)
  200. -- 更新阶段
  201. updateStage(human, 1)
  202. -- 推送最新数据给客户端
  203. CommonActMoneyTree_Query(human)
  204. end
  205. -- 抽一次奖
  206. function CommonActMoneyTree_Lottery(human)
  207. if not isOpenAct(human, COMMONACT_MONEYTREE_ID) then
  208. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  209. end
  210. local actData = getData(human)
  211. if (actData.nowStage or 0) ~= 1 then
  212. return Broadcast.sendErr(human, Lang.MONEYTREE_START)
  213. end
  214. if actData.basketItemArr then
  215. local weightNow = calcBasketWeight(actData.basketItemArr)
  216. if weightNow > BASKET_WEIGHT_MAX then
  217. return Broadcast.sendErr(human, Lang.MONEYTREE_IS_MAX)
  218. end
  219. end
  220. -- 扣除次数
  221. -- updateLotteryTimes(human, -1)
  222. local finalIdx = lottery()
  223. if finalIdx == 0 then
  224. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  225. end
  226. -- 把道具加入篮子
  227. insertBasketItemArr(human, finalIdx)
  228. local msgRet = Msg.gc.GC_ABS_COMONACT_MONEYTREE_LOTTERY
  229. local itemCfg = MoneyTreeConfig[finalIdx]
  230. Grid.makeItem(msgRet.reward, itemCfg.reward[1], itemCfg.reward[2])
  231. Msg.send(msgRet, human.fd)
  232. CommonActMoneyTree_Query(human)
  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. -- 检查当前篮子中道具重量是否超过限定重量, 如果超过则当前篮子中每个道具都有50%几率失去
  247. local itemArr = actData.basketItemArr
  248. local weightNow = calcBasketWeight(itemArr)
  249. if weightNow > BASKET_WEIGHT_MAX then
  250. itemArr = randBasketItem(itemArr)
  251. -- updateBasketItemArr(human, newBasketItemArr)
  252. -- CommonActMoneyTree_Query(human)
  253. end
  254. local itemList = {}
  255. for _, itemCfgIdx in ipairs(itemArr) do
  256. local itemCfg = MoneyTreeConfig[itemCfgIdx].reward
  257. local itemId, itemNum = itemCfg[1], itemCfg[2]
  258. itemList[itemId] = (itemList[itemId] or 0) + itemNum
  259. end
  260. -- 重置篮子中道具数据
  261. resetBasketArr(human)
  262. -- 更新阶段
  263. updateStage(human, 0)
  264. -- 发放道具
  265. BagLogic.addItemList(human, itemList, LOGTYPE)
  266. -- 推送最新数据给客户端
  267. CommonActMoneyTree_Query(human)
  268. -- 刷新红点
  269. updateRedDot(human)
  270. end