RoomData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RoomData{
  5. public List<SocketOnlineRoomItemData> dataList = new List<SocketOnlineRoomItemData>();
  6. public Dictionary<int, UserData> userList = new Dictionary<int, UserData>();//当前的房间里 听众列表
  7. private short cur_room_id =-1;//当前所在的房间的id号
  8. private AudioData CurAudioData = new AudioData();
  9. public short CurRoomId
  10. {
  11. get { return cur_room_id; }
  12. set {
  13. cur_room_id = value;
  14. }
  15. }
  16. /*
  17. public bool AudioOpenValue
  18. {
  19. get { return this.CurAudioData.isOpen; }
  20. }
  21. public bool AudioIsEmpty
  22. {
  23. get { return this.CurAudioData.roomId == null; }
  24. }
  25. private void AudioCreated(string audioId)
  26. {
  27. this.CurAudioData.roomId = audioId;
  28. this.CurAudioData.isOpen = true;
  29. MessageCenter.Instance.Broadcast(GameEnum.MESSAGE_SEND_AUDIO_SOCKET_DATA, this.CurAudioData);
  30. }
  31. public void OpenAudio()
  32. {
  33. this.CurAudioData.isOpen = true;
  34. MessageCenter.Instance.Broadcast(GameEnum.MESSAGE_SEND_AUDIO_SOCKET_DATA, this.CurAudioData);
  35. }
  36. private void CloseAudio()
  37. {
  38. this.CurAudioData.isOpen = false;
  39. MessageCenter.Instance.Broadcast(GameEnum.MESSAGE_SEND_AUDIO_SOCKET_DATA, this.CurAudioData);
  40. }
  41. */
  42. public void SetRoomId(Engine.Net.NetMsg iMsg)
  43. {
  44. CurRoomData data = new CurRoomData();
  45. iMsg.UnPackMsg(ref data);
  46. CurRoomId = data.room_id;
  47. CDebug.Log("重新登陆 拿到的我的 当前的房间号是" + CurRoomId);
  48. }
  49. public void InitData(Engine.Net.NetMsg iMsg)
  50. {
  51. CDebug.Log("socket房间列表信息的解析");
  52. SocketRoomItemArrayData data = new SocketRoomItemArrayData();
  53. iMsg.UnPackMsg(ref data);
  54. dataList.Clear();
  55. CDebug.Log("当前有" + data.ct + "个房间");
  56. for(int i= 0; i< data.ct; i++)
  57. {
  58. AddRoomItemData(iMsg);
  59. }
  60. //dataList = new List<SocketOnlineRoomItemData>(data.datas);//直接覆盖
  61. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_ROOM_LIST_VIEW);
  62. }
  63. private void AddRoomItemData(Engine.Net.NetMsg iMsg)
  64. {
  65. SocketOnlineRoomItemData data = new SocketOnlineRoomItemData();
  66. iMsg.UnPackMsg(ref data);
  67. dataList.Add(data);
  68. }
  69. public void AddRommData(Engine.Net.NetMsg iMsg)
  70. {
  71. SocketOnlineRoomItemData data = new SocketOnlineRoomItemData();
  72. iMsg.UnPackMsg(ref data);
  73. dataList.Add(data);
  74. //检查下创建房间的所有者者是是不是我自己
  75. if (data.owner_id.Equals(GamePlayerData.Instance.user_id) || data.owner_id.ToString().Equals(GamePlayerData.Instance.user_id))
  76. {
  77. GamePlayerData.Instance.room_data.CurRoomId = data.room_id;//创建了房间
  78. UserData myData = new UserData();
  79. myData.user_id = GamePlayerData.Instance.user_id;
  80. myData.user_name = GamePlayerData.Instance.user_name;
  81. myData.user_type = (short)GamePlayerData.Instance.user_type;
  82. GamePlayerData.Instance.room_data.userList.Clear();
  83. GamePlayerData.Instance.room_data.userList.Add(myData.user_id, myData);
  84. //获取课程信息 课程信息获取了才能进入房间
  85. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_ENTER_COURSE);
  86. //IvChatManager.Instance.CreateRoom(data.room_id.ToString());
  87. }
  88. else
  89. {
  90. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_ROOM_LIST_VIEW);
  91. }
  92. }
  93. public void UpdateRoomData(Engine.Net.NetMsg iMsg)
  94. {
  95. RoomOnlineData data = new RoomOnlineData();
  96. iMsg.UnPackMsg(ref data);
  97. foreach (SocketOnlineRoomItemData r_data in dataList)
  98. {
  99. if (r_data.room_id.Equals(data.room_id))
  100. {
  101. r_data.onlineCt = data.online_ct;
  102. break;
  103. }
  104. }
  105. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_ROOM_LIST_VIEW);
  106. }
  107. //销毁房间的消息
  108. public void RemoveRoomData(Engine.Net.NetMsg iMsg)
  109. {
  110. DeleteRoomData data = new DeleteRoomData();
  111. iMsg.UnPackMsg(ref data);
  112. foreach(SocketOnlineRoomItemData r_data in dataList)
  113. {
  114. if(r_data.room_id.Equals(data.room_id))
  115. {
  116. if(r_data.room_id == GamePlayerData.Instance.room_data.CurRoomId && !GamePlayerData.Instance.room_data.IsReconnectOwner())
  117. {
  118. if(GamePlayerData.Instance.room_data.CurRoomId == -1)
  119. {
  120. CDebug.Log("这里有严重的逻辑错误");
  121. return;
  122. }
  123. //当前的房间销毁了
  124. GamePlayerData.Instance.room_data.CurRoomId = -1;
  125. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_EXIT_COURSE);
  126. GameNetManager.Instance.mSocketMsgCenter.SendGetDatingData();//获取大厅的消息
  127. }
  128. dataList.Remove(r_data);
  129. //这里需要更新房间列表的删除情况
  130. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_ROOM_LIST_VIEW);
  131. break;
  132. }
  133. }
  134. }
  135. public void TestData(Engine.Net.NetMsg iMsg)
  136. {
  137. TestData data = new TestData();
  138. iMsg.UnPackMsg(ref data);
  139. CDebug.LogObj(data);
  140. }
  141. public List<System.Object> PublicList
  142. {
  143. get
  144. {
  145. return GetListBySubjectId(true);
  146. }
  147. }
  148. public List<System.Object> PrivateList
  149. {
  150. get
  151. {
  152. return GetListBySubjectId(false);
  153. }
  154. }
  155. private List<System.Object> GetListBySubjectId(bool id_str)
  156. {
  157. List<System.Object> t_list = new List<System.Object>();
  158. foreach (SocketOnlineRoomItemData data in dataList)
  159. {
  160. if (data.is_public == id_str)
  161. {
  162. object someBoxedStruct = data;
  163. t_list.Add(data);
  164. }
  165. }
  166. return t_list;
  167. }
  168. //初始化房间里的用户列表
  169. public void InitUserList(Engine.Net.NetMsg iMsg)
  170. {
  171. StudentListBackData data = new StudentListBackData();
  172. iMsg.UnPackMsg(ref data);
  173. userList.Clear();//清空数据
  174. for (int i = 0; i < data.ct; i++)
  175. {
  176. UserData student = new UserData();
  177. iMsg.UnPackMsg(ref student);
  178. if(student.user_id.Equals(GamePlayerData.Instance.user_id))
  179. {
  180. //continue;//排除掉自己
  181. }
  182. userList.Add(student.user_id, student);
  183. if (student.user_type == GameEnum.STUDENT_TYPE)
  184. StudentTaskReceiveManager.Instance.AddEmptyStudentPaper(student.user_id);
  185. }
  186. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_USER_LIST_DATA);
  187. }
  188. public void UpdateUser(Engine.Net.NetMsg iMsg)
  189. {
  190. UserEnterBackData data = new UserEnterBackData();
  191. iMsg.UnPackMsg(ref data);
  192. if(data.is_enter && !userList.ContainsKey(iMsg.UserId))
  193. {
  194. UserData student = new UserData();
  195. student.user_id = iMsg.UserId;
  196. student.user_type = data.user_type;
  197. student.user_name = data.user_name;
  198. userList.Add(iMsg.UserId, student);
  199. if(student.user_type == GameEnum.STUDENT_TYPE)
  200. StudentTaskReceiveManager.Instance.AddEmptyStudentPaper(student.user_id);
  201. if(GamePlayerData.Instance.IsFangzhu())
  202. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_MSG_TO_NEW_STUDENT, iMsg.UserId);//同步给刚进来的学生当前的操作的数据
  203. }
  204. if(!data.is_enter && userList.ContainsKey(iMsg.UserId))
  205. {
  206. userList.Remove(iMsg.UserId);
  207. StudentTaskReceiveManager.Instance.RemoveStudentPaper(iMsg.UserId);
  208. }
  209. CDebug.Log("当前房间的人数 "+ userList.Count);
  210. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_USER_LIST_DATA);//更新数据
  211. }
  212. //这个地方需要优化
  213. public bool NeedReconnect()
  214. {
  215. foreach(SocketOnlineRoomItemData r_data in dataList)
  216. {
  217. if(r_data.room_id == CurRoomId)
  218. {
  219. CDebug.Log("找到了已经存在的房间");
  220. if(r_data.owner_id == GamePlayerData.Instance.user_id)
  221. {
  222. CDebug.Log("是房主");
  223. }
  224. else
  225. {
  226. CDebug.Log("是听课的");
  227. }
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. public bool IsReconnectOwner()
  234. {
  235. foreach (SocketOnlineRoomItemData r_data in dataList)
  236. {
  237. if (r_data.room_id == CurRoomId)
  238. {
  239. if (r_data.owner_id == GamePlayerData.Instance.user_id)
  240. {
  241. return true;
  242. }
  243. }
  244. }
  245. return false;
  246. }
  247. public SocketOnlineRoomItemData CurOnlineData
  248. {
  249. get {
  250. foreach(SocketOnlineRoomItemData data in dataList)
  251. {
  252. if(data.room_id == CurRoomId)
  253. {
  254. return data;
  255. }
  256. }
  257. return null;
  258. }
  259. }
  260. //房间里有没有听众
  261. public bool IsEmptyUser
  262. {
  263. get {
  264. return userList.Count <= 1;//1是老师 一定是老师在里面 所以从1开始判定
  265. }
  266. }
  267. //切换房主
  268. public void ChangeOwnerId(Engine.Net.NetMsg iMsg)
  269. {
  270. ChangeOwner data = new ChangeOwner();
  271. iMsg.UnPackMsg(ref data);
  272. CDebug.Log("收到的切换房主的信息内容 新房主 " + data.owner_id + " 房间号 " + data.room_id);
  273. if(iMsg.UserId == GamePlayerData.Instance.user_id)
  274. {
  275. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_MSG_POP, "切换成功");
  276. //IvChatManager.Instance.SetSilentValue(true);
  277. }
  278. //更新大厅列表
  279. foreach (SocketOnlineRoomItemData r_data in dataList)
  280. {
  281. if (r_data.room_id.Equals(data.room_id))
  282. {
  283. r_data.owner_id = data.owner_id;
  284. if(r_data.owner_id == GamePlayerData.Instance.user_id)
  285. {
  286. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_MSG_POP, "你是新的房主");
  287. //IvChatManager.Instance.SetSilentValue(false);
  288. }
  289. break;
  290. }
  291. }
  292. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_ROOM_LIST_VIEW);
  293. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_CHANGE_ROOM_OWNER_REFRESH);//有房主的变动
  294. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_REFRESH_USER_LIST_DATA);
  295. }
  296. }