local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local HeroExcel = require("excel.hero")
local HeroLogic = require("hero.HeroLogic")
local Config = require("Config")
local RoleLogic = require("role.RoleLogic")
local ObjHuman = require("core.ObjHuman")
local RoleDefine = require("role.RoleDefine")
local AssessExcel = require("excel.assess")
local MoshouLogic = require("moshou.MoshouLogic")
local CombatDefine = require("combat.CombatDefine")
local CombatPosLogic = require("combat.CombatPosLogic")
local Msg = require("core.Msg")

db = db or { }

-- 启动后初始化
function initAfterStart()
    -- 中心服不处理
    if _G.is_middle == true then return end

    -- 处理排行榜数据
    local cnt = 0
    LuaMongo.find(DB.db_hero_pingfen, nil, { id = 1, attr=1 })
    while true do
        local net = { }
        if not LuaMongo.next(net) then
            break
        end

        cnt = cnt + 1
        if cnt % 1000 == 0 then
            _G.collectgarbage("step", 100000)
        end

        updateData(net.id, RoleDefine.PINGFEN_LV, net.attr[RoleDefine.PINGFEN_LV])
        updateData(net.id, RoleDefine.PINGFEN_STAR, net.attr[RoleDefine.PINGFEN_STAR])
        updateData(net.id, RoleDefine.PINGFEN_JINJIE, net.attr[RoleDefine.PINGFEN_JINJIE])
        updateData(net.id, RoleDefine.PINGFEN_EQUIP, net.attr[RoleDefine.PINGFEN_EQUIP])
        updateData(net.id, RoleDefine.PINGFEN_UNION, net.attr[RoleDefine.PINGFEN_UNION])
        updateData(net.id, RoleDefine.PINGFEN_FUWEN, net.attr[RoleDefine.PINGFEN_FUWEN])
    end
end 

function updateData(heroID, pingFengType, pingFeng)
    db[heroID] = db[heroID] or { }
    db[heroID][pingFengType] = db[heroID][pingFengType] or 0

    if pingFeng > db[heroID][pingFengType] then
        db[heroID][pingFengType] = pingFeng
    end
end

-- 提交当前战役阵容
local queryHeroByUuid = { uuid = nil}
local net = {}
function onLogin(human)
    if _G.is_middle == true then return end

    local combatHero = CombatPosLogic.getCombatHeros(human, CombatDefine.COMBAT_TYPE1)
    if not combatHero then return end

    local len = 0
    for k, uuid in pairs(combatHero) do
        local heroGrid = HeroLogic.getHeroGridByUuid(human, uuid)
        if heroGrid then
           local heroConfig = HeroExcel.hero[heroGrid.id]
           local attrs = ObjHuman.getHeroAttrs(human, heroGrid.bagIndex)
           
           net.index = heroGrid.bagIndex
           net.id = heroGrid.id
           net.lv = heroGrid.lv
           net.quality = heroGrid.quality
           net.camp = heroConfig.camp
           net.uuid = heroGrid.uuid
           net[RoleDefine.PINGFEN_LV] = attrs[RoleDefine.PINGFEN_LV]
           net[RoleDefine.PINGFEN_STAR] = attrs[RoleDefine.PINGFEN_STAR]
           net[RoleDefine.PINGFEN_JINJIE] = attrs[RoleDefine.PINGFEN_JINJIE]
           net[RoleDefine.PINGFEN_EQUIP] = attrs[RoleDefine.PINGFEN_EQUIP]
           net[RoleDefine.PINGFEN_UNION] = attrs[RoleDefine.PINGFEN_UNION]
           net[RoleDefine.PINGFEN_FUWEN] = attrs[RoleDefine.PINGFEN_FUWEN]
           
           add(human, net)
        end
    end
end

