TreasureChestLogic.lua 14 KB

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