--封号逻辑
local Config = require("Config")
Json = Json or require("common.Json")
local InnerMsg = require("core.InnerMsg")
local CommonDB = require("common.CommonDB")
local MiddleManager = require("middle.MiddleManager")
local ObjHuman = require("core.ObjHuman")
local CommonDefine = require("common.CommonDefine")
local Util = require("common.Util")

-- 禁止等级
local BAN_LEVEL1 = 1    --禁角色
local BAN_LEVEL2 = 2    --禁账号
local BAN_LEVEL3 = 3    --禁设备


------------------------------------------当前服是普通服的处理------------------------------------------
local function generateTag(args)
    local strTag = ""
    for _, str in ipairs(args) do
        if strTag == "" then
            strTag = str
        else
            strTag = strTag .. "|" .. str
        end
    end
    return strTag
end

-- 获取本服缓存的封禁数据
local function getBanCache()
    return CommonDB.GetBanCache()
end

-- 更新本服缓存的封禁数据
local function updateBanCache(banTag, banTime)
    CommonDB.UpdateBanCache(banTag, banTime)
end


local function isSame(str, account)
    local t = Util.split(str, "|")
    account = tostring(account)
    if t[3] == account then
        return true
    end
    return false
end


-- 通过uuid踢人
local function kickPlayerByuuid(uuid)
    if ObjHuman.onlineUuid[uuid] then
        local fd = ObjHuman.onlineUuid[uuid].fd
        ObjHuman.doDisconnect(ObjHuman.onlineUuid[uuid], CommonDefine.DISCONNECT_BAN_ROLE)

        local Handler = require("scene.Handler")
        Handler.sendDisconnect(fd, CommonDefine.DISCONNECT_BAN_ROLE)
    end
end

-- 通过account踢人
local function kickPlayerByAccount(account)
    for uTag, h in pairs(ObjHuman.onlineNewUniqueTag) do
        if isSame(uTag, account) then
            local fd = h.fd
            ObjHuman.doDisconnect(h, CommonDefine.DISCONNECT_BANUSER)
            local Handler = require("scene.Handler")
            Handler.sendDisconnect(fd, CommonDefine.DISCONNECT_BANUSER)
            break
        end
    end
end


-- 通过设备号踢人
local function kickPlayerByDeviceNo(deviceNo)
    for uTag, h in pairs(ObjHuman.onlineNewUniqueTag) do
        if h.deviceNo == deviceNo then
            local fd = h.fd
            ObjHuman.doDisconnect(h, CommonDefine.DISCONNECT_BANUSER)
            local Handler = require("scene.Handler")
            Handler.sendDisconnect(fd, CommonDefine.DISCONNECT_BANUSER)
            break
        end
    end
end


-- 向跨服请求封禁数据
function NS_Get_BanData()
    local tMsgData = InnerMsg.lw.LW_QUERY_BAN_INFO
    tMsgData.nSrcServerID = Config.SVR_INDEX
    InnerMsg.sendMsg(0, tMsgData)
end

-- 收到跨服推送的封禁数据
function NS_Init_BanData(banList)
    for k ,v in pairs(banList) do
        updateBanCache(k, v)
    end
end


-- 通知跨服更新封禁数据
function Update_CrossServer_BanData(banStr)
    local tMsgData = InnerMsg.lw.LW_UPDATE_BAN_INFO
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.banStr = banStr

    InnerMsg.sendMsg(0, tMsgData)
end

-- 收到跨服推送的要更新的封禁数据
function NS_Update_BanData(banStr)
    local banInfo = Json.Decode(banStr)

    -- 禁账号
    if banInfo.accountBanInfo then
        local banTime = tonumber(banInfo.accountBanInfo.banTime)
        local banTbl = {banInfo.accountBanInfo.channelTag, banInfo.accountBanInfo.accountTag}
        local banTag = generateTag(banTbl)

        if  banTime > 0 then
            kickPlayerByAccount(banInfo.accountBanInfo.accountTag)
        end

        updateBanCache(banTag, banTime)
    end


    -- 禁设备
    if banInfo.appBanInfo then
        local banTime = tonumber(banInfo.appBanInfo.banTime)
        if  banTime > 0 then
            kickPlayerByDeviceNo(banInfo.appBanInfo.appId)
        end

        updateBanCache(banInfo.appBanInfo.appId, tonumber(banInfo.appBanInfo.banTime))
    end

