TreasureChestLogic.lua 17 KB

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