TreasureChestLogic.lua 12 KB

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