local Friend_Controller = {} local EventSystem = require('Event/EventSystem') local private = {} local JSON = require("cjson") local chat_pb = require("Proto/chat_pb") local chat_operate_pb = require("Proto/chat_operate_pb") local TIME_WRITE_RECORD = 2 local REPORT_MSG_NUM = 10 private.lastWriteTimestamp = 0 Friend_Controller.MainIndex = { Main = 0, Rival = 1, Mail = 2 } local friendChatLimit local friendChatNum local chatMsgPath local changeCD function Friend_Controller.OnStartup() friendChatLimit = DataSheetManager.ConstData:GetDataByID("FriendChatLimit").IntValue friendChatNum = DataSheetManager.ConstData:GetDataByID("FriendChatNum").IntValue changeCD = DataSheetManager.ConstData:GetDataByID("ChangeCD").IntValue EventSystem.Regist(EventID.ApplicationPause, private.OnApplicationPause) end function Friend_Controller.OnShutdown() Friend_Controller.WriteChatHistory() end function Friend_Controller.OpenFriendPanel(mainIndex, subIndex, acid) local context = { MainIndex = mainIndex, SubIndex = subIndex or MDL.Friend.ListType.Recent, --对应Friend_Model.ListType Acid = acid, } local viewName = Const_UI_Lua.UIEnum.UIFriendBase if UIManager.IsViewOpened(viewName) then context.EnterType = 0 EventSystem.Dispatch(EventID.UI_OpenFriendPanel, context) else context.EnterType = 1 UIManager.OpenView(viewName, context) end end --私聊某人 function Friend_Controller.PrivateTalkToAcid(acid) if string.isNilOrEmpty(acid) then return end if BattleDataGetter.GetAcID() == acid then CTL.Tips.AddTipsMsg(Const_IDS["IDS_Friend_TalkToYourself"]) return end MDL.Friend.RecentContactMaintenanceReq(acid) Friend_Controller.OpenFriendPanel(CTL.Friend.MainIndex.Main, MDL.Friend.ListType.Recent, acid) end function Friend_Controller.OpenApplyPanel() UIManager.OpenView(Const_UI_Lua.UIEnum.UIFriendApply) end --私聊信息长度是否适合 function Friend_Controller.IsMsgLengthFit(input) local length = UTL.String.GetStrLen(input) if length > friendChatLimit then CTL.Tips.AddTipsMsg(Const_IDS.IDS_Friend_ChatLimit) return false end return true end function Friend_Controller.IsOutOfLengthWhenInsertContent(input) local length = UTL.String.GetStrLen(input) if length > friendChatLimit then CTL.Tips.AddTipsMsg(Const_IDS.IDS_Friend_ChatLimit) return true end return false end --是否在"换一批"推荐人CD时间内 function Friend_Controller.IsInChangeRecommendCD(showTips) if MDL.Friend.RecommendRequestTime then local offsetTime = changeCD - (UTL.Time.GetServerTimestamp() - MDL.Friend.RecommendRequestTime) if offsetTime > 0 then if showTips then CTL.Tips.AddTipsMsg(string.FormatByIdsId(Const_IDS.IDS_Friend_ChangeCD, offsetTime)) end return true end end return false end function Friend_Controller.CreatTimeShowMsg(timeStamp) return { IsTimeShow = true, Content = UTL.Time.FormatTimeForMsg(timeStamp) } end --获取对应玩家的未读信息数目 function Friend_Controller.GetUnreadMsgNum(acid) local unreadNum = 0 local msgList = Friend_Controller.GetChatMsgList(acid) if msgList then for _, v in ipairs(msgList) do if v.unRead and v.unRead == true then unreadNum = unreadNum + 1 end end end return unreadNum end --直接消除玩家的所有红点,防止有无法加载的消息产生未读红点 function Friend_Controller.ReadAllUnreadMsg(acid) local msgList = Friend_Controller.GetChatMsgList(acid) if msgList then for _, v in ipairs(msgList) do v.unRead = false end end end --上线获取最近、好友列表信息之后请求一遍离线消息,与本地的历史消息合并 function Friend_Controller.LoadOfflineChatMsg() local msg = chat_pb.HistoryMsg() msg.targetId = BattleDataGetter.GetAcID() msg.type = chat_pb.Personal msg.indexBegin = 0 msg.indexEnd = 0 AuxUtil.ChatMsg(chat_operate_pb.C2SHistroyMsg, msg:SerializeToString()) end --添加聊天记录,由于离线记录排序不稳定,先插入,再在读写的时候进行排序,删除多余的 function Friend_Controller.AddChatHistory(historyInfo) private.ReadChatHistory() local acid = historyInfo.isSelf and historyInfo.targetId or historyInfo.fromId local historyMsgList = private.ChatHistoryContainer[acid] if historyMsgList == nil then private.ChatHistoryContainer[acid] = { [1] = historyInfo } else table.insert(historyMsgList, historyInfo) end --防止聊天记录丢失,来一条写一条 --加个时间间隔,减少写入次数 local curTimestamp = UTL.Time.GetLocalTimestamp() if curTimestamp - private.lastWriteTimestamp > TIME_WRITE_RECORD then private.lastWriteTimestamp = curTimestamp Friend_Controller.WriteChatHistory() end end function private.SortByTime(a, b) return a.msgTime < b.msgTime end --读取缓存的消息列表(做个时间排序) function Friend_Controller.GetChatMsgList(acid) private.ReadChatHistory() local list = private.ChatHistoryContainer[acid] local isRemove = false --是否做了移除操作 if list then table.sort(list, private.SortByTime) local offset = #list - friendChatNum if offset > 0 then isRemove = true --记录超过上限,移除最早的,最上层 for i = 1, offset do table.remove(list, 1) end end end return list, isRemove end --举报私聊的文本,最近的十条对方的消息加时间,拼接格式为([时间]消息) function Friend_Controller.GetReportChatMsg(acid) local content = "" local chatList = CTL.Friend.GetChatMsgList(acid) if chatList then local index = 0 for i = #chatList, 1, -1 do local info = chatList[i] if info.isSelf == false then --服务器要求的格式([时间]文本)\\n content = content .. string.format("([%s]%s)\\n", UTL.Time.FormatTimeCompletely(info.msgTime), info.content) index = index + 1 if index == REPORT_MSG_NUM then break end end end end return content end function private.GetChatMsgPath() if chatMsgPath == nil then --路径带上玩家id local acid = BattleDataGetter.GetAcID() --路径不允许带: if UNITY_EDITOR then chatMsgPath = string.format("Chat/ChatHistory_%s.lg", string.gsub(acid, ":", "")) else chatMsgPath = string.format("ChatHistory_%s.lg", string.gsub(acid, ":", "")) end end return chatMsgPath end --读取本地的聊天记录 function private.ReadChatHistory() if private.ChatHistoryContainer == nil then local path = private.GetChatMsgPath() local content = LuaExt.FileReadText(path) if string.isNilOrEmpty(content) == false then xpcall(private.DecodeChatHistory, private.DecodeFailed, content) return end private.ChatHistoryContainer = {} end end function private.DecodeChatHistory(content) private.ChatHistoryContainer = JSON.decode(content) end --json解析失败,删除聊天记录 function private.DecodeFailed(msg) logging.error(LTAG.ERROR, "personal chat local history decode failed, delete exist file.", msg) LuaExt.FileDelete(private.GetChatMsgPath()) private.ChatHistoryContainer = {} end function private.OnApplicationPause(pause) if pause then Friend_Controller.WriteChatHistory() end end --将好友的聊天记录写到本地 --原来是在关闭设备或者重启时写入本地,发现会有丢失的情况,现在改成实时写入 function Friend_Controller.WriteChatHistory() if private.ChatHistoryContainer then local index = 0 --删一波超上限的聊天数据 for k, v in pairs(private.ChatHistoryContainer) do Friend_Controller.GetChatMsgList(k) index = index + 1 end if index > 0 then local content = JSON.encode(private.ChatHistoryContainer) if not string.isNilOrEmpty(content) then local path = private.GetChatMsgPath() LuaExt.FileWriteText(path, content) end end end end function private.SortTagData(a, b) return a.Sort > b.Sort end local MaxShowTagNum = 3 function Friend_Controller.GetPlayerTagData(info) local sortTagData = {} local hasSportsTag = false --竞技达人只留1个 for i, v in DataSheetManager.Tag:GetEnumerator() do local isShow = false if v.ConstDataID == "PowerTags" then local PowerLimit = DataSheetManager.ConstData:GetDataByID(v.ConstDataID).IntValue isShow = info.GsRank > 0 and info.GsRank <= PowerLimit elseif v.ConstDataID == "ActiveTags" then isShow = info.ActivePlayer elseif v.ConstDataID == "SportsTags1" then local TvTLimitRank = DataSheetManager.ConstData:GetDataByID(v.ConstDataID).IntValue if hasSportsTag then isShow = false else isShow = info.TvTRank > 0 and info.TvTRank <= TvTLimitRank if isShow then hasSportsTag = true end end elseif v.ConstDataID == "SportsTags2" then local BossChallengeLimitRank = DataSheetManager.ConstData:GetDataByID(v.ConstDataID).IntValue if hasSportsTag then isShow = false else isShow = info.BossChallengeRank > 0 and info.BossChallengeRank <= BossChallengeLimitRank if isShow then hasSportsTag = true end end elseif v.ConstDataID == "GuildTag" then local guildId = MDL.Guild.GetMyGuildId() if not string.isNilOrEmpty(guildId) then isShow = guildId == (info.PlayerInfo and info.PlayerInfo.GuildID or info.guildID) else isShow = false end end if isShow then table.insert(sortTagData, v) end end if #sortTagData > 0 then table.sort(sortTagData, private.SortTagData) if #sortTagData > MaxShowTagNum then for i = #sortTagData, MaxShowTagNum + 1, -1 do table.remove(sortTagData, i) end end end return sortTagData end -- 去掉了,直接放在ExpLevelFunction里去了 -- function private.ShowRivalEffect(playerID, entity) -- local playerInfo = BattleDataGetter.GetPlayerInfo(playerID) -- if MDL.Friend.IsPlayerRival(playerInfo.acid) or MDL.Friend.IsPlayerRivalSworn(playerInfo.acid) then -- if entity then -- entity.ExpEntity:SetModelRivalVisible(true,80) -- 设置 仇 显示,位置 :目前最高gameObject的中心位置 再 + 80 像素 -- else -- logging.warn(LTAG.WARN, "展示仇字特效失败 ,原因是entity还没被创建!") -- end -- end -- end return Friend_Controller