--------------------------------
-- 文件名       :   BusThreeActDice.lua
-- 文件说明     :   新商业活动3-天选骰子
-- 创建时间     :   2025/09/02
-- 创建人       :   FC
--------------------------------


local tDiceConf = require("excel.BusThreeActivity").Dice
local Util = require("common.Util")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local CommonDefine = require("common.CommonDefine")
local AbsActLogic = require("absAct.AbsActLogic")
local BuyLogic = require("topup.BuyLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local BusActGrandGift = require("absAct.BusThreeActGrandGift")

local BUSTHREEDICEABSID = 7404        -- 对应ABS活动ID
-- 骰子类型
local BUSDICE_TYPE_1 = 1              -- 数字全相同
local BUSDICE_TYPE_2 = 2              -- 数字连续
local BUSDICE_TYPE_3 = 3              -- 任意2个数字相同
local BUSDICE_TYPE_4 = 4              -- 剩余情况

local BUSDICE_GOODID = 1041          -- 物品ID

local typeFuncMap = nil

----------------------------------------- 内部处理开始 -------------------------------------
local function BusThreeActDice_InitTypeFuncMap()
    typeFuncMap = {
        [BUSDICE_TYPE_1] = function (nPoint)
            return nPoint.."|"..nPoint.."|"..nPoint
        end,
        [BUSDICE_TYPE_2] = function (nPoint)
            local nRandNum = math.random(1, 6)
            if nRandNum == 1 then
                return nRandNum.."|"..(nRandNum+1).."|"..(nRandNum+2)
            elseif nRandNum == 6 then
                return (nRandNum-2).."|"..(nRandNum-1).."|"..nRandNum
            else
               return (nRandNum-1).."|"..nRandNum.."|"..(nRandNum+1)
            end
        end,
        [BUSDICE_TYPE_3] = function (nPoint)
            local nRandNum = math.random(1, 6)
            local nOtherNum = nRandNum
            local nDoLen = 0
            while nOtherNum == nRandNum do
                nOtherNum = math.random(1, 6)
                nDoLen = nDoLen + 1
                if nDoLen > 10 then
                    break
                end
            end

            if nRandNum == nOtherNum then
                local nDifNum = math.random(1, 2)
                nOtherNum = nDifNum == 1 and nRandNum - 1 or nRandNum + 1
            end

            local tPoint = {nRandNum, nRandNum, nOtherNum}
            table.sort(tPoint)
            return table.concat(tPoint, "|")
        end,
        [BUSDICE_TYPE_4] = function (nPoint)
            local tPoint = {}
            while #tPoint < 3 do
                local nRandNum = math.random(1, 6)
                local bExist = false
                for _, v in ipairs(tPoint) do
                    if v == nRandNum then
                        bExist = true
                        break
                    end
                end

                if not bExist then
                    table.insert(tPoint, nRandNum)
                end
            end

            table.sort(tPoint)
            -- 连续判断
            if tPoint[1] + 1 == tPoint[2] and tPoint[2] + 1 == tPoint[3] then
                if tPoint[1] > 1 then
                    tPoint[1] = tPoint[1] - 1
                else
                    tPoint[3] = tPoint[3] + 1
                end
            end
            return table.concat(tPoint, "|")
        end,
    }
end

-- 根据类型获取对应的随机点数
local function BusThreeGrandGift_GetPoint(nType, nAppointPoint)
    if not typeFuncMap then
        BusThreeActDice_InitTypeFuncMap()
    end

    if not typeFuncMap or not typeFuncMap[nType] then
        return false, nil
    end

    local func = typeFuncMap[nType]
    return true, func(nAppointPoint)
end

local function BusThreeActDice_DiceDo(human, tMsgData, nNum)
    local nAllWeight = 0
    local tPrizeData = {}
    for _, v in ipairs(tDiceConf) do
        nAllWeight = nAllWeight + v.nWeight
        table.insert(tPrizeData, {nWeight = nAllWeight, tPrize = v.Prize, nType = v.nType, nPoint = v.nPoint})
    end

    
    local tItemList  = {}
    tMsgData.tPrize[0] = nNum
    for i = 1, nNum, 1 do
        local tOneDiceData = tMsgData.tPrize[i]
        local nRandWeight = math.random(1, nAllWeight)
        local tChoseData = nil
        for _, v in ipairs(tPrizeData) do
            if nRandWeight <= v.nWeight then
                tChoseData = v
                break
            end
        end

        if not tChoseData then
            return false
        end

        local bRet = false
        bRet, tOneDiceData.tPoint = BusThreeGrandGift_GetPoint(tChoseData.nType, tChoseData.nPoint)
        if false == bRet then
            print("[BusThreeActDice_DiceDo] 获取点数失败")
            return false
        end

        -- print("[BusThreeActDice_DiceDo] 第 "..i.." 次 nType = "..tChoseData.nType.." nPoint = "..tChoseData.nPoint)
        -- table.print_lua_table(tChoseData.tPrize)

        tOneDiceData.titemData[0] = #tChoseData.tPrize
        for j, v in ipairs(tChoseData.tPrize) do
            Grid.makeItem(tOneDiceData.titemData[j], v[1], v[2])
            if not tItemList[v[1]] then
                tItemList[v[1]] = 0
            end

            tItemList[v[1]] = tItemList[v[1]] + v[2]
        end

        -- print("[BusThreeActDice_DiceDo] 第"..i.."次掷骰子结果: "..tOneDiceData.tPoint)
    end

    for nGoodID, nGoodNum in pairs(tItemList) do
        BagLogic.addItem(human, nGoodID, nGoodNum, "BusThreeActDice")
    end

    return true
end


----------------------------------------- 客户端请求 -------------------------------------
function BusThreeActDice_DO(human, nNum)
    if 1 ~= nNum and 10 ~= nNum then
        return
    end

    print("[BusThreeActDice_DO] 进行抽取 nNum = "..nNum)

    local nGoodsNum = BagLogic.getItemCnt(human, BUSDICE_GOODID)
    if nGoodsNum < nNum then
        return
    end

    print("[BusThreeActDice_DO] 物品数量足够 nGoodsNum = "..nGoodsNum)

    local tMsgData = Msg.gc.GC_NEW_BUSTHREEACT_DICEDO
    local bRet = BusThreeActDice_DiceDo(human, tMsgData, nNum)
    if not bRet then
        print("[BusThreeActDice_DO] 进行抽取报错")
        return
    end

    BagLogic.delItem(human, BUSDICE_GOODID, nNum, "BusThreeActDice")

    BusActGrandGift.BusThreeGrandGift_NumChange(human, nNum)
    print("[BusThreeActDice_DO] 物品扣除完成 nGoodsNum = "..nNum)
    Msg.send(tMsgData, human.fd)
    print("[BusThreeActDice_DO] 发送协议完成")
end

