| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- --------------------------------
- -- 文件名 : TreasureChestLogic.lua
- -- 文件说明 : 宝箱系统
- -- 创建时间 : 2025/03/10
- -- 创建人 : FC
- --------------------------------
- ---
- local Util = require("common.Util")
- local Msg = require("core.Msg")
- local BagLogic = require("bag.BagLogic")
- local Log = require("common.Log")
- local TreasureConf = require("excel.treasurechest")
- local CommonDefine = require("common.CommonDefine")
- local Grid = require("bag.Grid")
- -- 奖励缓存有序 key为宝箱类型
- local tCacheBoxPrize = nil
- -- 一次性最多打开宝箱数量
- local TREASURECHEST_OPEN_NUM = 9999
- ----------------------------------------- 内部处理开始 -------------------------------------
- -- 写日志
- local function TreasureChestLogic_WriteLog(human, szText)
- Log.write(Log.LOGID_OSS_COMMON, "name = "..human.db.name.." id = "..human.db._id..szText)
- end
- -- 获取宝箱配置
- local function TreasureChestLogic_GetBoxTypeConf()
- return TreasureConf.boxtype
- end
- -- 获取宝箱积分配置
- local function TreasureChestLogic_GetPointConf()
- return TreasureConf.boxpoint
- end
- -- 获取宝箱奖励配置
- local function TreasureChestLogic_GetPointPrizeConf()
- if not tCacheBoxPrize then
- tCacheBoxPrize = {}
- for _, v in pairs(TreasureConf.boxprize) do
- local nType = v.nType
- if not tCacheBoxPrize[nType] then
- tCacheBoxPrize[nType] = {}
- end
- table.insert(tCacheBoxPrize[nType], v)
- end
- for _, v in pairs(tCacheBoxPrize) do
- table.sort(v, function (l, r)
- return l.nPro < r.nPro
- end)
- end
- end
- return tCacheBoxPrize
- end
- -- 重置积分奖励数据
- local function TreasureChestLogic_ResetDBPointPrize(human)
- local tBoxPoint = TreasureChestLogic_GetPointConf()
-
- for key, v in pairs(tBoxPoint) do
- if human.db.TreasureChest.tPointPrize[key] then
- human.db.TreasureChest.tPointPrize[key] = CommonDefine.COMMON_PRIZE_STATE_NOGET
- end
- end
- end
- -- 创建DB数据
- local function TreasureChestLogic_CreateDB(human)
- human.db.TreasureChest = {
- nPoint = 0,
- tPointPrize = {},
- }
- TreasureChestLogic_ResetDBPointPrize(human)
- end
- -- 获取当前积分
- local function TreasureChestLogic_GetDBPoint(human)
- if not human.db.TreasureChest then
- TreasureChestLogic_CreateDB(human)
- end
- return human.db.TreasureChest.nPoint
- end
- -- 设置当前积分
- local function TreasureChestLogic_SetDBPoint(human, nValue)
- if not human.db.TreasureChest then
- TreasureChestLogic_CreateDB(human)
- end
- human.db.TreasureChest.nPoint = nValue
- end
- -- 获取当前积分奖励状态
- local function TreasureChestLogic_GetDBPointPrize(human, nID)
- if not human.db.TreasureChest then
- TreasureChestLogic_CreateDB(human)
- end
- return human.db.TreasureChest.tPointPrize[nID]
- end
- -- 设置当前积分奖励状态
- local function TreasureChestLogic_SetDBPointPrize(human, nID, nState)
- if not human.db.TreasureChest then
- TreasureChestLogic_CreateDB(human)
- end
- human.db.TreasureChest.tPointPrize[nID] = nState
- end
- -- 更新积分奖励状态
- local function TreasureChestLogic_UpdatePointPrize(human)
- local nNowPoint = TreasureChestLogic_GetDBPoint(human)
- local tBoxPointCof = TreasureChestLogic_GetPointConf()
- for nID, v in ipairs(tBoxPointCof) do
- if nNowPoint >= v.nPoint then
- local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
- if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState then
- TreasureChestLogic_SetDBPointPrize(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
- nNowPoint = nNowPoint - v.nPoint
- end
- end
- end
- end
- -- 打开宝箱操作
- local function TreasureChestLogic_OpenBox(nType, nBoxNum)
- local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
- local tBoxPrize = TreasureChestLogic_GetPointPrizeConf()
- if not tBoxTypeConf[nType] or not tBoxPrize[nType] then
- return nil
- end
- local nOpenNum = tBoxTypeConf[nType].nOpenNum
- local tBoxTypePrize = tBoxPrize[nType]
-
- -- 整合权重
- local nWeight, tWeightPrize = 0, {}
- for _, v in ipairs(tBoxTypePrize) do
- nWeight = nWeight + v.nPro
- tWeightPrize[nWeight] = v
- end
- local tOpenPrize = {}
- for i = 1, nBoxNum, 1 do
- for j = 1, nOpenNum, 1 do
- -- 随机权重
- local nRandNum = math.random(1, nWeight)
- for nkey, v in pairs(tWeightPrize) do
- if nRandNum <= nkey then
- if not tOpenPrize[v.nItemID] then
- tOpenPrize[v.nItemID] = 0
- end
- tOpenPrize[v.nItemID] = tOpenPrize[v.nItemID] + v.nItemNum
- break
- end
- end
- end
- end
- return tOpenPrize
- end
- -- 删除物品
- local function TreasureChestLogic_DelGoods(human, nGoodsID, nGoodsNum)
- BagLogic.delItem(human, nGoodsID, nGoodsNum, "treasurechest")
- end
- ----------------------------------------- 客户端请求 -------------------------------------
- -- 请求宝箱界面信息
- function TreasureChestLogic_Query(human)
- if not human then
- return
- end
- if not human.db.TreasureChest then
- TreasureChestLogic_CreateDB(human)
- end
- local tMsgData = Msg.gc.GC_TEEASURECHEST_QUERY
- tMsgData.nNowPoint = TreasureChestLogic_GetDBPoint(human)
- local tBoxPointConf = TreasureChestLogic_GetPointConf()
- local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
- -- 下一阶段需要的积分信息
- for nID, v in ipairs(tBoxPointConf) do
- local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
- if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState then
- tMsgData.nID = nID
- tMsgData.nNextPoint = v.nPoint
- tMsgData.nState = CommonDefine.COMMON_PRIZE_STATE_NOGET
- Grid.makeItem(tMsgData.tPointPirze, v.tPrize[1], v.tPrize[2])
- break
- end
- end
- -- 奖励信息
- local nLen = 0
- for nType, v in pairs(tBoxTypeConf) do
- nLen = nLen + 1
- tMsgData.tList[0] = nLen
- local tData = tMsgData.tList[nLen]
- tData.nType = nType
- local nGoodsID = v.nItemID
- local nGoodsNum = BagLogic.getItemCnt(human, nGoodsID)
- Grid.makeItem(tData.tItemData, nGoodsID, nGoodsNum)
- end
- Msg.send(tMsgData, human.fd)
- end
- -- 请求宝箱内奖励信息
- function TreasureChestLogic_QueryPrize(human, nBoxType)
- local tBoxPrize = TreasureChestLogic_GetPointPrizeConf()
- if not tBoxPrize[nBoxType] then
- print("[TreasureChestLogic_QueryPrize] 不存在对应的奖励配置 nBoxType = "..nBoxType)
- return
- end
- local tBoxTypePrize = tBoxPrize[nBoxType]
- local tMsgData = Msg.gc.GC_TEEASURECHEST_PRIZE_QUERY
- local nLen = 0
- tMsgData.tItemData[0] = nLen
- for _, v in pairs(tBoxTypePrize) do
- nLen = nLen + 1
- tMsgData.tItemData[0] = nLen
- local tData = tMsgData.tItemData[nLen]
- Grid.makeItem(tData, v.nItemID, v.nItemNum)
- end
- Msg.send(tMsgData, human.fd)
- end
- -- 请求打开宝箱
- function TreasureChestLogic_Open(human, nBoxType, nBoxNum)
- local szText = "[TreasureChestLogic_Open] 玩家请求打开宝箱 nBoxType = "..nBoxType.." nBoxNum = "..nBoxNum
- TreasureChestLogic_WriteLog(human, szText)
- if nBoxNum >= TREASURECHEST_OPEN_NUM then
- szText = szText .. " 失败不正确的打开数量"
- TreasureChestLogic_WriteLog(human, szText)
- return
- end
- -- 检测配置
- local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
- if not tBoxTypeConf[nBoxType] then
- print("[TreasureChestLogic_Open] 不存在对应的宝箱类型 nBoxType = "..nBoxType)
- szText = szText.." 失败不存在对应宝箱类型"
- TreasureChestLogic_WriteLog(human, szText)
- return
- end
- local nGoodsID = tBoxTypeConf[nBoxType].nItemID
- local nGoodsNum = BagLogic.getItemCnt(human, nGoodsID)
- if nBoxNum > nGoodsNum then
- print("[TreasureChestLogic_Open] 玩家拥有宝箱数量不足 nBoxType = "
- ..nBoxType.." nBoxNum = "..nBoxNum.." nGoodsNum = "..nGoodsNum)
- szText = szText.." 数量不正确 nGoodsNum = "..nGoodsNum
- TreasureChestLogic_WriteLog(human, szText)
- return
- end
- local tPrize = TreasureChestLogic_OpenBox(nBoxType, nBoxNum)
- -- 发送奖励
- BagLogic.addItemList(human, tPrize, "treasurechest")
- BagLogic.sendItemGetList(human, tPrize, "treasurechest")
- szText = szText .." 发送奖励成功"
- TreasureChestLogic_WriteLog(human, szText)
- -- 加积分
- local nAddPoint = nBoxNum * tBoxTypeConf[nBoxType].nPoint
- local nNowPoint = TreasureChestLogic_GetDBPoint(human)
- nNowPoint = nAddPoint + nNowPoint
- TreasureChestLogic_SetDBPoint(human, nNowPoint)
- szText = szText.." 增加积分 nAddPoint = "..nAddPoint.." nNowPoint = "..nNowPoint
- TreasureChestLogic_WriteLog(human, szText)
- -- 删除使用了的物品
- TreasureChestLogic_DelGoods(human, nGoodsID, nBoxNum)
- -- 更新积分奖励状态
- TreasureChestLogic_UpdatePointPrize(human)
- TreasureChestLogic_Query(human)
- end
- -- 请求自动打开宝箱
- function TreasureChestLogic_AutoOpen(human, nBoxType)
- local szText = "[TreasureChestLogic_AutoOpen] 玩家请求打开宝箱开始 nBoxType = "..nBoxType
- local tBoxTypeConf = TreasureChestLogic_GetBoxTypeConf()
- if not tBoxTypeConf[nBoxType] then
- print("[TreasureChestLogic_AutoOpen] 不存在对应的宝箱类型 nBoxType = "..nBoxType)
- szText = szText.." 失败不存在对应宝箱类型"
- TreasureChestLogic_WriteLog(human, szText)
- return
- end
- local nGoodsID = tBoxTypeConf[nBoxType].nItemID
- local nGoodsNum = BagLogic.getItemCnt(human, nGoodsID)
- if 0 >= nGoodsNum then
- return
- end
- TreasureChestLogic_WriteLog(human, szText)
- TreasureChestLogic_Open(human, nBoxType, 1)
- end
- -- 请求领取积分奖励
- function TreasureChestLogic_GetPointPrize(human, nID)
- local szText = "[TreasureChestLogic_GetPointPrize] 玩家请求领取积分奖励 nID = "..nID
- local tPointPrize = TreasureChestLogic_GetPointConf()
- if not tPointPrize[nID] then
- print("[TreasureChestLogic_GetPointPrize] 不存在对应的积分ID nID = "..nID)
- return
- end
- -- 积分检测
- local nNowPoint = TreasureChestLogic_GetDBPoint(human)
- if nNowPoint < tPointPrize[nID].nPoint then
- print("[TreasureChestLogic_GetPointPrize] 玩家当前积分不足 nNowPoint = "
- ..nNowPoint.." nNeedPoint = "..tPointPrize[nID].nPoint)
- return
- end
- local nState = TreasureChestLogic_GetDBPointPrize(human, nID)
- if CommonDefine.COMMON_PRIZE_STATE_CANGET ~= nState then
- print("[TreasureChestLogic_GetPointPrize] 玩家奖励状态不正确 nNowPoint = "
- ..nNowPoint.." nState = "..nState.."nID = "..nID)
- return
- end
- local tGoodsInfo =
- {
- [tPointPrize[nID].tPrize[1]] = tPointPrize[nID].tPrize[2]
- }
- -- 添加奖励
- BagLogic.addItemList(human, tGoodsInfo, "treasurechest")
- BagLogic.sendItemGetList(human, tGoodsInfo, "treasurechest")
- TreasureChestLogic_SetDBPointPrize(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
- local szSendPrize = szText .. " 发送奖励成功 nGoodsID = "..tPointPrize[nID].tPrize[1]
- .." nGoodsNum = "..tPointPrize[nID].tPrize[2]
- TreasureChestLogic_WriteLog(human, szSendPrize)
- -- 改变积分
- local nNewPoint = nNowPoint - tPointPrize[nID].nPoint
- TreasureChestLogic_SetDBPoint(human, nNewPoint)
- local szPointPrize = szText.." nNowPoint = "..nNowPoint.." nDelPoint = "
- ..tPointPrize[nID].nPoint.." nNewPoint = "..nNewPoint
- TreasureChestLogic_WriteLog(human, szPointPrize)
- if 0 == tPointPrize[nID].nNextID then
- TreasureChestLogic_ResetDBPointPrize(human)
- local szResetText = szText.." 玩家领取完最后的奖励进行重置"
- TreasureChestLogic_WriteLog(human, szResetText)
- end
- -- 更新积分奖励状态
- TreasureChestLogic_UpdatePointPrize(human)
- TreasureChestLogic_Query(human)
- end
|