end


-- Web请求普通游戏服更新封号数据
function Update_Ban_Info(banInfo)
    -- local banInfo = Json.Decode(banStr)

    -- 禁角色
    if banInfo.roleBanInfo then
        local banTime = tonumber(banInfo.roleBanInfo.banTime)
        local banTag = banInfo.roleBanInfo.roleTag

        if banTime > os.time() then
            kickPlayerByuuid(banTag)
        end

        -- 更新本地
        updateBanCache(banTag, banTime)
    end

    Update_CrossServer_BanData(Json.Encode(banInfo))
end

-- 是否被禁止登录
function CheckBan(uuid, channelId, account, appId)
    local banListCache = getBanCache()

    local now = os.time()
    if banListCache[uuid] and banListCache[uuid] > now then
        return true
    end

    local banTag = generateTag({channelId, account})
    if banListCache[banTag] and banListCache[banTag] > now then
        return true
    end

    appId = tostring(appId)
    if banListCache[appId] and banListCache[appId] > now then
        return true
    end
    return false
end

-------------------------------------------------------------------------------------------







------------------------------------------当前服是跨服的处理------------------------------------------

local function getBanData()
    return CommonDB.getValueByKey(CommonDB.KEY_BAN_DATA)
end

local function updateBanData(newData)
    CommonDB.UpdateBanData(newData)
end



-- 当前服为跨服, 收到普通服请求获取封禁数据
function CS_Get_BanData(msg)
    local banData = getBanData()

    if banData then
        local tMsgData = InnerMsg.wl.WL_QUERY_BAN_INFO

        -- for k, v in pairs(banData) do
        --     tMsgData.banList[k] = v
        -- end
        tMsgData.banList = banData

        local fd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
        InnerMsg.sendMsg(fd, tMsgData)
    end

end

-- 当前服为跨服, 收到普通服请求更新封禁数据
function CS_Update_BanData(banStr)
    local banTag, banTime
    local bl = false
    local banInfo = Json.Decode(banStr)

    local banData = getBanData()
    banData = banData or {}

    -- 禁角色
    if banInfo.roleBanInfo then
        banTag = banInfo.roleBanInfo.roleTag
        banTime = tonumber(banInfo.roleBanInfo.banTime)

        if banTime == 0 or banTime > os.time() then
            bl = true
            banData[banTag] = banTime
        end

        if banTime == 0 then
            banData[banTag] = nil
        end
    end


    -- 禁账号
    if banInfo.accountBanInfo then
        banTime = tonumber(banInfo.accountBanInfo.banTime)
        local banTbl = {banInfo.accountBanInfo.channelTag, banInfo.accountBanInfo.accountTag}
        banTag = generateTag(banTbl)

        if banTime == 0 or banTime > os.time() then
            bl = true
            banData[banTag] = banTime
        end

        if banTime == 0 then
            banData[banTag] = nil
        end
    end


    -- 禁设备
    if banInfo.appBanInfo then
        banTag = tostring(banInfo.appBanInfo.appId)
        banTime = tonumber(banInfo.appBanInfo.banTime)

        if banTime == 0 or banTime > os.time() then
            bl = true
            banData[banTag] = banTime
        end

        if banTime == 0 then
            banData[banTag] = nil
        end
    end

    if not bl then
        return
    end

    --更新数据库
    updateBanData(banData)

    -- 广播给全部区服
    if banInfo.accountBanInfo or banInfo.appBanInfo then
        banInfo.roleBanInfo = nil
        banStr = Json.Encode(banInfo)

        local tMsgData = InnerMsg.wl.WL_UPDATE_BAN_INFO
        tMsgData.banStr = banStr

        local fdList = MiddleManager.MiddleManager_GetAllFD()
        for _, fd in pairs(fdList) do
            pcall(InnerMsg.sendMsg, fd, tMsgData)
        end
    end
end



-------------------------------------------------------------------------------------------
