TreasureChestLogic.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. --------------------------------
  2. -- 文件名 : TreasureChestLogic.lua
  3. -- 文件说明 : 宝箱系统
  4. -- 创建时间 : 2025/03/10
  5. -- 创建人 : FC
  6. --------------------------------
  7. ---
  8. local Util = require("common.Util")
  9. local Msg = require("core.Msg")
  10. local BagLogic = require("bag.BagLogic")
  11. local Log = require("common.Log")
  12. local TreasureConf = require("excel.treasurechest")
  13. local CommonDefine = require("common.CommonDefine")
  14. local Grid = require("bag.Grid")
  15. local MainDianLogic = require("MaiDian.MaiDianLogic")
  16. local MaiDianDefine = require("MaiDian.MaiDianDefine")
  17. local TriggerDefine = require("trigger.TriggerDefine")
  18. local TriggerLogic = require("trigger.TriggerLogic")
  19. local WeekTaskLogic = require("dailyTask.WeekTaskLogic")
  20. -- 奖励缓存有序 key为宝箱类型
  21. local tCacheBoxPrize = nil
  22. -- 一次性最多打开宝箱数量
  23. local TREASURECHEST_OPEN_NUM = 9999
  24. -- 自动开宝箱解锁条件
  25. local TREASURECHEST_OPEN_AUTO_TYPE = 5 -- 钻石
  26. local TREASURECHEST_OPEN_AUTO_NUM = 2 -- 开两次
  27. ----------------------------------------- 内部处理开始 -------------------------------------
  28. -- 写日志
  29. local function TreasureChestLogic_WriteLog(human, szText)
  30. Log.write(Log.LOGID_OSS_COMMON, "name = "..human.db.name.." id = "..human.db._id..szText)
  31. end
  32. -- 获取宝箱配置
  33. local function TreasureChestLogic_GetBoxTypeConf()
  34. return TreasureConf.boxtype
  35. end
  36. -- 获取宝箱积分配置
  37. local function TreasureChestLogic_GetPointConf()
  38. return TreasureConf.boxpoint
  39. end
  40. -- 获取宝箱奖励配置
  41. local function TreasureChestLogic_GetPointPrizeConf()
  42. if not tCacheBoxPrize then
  43. tCacheBoxPrize = {}
  44. for _, v in pairs(TreasureConf.boxprize) do
  45. local nType = v.nType
  46. if not tCacheBoxPrize[nType] then
  47. tCacheBoxPrize[nType] = {}
  48. end
  49. table.insert(tCacheBoxPrize[nType], v)
  50. end
  51. -- 变成有序
  52. for _, v in pairs(tCacheBoxPrize) do
  53. table.sort(v, function (l, r)
  54. return l.nPro < r.nPro
  55. end)
  56. end
  57. -- 整合权重
  58. for _, data in ipairs(tCacheBoxPrize) do
  59. local nAllWeight = 0
  60. for _, v in ipairs(data) do
  61. if nAllWeight == 0 then
  62. nAllWeight = v.nPro
  63. else
  64. nAllWeight = nAllWeight + v.nPro
  65. end
  66. v.nAllPro = nAllWeight
  67. end
  68. end
  69. end
  70. return tCacheBoxPrize
  71. end
  72. -- 重置积分奖励数据
  73. local function TreasureChestLogic_ResetDBPointPrize(human)
  74. local tBoxPoint = TreasureChestLogic_GetPointConf()
  75. for key, v in pairs(tBoxPoint) do
  76. human.db.TreasureChest.tPointPrize[key] = CommonDefine.COMMON_PRIZE_STATE_NOGET
  77. end
  78. end
  79. -- 创建DB数据
  80. local function TreasureChestLogic_CreateDB(human)
  81. human.db.TreasureChest = {
  82. nPoint = 0,
  83. nOpenNum = 0,
  84. tPointPrize = {},
  85. tItem = {}, -- 宝箱数量
  86. }
  87. TreasureChestLogic_ResetDBPointPrize(human)
  88. end
  89. -- 获取当前积分
  90. local function TreasureChestLogic_GetDBPoint(human)
  91. if not human.db.TreasureChest then
  92. TreasureChestLogic_CreateDB(human)
  93. end
  94. return human.db.TreasureChest.nPoint
  95. end
  96. -- 设置当前积分
  97. local function TreasureChestLogic_SetDBPoint(human, nValue)
  98. if not human.db.TreasureChest then
  99. TreasureChestLogic_CreateDB(human)
  100. end
  101. human.db.TreasureChest.nPoint = nValue
  102. end
  103. -- 获取当前积分奖励状态
  104. local function TreasureChestLogic_GetDBPointPrize(human, nID)
  105. if not human.db.TreasureChest then
  106. TreasureChestLogic_CreateDB(human)
  107. end
  108. return human.db.TreasureChest.tPointPrize[nID]
  109. end
  110. -- 设置当前积分奖励状态
  111. local function TreasureChestLogic_SetDBPointPrize(human, nID, nState)
  112. if not human.db.TreasureChest then
  113. TreasureChestLogic_CreateDB(human)
  114. end
  115. human.db.TreasureChest.tPointPrize[nID] = nState
  116. end
  117. -- 更新积分奖励状态
  118. local function TreasureChestLogic_UpdatePointPrize(human)
  119. local nNowPoint = TreasureChestLogic_GetDBPoint(human)
  120. local tBoxPointCof = TreasureChestLogic_GetPointConf()
  121. for nID, v in ipairs(tBoxPointCof) do
  122. local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
  123. if nNowPoint >= v.nPoint then
  124. --print("[TreasureChestLogic_UpdatePointPrize] nID = "..nID.." nNowPoint "..nNowPoint.." nPoint "..v.nPoint.." nState "..nState)
  125. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState then
  126. TreasureChestLogic_SetDBPointPrize(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
  127. nNowPoint = nNowPoint - v.nPoint
  128. elseif CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
  129. nNowPoint = nNowPoint - v.nPoint
  130. end
  131. else
  132. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState then
  133. break
  134. end
  135. end
  136. end
  137. end
  138. -- 打开宝箱操作
  139. local function TreasureChestLogic_OpenBox(nType, nBoxNum)
  140. local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
  141. local tBoxPrize = TreasureChestLogic_GetPointPrizeConf()
  142. if not tBoxTypeConf[nType] or not tBoxPrize[nType] then
  143. return nil
  144. end
  145. local nOpenNum = tBoxTypeConf[nType].nOpenNum
  146. -- 获取的表是有序的
  147. local tBoxTypePrize = tBoxPrize[nType]
  148. local nLen = #tBoxTypePrize
  149. local nAllWeight = tBoxTypePrize[nLen].nAllPro
  150. --print("[TreasureChestLogic_OpenBox] nAllWeight = "..nAllWeight)
  151. local tOpenPrize = {}
  152. for i = 1, nBoxNum, 1 do
  153. for j = 1, nOpenNum, 1 do
  154. -- 随机权重
  155. local nRandNum = math.random(1, nAllWeight)
  156. --print("[TreasureChestLogic_OpenBox] j = "..j.." nRandNum = "..nRandNum)
  157. for _, v in pairs(tBoxTypePrize) do
  158. --print("[TreasureChestLogic_OpenBox] nAllPro = "..v.nAllPro.." ID = "..v.tPrize[1].." num = "..v.tPrize[2].."\n")
  159. if nRandNum <= v.nAllPro then
  160. table.insert(tOpenPrize, v.tPrize)
  161. --print("[TreasureChestLogic_OpenBox] 获得的道具 nID = "..v.tPrize[1].." nNum = "..v.tPrize[2].." nKey "..v.nAllPro)
  162. break
  163. end
  164. end
  165. end
  166. end
  167. return tOpenPrize
  168. end
  169. -- 获取宝箱数量
  170. local function TreasureChestLogic_GetGoodsNum(human, nGoodsID)
  171. if not human.db.TreasureChest.tItem[nGoodsID] then
  172. human.db.TreasureChest.tItem[nGoodsID] = 0
  173. end
  174. return human.db.TreasureChest.tItem[nGoodsID]
  175. end
  176. -- 添加宝箱物品
  177. local function TreasureChestLogic_AddGoods(human, nGoodsID, nGoodsNum)
  178. if not human.db.TreasureChest.tItem[nGoodsID] then
  179. human.db.TreasureChest.tItem[nGoodsID] = 0
  180. end
  181. human.db.TreasureChest.tItem[nGoodsID] = human.db.TreasureChest.tItem[nGoodsID] + nGoodsNum
  182. end
  183. -- 删除物品
  184. local function TreasureChestLogic_DelGoods(human, nGoodsID, nGoodsNum)
  185. human.db.TreasureChest.tItem[nGoodsID] = human.db.TreasureChest.tItem[nGoodsID] - nGoodsNum
  186. if 0 > human.db.TreasureChest.tItem[nGoodsID] then
  187. human.db.TreasureChest.tItem[nGoodsID] = 0
  188. end
  189. TreasureChestLogic_WriteLog(human, "减少了宝箱道具 nItemID "..nGoodsID.." nDelNum "..nGoodsNum)
  190. end
  191. -- 获取当前打开的钻石宝箱数量
  192. local function TreasureChestLogic_GetAutoOpenNum(human)
  193. if not human.db.TreasureChest then
  194. TreasureChestLogic_CreateDB(human)
  195. end
  196. return human.db.TreasureChest.nOpenNum
  197. end
  198. -- 记录打开钻石宝箱数量
  199. local function TreasureChestLogic_AddAutoNum(human, nType, nNum)
  200. if TREASURECHEST_OPEN_AUTO_TYPE ~= nType then
  201. print("[TreasureChestLogic_AddAutoNum] 类型不正确返回")
  202. return
  203. end
  204. local nNowNum = TreasureChestLogic_GetAutoOpenNum(human)
  205. if TREASURECHEST_OPEN_AUTO_NUM <= nNowNum then
  206. print("[TreasureChestLogic_AddAutoNum] 数量已经足够 nNowNum = "..nNowNum)
  207. return
  208. end
  209. human.db.TreasureChest.nOpenNum = human.db.TreasureChest.nOpenNum + nNum
  210. end
  211. ----------------------------------------- 客户端请求 -------------------------------------
  212. -- 请求宝箱界面信息
  213. function TreasureChestLogic_Query(human)
  214. if not human then
  215. return
  216. end
  217. if not human.db.TreasureChest then
  218. TreasureChestLogic_CreateDB(human)
  219. end
  220. TreasureChestLogic_UpdatePointPrize(human)
  221. -- table.print_lua_table(human.db.TreasureChest)
  222. -- print("\n")
  223. --local nOpenNum = TreasureChestLogic_GetAutoOpenNum(human)
  224. local tMsgData = Msg.gc.GC_TEEASURECHEST_QUERY
  225. tMsgData.nNowPoint = TreasureChestLogic_GetDBPoint(human)
  226. tMsgData.nAuto = TreasureChestLogic_GetAutoOpenNum(human) >= TREASURECHEST_OPEN_AUTO_NUM and 1 or 0
  227. tMsgData.nID = 0
  228. tMsgData.nNextPoint = 0
  229. tMsgData.nState = 0
  230. local tBoxPointConf = TreasureChestLogic_GetPointConf()
  231. local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
  232. --print("[TreasureChestLogic_Query] nAuto = "..tMsgData.nAuto.." num = "..nOpenNum)
  233. -- 下一阶段需要的积分信息
  234. for nID, v in ipairs(tBoxPointConf) do
  235. local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
  236. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState or CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
  237. tMsgData.nID = nID
  238. tMsgData.nNextPoint = v.nPoint
  239. tMsgData.nState = nState
  240. Grid.makeItem(tMsgData.tPointPirze, v.tPrize[1], v.tPrize[2])
  241. break
  242. end
  243. end
  244. -- 奖励信息
  245. local nLen = 0
  246. for nType, v in pairs(tBoxTypeConf) do
  247. nLen = nLen + 1
  248. tMsgData.tList[0] = nLen
  249. local tData = tMsgData.tList[nLen]
  250. tData.nType = nType
  251. local nGoodsID = v.nItemID
  252. local nGoodsNum = TreasureChestLogic_GetGoodsNum(human, nGoodsID)
  253. Grid.makeItem(tData.tItemData, nGoodsID, nGoodsNum)
  254. end
  255. Msg.send(tMsgData, human.fd)
  256. end
  257. -- 请求宝箱内奖励信息
  258. function TreasureChestLogic_QueryPrize(human, nBoxType)
  259. local tBoxPrize = TreasureChestLogic_GetPointPrizeConf()
  260. if not tBoxPrize[nBoxType] then
  261. print("[TreasureChestLogic_QueryPrize] 不存在对应的奖励配置 nBoxType = "..nBoxType)
  262. return
  263. end
  264. local tBoxTypePrize = tBoxPrize[nBoxType]
  265. local tMsgData = Msg.gc.GC_TEEASURECHEST_PRIZE_QUERY
  266. local nLen = 0
  267. tMsgData.tItemData[0] = nLen
  268. for _, v in pairs(tBoxTypePrize) do
  269. nLen = nLen + 1
  270. tMsgData.tItemData[0] = nLen
  271. local tData = tMsgData.tItemData[nLen]
  272. local nGoodsID = v.prize[1]
  273. local nGoodsNum = v.prize[2]
  274. local quality = v.prize[3]
  275. Grid.makeItem(tData, nGoodsID, nGoodsNum, nil, nil, nil, nil, quality)
  276. end
  277. Msg.send(tMsgData, human.fd)
  278. end
  279. -- 请求打开宝箱
  280. function TreasureChestLogic_Open(human, nBoxType, nBoxNum)
  281. print("[TreasureChestLogic_Open]玩家当前请求打开的宝箱类型 ntyepe = "..nBoxType)
  282. local szText = "[TreasureChestLogic_Open] 玩家请求打开宝箱 nBoxType = "..nBoxType.." nBoxNum = "..nBoxNum
  283. TreasureChestLogic_WriteLog(human, szText)
  284. if nBoxNum >= TREASURECHEST_OPEN_NUM then
  285. szText = szText .. " 失败不正确的打开数量"
  286. TreasureChestLogic_WriteLog(human, szText)
  287. return
  288. end
  289. -- 检测配置
  290. local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
  291. if not tBoxTypeConf[nBoxType] then
  292. print("[TreasureChestLogic_Open] 不存在对应的宝箱类型 nBoxType = "..nBoxType)
  293. szText = szText.." 失败不存在对应宝箱类型"
  294. TreasureChestLogic_WriteLog(human, szText)
  295. return
  296. end
  297. local nGoodsID = tBoxTypeConf[nBoxType].nItemID
  298. local nGoodsNum = TreasureChestLogic_GetGoodsNum(human, nGoodsID)
  299. if nBoxNum > nGoodsNum then
  300. print("[TreasureChestLogic_Open] 玩家拥有宝箱数量不足 nBoxType = "
  301. ..nBoxType.." nBoxNum = "..nBoxNum.." nGoodsNum = "..nGoodsNum)
  302. szText = szText.." 数量不正确 nGoodsNum = "..nGoodsNum
  303. TreasureChestLogic_WriteLog(human, szText)
  304. return
  305. end
  306. local tPrize = TreasureChestLogic_OpenBox(nBoxType, nBoxNum)
  307. -- 发送奖励
  308. BagLogic.addItemList(human, tPrize, "treasurechest")
  309. --BagLogic.sendItemGetList(human, tPrize, "treasurechest")
  310. szText = szText .." 发送奖励成功"
  311. TreasureChestLogic_WriteLog(human, szText)
  312. -- 加积分
  313. local nAddPoint = nBoxNum * tBoxTypeConf[nBoxType].nPoint
  314. local nNowPoint = TreasureChestLogic_GetDBPoint(human)
  315. nNowPoint = nAddPoint + nNowPoint
  316. TreasureChestLogic_SetDBPoint(human, nNowPoint)
  317. szText = szText.." 增加积分 nAddPoint = "..nAddPoint.." nNowPoint = "..nNowPoint
  318. TreasureChestLogic_WriteLog(human, szText)
  319. -- 删除使用了的物品
  320. TreasureChestLogic_DelGoods(human, nGoodsID, nBoxNum)
  321. -- 更新积分奖励状态
  322. TreasureChestLogic_UpdatePointPrize(human)
  323. MainDianLogic.MaiDian_Begin(human, MaiDianDefine.MAIDIAN_TYPE_CHEST_OPEN,{nValue = nBoxNum})
  324. if TREASURECHEST_OPEN_AUTO_TYPE == nBoxType then
  325. TreasureChestLogic_AddAutoNum(human, nBoxType, nBoxNum)
  326. end
  327. TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_OPENBOX, human.db._id, nBoxNum,nBoxType)
  328. print("TreasureChestLogic..当前已经进入商业活动注册事件",nBoxType)
  329. --周任务开启宝箱
  330. WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_5, 1)
  331. TreasureChestLogic_Query(human)
  332. end
  333. -- 请求自动打开宝箱
  334. function TreasureChestLogic_AutoOpen(human, nBoxType)
  335. local szText = "[TreasureChestLogic_AutoOpen] 玩家请求打开宝箱开始 nBoxType = "..nBoxType
  336. local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
  337. if not tBoxTypeConf[nBoxType] then
  338. print("[TreasureChestLogic_AutoOpen] 不存在对应的宝箱类型 nBoxType = "..nBoxType)
  339. szText = szText.." 失败不存在对应宝箱类型"
  340. TreasureChestLogic_WriteLog(human, szText)
  341. return
  342. end
  343. local nGoodsID = tBoxTypeConf[nBoxType].nItemID
  344. local nGoodsNum = TreasureChestLogic_GetGoodsNum(human, nGoodsID)
  345. if 0 >= nGoodsNum then
  346. return
  347. end
  348. if TreasureChestLogic_GetAutoOpenNum(human) < TREASURECHEST_OPEN_AUTO_NUM then
  349. return
  350. end
  351. TreasureChestLogic_WriteLog(human, szText)
  352. TreasureChestLogic_Open(human, nBoxType, 1)
  353. end
  354. -- 请求领取积分奖励
  355. function TreasureChestLogic_GetPointPrize(human, nID)
  356. local szText = "[TreasureChestLogic_GetPointPrize] 玩家请求领取积分奖励 nID = "..nID
  357. local tPointPrize = TreasureChestLogic_GetPointConf()
  358. if not tPointPrize[nID] then
  359. print("[TreasureChestLogic_GetPointPrize] 不存在对应的积分ID nID = "..nID)
  360. return
  361. end
  362. -- 积分检测
  363. local nNowPoint = TreasureChestLogic_GetDBPoint(human)
  364. if nNowPoint < tPointPrize[nID].nPoint then
  365. print("[TreasureChestLogic_GetPointPrize] 玩家当前积分不足 nNowPoint = "
  366. ..nNowPoint.." nNeedPoint = "..tPointPrize[nID].nPoint)
  367. return
  368. end
  369. local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
  370. if CommonDefine.COMMON_PRIZE_STATE_CANGET ~= nState then
  371. print("[TreasureChestLogic_GetPointPrize] 玩家奖励状态不正确 nNowPoint = "
  372. ..nNowPoint.." nState = "..nState.."nID = "..nID)
  373. return
  374. end
  375. local tGoodsInfo =
  376. {
  377. [tPointPrize[nID].tPrize[1]] = tPointPrize[nID].tPrize[2]
  378. }
  379. -- 添加奖励
  380. BagLogic.addItemList(human, tGoodsInfo, "treasurechest")
  381. -- BagLogic.sendItemGetList(human, tGoodsInfo, "treasurechest")
  382. TreasureChestLogic_SetDBPointPrize(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
  383. local szSendPrize = szText .. " 发送奖励成功 nGoodsID = "..tPointPrize[nID].tPrize[1]
  384. .." nGoodsNum = "..tPointPrize[nID].tPrize[2]
  385. TreasureChestLogic_WriteLog(human, szSendPrize)
  386. -- 改变积分
  387. local nNewPoint = nNowPoint - tPointPrize[nID].nPoint
  388. TreasureChestLogic_SetDBPoint(human, nNewPoint)
  389. local szPointPrize = szText.." nNowPoint = "..nNowPoint.." nDelPoint = "
  390. ..tPointPrize[nID].nPoint.." nNewPoint = "..nNewPoint
  391. TreasureChestLogic_WriteLog(human, szPointPrize)
  392. if 0 == tPointPrize[nID].nNextID then
  393. TreasureChestLogic_ResetDBPointPrize(human)
  394. local szResetText = szText.." 玩家领取完最后的奖励进行重置"
  395. TreasureChestLogic_WriteLog(human, szResetText)
  396. end
  397. -- 更新积分奖励状态
  398. TreasureChestLogic_UpdatePointPrize(human)
  399. TreasureChestLogic_Query(human)
  400. end
  401. function TreasureChestLogic_GmClear(human)
  402. TreasureChestLogic_CreateDB(human)
  403. end
  404. -- 增加道具
  405. function TreasureChestLogic_AddItem(human, nItemID, nAddNum)
  406. if not human.db.TreasureChest then
  407. TreasureChestLogic_CreateDB(human)
  408. end
  409. if 0 >= nAddNum then
  410. return
  411. end
  412. TreasureChestLogic_AddGoods(human, nItemID, nAddNum)
  413. TreasureChestLogic_WriteLog(human, "增加了宝箱道具 nItemID "..nItemID.." nAddNum "..nAddNum)
  414. end
  415. -- 购买终身月卡解锁自动开宝箱
  416. function TreasureChestLogic_BuyOpenAuto(human)
  417. --local nOpenNum = TreasureChestLogic_GetAutoOpenNum(human)
  418. --print("[TreasureChestLogic_BuyOpenAuto] nOpenNum = "..nOpenNum)
  419. TreasureChestLogic_AddAutoNum(human, TREASURECHEST_OPEN_AUTO_TYPE, TREASURECHEST_OPEN_AUTO_NUM)
  420. end