--杂数据处理模块

--db
--[=[
    human.db.clutterData = {
        [type] = value,
    },

]=]--


local Msg = require("core.Msg")
local Lang  = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local ClutterDataDefine = require("clutter.ClutterDataDefine")


local DAILY_LIMIT_TIMES = 99999 --抽奖和寻星每日最大次数



local function initAllClutterData(human)
    human.db.clutterData = {}

    human.db.clutterData[ClutterDataDefine.CLUTTER_TYPE1] = DAILY_LIMIT_TIMES
    human.db.clutterData[ClutterDataDefine.CLUTTER_TYPE2] = DAILY_LIMIT_TIMES
    human.db.clutterData[ClutterDataDefine.CLUTTER_TYPE3] = DAILY_LIMIT_TIMES
    human.db.clutterData[ClutterDataDefine.CLUTTER_TYPE4] = DAILY_LIMIT_TIMES
    human.db.clutterData[ClutterDataDefine.CLUTTER_TYPE5] = DAILY_LIMIT_TIMES
    human.db.clutterData[ClutterDataDefine.CLUTTER_TYPE6] = DAILY_LIMIT_TIMES
end

local function getAllClutterData(human)
    return human.db.clutterData
end

--根据类型获取杂数据
local function getDiffClutterData(human, clutteryType)
    if not clutteryType then
        return
    end

    local clutterData = getAllClutterData(human)

    if not clutterData then
        return
    end

    return clutterData[clutteryType]
end

--更新不同类型的杂数据
local function updateDiffClutterData(human, clutteryType, newValue, isAdd)
    if not clutteryType then
        return
    end

    local clutterData = getAllClutterData(human)

    if not clutterData then
        return
    end

    if isAdd then
        clutterData[clutteryType] = (clutterData[clutteryType] or 0) + newValue
    else
        clutterData[clutteryType] = (clutterData[clutteryType] or 0) - newValue
    end
end


-- 获取不同类型的抽奖次数
function GetDiffLotteryTimes(human, clutteryType)
    return getDiffClutterData(human, clutteryType) or 0
end

-- 更新不同类型的抽奖次数
function UpdateDiffLotteryTimes(human, clutteryType, val, type2)
    updateDiffClutterData(human, clutteryType, val)

    QueryDiffLotteryTimes(human, type2)
end


--寻星次数
function GetZhuanPanTimes(human)
    return getDiffClutterData(human, ClutterDataDefine.CLUTTER_TYPE6) or 0
end

--更新寻星次数
function UpdateZhuanPanTimes(human, val)
    updateDiffClutterData(human, ClutterDataDefine.CLUTTER_TYPE6, val)

    QueryZhuanPanTimes(human)
end

--上线
function onLogin(human)
    local clutterData = getAllClutterData(human)
    if not clutterData then
        initAllClutterData(human)
    end
end


--跨天
function updateDaily(human)
    initAllClutterData(human)
end

-------------------------------------------Respone----------------------------------

--请求不同类型抽奖次数
function QueryDiffLotteryTimes(human, lotteryType)
    if not lotteryType then
        return Broadcast.sendErr(human, Lang.COMMON_TYPE_ERROR)
    end

    --临时处理下, 后续再看看怎么把类型对应起来
    local targetType = lotteryType
    if lotteryType == 6 then
    elseif lotteryType == 8 then
        targetType = ClutterDataDefine.CLUTTER_TYPE5
    end


    local lotteryTimes = getDiffClutterData(human, targetType)
    if not lotteryTimes then
        return Broadcast.sendErr(human, Lang.COMMON_TYPE_ERROR)
    end

    local msgRet = Msg.gc.GC_QUERY_DAILYLOTTERYLIMIT
    msgRet.lotteryType = lotteryType
    msgRet.lotteryTimes = math.max(0, lotteryTimes)

    Msg.send(msgRet, human.fd)
end


--请求寻星次数
function QueryZhuanPanTimes(human)
    local zhuanpanTimes = getDiffClutterData(human, ClutterDataDefine.CLUTTER_TYPE6) or 0

    if not zhuanpanTimes then
        return
    end

    local msgRet = Msg.gc.GC_QUERY_DAY_ZHUANPAN_TIMES
    msgRet.zhuanpanTimes = math.max(0, zhuanpanTimes)

    Msg.send(msgRet, human.fd)
end