少年修仙传客户端代码仓库
client_linchunjie
2018-11-12 dbad1c5240c9f77c6403cbe3a50390b788655f66
4712 【1.3】【前端】聊天新增本地保存聊天记录
2个文件已添加
4个文件已修改
367 ■■■■■ 已修改文件
Core/GameEngine/Model/Player/Fairy/PlayerFairyData.cs 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
System/Chat/ChatCenter.cs 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
System/Chat/ChatCtrl.cs 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
System/Chat/ChatData.cs 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
System/Chat/LocalChatHistory.cs 304 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
System/Chat/LocalChatHistory.cs.meta 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Core/GameEngine/Model/Player/Fairy/PlayerFairyData.cs
@@ -281,6 +281,7 @@
        else
        {
            ClearFairyData();
            LocalChatHistory.Clear(ChatInfoType.Fairy);
            //Z-xw写
            playerFairyAuTreasureData._DicfamilyStoreItem.Clear();
        }
System/Chat/ChatCenter.cs
@@ -26,6 +26,7 @@
            WindowCenter.Instance.windowAfterOpenEvent += WindowAfterOpenEvent;
            StageManager.Instance.onStageLoadFinish += OnStageLoadFinish;
            PlayerRealmData.OnPlayerCollectEnd += OnPlayerCollectEnd;
            SnxxzGame.Instance.AddApplicationOutAction(OnApplicationQuit);
            var time = bandTime;
            banTimeArray[0] = time.Year;
@@ -45,6 +46,7 @@
        public int chatCharacterLimit { get; private set; }
        public int bugleItem { get; private set; }
        public bool beforeDungeon { get; private set; }
        public List<ChatInfoType> chatChannels { get; private set; }
        public int chatInputLength { get; set; }
@@ -69,6 +71,12 @@
            repeatCountLimit = int.Parse(config.Numerical2);
            maliceCheckCount = int.Parse(config.Numerical3);
            maliceLimitCount = int.Parse(config.Numerical4);
            config = Config.Instance.Get<FuncConfigConfig>("LocalChatHistoryCount");
            if (config != null)
            {
                LocalChatHistory.localSaveCount = int.Parse(config.Numerical1);
            }
        }
        public event Action<string, bool, bool> UpdateChatValueEvent;
@@ -186,6 +194,15 @@
            {
                openChatAfterCollect = false;
            }
            if (!beforeDungeon && isDungeon)
            {
                LocalChatHistory.Read();
            }
            if (beforeDungeon && !isDungeon)
            {
                LocalChatHistory.Save();
            }
            beforeDungeon = isDungeon;
        }
        void ClearAllVoice()
@@ -1016,6 +1033,11 @@
        }
        #endregion
        private void OnApplicationQuit()
        {
            LocalChatHistory.Save();
        }
    }
}
System/Chat/ChatCtrl.cs
@@ -432,6 +432,7 @@
            content = StringUtility.Contact(content, Language.Get("InviteTeam", vNetData.PlayerID));
        }
        chatData = new ChatWorldData(content, (int)vNetData.PlayerID, vNetData.Name, vNetData.Extras);
        LocalChatHistory.Save(chatData as ChatUeseData);
        KeepChatInfo(chatData);
    }
    /// <summary>
@@ -441,6 +442,7 @@
    public void RevChatInfo(H0207_tagTalkArea vNetData)
    {
        ChatData chatData = new ChatAreaData(vNetData.Content, (int)vNetData.PlayerID, vNetData.SrcName, vNetData.Extras);
        LocalChatHistory.Save(chatData as ChatUeseData);
        KeepChatInfo(chatData);
    }
    /// <summary>
@@ -450,6 +452,7 @@
    public void RevChatInfo(HA9A3_tagGCPYSpeakerContent vNetData)
    {
        ChatData chatData = new ChatTrumpetData(vNetData.Text, (int)vNetData.PlayerID, vNetData.Name, vNetData.Extras, vNetData.SpeakerType, vNetData.AccID);
        LocalChatHistory.Save(chatData as ChatUeseData);
        KeepChatInfo(chatData);
        ServerTipDetails.ShowTrumpetTip(chatData as ChatTrumpetData);
    }
@@ -465,6 +468,7 @@
            content = StringUtility.Contact(content, Language.Get("InviteTeam", vNetData.PlayerID));
        }
        ChatData chatData = new ChatFamilyData(content, (int)vNetData.PlayerID, vNetData.Name, vNetData.Extras);
        LocalChatHistory.Save(chatData as ChatUeseData);
        KeepChatInfo(chatData);
        ReceiveNewChat(ChatInfoType.Fairy);
    }
@@ -488,6 +492,7 @@
        ChatFriendData chatData = new ChatFriendData(vNetData.Content, (int)vNetData.PlayerID, vNetData.SrcName, vNetData.Extras, vNetData.ToName, vNetData.TalkType, vNetData.ToPlayerID);
        FitterChat(chatData);
        AddPteChat(chatData);
        LocalChatHistory.Save(chatData as ChatUeseData);
        if (chatData.player == PlayerDatas.Instance.baseData.PlayerID)
        {
            if (OnRefreshSelf != null)
@@ -577,7 +582,9 @@
                break;
        }
        if (chatData != null)
        {
            KeepChatInfo(chatData);
        }
    }
    /// <summary>
    /// 获取频道聊天数据
