ChatManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using LitJson;
  6. public class ChatManager
  7. {
  8. public const int MAX_GLOBAL = 50;
  9. public const int MAX_FOLLOW = 25;
  10. public const int MAX_MAIL = 50;
  11. public const int MAX_CLAN = 50;
  12. private bool m_Initialized = false;
  13. public bool initialized
  14. {
  15. get{ return m_Initialized;}
  16. }
  17. private List<ChatMsg> globalList;
  18. private Dictionary<int, ChatFollow> followDict;
  19. private List<ChatFollow> followList;
  20. private List<ChatMsg> mailList;
  21. private int newMsgGlobalCount = 0;
  22. private int newMsgMailCount = 0;
  23. public ChatMsgEvent GlobalMsgGot = new ChatMsgEvent();
  24. public ChatMsgEvent FollowMsgGot = new ChatMsgEvent();
  25. public ChatMsgEvent MailMsgGot = new ChatMsgEvent();
  26. public ChatMsgEvent ClanMsgGot = new ChatMsgEvent();
  27. public UnityEvent MsgUpdate = new UnityEvent();
  28. public FindFriendResultEvent FindFriendResult = new FindFriendResultEvent();
  29. public ChatManager()
  30. {
  31. globalList = new List<ChatMsg> ();
  32. followDict = new Dictionary<int, ChatFollow> ();
  33. followList = new List<ChatFollow> ();
  34. mailList = new List<ChatMsg> ();
  35. SyncManager.Remove (SyncManager.SyncType.Chat);
  36. SyncManager.Add (SyncManager.SyncType.Chat, Sync, 60f);
  37. }
  38. public void Clear()
  39. {
  40. globalList.Clear ();
  41. followDict.Clear ();
  42. followList.Clear ();
  43. mailList.Clear ();
  44. m_Initialized = false;
  45. }
  46. private void CheckLatestId(int id)
  47. {
  48. if(latestId < id)
  49. {
  50. latestId = id;
  51. }
  52. }
  53. public void Init()
  54. {
  55. if (initialized)
  56. return;
  57. if (!Session.GetInstance ().myUserData.isLogin)
  58. return;
  59. JsonData list = LocalSaver.GetInstance ().LoadChat (Session.myUserId);
  60. Fill (list, true, false);
  61. m_Initialized = true;
  62. }
  63. private int latestId = 0;
  64. public void Sync()
  65. {
  66. Init ();
  67. if (!initialized)
  68. return;
  69. ChatRequest.Sync (latestId).ResultEvent.AddListener((bool success, JsonData data)=>{
  70. if(success)
  71. {
  72. Fill(data, latestId==0, true);
  73. MsgUpdate.Invoke();
  74. }
  75. });
  76. }
  77. private void Fill(JsonData data, bool isLocal, bool isNew)
  78. {
  79. int myUserId = Session.myUserId;
  80. for(int i=0; i<data.Count; i++)
  81. {
  82. ChatMsg chatMsg = new ChatMsg(data [i]);
  83. CheckLatestId (chatMsg.id);
  84. if (!isLocal && LocalSaver.GetInstance().HasChatId(chatMsg.id))
  85. continue;
  86. switch(chatMsg.categovy)
  87. {
  88. // case ChatMsg.Category.Clan:
  89. case ChatMsg.Category.Replay:
  90. AddGlobalMsg (chatMsg);
  91. newMsgGlobalCount += isNew ? 1 : 0;
  92. break;
  93. case ChatMsg.Category.Follow:
  94. AddFollowMsg (chatMsg);
  95. break;
  96. case ChatMsg.Category.Request:
  97. newMsgMailCount += isNew ? 1 : 0;
  98. AddMailMsg (chatMsg);
  99. break;
  100. }
  101. if (isNew)
  102. SaveMsg (chatMsg);
  103. }
  104. }
  105. private void SaveMsg(ChatMsg chatMsg)
  106. {
  107. LocalSaver.GetInstance ().AddChat (chatMsg, Session.myUserId);
  108. }
  109. private void UnsaveMsg(ChatMsg chatMsg)
  110. {
  111. LocalSaver.GetInstance ().RemoveChat (chatMsg.id, Session.myUserId);
  112. }
  113. public void AddReplayMsg(string info, string fileName)
  114. {
  115. int myUserId = Session.myUserId;
  116. ChatMsg chatMsg = new ChatMsg ();
  117. chatMsg.id = LocalSaver.GetInstance().NewTempChatId(myUserId);
  118. chatMsg.categovy = ChatMsg.Category.Replay;
  119. chatMsg.sender = myUserId;
  120. chatMsg.content = info;
  121. chatMsg.extra = fileName;
  122. chatMsg.dateTime = ChatMsg.GetNowDateTime ();
  123. SaveMsg (chatMsg);
  124. AddGlobalMsg (chatMsg);
  125. newMsgGlobalCount++;
  126. MsgUpdate.Invoke();
  127. }
  128. public void ShareReplayMsg(ChatMsg chatMsg, ItemRenderer itemRender)
  129. {
  130. if (chatMsg == null)
  131. return;
  132. ChatRequest.Replay (chatMsg).ResultEvent.AddListener ((bool success, JsonData data)=>{
  133. if(success)
  134. {
  135. int id = JsonUtil.ToInt(data["id"]);
  136. string time = data["time"].ToString();
  137. int oldId = chatMsg.id;
  138. chatMsg.id = id;
  139. chatMsg.SetDateTime(time);
  140. LocalSaver.GetInstance().ReplaceChat(oldId, chatMsg, Session.myUserId);
  141. itemRender.data = itemRender.data;
  142. }
  143. });
  144. }
  145. public void AddGlobalMsg(ChatMsg chatMsg)
  146. {
  147. globalList.Add (chatMsg);
  148. GlobalMsgGot.Invoke (chatMsg);
  149. while(globalList.Count > MAX_GLOBAL)
  150. {
  151. ChatMsg firstMsg = globalList [0];
  152. globalList.RemoveAt (0);
  153. UnsaveMsg (firstMsg);
  154. }
  155. }
  156. public List<ChatMsg> GetGlobalMsgList()
  157. {
  158. Init ();
  159. return globalList;
  160. }
  161. public int GetNewMsgGlobalCount()
  162. {
  163. return newMsgGlobalCount;
  164. }
  165. public void ResetNewMsgClanCount()
  166. {
  167. newMsgGlobalCount = 0;
  168. MsgUpdate.Invoke();
  169. }
  170. public void FindFriend(string key)
  171. {
  172. UserRequest.Find (key).ResultEvent.AddListener ((bool success, JsonData data)=>{
  173. if(success)
  174. {
  175. List<UserData> list = new List<UserData>();
  176. for(int i=0; i<data.Count; i++)
  177. {
  178. UserData user = UserCache.Add(data[i]);
  179. list.Add(user);
  180. }
  181. FindFriendResult.Invoke(list);
  182. }
  183. });
  184. }
  185. public void AddMyFollowMsg(int id, int receiver, string content, string time)
  186. {
  187. UserData myUserData = Session.GetInstance ().myUserData;
  188. ChatMsg chatMsg = new ChatMsg ();
  189. chatMsg.id = id;
  190. chatMsg.categovy = ChatMsg.Category.Follow;
  191. chatMsg.sender = myUserData.id;
  192. chatMsg.receiver = receiver;
  193. chatMsg.content = content;
  194. chatMsg.dateTime = System.Convert.ToDateTime (time);
  195. AddFollowMsg (chatMsg);
  196. }
  197. public void AddFollowMsg(ChatMsg chatMsg)
  198. {
  199. int userId = chatMsg.sender == Session.GetInstance ().myUserData.id ? chatMsg.receiver : chatMsg.sender;
  200. ChatFollow chatFollow = null;
  201. followDict.TryGetValue (userId, out chatFollow);
  202. if (chatFollow == null) {
  203. chatFollow = new ChatFollow ();
  204. followDict.Add (userId, chatFollow);
  205. followList.Add (chatFollow);
  206. chatFollow.userId = userId;
  207. }
  208. chatFollow.AddMsg (chatMsg);
  209. }
  210. public List<ChatMsg> GetFollowMsgList(int userId)
  211. {
  212. ChatFollow chatFollow = null;
  213. followDict.TryGetValue (userId, out chatFollow);
  214. if (chatFollow == null) {
  215. return null;
  216. }
  217. return chatFollow.GetChatList ();
  218. }
  219. public void AddClanMsg(int sender, string content)
  220. {
  221. ChatMsg chatMsg = new ChatMsg ();
  222. chatMsg.categovy = ChatMsg.Category.Clan;
  223. chatMsg.sender = sender;
  224. chatMsg.content = content;
  225. chatMsg.dateTime = ChatMsg.GetNowDateTime ();
  226. ClanMsgGot.Invoke (chatMsg);
  227. }
  228. public void AddMailMsg(ChatMsg chatMsg)
  229. {
  230. mailList.Add (chatMsg);
  231. MailMsgGot.Invoke (chatMsg);
  232. while(mailList.Count > MAX_MAIL)
  233. {
  234. mailList.RemoveAt (0);
  235. }
  236. }
  237. public List<ChatMsg> GetMailMsgList()
  238. {
  239. Init ();
  240. return mailList;
  241. }
  242. private static ChatManager instance;
  243. public static ChatManager GetInstance()
  244. {
  245. if (instance == null)
  246. instance = new ChatManager ();
  247. return instance;
  248. }
  249. }
  250. public class ChatMsgEvent : UnityEvent<ChatMsg>{}
  251. public class FindFriendResultEvent : UnityEvent<List<UserData>>{}