---------------------------------------------
-- 活动跨服分配
---------------------------------------------
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local Util = require("common.Util")
local InnerMsg = require("core.InnerMsg")
local MiddleManager = require("middle.MiddleManager")

ACTID_2_SVRGROUP = ACTID_2_SVRGROUP or {} 		-- 活动Id->分组信息
ACTID_2_GOURPLIST = ACTID_2_GOURPLIST or {} 	-- [actID][groupID] = list

-- 活动ID
ACTID1 = 1 			-- 

-- X个服务器分为一组
ACTID_2_SVRNUM = {
	[ACTID1] = 100,
}

function cleanSvrGroup(actID)
	ACTID_2_SVRGROUP[actID] = nil
	ACTID_2_GOURPLIST[actID] = nil
end

function getMiddleSvrNum(actID)
	return ACTID_2_SVRNUM[actID] or 3
end	

function getActGroupInfo(actID)
	local groupInfo = {}
	groupInfo.svrGroup = ACTID_2_SVRGROUP[actID]
	groupInfo.groupList = ACTID_2_GOURPLIST[actID]
	return groupInfo
end

function setActGroupInfo(actID, groupInfo)
	ACTID_2_SVRGROUP[actID] = groupInfo and groupInfo.svrGroup
	ACTID_2_GOURPLIST[actID] = groupInfo and groupInfo.groupList
end

function initSvrGroup(actID)
	ACTID_2_SVRGROUP[actID] = {}
	ACTID_2_GOURPLIST[actID] = {}
	local svrGroup = ACTID_2_SVRGROUP[actID]
	local groups = ACTID_2_GOURPLIST[actID]
	local num = getMiddleSvrNum(actID)
	local cnt = 0
	local groupID = 1
	for svrIndex in Util.pairsByKeys(MiddleManager.SVRINDEX_2_FD) do
		if cnt >= num then
			cnt = 0
			groupID = groupID + 1
		end
		cnt = cnt + 1
		svrGroup[svrIndex] = groupID
		groups[groupID] = groups[groupID] or {}
		groups[groupID][cnt] = svrIndex
	end
	svrGroup.cnt = cnt
	svrGroup.groupID = groupID
	return svrGroup
end

local function addSvrGroup(actID, svrIndex)
	local svrGroup = ACTID_2_SVRGROUP[actID]
	if svrGroup[svrIndex] then
		return
	end

	local groups = ACTID_2_GOURPLIST[actID]
	local num = getMiddleSvrNum(actID)
	local cnt = svrGroup.cnt 
	local groupID = svrGroup.groupID
	if cnt >= num then
		cnt = 0
		groupID = groupID + 1
	end
	cnt = cnt + 1
	svrGroup[svrIndex] = groupID
	svrGroup.cnt = cnt
	svrGroup.groupID = groupID
	groups[groupID] = groups[groupID] or {}
	groups[groupID][cnt] = svrIndex
end

function getSvrGroup(actID, svrIndex)
	if not svrIndex then return end
	local realSvrIndex = MiddleManager.getLastSvrIndex(svrIndex)
	if not realSvrIndex then return end

	local isChange = nil
	if not ACTID_2_SVRGROUP[actID] then
		initSvrGroup(actID)	
		isChange = true	
	end
	local svrGroup = ACTID_2_SVRGROUP[actID]
	if not svrGroup[realSvrIndex] then
		addSvrGroup(actID, realSvrIndex)
		isChange = true
	end
	
	if isChange == true then
		updateDB(actID)
	end
	
	return svrGroup[realSvrIndex], isChange
end

-- 根据活动和分组id获得服务器列表
function getSvrListByID(actID, groupID)
	local groups = ACTID_2_GOURPLIST[actID]
	if not groups then return end
	return groups[groupID]
end

-- 发送到分组
function send2Group(msg, actID, groupID, noSvrIndex)
	if not _G.is_middle then return end

	local svrList = getSvrListByID(actID, groupID)
	if not svrList then return end

	for _, svrIndex in ipairs(svrList) do
		local fd = MiddleManager.getFDBySvrIndex(svrIndex)
		local noFd = MiddleManager.getFDBySvrIndex(noSvrIndex)
		if fd and fd ~= noFd then			
			InnerMsg.sendMsg(fd, msg)
		end
	end	
end

function initAfterStart()
	if not _G.is_middle then return end	
	
	print("initAfterStart")
	LuaMongo.find(DB.db_middle_act_group)
    local groupData = {}
    if LuaMongo.next(groupData) then
		for k, v in pairs(ACTID_2_SVRNUM) do
			setActGroupInfo(k, groupData[k])
		end
    else
    	LuaMongo.insert(DB.db_middle_act_group, groupData)
    end
end

local query = {}
function updateDB(actID)
	if not _G.is_middle then return end

	if ACTID_2_SVRNUM[actID] == nil then
		assert(nil)
	end

	LuaMongo.find(DB.db_middle_act_group)
	local groupData = {}
	if LuaMongo.next(groupData) then
		groupData[actID] = groupData[actID] or {}
		groupData[actID].svrGroup = ACTID_2_SVRGROUP[actID]
		groupData[actID].groupList = ACTID_2_GOURPLIST[actID]
		
		query._id = groupData._id
		LuaMongo.update(DB.db_middle_act_group, query, groupData)		
	end 
end