TreasureChestLogic.lua 17 KB

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