TreasureChestLogic.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. return human.db.TreasureChest.nOpenNum
  189. end
  190. -- 记录打开钻石宝箱数量
  191. local function TreasureChestLogic_AddAutoNum(human, nType, nNum)
  192. if TREASURECHEST_OPEN_AUTO_TYPE ~= nType then
  193. return
  194. end
  195. local nNowNum = TreasureChestLogic_GetAutoOpenNum(human)
  196. if TREASURECHEST_OPEN_AUTO_NUM <= nNowNum then
  197. return
  198. end
  199. human.db.TreasureChest.nOpenNum = human.db.TreasureChest.nOpenNum + nNum
  200. end
  201. ----------------------------------------- 客户端请求 -------------------------------------
  202. -- 请求宝箱界面信息
  203. function TreasureChestLogic_Query(human)
  204. if not human then
  205. return
  206. end
  207. if not human.db.TreasureChest then
  208. TreasureChestLogic_CreateDB(human)
  209. end
  210. TreasureChestLogic_UpdatePointPrize(human)
  211. --table.print_lua_table(human.db.TreasureChest)
  212. --print("\n")
  213. local tMsgData = Msg.gc.GC_TEEASURECHEST_QUERY
  214. tMsgData.nNowPoint = TreasureChestLogic_GetDBPoint(human)
  215. tMsgData.nAuto = TreasureChestLogic_GetAutoOpenNum(human) >= TREASURECHEST_OPEN_AUTO_NUM and 1 or 0
  216. tMsgData.nID = 0
  217. tMsgData.nNextPoint = 0
  218. tMsgData.nState = 0
  219. local tBoxPointConf = TreasureChestLogic_GetPointConf()
  220. local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
  221. -- 下一阶段需要的积分信息
  222. for nID, v in ipairs(tBoxPointConf) do
  223. local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
  224. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState or CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
  225. tMsgData.nID = nID
  226. tMsgData.nNextPoint = v.nPoint
  227. tMsgData.nState = nState
  228. Grid.makeItem(tMsgData.tPointPirze, v.tPrize[1], v.tPrize[2])
  229. break
  230. end
  231. end
  232. -- 奖励信息
  233. local nLen = 0
  234. for nType, v in pairs(tBoxTypeConf) do
  235. nLen = nLen + 1
  236. tMsgData.tList[0] = nLen
  237. local tData = tMsgData.tList[nLen]
  238. tData.nType = nType
  239. local nGoodsID = v.nItemID
  240. local nGoodsNum = TreasureChestLogic_GetGoodsNum(human, nGoodsID)
  241. Grid.makeItem(tData.tItemData, nGoodsID, nGoodsNum)
  242. end
  243. Msg.send(tMsgData, human.fd)
  244. end
  245. -- 请求宝箱内奖励信息
  246. function TreasureChestLogic_QueryPrize(human, nBoxType)
  247. local tBoxPrize = TreasureChestLogic_GetPointPrizeConf()
  248. if not tBoxPrize[nBoxType] then
  249. print("[TreasureChestLogic_QueryPrize] 不存在对应的奖励配置 nBoxType = "..nBoxType)
  250. return
  251. end
  252. local tBoxTypePrize = tBoxPrize[nBoxType]
  253. local tMsgData = Msg.gc.GC_TEEASURECHEST_PRIZE_QUERY
  254. local nLen = 0
  255. tMsgData.tItemData[0] = nLen
  256. for _, v in pairs(tBoxTypePrize) do
  257. nLen = nLen + 1
  258. tMsgData.tItemData[0] = nLen
  259. local tData = tMsgData.tItemData[nLen]
  260. local nGoodsID = v.prize[1]
  261. local nGoodsNum = v.prize[2]
  262. local quality = v.prize[3]
  263. Grid.makeItem(tData, nGoodsID, nGoodsNum, nil, nil, nil, nil, quality)
  264. end
  265. Msg.send(tMsgData, human.fd)
  266. end
  267. -- 请求打开宝箱
  268. function TreasureChestLogic_Open(human, nBoxType, nBoxNum)
  269. local szText = "[TreasureChestLogic_Open] 玩家请求打开宝箱 nBoxType = "..nBoxType.." nBoxNum = "..nBoxNum
  270. TreasureChestLogic_WriteLog(human, szText)
  271. if nBoxNum >= TREASURECHEST_OPEN_NUM then
  272. szText = szText .. " 失败不正确的打开数量"
  273. TreasureChestLogic_WriteLog(human, szText)
  274. return
  275. end
  276. -- 检测配置
  277. local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
  278. if not tBoxTypeConf[nBoxType] then
  279. print("[TreasureChestLogic_Open] 不存在对应的宝箱类型 nBoxType = "..nBoxType)
  280. szText = szText.." 失败不存在对应宝箱类型"
  281. TreasureChestLogic_WriteLog(human, szText)
  282. return
  283. end
  284. local nGoodsID = tBoxTypeConf[nBoxType].nItemID
  285. local nGoodsNum = TreasureChestLogic_GetGoodsNum(human, nGoodsID)
  286. if nBoxNum > nGoodsNum then
  287. print("[TreasureChestLogic_Open] 玩家拥有宝箱数量不足 nBoxType = "
  288. ..nBoxType.." nBoxNum = "..nBoxNum.." nGoodsNum = "..nGoodsNum)
  289. szText = szText.." 数量不正确 nGoodsNum = "..nGoodsNum
  290. TreasureChestLogic_WriteLog(human, szText)
  291. return
  292. end
  293. local tPrize = TreasureChestLogic_OpenBox(nBoxType, nBoxNum)
  294. -- 发送奖励
  295. BagLogic.addItemList(human, tPrize, "treasurechest")
  296. --BagLogic.sendItemGetList(human, tPrize, "treasurechest")
  297. szText = szText .." 发送奖励成功"
  298. TreasureChestLogic_WriteLog(human, szText)
  299. -- 加积分
  300. local nAddPoint = nBoxNum * tBoxTypeConf[nBoxType].nPoint
  301. local nNowPoint = TreasureChestLogic_GetDBPoint(human)
  302. nNowPoint = nAddPoint + nNowPoint
  303. TreasureChestLogic_SetDBPoint(human, nNowPoint)
  304. szText = szText.." 增加积分 nAddPoint = "..nAddPoint.." nNowPoint = "..nNowPoint
  305. TreasureChestLogic_WriteLog(human, szText)
  306. -- 删除使用了的物品
  307. TreasureChestLogic_DelGoods(human, nGoodsID, nBoxNum)
  308. -- 更新积分奖励状态
  309. TreasureChestLogic_UpdatePointPrize(human)
  310. if TREASURECHEST_OPEN_AUTO_TYPE == nBoxType then
  311. TreasureChestLogic_AddAutoNum(human, nBoxType, nBoxNum)
  312. end
  313. TreasureChestLogic_Query(human)
  314. end
  315. -- 请求自动打开宝箱
  316. function TreasureChestLogic_AutoOpen(human, nBoxType)
  317. local szText = "[TreasureChestLogic_AutoOpen] 玩家请求打开宝箱开始 nBoxType = "..nBoxType
  318. local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
  319. if not tBoxTypeConf[nBoxType] then
  320. print("[TreasureChestLogic_AutoOpen] 不存在对应的宝箱类型 nBoxType = "..nBoxType)
  321. szText = szText.." 失败不存在对应宝箱类型"
  322. TreasureChestLogic_WriteLog(human, szText)
  323. return
  324. end
  325. local nGoodsID = tBoxTypeConf[nBoxType].nItemID
  326. local nGoodsNum = TreasureChestLogic_GetGoodsNum(human, nGoodsID)
  327. if 0 >= nGoodsNum then
  328. return
  329. end
  330. if TreasureChestLogic_GetAutoOpenNum(human) < TREASURECHEST_OPEN_AUTO_NUM then
  331. return
  332. end
  333. TreasureChestLogic_WriteLog(human, szText)
  334. TreasureChestLogic_Open(human, nBoxType, 1)
  335. end
  336. -- 请求领取积分奖励
  337. function TreasureChestLogic_GetPointPrize(human, nID)
  338. local szText = "[TreasureChestLogic_GetPointPrize] 玩家请求领取积分奖励 nID = "..nID
  339. local tPointPrize = TreasureChestLogic_GetPointConf()
  340. if not tPointPrize[nID] then
  341. print("[TreasureChestLogic_GetPointPrize] 不存在对应的积分ID nID = "..nID)
  342. return
  343. end
  344. -- 积分检测
  345. local nNowPoint = TreasureChestLogic_GetDBPoint(human)
  346. if nNowPoint < tPointPrize[nID].nPoint then
  347. print("[TreasureChestLogic_GetPointPrize] 玩家当前积分不足 nNowPoint = "
  348. ..nNowPoint.." nNeedPoint = "..tPointPrize[nID].nPoint)
  349. return
  350. end
  351. local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
  352. if CommonDefine.COMMON_PRIZE_STATE_CANGET ~= nState then
  353. print("[TreasureChestLogic_GetPointPrize] 玩家奖励状态不正确 nNowPoint = "
  354. ..nNowPoint.." nState = "..nState.."nID = "..nID)
  355. return
  356. end
  357. local tGoodsInfo =
  358. {
  359. [tPointPrize[nID].tPrize[1]] = tPointPrize[nID].tPrize[2]
  360. }
  361. -- 添加奖励
  362. BagLogic.addItemList(human, tGoodsInfo, "treasurechest")
  363. -- BagLogic.sendItemGetList(human, tGoodsInfo, "treasurechest")
  364. TreasureChestLogic_SetDBPointPrize(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
  365. local szSendPrize = szText .. " 发送奖励成功 nGoodsID = "..tPointPrize[nID].tPrize[1]
  366. .." nGoodsNum = "..tPointPrize[nID].tPrize[2]
  367. TreasureChestLogic_WriteLog(human, szSendPrize)
  368. -- 改变积分
  369. local nNewPoint = nNowPoint - tPointPrize[nID].nPoint
  370. TreasureChestLogic_SetDBPoint(human, nNewPoint)
  371. local szPointPrize = szText.." nNowPoint = "..nNowPoint.." nDelPoint = "
  372. ..tPointPrize[nID].nPoint.." nNewPoint = "..nNewPoint
  373. TreasureChestLogic_WriteLog(human, szPointPrize)
  374. if 0 == tPointPrize[nID].nNextID then
  375. TreasureChestLogic_ResetDBPointPrize(human)
  376. local szResetText = szText.." 玩家领取完最后的奖励进行重置"
  377. TreasureChestLogic_WriteLog(human, szResetText)
  378. end
  379. -- 更新积分奖励状态
  380. TreasureChestLogic_UpdatePointPrize(human)
  381. TreasureChestLogic_Query(human)
  382. end
  383. function TreasureChestLogic_GmClear(human)
  384. TreasureChestLogic_CreateDB(human)
  385. end
  386. -- 增加道具
  387. function TreasureChestLogic_AddItem(human, nItemID, nAddNum)
  388. if not human.db.TreasureChest then
  389. TreasureChestLogic_CreateDB(human)
  390. end
  391. if 0 >= nAddNum then
  392. return
  393. end
  394. TreasureChestLogic_AddGoods(human, nItemID, nAddNum)
  395. TreasureChestLogic_WriteLog(human, "增加了宝箱道具 nItemID "..nItemID.." nAddNum "..nAddNum)
  396. end