@@ -623,6 +630,20 @@
        }
        return null;
    }
    public void KeepLocalChat(ChatData chat)
    {
        if (chat.type == ChatInfoType.Friend)
        {
            FitterChat(chat);
            AddPteChat(chat as ChatFriendData);
        }
        else
        {
            KeepChatInfo(chat);
        }
    }
    private void KeepChatInfo(ChatData data)
    {
        KeepAllTypeChat(data);
@@ -720,7 +741,9 @@
            data.richText.Append(content.Substring(index, content.Length - index));
        }
        if (data.type == ChatInfoType.Friend)
        {
            return true;
        }
        if (data.content.Equals(string.Empty))
        {
            return true;
@@ -875,6 +898,7 @@
        chatDics.Clear();
        chatlist.Clear();
        chatUpList.Clear();
        pteChatDics.Clear();
        if (OnRefreshChat != null)
        {
            OnRefreshChat(presentChatType);
System/Chat/ChatData.cs
@@ -39,7 +39,7 @@
    public ChatInfoType type { get; protected set; }
    public DateTime createTime { get; private set; }
    public DateTime createTime { get; set; }
    private ChatInfoType m_DetailType = ChatInfoType.World;
    public ChatInfoType detailType
@@ -146,7 +146,7 @@
public class ChatTeamData : ChatUeseData
{
    public ChatTeamData(string content, int player, string name, string extra, ChatInfoType detailType = ChatInfoType.World) : base(content, player, name, extra)
    public ChatTeamData(string content, int player, string name, string extra, ChatInfoType detailType = ChatInfoType.Team) : base(content, player, name, extra)
    {
        type = ChatInfoType.Team;
        this.detailType = detailType;
System/Chat/LocalChatHistory.cs
New file
@@ -0,0 +1,304 @@
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System;
using System.IO;
namespace Snxxz.UI
{
    public class LocalChatHistory
    {
        public static Dictionary<ChatInfoType, List<string>> chatHistory = new Dictionary<ChatInfoType, List<string>>();
        const string fileName = "ChatHistory";
        public static int localSaveCount = 50;
        static StringBuilder sb = new StringBuilder();
        public static void Save()
        {
            if (!ExistAnyLocalChatHistory())
            {
                return;
            }
            var playerId = PlayerDatas.Instance.baseData.PlayerID;
            var path = StringUtility.Contact(Application.persistentDataPath, "//", fileName, "_", playerId, ".log");
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    foreach (var type in chatHistory.Keys)
                    {
                        var list = chatHistory[type];
                        if (list.Count > 0)
                        {
                            sw.WriteLine(StringUtility.Contact("channel:", (int)type));
                            for (int i = 0; i < list.Count; i++)
                            {
                                sw.WriteLine(list[i]);
                            }
                        }
                    }
                }
            }
            Clear();
        }
        static bool ExistAnyLocalChatHistory()
        {
            foreach (var list in chatHistory.Values)
            {
                if (list.Count > 0)
                {
                    return true;
                }
            }
            return false;
        }
        public static void Save<T>(T chat) where T : ChatUeseData
        {
            if (null == chat)
            {
                return;
            }
            switch (chat.type)
            {
                case ChatInfoType.World:
                case ChatInfoType.Area:
                case ChatInfoType.Trumpet:
                case ChatInfoType.Fairy:
                case ChatInfoType.Friend:
                    if (chat.IsSound && string.IsNullOrEmpty(chat.content))
                    {
                        return;
                    }
                    var jsonString = ChatToString(chat);
                    if (!string.IsNullOrEmpty(jsonString))
                    {
                        List<string> list;
                        if (!chatHistory.TryGetValue(chat.type, out list))
                        {
                            list = new List<string>();
                            chatHistory.Add(chat.type, list);
                        }
                        if (list.Count >= localSaveCount)
                        {
                            list.RemoveAt(0);
                        }
                        list.Add(jsonString);
                    }
                    break;
            }
        }
        static void Save(ChatInfoType type, string line)
        {
            if (!string.IsNullOrEmpty(line))
            {
                List<string> list;
                if (!chatHistory.TryGetValue(type, out list))
                {
                    list = new List<string>();
                    chatHistory.Add(type, list);
                }
                if (list.Count >= localSaveCount)
                {
                    list.RemoveAt(0);
                }
                list.Add(line);
            }
        }
        public static string ChatToString(ChatUeseData chat)
        {
            switch (chat.type)
            {
                case ChatInfoType.World:
                case ChatInfoType.Area:
                case ChatInfoType.Team:
                case ChatInfoType.Fairy:
                    {
                        LocalChat localChat = new LocalChat();
                        localChat.type = chat.type;
                        localChat.player = chat.player;
                        localChat.name = chat.name;
                        localChat.extra = chat.extra;
                        localChat.content = chat.content;
                        localChat.time = chat.createTime;
                        return LitJson.JsonMapper.ToJson(localChat);
                    }
                case ChatInfoType.Trumpet:
                    {
                        var chatTrumpet = chat as ChatTrumpetData;
                        LocalTrumpetChat localChat = new LocalTrumpetChat();
                        localChat.player = chat.player;
                        localChat.name = chat.name;
                        localChat.extra = chat.extra;
                        localChat.content = chat.content;
                        localChat.speakType = chatTrumpet.speakType;
                        localChat.accId = chatTrumpet.accId;
                        localChat.time = chat.createTime;
                        return LitJson.JsonMapper.ToJson(localChat);
                    }
                case ChatInfoType.Friend:
                    {
                        var chatFriend = chat as ChatFriendData;
                        LocalFriendChat localChat = new LocalFriendChat();
                        localChat.player = chat.player;
                        localChat.name = chat.name;
                        localChat.extra = chat.extra;
                        localChat.toName = chatFriend.toName;
                        localChat.toPlayer = chatFriend.toPlayer;
                        localChat.talkType = chatFriend.talkType;
                        localChat.content = chat.content;
                        localChat.time = chat.createTime;
                        return LitJson.JsonMapper.ToJson(localChat);
                    }
            }
            return string.Empty;
        }
        public static void Read()
        {
            Clear();
            var playerId = PlayerDatas.Instance.baseData.PlayerID;
            var path = StringUtility.Contact(Application.persistentDataPath, "//", fileName, "_", playerId, ".log");
            if (!File.Exists(path))
            {
                return;
            }
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                {
                    ChatInfoType type = ChatInfoType.World;
                    while (!sr.EndOfStream)
                    {
                        try
                        {
                            var line = sr.ReadLine();
                            if (line.StartsWith("channel"))
                            {
                                type = (ChatInfoType)int.Parse(line.Split(':')[1]);
                                continue;
                            }
                            if (type == ChatInfoType.Fairy)
                            {
                                if (PlayerDatas.Instance.baseData.Family == 0)
                                {
                                    continue;
                                }
                            }
                            ChatData chat = null;
                            switch (type)
                            {
                                case ChatInfoType.World:
                                case ChatInfoType.Area:
                                case ChatInfoType.Team:
                                case ChatInfoType.Fairy:
                                    {
                                        LocalChat localChat = LitJson.JsonMapper.ToObject<LocalChat>(line);
                                        if (type == ChatInfoType.World)
                                        {
                                            chat = new ChatWorldData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        else if (type == ChatInfoType.Area)
                                        {
                                            chat = new ChatAreaData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        else if (type == ChatInfoType.Team)
                                        {
                                            chat = new ChatTeamData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        else if (type == ChatInfoType.Fairy)
                                        {
                                            chat = new ChatFamilyData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        chat.createTime = localChat.time;
                                    }
                                    break;
                                case ChatInfoType.Trumpet:
                                    {
                                        LocalTrumpetChat localChat = LitJson.JsonMapper.ToObject<LocalTrumpetChat>(line);
                                        chat = new ChatTrumpetData(localChat.content, localChat.player, localChat.name,
                                            localChat.extra, localChat.speakType, localChat.accId);
                                        chat.createTime = localChat.time;
                                    }
                                    break;
                                case ChatInfoType.Friend:
                                    {
                                        LocalFriendChat localChat = LitJson.JsonMapper.ToObject<LocalFriendChat>(line);
                                        chat = new ChatFriendData(localChat.content, localChat.player, localChat.name,
                                            localChat.extra, localChat.toName, localChat.talkType, (uint)localChat.toPlayer);
                                        chat.createTime = localChat.time;
                                    }
                                    break;
                            }
                            if (chat != null)
                            {
                                ChatCtrl.Inst.KeepLocalChat(chat);
                            }
                            Save(type, line);
                        }
                        catch (Exception e)
                        {
                            DebugEx.Log(e.Message);
                            continue;
                        }
                    }
                }
            }
        }
        public static void Clear(ChatInfoType type)
        {
            if (chatHistory.ContainsKey(type))
            {
                chatHistory.Remove(type);
            }
        }
        public static void Clear()
        {
            chatHistory.Clear();
        }
        public struct LocalChat
        {
            public ChatInfoType type;
            public DateTime time;
            public int player;
            public string name;
            public string extra;
            public string content;
        }
        public struct LocalFriendChat
        {
            public DateTime time;
            public int player;
            public string name;
            public string extra;
            public string content;
            public string toName;
            public byte talkType;
            public int toPlayer;
        }
        public struct LocalTrumpetChat
        {
            public DateTime time;
            public int player;
            public string name;
            public string extra;
            public string content;
            public byte speakType;
            public string accId;
        }
    }
}
System/Chat/LocalChatHistory.cs.meta
New file
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 518a87d1c24619f4b855261679120f6b
timeCreated: 1541992350
licenseType: Pro
MonoImporter:
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant: