RoomManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading.Channels;
  7. using ProtoDataBuff;
  8. using CSharpUtil;
  9. using MultiDup;
  10. namespace BattleRoom
  11. {
  12. /// <summary>
  13. /// 房间管理者, 负责 将连接上来的客户端转移到房间中
  14. /// </summary>
  15. internal class RoomManager : Singleton<RoomManager>
  16. {
  17. /// <summary>
  18. /// 服务器间的连接
  19. /// </summary>
  20. public ServerPeer ServerPeer;
  21. /// <summary>
  22. /// 客户端
  23. /// </summary>
  24. public Dictionary<int, Peer> ClientPeers = new Dictionary<int, Peer>();
  25. /// <summary>
  26. /// 消息分发
  27. /// </summary>
  28. private Dictionary<eProtocalCommand, Action<int, sSocketData>> callbacks = new Dictionary<eProtocalCommand, Action<int, sSocketData>>();
  29. /// <summary>
  30. /// 客户端消息队列
  31. /// </summary>
  32. public Channel<KeyValuePair<int, sSocketData>> MsgChannel = Channel.CreateBounded<KeyValuePair<int, sSocketData>>(1000);
  33. /// <summary>
  34. /// 房间列表
  35. /// </summary>
  36. Dictionary<int, Room> roomDic = new Dictionary<int, Room>();
  37. public RoomManager()
  38. {
  39. this.callbacks.Add(eProtocalCommand.CsBtLogin, On_BtLogin);
  40. this.callbacks.Add(eProtocalCommand.CsLeaveRoom, On_Leave);
  41. var t = Task.Run(MsgLoop);
  42. }
  43. private object lock_peers = new object();
  44. public void AddPeer(Peer p)
  45. {
  46. lock (lock_peers)
  47. {
  48. this.ClientPeers.Add(p.Id, p);
  49. Console.WriteLine(p.Id + " 连接上来了!");
  50. }
  51. }
  52. public void RemovePeer(int peerId)
  53. {
  54. lock (lock_peers)
  55. {
  56. if (this.ClientPeers.ContainsKey(peerId))
  57. {
  58. this.ClientPeers.Remove(peerId);
  59. }
  60. }
  61. }
  62. public void OnNewPeerConnected(Peer peer)
  63. {
  64. AddPeer(peer);
  65. }
  66. /// <summary>
  67. /// 进入大厅(登陆自己的uid)
  68. /// </summary>
  69. /// <param name="data"></param>
  70. void On_BtLogin(int peerId, sSocketData data)
  71. {
  72. var msg = CS_BT_Login.Parser.ParseFrom(data._data);
  73. Console.WriteLine("登录逻辑");
  74. if (this.ClientPeers.TryGetValue(peerId, out var peer))
  75. {
  76. Console.WriteLine("寻找对应的房间");
  77. roomDic.Values.ToList().ForEach(r =>
  78. {
  79. if (r.PlayerUids.Contains(msg.Uid))
  80. {
  81. peer.room = r; // 设置自己的room
  82. r.AddPeer(peer);
  83. Console.WriteLine("加入对应的房间");
  84. }
  85. });
  86. if (null == peer.room)
  87. {
  88. Console.WriteLine("未找到对应的房间");
  89. peer.SendEvent(eProtocalCommand.ScBtLogin, new SC_BT_Login() { Msg = "未找到对应房间!" });
  90. }
  91. else
  92. {
  93. peer.Properties.Add(PropertyName.Uid, msg.Uid);
  94. peer.Properties.Add(PropertyName.Zoneid, msg.Zoneid);
  95. peer.CurrentState = ClientState.InGame;
  96. peer.SendEvent(eProtocalCommand.ScBtLogin, new SC_BT_Login() { Msg = peer.room.Id.ToString() });
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// 创建一个房间(副本)
  102. /// </summary>
  103. /// <param name="data"></param>
  104. public void CreateRoom(sSocketData data)
  105. {
  106. Console.WriteLine("接收到创建房间指令!");
  107. var msg = SS_CreateBattleServer.Parser.ParseFrom(data._data);
  108. var room = new Room(msg.RoomId, msg.PlayerUids);
  109. roomDic.Add(room.Id, room);
  110. ServerPeer.SendEvent(eProtocalCommand.SsCreateBattleRoomOk, new SS_CreateBattleServerOK() { });
  111. // todo: 建立玩家列表, 当玩家登录进来以后, 将其连接转入房间
  112. }
  113. /// <summary>
  114. /// 离开房间(战斗结束)
  115. /// </summary>
  116. /// <param name="data"></param>
  117. void On_Leave(int peerId, sSocketData data)
  118. {
  119. if (this.ClientPeers.TryGetValue(peerId, out var peer))
  120. {
  121. peer.SendEvent(eProtocalCommand.ScMdLeaveRoom, new SC_MD_LeaveRoom() { });
  122. peer.Close();
  123. }
  124. }
  125. async void MsgLoop()
  126. {
  127. while (true)
  128. {
  129. var msg = await MsgChannel.Reader.ReadAsync();
  130. if (callbacks.ContainsKey(msg.Value._protocallType))
  131. {
  132. callbacks[msg.Value._protocallType](msg.Key, msg.Value);
  133. }
  134. else
  135. {
  136. // 未找到消息处理逻辑
  137. }
  138. }
  139. }
  140. }
  141. }