123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using UnityEngine;
- using UnityEngine.Events;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- public class ChatManager
- {
- public const int MAX_GLOBAL = 50;
- public const int MAX_FOLLOW = 25;
- public const int MAX_MAIL = 50;
- public const int MAX_CLAN = 50;
- private bool m_Initialized = false;
- public bool initialized
- {
- get{ return m_Initialized;}
- }
- private List<ChatMsg> globalList;
- private Dictionary<int, ChatFollow> followDict;
- private List<ChatFollow> followList;
- private List<ChatMsg> mailList;
- private int newMsgGlobalCount = 0;
- private int newMsgMailCount = 0;
- public ChatMsgEvent GlobalMsgGot = new ChatMsgEvent();
- public ChatMsgEvent FollowMsgGot = new ChatMsgEvent();
- public ChatMsgEvent MailMsgGot = new ChatMsgEvent();
- public ChatMsgEvent ClanMsgGot = new ChatMsgEvent();
- public UnityEvent MsgUpdate = new UnityEvent();
- public FindFriendResultEvent FindFriendResult = new FindFriendResultEvent();
- public ChatManager()
- {
- globalList = new List<ChatMsg> ();
- followDict = new Dictionary<int, ChatFollow> ();
- followList = new List<ChatFollow> ();
- mailList = new List<ChatMsg> ();
- SyncManager.Remove (SyncManager.SyncType.Chat);
- SyncManager.Add (SyncManager.SyncType.Chat, Sync, 60f);
- }
- public void Clear()
- {
- globalList.Clear ();
- followDict.Clear ();
- followList.Clear ();
- mailList.Clear ();
- m_Initialized = false;
- }
- private void CheckLatestId(int id)
- {
- if(latestId < id)
- {
- latestId = id;
- }
- }
- public void Init()
- {
- if (initialized)
- return;
- if (!Session.GetInstance ().myUserData.isLogin)
- return;
- JsonData list = LocalSaver.GetInstance ().LoadChat (Session.myUserId);
- Fill (list, true, false);
- m_Initialized = true;
- }
- private int latestId = 0;
- public void Sync()
- {
- Init ();
- if (!initialized)
- return;
- ChatRequest.Sync (latestId).ResultEvent.AddListener((bool success, JsonData data)=>{
- if(success)
- {
- Fill(data, latestId==0, true);
- MsgUpdate.Invoke();
- }
- });
- }
- private void Fill(JsonData data, bool isLocal, bool isNew)
- {
- int myUserId = Session.myUserId;
- for(int i=0; i<data.Count; i++)
- {
- ChatMsg chatMsg = new ChatMsg(data [i]);
- CheckLatestId (chatMsg.id);
- if (!isLocal && LocalSaver.GetInstance().HasChatId(chatMsg.id))
- continue;
- switch(chatMsg.categovy)
- {
- // case ChatMsg.Category.Clan:
- case ChatMsg.Category.Replay:
- AddGlobalMsg (chatMsg);
- newMsgGlobalCount += isNew ? 1 : 0;
- break;
- case ChatMsg.Category.Follow:
- AddFollowMsg (chatMsg);
- break;
- case ChatMsg.Category.Request:
- newMsgMailCount += isNew ? 1 : 0;
- AddMailMsg (chatMsg);
- break;
- }
- if (isNew)
- SaveMsg (chatMsg);
- }
- }
- private void SaveMsg(ChatMsg chatMsg)
- {
- LocalSaver.GetInstance ().AddChat (chatMsg, Session.myUserId);
- }
- private void UnsaveMsg(ChatMsg chatMsg)
- {
- LocalSaver.GetInstance ().RemoveChat (chatMsg.id, Session.myUserId);
- }
- public void AddReplayMsg(string info, string fileName)
- {
- int myUserId = Session.myUserId;
- ChatMsg chatMsg = new ChatMsg ();
- chatMsg.id = LocalSaver.GetInstance().NewTempChatId(myUserId);
- chatMsg.categovy = ChatMsg.Category.Replay;
- chatMsg.sender = myUserId;
- chatMsg.content = info;
- chatMsg.extra = fileName;
- chatMsg.dateTime = ChatMsg.GetNowDateTime ();
- SaveMsg (chatMsg);
- AddGlobalMsg (chatMsg);
- newMsgGlobalCount++;
- MsgUpdate.Invoke();
- }
- public void ShareReplayMsg(ChatMsg chatMsg, ItemRenderer itemRender)
- {
- if (chatMsg == null)
- return;
-
- ChatRequest.Replay (chatMsg).ResultEvent.AddListener ((bool success, JsonData data)=>{
- if(success)
- {
- int id = JsonUtil.ToInt(data["id"]);
- string time = data["time"].ToString();
- int oldId = chatMsg.id;
- chatMsg.id = id;
- chatMsg.SetDateTime(time);
- LocalSaver.GetInstance().ReplaceChat(oldId, chatMsg, Session.myUserId);
- itemRender.data = itemRender.data;
- }
- });
- }
- public void AddGlobalMsg(ChatMsg chatMsg)
- {
- globalList.Add (chatMsg);
- GlobalMsgGot.Invoke (chatMsg);
- while(globalList.Count > MAX_GLOBAL)
- {
- ChatMsg firstMsg = globalList [0];
- globalList.RemoveAt (0);
- UnsaveMsg (firstMsg);
- }
- }
- public List<ChatMsg> GetGlobalMsgList()
- {
- Init ();
- return globalList;
- }
- public int GetNewMsgGlobalCount()
- {
- return newMsgGlobalCount;
- }
- public void ResetNewMsgClanCount()
- {
- newMsgGlobalCount = 0;
- MsgUpdate.Invoke();
- }
- public void FindFriend(string key)
- {
- UserRequest.Find (key).ResultEvent.AddListener ((bool success, JsonData data)=>{
- if(success)
- {
- List<UserData> list = new List<UserData>();
- for(int i=0; i<data.Count; i++)
- {
- UserData user = UserCache.Add(data[i]);
- list.Add(user);
- }
- FindFriendResult.Invoke(list);
- }
- });
- }
- public void AddMyFollowMsg(int id, int receiver, string content, string time)
- {
- UserData myUserData = Session.GetInstance ().myUserData;
- ChatMsg chatMsg = new ChatMsg ();
- chatMsg.id = id;
- chatMsg.categovy = ChatMsg.Category.Follow;
- chatMsg.sender = myUserData.id;
- chatMsg.receiver = receiver;
- chatMsg.content = content;
- chatMsg.dateTime = System.Convert.ToDateTime (time);
- AddFollowMsg (chatMsg);
- }
- public void AddFollowMsg(ChatMsg chatMsg)
- {
- int userId = chatMsg.sender == Session.GetInstance ().myUserData.id ? chatMsg.receiver : chatMsg.sender;
- ChatFollow chatFollow = null;
- followDict.TryGetValue (userId, out chatFollow);
- if (chatFollow == null) {
- chatFollow = new ChatFollow ();
- followDict.Add (userId, chatFollow);
- followList.Add (chatFollow);
- chatFollow.userId = userId;
- }
- chatFollow.AddMsg (chatMsg);
- }
- public List<ChatMsg> GetFollowMsgList(int userId)
- {
- ChatFollow chatFollow = null;
- followDict.TryGetValue (userId, out chatFollow);
- if (chatFollow == null) {
- return null;
- }
- return chatFollow.GetChatList ();
- }
- public void AddClanMsg(int sender, string content)
- {
- ChatMsg chatMsg = new ChatMsg ();
- chatMsg.categovy = ChatMsg.Category.Clan;
- chatMsg.sender = sender;
- chatMsg.content = content;
- chatMsg.dateTime = ChatMsg.GetNowDateTime ();
- ClanMsgGot.Invoke (chatMsg);
- }
- public void AddMailMsg(ChatMsg chatMsg)
- {
- mailList.Add (chatMsg);
- MailMsgGot.Invoke (chatMsg);
- while(mailList.Count > MAX_MAIL)
- {
- mailList.RemoveAt (0);
- }
- }
- public List<ChatMsg> GetMailMsgList()
- {
- Init ();
- return mailList;
- }
- private static ChatManager instance;
- public static ChatManager GetInstance()
- {
- if (instance == null)
- instance = new ChatManager ();
- return instance;
- }
- }
- public class ChatMsgEvent : UnityEvent<ChatMsg>{}
- public class FindFriendResultEvent : UnityEvent<List<UserData>>{}
|