Prechádzať zdrojové kódy

提交完整初版宝箱代码

SCFC 1 rok pred
rodič
commit
5eac727aa5

+ 47 - 39
script/common/CommonDefine.lua

@@ -1,39 +1,47 @@
-local Lang = require("common.Lang")
-
-MAX_ONLINE_COUNT = 5000 		--	最大同时在线人数
-
--- 断开连接提示码
-DISCONNECT_NORMAL = 1			-- 正常断开连接
-DISCONNECT_KICK_SINGLE = 2		-- 管理后台踢人(单独)
-DISCONNECT_KICK_ALL = 3			-- 管理后台踢人(全服)
-DISCONNECT_DUPLICATE = 4		-- 重复帐号登录
-DISCONNECT_AUTH_FAIL = 5		-- 登录认证失败
-DISCONNECT_NORMAL_AFTER = 6		-- 正常断开后几秒后销毁
-DISCONNECT_MAX_ONLINE = 7		-- 最大登录人数限制
-DISCONNECT_BANUSER = 8 			-- 封号
-DISCONNECT_BANIP = 9 			-- 封IP
-DISCONNECT_FCM_CHILD = 10 		-- 防沉迷(未成年达到每日时间上限)
-DISCONNECT_FCM_HEALTH = 11 		-- 防沉迷(未成年/游客不得在22-08进行游戏)
-
-ZERO = 0.000000001
-
-DISCONNECT_MSG = {}
-DISCONNECT_MSG[DISCONNECT_NORMAL] = ""
-DISCONNECT_MSG[DISCONNECT_KICK_SINGLE] = Lang.DISCONNECT_KICK_SINGLE
-DISCONNECT_MSG[DISCONNECT_KICK_ALL] = Lang.DISCONNECT_KICK_ALL
-DISCONNECT_MSG[DISCONNECT_DUPLICATE] = Lang.DISCONNECT_DUPLICATE
-DISCONNECT_MSG[DISCONNECT_AUTH_FAIL] = Lang.DISCONNECT_AUTH_FAIL
-DISCONNECT_MSG[DISCONNECT_NORMAL_AFTER] = ""
-DISCONNECT_MSG[DISCONNECT_MAX_ONLINE] = Lang.DISCONNECT_MAX_ONLINE
-DISCONNECT_MSG[DISCONNECT_BANUSER] = Lang.DISCONNECT_BANUSER
-DISCONNECT_MSG[DISCONNECT_BANIP] = Lang.DISCONNECT_IP
-DISCONNECT_MSG[DISCONNECT_FCM_CHILD] = ""
-DISCONNECT_MSG[DISCONNECT_FCM_HEALTH] = ""
-
-
------------------- 通用排行榜类型定义开始 ---------------------------
-COMMONRANK_TYPE_MIN = 1             -- 最小类型
-COMMONRANK_TYPE_WEEKRANK = 1        -- 周活动排行榜类型
-
-COMMONRANK_TYPE_MAX = COMMONRANK_TYPE_WEEKRANK            -- 最大类型
------------------- 通用排行榜类型定义结束 ---------------------------
+local Lang = require("common.Lang")
+
+MAX_ONLINE_COUNT = 5000 		--	最大同时在线人数
+
+-- 断开连接提示码
+DISCONNECT_NORMAL = 1			-- 正常断开连接
+DISCONNECT_KICK_SINGLE = 2		-- 管理后台踢人(单独)
+DISCONNECT_KICK_ALL = 3			-- 管理后台踢人(全服)
+DISCONNECT_DUPLICATE = 4		-- 重复帐号登录
+DISCONNECT_AUTH_FAIL = 5		-- 登录认证失败
+DISCONNECT_NORMAL_AFTER = 6		-- 正常断开后几秒后销毁
+DISCONNECT_MAX_ONLINE = 7		-- 最大登录人数限制
+DISCONNECT_BANUSER = 8 			-- 封号
+DISCONNECT_BANIP = 9 			-- 封IP
+DISCONNECT_FCM_CHILD = 10 		-- 防沉迷(未成年达到每日时间上限)
+DISCONNECT_FCM_HEALTH = 11 		-- 防沉迷(未成年/游客不得在22-08进行游戏)
+
+ZERO = 0.000000001
+
+DISCONNECT_MSG = {}
+DISCONNECT_MSG[DISCONNECT_NORMAL] = ""
+DISCONNECT_MSG[DISCONNECT_KICK_SINGLE] = Lang.DISCONNECT_KICK_SINGLE
+DISCONNECT_MSG[DISCONNECT_KICK_ALL] = Lang.DISCONNECT_KICK_ALL
+DISCONNECT_MSG[DISCONNECT_DUPLICATE] = Lang.DISCONNECT_DUPLICATE
+DISCONNECT_MSG[DISCONNECT_AUTH_FAIL] = Lang.DISCONNECT_AUTH_FAIL
+DISCONNECT_MSG[DISCONNECT_NORMAL_AFTER] = ""
+DISCONNECT_MSG[DISCONNECT_MAX_ONLINE] = Lang.DISCONNECT_MAX_ONLINE
+DISCONNECT_MSG[DISCONNECT_BANUSER] = Lang.DISCONNECT_BANUSER
+DISCONNECT_MSG[DISCONNECT_BANIP] = Lang.DISCONNECT_IP
+DISCONNECT_MSG[DISCONNECT_FCM_CHILD] = ""
+DISCONNECT_MSG[DISCONNECT_FCM_HEALTH] = ""
+
+
+------------------ 通用排行榜类型定义开始 ---------------------------
+COMMONRANK_TYPE_MIN = 1             -- 最小类型
+COMMONRANK_TYPE_WEEKRANK = 1        -- 周活动排行榜类型
+
+COMMONRANK_TYPE_MAX = COMMONRANK_TYPE_WEEKRANK            -- 最大类型
+------------------ 通用排行榜类型定义结束 ---------------------------
+
+
+
+------------------ 通用奖励领取状态定义开始 ---------------------------
+COMMON_PRIZE_STATE_NOGET        =   0       -- 不可领取
+COMMON_PRIZE_STATE_CANGET       =   1       -- 可领取
+COMMON_PRIZE_STATE_GET          =   2       -- 已获取
+------------------ 通用奖励领取状态定义结束 ---------------------------

