-- 大R玩家实力显示

local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local BillboardLogic = require("billboard.BillboardLogic")
local BillboardDefine = require("billboard.BillboardDefine")
local Util = require("common.Util")
-- local CommonDB = require("common.CommonDB")

-- local pRank1lastBroadcastTime


-- local function getBigRPlayerData()
--     return CommonDB.getValueByKey(CommonDB.KEY_BIGRPLAYER_SHOW_DATA)
-- end

-- local function updateBigRPlayerData(newData)
--     CommonDB.updateValue(CommonDB.KEY_BIGRPLAYER_SHOW_DATA, newData)
-- end

-- local function getpRank1LastBroadcastTime()
--     local data = getBigRPlayerData()
--     return data and data.pRank1lastBroadcastTime
-- end

-- local function updatepRank1LastBroadcastTime()
--     local data = getBigRPlayerData()
--     data = data or {}
--     local now  = os.time()
--     data.pRank1lastBroadcastTime = now
--     updateBigRPlayerData(data)

--     -- 更新缓存
--     pRank1lastBroadcastTime = now
-- end

local function isShowTips(human)
    local bl = true
    if not human.db.showTipsData then
        return bl
    end

    if human.db.lv < 10 then
        return bl
    end

    local showTipsData = human.db.showTipsData
    local pRank1LoginShowTime = showTipsData.pRank1LoginShowTime
    if not pRank1LoginShowTime then
        return bl
    end

    if Util.isSameDay(pRank1LoginShowTime) then
        bl = false
    end

    return bl
end

local function updateShowTipsData(human)
    human.db.showTipsData = human.db.showTipsData or {}
    human.db.showTipsData.pRank1LoginShowTime = os.time()
end



-- 战力排行榜排名第一的玩家上线的处理
local function powerRankTopOneLogin(human)
    -- if not pRank1lastBroadcastTime then
    --     pRank1lastBroadcastTime = getpRank1LastBroadcastTime()
    -- end

    -- if pRank1lastBroadcastTime and Util.isSameDay(pRank1lastBroadcastTime) then
    --     return
    -- end

    local rankList = BillboardLogic.GetRankList(BillboardDefine.TYPE_ZHANDOULI)
    if not rankList or not rankList[1] or rankList[1].uuid ~= human.db._id then
        return
    end

    local msg = Msg.gc.GC_POWERRANK_TOPONE_LOGIN
    msg.playerName = human.db.name

    for uuid, h in pairs(ObjHuman.onlineUuid) do
        if uuid ~= human.db._id and isShowTips(h) then
            Msg.send(msg, h.fd)

            updateShowTipsData(h)
        end
    end

    -- updatepRank1LastBroadcastTime()
end




function onLogin(human)
    powerRankTopOneLogin(human)
end