--
function add(human, grid)
    local heroConfig = HeroExcel.hero[grid.id]
    local net = { }
    net.id = grid.id
    net.uuid = grid.uuid or 0
    net.icon = heroConfig.head
    net.camp = heroConfig.camp
    net.job = heroConfig.job
    net.star = heroConfig.star
    net.lv = grid.lv or 1
    net.quality = grid.quality or 0
    net.srvIndex = Config.SVR_INDEX
    net.srvName = Config.SVR_NAME
    net.attr = {}
    net.attr[RoleDefine.PINGFEN_LV] = grid[RoleDefine.PINGFEN_LV]
    net.attr[RoleDefine.PINGFEN_STAR] = grid[RoleDefine.PINGFEN_STAR]
    net.attr[RoleDefine.PINGFEN_JINJIE] = grid[RoleDefine.PINGFEN_JINJIE]
    net.attr[RoleDefine.PINGFEN_EQUIP] = grid[RoleDefine.PINGFEN_EQUIP]
    net.attr[RoleDefine.PINGFEN_UNION] = grid[RoleDefine.PINGFEN_UNION]
    net.attr[RoleDefine.PINGFEN_FUWEN] = grid[RoleDefine.PINGFEN_FUWEN]
    net.roleBase = {}
    RoleLogic.makeRoleBase(human.db, net.roleBase)

    updateData(net.id, RoleDefine.PINGFEN_LV, net.attr[RoleDefine.PINGFEN_LV])
    updateData(net.id, RoleDefine.PINGFEN_STAR, net.attr[RoleDefine.PINGFEN_STAR])
    updateData(net.id, RoleDefine.PINGFEN_JINJIE, net.attr[RoleDefine.PINGFEN_JINJIE])
    updateData(net.id, RoleDefine.PINGFEN_EQUIP, net.attr[RoleDefine.PINGFEN_EQUIP])
    updateData(net.id, RoleDefine.PINGFEN_UNION, net.attr[RoleDefine.PINGFEN_UNION])
    updateData(net.id, RoleDefine.PINGFEN_FUWEN, net.attr[RoleDefine.PINGFEN_FUWEN])

    local data = queryDB(grid.uuid, pingFengType)
    if data ~= nil then
        queryHeroByUuid.uuid = grid.uuid
        local setTb = { ["$set"] = net }
        LuaMongo.update(DB.db_hero_pingfen, queryHeroByUuid, setTb)
        return
    end
    LuaMongo.insert(DB.db_hero_pingfen, net)
end


-- 查询队列
function queryDB(uuid)
    queryHeroByUuid.uuid = uuid
    local heroData = { }
    LuaMongo.find(DB.db_hero_pingfen, queryHeroByUuid)
    if not LuaMongo.next(heroData) then
        return nil
    end
    return heroData
end

	
--
function query(human, id)
    local dbData = db[id]
    if not dbData then return end

    local msgRet = Msg.gc.GC_HERO_STRONG_QUERY
    local len = 0
    for k, v in pairs(dbData) do
        len = len + 1
        msgRet.pingFengType[len] = k
        msgRet.pingFengValue[len] = v
    end
    msgRet.pingFengType[0] = len
    msgRet.pingFengValue[0] = len
    Msg.send(msgRet, human.fd)
end


function doCalcMoShouPingFen(human, attr)
    -- 魔兽评分
    local moShouPingFen = 0
    local moShouLv = MoshouLogic.getMoshouLv(human)
    local moShouLvPingFen = AssessExcel.moShouLv[moShouLv] and AssessExcel.moShouLv[moShouLv].grade or 0
    moShouPingFen = moShouPingFen + moShouLvPingFen

    local jingLian = MoshouLogic.getMoShouJiLian(human)
    local jingLianPingFen = AssessExcel.moShouJingLian[jingLian] and AssessExcel.moShouJingLian[jingLian].grade or 0
    moShouPingFen = moShouPingFen + jingLianPingFen

    local zhuHun = MoshouLogic.getMoShouZhuHunNum(human)
    local zhuHunPingFen = AssessExcel.moShouZhuHun[zhuHun] and AssessExcel.moShouZhuHun[zhuHun].grade or 0
    moShouPingFen = moShouPingFen + zhuHunPingFen

    local skillLV = MoshouLogic.getMoShouSkillLv(human)
    local skillLVPingFen = AssessExcel.moShouSkillLv[skillLV] and AssessExcel.moShouSkillLv[skillLV].grade or 0
    moShouPingFen = moShouPingFen + skillLVPingFen

    attr[RoleDefine.PINGFEN_MOSHOU] = moShouPingFen
    human.db.moShouPingFen = moShouPingFen
end