-- 数据缓存文件

local DB = require("common.DB")
local LuaMongo = _G.lua_mongo
local CommonDB = require("common.CommonDB")
local Util = require("common.Util")


-- 开服第10天以后才缓存玩家uid
local CACHE_UUID_STARTDAY = 1  --10, 改为开服就缓存


local player_uuid_list = {}


local function isCacheUuid()
    local openDay = CommonDB.getServerOpenDay()
    if CACHE_UUID_STARTDAY > openDay or _G.is_middle == true then
        return false
    end

    return true
end


-- 加载所有玩家的uid, 后续优化下
local function load_playeruuid()
    _G.collectgarbage("step", 1000000)

    LuaMongo.find(DB.db_char, nil, {lv = 1})

    while true do
		local data = {}
		if not LuaMongo.next(data) then
			break
        end

        player_uuid_list[data._id] = data.lv
	end

    _G.collectgarbage("step", 1000000)
end




function Update_PlayerUuidList(playerList)
    if not isCacheUuid() then
        return
    end
    player_uuid_list = playerList
end


function Insert_PlayerUuidList(newUuid, lv)
    if not isCacheUuid() then
        return
    end
    player_uuid_list[newUuid] = lv
end


function Update_PlayerLv(playerUid, newLV)
    if not newLV then
        return
    end
    player_uuid_list[playerUid] = newLV
end


function Get_PlayerUuidList()
    if not isCacheUuid() then
        return
    end

    if not next(player_uuid_list) then
        load_playeruuid()
    end

    return player_uuid_list
end

