CommonActFindTreasure.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. --------------------------------
  2. -- 文件名 : CommonActCharge.lua
  3. -- 文件说明 : 通用节日活动-连充豪礼
  4. -- 创建时间 : 2025/09
  5. -- 创建人 : FC
  6. --------------------------------
  7. local Msg = require("core.Msg")
  8. local Grid = require("bag.Grid")
  9. local BagLogic = require("bag.BagLogic")
  10. local AbsActLogic = require("absAct.AbsActLogic")
  11. local Broadcast = require("broadcast.Broadcast")
  12. local Lang = require("common.Lang")
  13. local Config = require("excel.commonact").findprize
  14. local CommonDefine = require("common.CommonDefine")
  15. local Util = require("common.Util")
  16. local YunYingLogic = require("yunying.YunYingLogic")
  17. local AbsActExcel = require("excel.absAct")
  18. local COMMONACTFINDABSID = 7504 -- 对应ABS活动ID
  19. local COMMONACTFINDUSEITEM = 1243 -- 寻宝使用道具ID
  20. local COMMONACTDOMAXNUM = nil -- 最大抽取保底次数
  21. ----------------------------------------- 内部处理开始 -------------------------------------
  22. local function ComActFindTreasure_CreateDB(human)
  23. if not human.db.absAct[COMMONACTFINDABSID] then
  24. human.db.absAct[COMMONACTFINDABSID] = {}
  25. end
  26. human.db.absAct[COMMONACTFINDABSID].tExtract = {
  27. nFreeNum = 1, -- 免费次数
  28. nFreeRefreshTime = os.time(), -- 免费刷新次数
  29. nAllNum = 0, -- 总次数
  30. }
  31. end
  32. -- 检测并创建DB数据
  33. local function ComActFindTreasure_CheckAndCreateDB(human)
  34. if not human.db.absAct[COMMONACTFINDABSID] or not human.db.absAct[COMMONACTFINDABSID].tExtract then
  35. ComActFindTreasure_CreateDB(human)
  36. end
  37. end
  38. local function ComActFindTreasure_GetDoMaxNum()
  39. if nil ~= COMMONACTDOMAXNUM then
  40. return COMMONACTDOMAXNUM
  41. end
  42. for _, v in ipairs(Config) do
  43. if not COMMONACTDOMAXNUM then
  44. COMMONACTDOMAXNUM = v.nNeedNum
  45. else
  46. COMMONACTDOMAXNUM = math.max(COMMONACTDOMAXNUM, v.nNeedNum)
  47. end
  48. end
  49. return COMMONACTDOMAXNUM
  50. end
  51. -- 获取免费次数
  52. local function ComActFindTreasure_GetDBFreeNum(human)
  53. return human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeNum
  54. end
  55. -- 设置免费次数
  56. local function ComActFindTreasure_SetDBFreeNum(human, nNum)
  57. human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeNum = nNum
  58. end
  59. -- 获取刷新时间
  60. local function ComActFindTreasure_GetDBFreeRefreshTime(human)
  61. return human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeRefreshTime
  62. end
  63. -- 设置刷新时间
  64. local function ComActFindTreasure_SetDBFreeRefreshTime(human, nTime)
  65. human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeRefreshTime = nTime
  66. end
  67. -- 获取总次数
  68. local function ComActFindTreasure_GetDBAllNum(human)
  69. return human.db.absAct[COMMONACTFINDABSID].tExtract.nAllNum
  70. end
  71. -- 设置总次数
  72. local function ComActFindTreasure_SetDBAllNum(human, nNum)
  73. human.db.absAct[COMMONACTFINDABSID].tExtract.nAllNum = nNum
  74. end
  75. -- 抽取动作
  76. local function ComActFindTreasure_FindDo(human, nDoNum)
  77. local nAllNum = ComActFindTreasure_GetDBAllNum(human)
  78. local nMaxDoNum = ComActFindTreasure_GetDoMaxNum()
  79. local nAllWeight, tItemInfo = 0, {}
  80. for _, v in ipairs(Config) do
  81. nAllWeight = nAllWeight + v.nWeight
  82. local bBaoDi = false
  83. if nMaxDoNum == v.nNeedNum then
  84. bBaoDi = true
  85. end
  86. table.insert(tItemInfo, {nWeight = nAllWeight, tPrize = v.tPrize, bBaoDi = bBaoDi});
  87. end
  88. local tGoodsInfo = {}
  89. for i = 1, nDoNum, 1 do
  90. local bUseBaoDi = false
  91. nAllNum = nAllNum + 1
  92. if nAllNum >= nMaxDoNum then
  93. nAllNum = 0
  94. bUseBaoDi = true
  95. end
  96. local nRandWeight = math.random(1, nAllWeight)
  97. for _, v in ipairs(tItemInfo) do
  98. if true == bUseBaoDi then
  99. if v.bBaoDi == true then
  100. if not tGoodsInfo[v.tPrize[1]] then
  101. tGoodsInfo[v.tPrize[1]] = 0
  102. end
  103. tGoodsInfo[v.tPrize[1]] = tGoodsInfo[v.tPrize[1]] + v.tPrize[2]
  104. break
  105. end
  106. else
  107. if nRandWeight <= v.nWeight then
  108. if not tGoodsInfo[v.tPrize[1]] then
  109. tGoodsInfo[v.tPrize[1]] = 0
  110. end
  111. tGoodsInfo[v.tPrize[1]] = tGoodsInfo[v.tPrize[1]] + v.tPrize[2]
  112. if v.bBaoDi == true then
  113. nAllNum = 0
  114. end
  115. break
  116. end
  117. end
  118. end
  119. end
  120. ComActFindTreasure_SetDBAllNum(human, nAllNum)
  121. return true, tGoodsInfo
  122. end
  123. ----------------------------------------- 内部处理结束 -------------------------------------
  124. function isOpen(human, YYInfo, funcConfig)
  125. local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID and funcConfig.funcID or COMMONACTFINDABSID)
  126. if not state then
  127. print("[CommonActFindTreasure_isOpen] 当前活动未开启")
  128. return
  129. end
  130. -- print("[CommonActFindTreasure_isOpen] 进入判断 endTime = "..endTime.." startTime = "..startTime)
  131. return state, endTime, startTime
  132. end
  133. function isActive(human, YYInfo, funcConfig)
  134. local state = isOpen(human, YYInfo, funcConfig)
  135. return not state
  136. end
  137. function isRed(human)
  138. local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  139. if not state then
  140. return false
  141. end
  142. ComActFindTreasure_CheckAndCreateDB(human)
  143. local nFreeNum = ComActFindTreasure_GetDBFreeNum(human)
  144. return nFreeNum > 0
  145. end
  146. function onZero(human, funcID)
  147. local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  148. if not state then
  149. return
  150. end
  151. ComActFindTreasure_SetDBFreeNum(human, 1)
  152. ComActFindTreasure_SetDBFreeRefreshTime(human, os.time())
  153. end
  154. function onLogin(human, funcID)
  155. local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  156. if not state then
  157. return
  158. end
  159. ComActFindTreasure_CheckAndCreateDB(human)
  160. local nFreeRefreshTime = ComActFindTreasure_GetDBFreeRefreshTime(human)
  161. if true ~= Util.isSameDayByTimes(nFreeRefreshTime, os.time()) then
  162. ComActFindTreasure_SetDBFreeNum(human, 1)
  163. ComActFindTreasure_SetDBFreeRefreshTime(human, os.time())
  164. end
  165. end
  166. ------------------------------------ 客户端请求处理开始 ---------------------------------
  167. function CommonActFindTreasure_Query(human)
  168. local nStatus = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  169. if not nStatus then
  170. return
  171. end
  172. ComActFindTreasure_CheckAndCreateDB(human)
  173. local msgRet = Msg.gc.GC_ABS_FESTIVAL_SEVENDAY_CARD_QUERY
  174. msgRet.nFreeNum = ComActFindTreasure_GetDBFreeNum(human)
  175. msgRet.nNowNum = ComActFindTreasure_GetDBAllNum(human)
  176. msgRet.nMaxNum = ComActFindTreasure_GetDoMaxNum()
  177. Grid.makeItem(msgRet.tUseItem, COMMONACTFINDUSEITEM, 1)
  178. msgRet.tPrize[0] = #Config
  179. for index, v in ipairs(Config) do
  180. local tData = msgRet.tPrize[index]
  181. tData.nID = index
  182. tData.nRare = v.nRare
  183. Grid.makeItem(tData.tPrize, v.tPrize[1], v.tPrize[2])
  184. end
  185. Msg.send(msgRet, human.fd)
  186. end
  187. function CommonActFindTreasure_Do(human, nType)
  188. local nStatus = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  189. if not nStatus then
  190. return
  191. end
  192. if 1 ~= nType and 2 ~= nType then
  193. print("[CommonActFindTreasure_Do] 上传的类型不正确 nType = "..nType)
  194. return
  195. end
  196. local nUseNum = 1 == nType and 1 or 10
  197. ComActFindTreasure_CheckAndCreateDB(human)
  198. local nFreeNum = ComActFindTreasure_GetDBFreeNum(human)
  199. local nDelNum = nUseNum - nFreeNum
  200. if nDelNum < 0 then
  201. nDelNum = 0
  202. end
  203. if nDelNum > 0 and not BagLogic.checkItemCnt(human, COMMONACTFINDUSEITEM, nDelNum) then
  204. print("[CommonActFindTreasure_Do] 消耗的物品不足 nFreeNum = "..nFreeNum.." nDelNum = "..nDelNum)
  205. return
  206. end
  207. local bRet, tItem = ComActFindTreasure_FindDo(human, nUseNum)
  208. if false == bRet then
  209. print("[CommonActFindTreasure_Do] 抽取动作执行失败")
  210. return
  211. end
  212. if nFreeNum > 0 then
  213. nFreeNum = nFreeNum >= nUseNum and nFreeNum - nUseNum or 0
  214. ComActFindTreasure_SetDBFreeNum(human, nFreeNum)
  215. end
  216. BagLogic.delItem(human, COMMONACTFINDUSEITEM, nDelNum, "CommonActFindTreasure")
  217. local tGoods = {}
  218. for nID, nNum in pairs(tItem) do
  219. table.insert(tGoods, {nID, nNum})
  220. end
  221. if nil ~= _G.next(tGoods) then
  222. BagLogic.addItemList(human, tGoods, "CommonActFindTreasure")
  223. CommonActFindTreasure_Query(human)
  224. end
  225. YunYingLogic.sendBanner(human)
  226. local config = AbsActExcel.absActivity[COMMONACTFINDABSID]
  227. YunYingLogic.sendGroupUpdate(YYInfo[COMMONACTFINDABSID], human, config.panelID)
  228. end