+ 1 - 0
script/common/LogDefine.lua

@@ -246,6 +246,7 @@ DEFINE = {
 	open_server_PowerUpPrize = 250,			 -- 开服活动-战力冲刺
 	week_loop_act			= 251,			 -- 周活动
 	voucher_use 			= 252,			 -- 代金券使用
+	treasurechest 			= 253,			 -- 宝箱打开添加
 	abs_totalReach          = 500,           -- 周期性活动累计
     abs_singleReach         = 501,           -- 周期性活动单次
     draw_ad_reward         = 601,           -- 观看广告送召唤券

+ 2 - 1
script/common/ProtoID.lua

@@ -1345,4 +1345,5 @@ _ENV[1381]="GC_TEEASURECHEST_QUERY"
 _ENV[1382]="CG_TEEASURECHEST_PRIZE_QUERY"
 _ENV[1383]="GC_TEEASURECHEST_PRIZE_QUERY"
 _ENV[1384]="CG_TEEASURECHEST_OPEN"
-_ENV[1385]="CG_TEEASURECHEST_AUTO_OPEN"
+_ENV[1385]="CG_TEEASURECHEST_AUTO_OPEN"
+_ENV[1386]="CG_TEEASURECHEST_GET_POINT_PRIZE"

+ 9 - 4
script/module/treasurechest/Handler.lua

@@ -2,20 +2,25 @@ local TreasureChestLogic = require("treasurechest.TreasureChestLogic")
 
 -- 请求宝箱界面信息
 function CG_TEEASURECHEST_QUERY(human, msg)
-    
+    TreasureChestLogic.TreasureChestLogic_Query(human)
 end
 
 -- 请求宝箱内奖励信息
 function CG_TEEASURECHEST_PRIZE_QUERY(human, msg)
-    
+    TreasureChestLogic.TreasureChestLogic_QueryPrize(human, msg.nBoxType)
 end
 
 -- 请求打开宝箱
 function CG_TEEASURECHEST_OPEN(human, msg)
-    
+    TreasureChestLogic.TreasureChestLogic_Open(human, msg.nBoxType, msg.nNum)
 end
 
 -- 请求自动打开宝箱
 function CG_TEEASURECHEST_AUTO_OPEN(human, msg)
-    
+    TreasureChestLogic.TreasureChestLogic_AutoOpen(human, msg.nBoxType)
 end
+
+-- 请求领取积分奖励
+function CG_TEEASURECHEST_GET_POINT_PRIZE(human, msg)
+    TreasureChestLogic.TreasureChestLogic_GetPointPrize(human, msg.nID)
+end

+ 9 - 0
script/module/treasurechest/Proto.lua

@@ -15,6 +15,9 @@ CG_TEEASURECHEST_QUERY =
 GC_TEEASURECHEST_QUERY = {
     {"nNowPoint",       1,          "int"},
     {"nNextPoint",      1,          "int"},
+    {"nID",             1,          "int"},         -- 积分奖励ID
+    {"nState",          1,          "int"},         -- 积分奖励状态
+    {"tPointPirze",     1,          ItemData},      -- 积分奖励礼包信息
     {"tList",           8,          tBoxInfo},
 }
 
@@ -41,4 +44,10 @@ CG_TEEASURECHEST_OPEN =
 CG_TEEASURECHEST_AUTO_OPEN = 
 {
     {"nBoxType",        1,           "byte"}
+}
+
+-- 请求领取积分奖励
+CG_TEEASURECHEST_GET_POINT_PRIZE = 
+{
+    {"nID",             1,            "int"}
 }

+ 361 - 1
script/module/treasurechest/TreasureChestLogic.lua

@@ -8,4 +8,364 @@
 local Util = require("common.Util")
 local Msg = require("core.Msg")
 local BagLogic = require("bag.BagLogic")
-local Log = require("common.Log")
+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
+
+----------------------------------------- 客户端请求 -------------------------------------
+-- 请求宝箱界面信息
+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_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, "week_loop_act")
+    BagLogic.sendItemGetList(human, tGoodsInfo, "week_loop_act")
+    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