-- 诸神圣域
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local Log = require("common.Log")
local CreateRole = require("role.CreateRole")
local GodsAreaConfig = require("excel.godsArea").rankList


local rankList = {}

local updateDBFile = {id = nil}


local UPDATE_TAG = "update"
local DELTE_TAG = "delete"


local function generatePlayer(rank)
    local player = {}

    player.rank = rank
    player.head = CreateRole.getRandomHead()
	player.name = CreateRole.getRandomName()
	player.body = CreateRole.getRandomBody()
    player.headFrame = CreateRole.getRandomHeadFrame()
    player.level = 100

    -- 真实玩家才有这些数据
    -- player.uid = ""
    -- player.serverId = 0
    -- player.power = 0
    -- player.heroList = 
    -- {
        -- heroUuid = heroUuid,
        -- heroStar = heroStar,
        -- heroLevel = heroLevel,
        -- heroCamp =  heroCamp,
        -- heroBody = heroBody,
    -- }

    return player
end

local function writeLog(logStr)
    Log.write(Log.LOGID_OSS_GODSAREA_CHALLENGE, logStr)
end



local function loadData()
    local len = 0
    LuaMongo.find(DB.db_gods_area)
    while true do
        local player = {}
        if not LuaMongo.next(player) then
            break
        end

        len = len + 1
        rankList[len] = player
    end

    if #rankList > 0 then
        table.sort(rankList, function (a, b)
            return a.rank < b.rank
        end)
    end
end

local function insertData(data)
    LuaMongo.insert(DB.db_gods_area, data)
end

local function updateData(data)
    updateDBFile._id = data._id
    LuaMongo.update(DB.db_gods_area, updateDBFile, data)
end



local function initRankList()
    for rank in ipairs(GodsAreaConfig) do
        rankList[rank] = generatePlayer(rank)

        insertData(rankList[rank])
    end
end


function initAfterStart()
    if _G.is_middle ~= true then return end
    if #rankList > 0 then
        return
    end

    loadData()

    if #rankList <= 0 then
        initRankList()
    end
end




-- 合服后处理排行榜
function UpdateAfterMergeSrv(newSrvIdTb, removeTb)
    if #rankList <= 0 then
        initAfterStart()
    end

    local bl = false
    local opType = UPDATE_TAG
    for rank, rankData in ipairs(rankList) do
        bl = false
        local serverId = rankData.serverId
        local oldName = rankData.name

        if serverId then
            if newSrvIdTb[serverId] then
                rankData.serverId = newSrvIdTb[serverId]
                bl = true
                opType = UPDATE_TAG
            end

            if removeTb[serverId] then
                local id = rankList[rank]._id
                rankList[rank] = generatePlayer(rank)
                rankList[rank]._id = id

                bl = true
                opType = DELTE_TAG
            end

            if bl then
                updateData(rankList[rank])

                local logStr = string.format("opType: %s, rank: %d, oldUuid: %s, newUuid: %s, oldName: %s, newName: %s, oldServerId: %d, newServerId: %d",
                        opType, rank, rankData.uid, rankList[rank].uid or "", oldName, rankList[rank].name or "", serverId, rankList[rank].serverId or 0)

                writeLog(logStr)
            end
        end
    end
end


function GetRankList()
    return rankList
end


function UpdateRankData(rank, newRankData)
    rankList[rank] = newRankData

    updateData(rankList[rank])
end




