CommonActFindTreasure.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 COMMONACTFINDABSID = 7778 -- 对应ABS活动ID
  17. local COMMONACTFINDUSEITEM = 1243 -- 寻宝使用道具ID
  18. local COMMONACTDOMAXNUM = nil -- 最大抽取保底次数
  19. ----------------------------------------- 内部处理开始 -------------------------------------
  20. local function ComActFindTreasure_CreateDB(human)
  21. if not human.db.absAct[COMMONACTFINDABSID] then
  22. human.db.absAct[COMMONACTFINDABSID] = {}
  23. end
  24. human.db.absAct[COMMONACTFINDABSID].tExtract = {
  25. nFreeNum = 1, -- 免费次数
  26. nFreeRefreshTime = os.time(), -- 免费刷新次数
  27. nAllNum = 0, -- 总次数
  28. }
  29. end
  30. -- 检测并创建DB数据
  31. local function ComActFindTreasure_CheckAndCreateDB(human)
  32. if not human.db.absAct[COMMONACTFINDABSID] or not human.db.absAct[COMMONACTFINDABSID].tExtract then
  33. ComActFindTreasure_CreateDB(human)
  34. end
  35. end
  36. local function ComActFindTreasure_GetDoMaxNum()
  37. if nil ~= COMMONACTDOMAXNUM then
  38. return COMMONACTDOMAXNUM
  39. end
  40. for _, v in ipairs(Config) do
  41. if not COMMONACTDOMAXNUM then
  42. COMMONACTDOMAXNUM = v.nNeedNum
  43. else
  44. COMMONACTDOMAXNUM = math.max(COMMONACTDOMAXNUM, v.nNeedNum)
  45. end
  46. end
  47. return COMMONACTDOMAXNUM
  48. end
  49. -- 获取免费次数
  50. local function ComActFindTreasure_GetDBFreeNum(human)
  51. return human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeNum
  52. end
  53. -- 设置免费次数
  54. local function ComActFindTreasure_SetDBFreeNum(human, nNum)
  55. human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeNum = nNum
  56. end
  57. -- 获取刷新时间
  58. local function ComActFindTreasure_GetDBFreeRefreshTime(human)
  59. return human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeRefreshTime
  60. end
  61. -- 设置刷新时间
  62. local function ComActFindTreasure_SetDBFreeRefreshTime(human, nTime)
  63. human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeRefreshTime = nTime
  64. end
  65. -- 获取总次数
  66. local function ComActFindTreasure_GetDBAllNum(human)
  67. return human.db.absAct[COMMONACTFINDABSID].tExtract.nAllNum
  68. end
  69. -- 设置总次数
  70. local function ComActFindTreasure_SetDBAllNum(human, nNum)
  71. human.db.absAct[COMMONACTFINDABSID].tExtract.nAllNum = nNum
  72. end
  73. -- 抽取动作
  74. local function ComActFindTreasure_FindDo(human, nDoNum)
  75. local nAllNum = ComActFindTreasure_GetDBAllNum(human)
  76. local nMaxDoNum = ComActFindTreasure_GetDoMaxNum()
  77. local nAllWeight, tItemInfo = 0, {}
  78. for _, v in ipairs(Config) do
  79. nAllWeight = nAllWeight + v.nWeight
  80. local bBaoDi = false
  81. if nMaxDoNum == v.nNeedNum then
  82. bBaoDi = true
  83. end
  84. table.insert(tItemInfo, {nWeight = nAllWeight, tPrize = v.tPrize, bBaoDi = bBaoDi});
  85. end
  86. local tGoodsInfo = {}
  87. for i = 1, nDoNum, 1 do
  88. nAllNum = nAllNum + 1
  89. local bUseBaoDi = false
  90. if nAllNum >= nMaxDoNum then
  91. nAllNum = 0
  92. bUseBaoDi = true
  93. else
  94. nAllNum = nAllNum + 1
  95. end
  96. local nRandWeight = math.random(1, nAllWeight)
  97. for _, v in ipairs(tItemInfo) do
  98. if true == bUseBaoDi and v.bBaoDi == true then
  99. if not tGoodsInfo[v.tPrize[1]] then
  100. tGoodsInfo[v.tPrize[1]] = 0
  101. end
  102. tGoodsInfo[v.tPrize[1]] = tGoodsInfo[v.tPrize[1]] + v.tPrize[2]
  103. break
  104. else
  105. if nRandWeight <= v.nWeight then
  106. if not tGoodsInfo[v.tPrize[1]] then
  107. tGoodsInfo[v.tPrize[1]] = 0
  108. end
  109. tGoodsInfo[v.tPrize[1]] = tGoodsInfo[v.tPrize[1]] + v.tPrize[2]
  110. break
  111. end
  112. end
  113. end
  114. end
  115. ComActFindTreasure_SetDBAllNum(human, nAllNum)
  116. return true, tGoodsInfo
  117. end
  118. ----------------------------------------- 内部处理结束 -------------------------------------
  119. function isOpen(human, YYInfo, funcConfig)
  120. local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID and funcConfig.funcID or COMMONACTFINDABSID)
  121. return state, endTime, startTime
  122. end
  123. function isActive(human, YYInfo, funcConfig)
  124. local state = isOpen(human, YYInfo, funcConfig)
  125. return not state
  126. end
  127. function isRed(human)
  128. local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  129. if not state then
  130. return false
  131. end
  132. return false
  133. end
  134. function onZero(human, funcID)
  135. local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  136. if not state then
  137. return
  138. end
  139. ComActFindTreasure_SetDBFreeNum(human, 1)
  140. ComActFindTreasure_SetDBFreeRefreshTime(human, os.time())
  141. end
  142. function onLogin(human, funcID)
  143. local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
  144. if not state then
  145. return
  146. end
  147. ComActFindTreasure_CheckAndCreateDB(human)
  148. local nFreeRefreshTime = ComActFindTreasure_GetDBFreeRefreshTime(human)
  149. if true ~= Util.isSameDayByTimes(nFreeRefreshTime, os.time()) then
  150. ComActFindTreasure_SetDBFreeNum(human, 1)
  151. ComActFindTreasure_SetDBFreeRefreshTime(human, os.time())
  152. end
  153. end
  154. ------------------------------------ 客户端请求处理开始 ---------------------------------
  155. function CommonActFindTreasure_Query(human)
  156. local nStatus = isOpen(human)
  157. if not nStatus then
  158. return
  159. end
  160. ComActFindTreasure_CheckAndCreateDB(human)
  161. local msgRet = Msg.gc.GC_ABS_FESTIVAL_SEVENDAY_CARD_QUERY
  162. msgRet.nNowNum = ComActFindTreasure_GetDBFreeNum(human)
  163. msgRet.nMaxNum = ComActFindTreasure_GetDBAllNum(human)
  164. Grid.makeItem(msgRet.tUseItem, COMMONACTFINDUSEITEM, 1)
  165. msgRet.tPrize[0] = #Config
  166. for index, v in ipairs(Config) do
  167. local tData = msgRet.tPrize[index]
  168. tData.nID = index
  169. tData.nRare = v.nRare
  170. Grid.makeItem(tData.tPrize, v.tPrize[1], v.tPrize[2])
  171. end
  172. Msg.send(msgRet, human.fd)
  173. end
  174. function CommonActFindTreasure_Do(human, nType)
  175. local nStatus = isOpen(human)
  176. if not nStatus then
  177. return
  178. end
  179. if 1 ~= nType and 10 ~= nType then
  180. return
  181. end
  182. ComActFindTreasure_CheckAndCreateDB(human)
  183. local nFreeNum = ComActFindTreasure_GetDBFreeNum(human)
  184. local nDelNum = nType - nFreeNum
  185. if nDelNum < 0 then
  186. nDelNum = 0
  187. end
  188. if not BagLogic.checkItemCnt(human, COMMONACTFINDUSEITEM, nDelNum) then
  189. return
  190. end
  191. local bRet, tItem = ComActFindTreasure_FindDo(human, nType)
  192. if false == bRet then
  193. return
  194. end
  195. if nFreeNum > 0 then
  196. nFreeNum = nFreeNum > nType and nFreeNum - nType or 0
  197. ComActFindTreasure_SetDBFreeNum(human, nFreeNum)
  198. end
  199. BagLogic.delItem(human, COMMONACTFINDUSEITEM, nDelNum, "CommonActFindTreasure")
  200. local tGoods = {}
  201. for nID, nNum in pairs(tItem) do
  202. table.insert(tGoods, {nID, nNum})
  203. end
  204. if nil ~= _G.next(tGoods) then
  205. BagLogic.addItemList(human, tGoods, "CommonActFindTreasure")
  206. CommonActFindTreasure_Query(human)
  207. end
  208. end