--[[

   跨服聊天
]]

local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local ChatRecord = require("chat.ChatRecord")
local Util = require("common.Util")
local InnerMsg = require("core.InnerMsg")
local MiddleManager	= require("middle.MiddleManager")

function getChatMsgFromLogic(fd,msg)
    local msgRet = InnerMsg.wl.WL_MIDDLE_CHAT
    msgRet.msg = msg.msg
    local tb = MiddleManager.SVRINDEX_2_FD

    -- 发送协议到连接此中心服的所有本地服
    for _,v in pairs(tb) do
        InnerMsg.sendMsg(v,msgRet)
    end
end

function getChatMsgFromMiddle(fd, msg)
    -- 本地记录数据
    local chatItem = msg.msg.item
    local msgType = chatItem.msgType
    -- 普通跨服
	if ChatRecord.CHAT_RECORD[msgType] == nil then
       ChatRecord.CHAT_RECORD[msgType] = {}   
    end

    local record = ChatRecord.CHAT_RECORD[msgType]
    local newTb = Util.copyTable(chatItem)
	table.insert(record, newTb)
	if #record > ChatRecord.CHAT_RECORD_CNT then
		table.remove(record, 1)
	end
    
    -- 发送协议聊天记录到跨服频道
    Msg.sendWorld(msg.msg)

    -- 记录进度
    ChatRecord.CHAT_RECORD_JINDU[msgType] = ChatRecord.CHAT_RECORD_JINDU[msgType] or 0
    ChatRecord.CHAT_RECORD_JINDU[msgType] = ChatRecord.CHAT_RECORD_JINDU[msgType] + 1

    local human = ObjHuman.onlineUuid[msg.msg.item.roleBase.uuid]
    if human then
        human.db.chatRead = human.db.chatRead or {}
        human.db.chatRead[msgType] = human.db.chatRead[msgType] or 0
        human.db.chatRead[msgType] = ChatRecord.CHAT_RECORD_JINDU[msgType]
    end
end
