微信登录

Server - Room.cs - 房间

  1. //房间状态
  2. enum RoomState {}
  3. class Room
  4. {
  5. private const int MAX_HP = 200;
  6. private List<Client> clientRoom = new List<Client>();
  7. private RoomState state = RoomState.WaitingJoin;
  8. private Server server;
  9. public Room(Server server) {}
  10. //等待加入
  11. public bool IsWaitingJoin(){}
  12. //加入用户
  13. public void AddClient(客户端){}
  14. //移除用户
  15. public void RemoveClient(客户端){}
  16. //获得房间的数据
  17. public string GetHouseOwnerData() {}
  18. //获得ID
  19. public int GetId(){}
  20. //获得房间的数据
  21. public String GetRoomData(){}
  22. //广播信息
  23. public void BroadcastMessage(其他客户端,动作码,数据){}
  24. //是否房主
  25. public bool IsHouseOwner(客户端){}
  26. //离开房间
  27. public void QuitRoom(客户端){}
  28. //关闭房间
  29. public void Close(){}
  30. //开始计时
  31. public void StartTimer(){}
  32. //持续时间
  33. private void RunTimer(){}
  34. //受到伤害
  35. public void TakeDamage(伤害,其他客户端){}
  36. }
  1. //房间状态
  2. enum RoomState {}
  3. class Room
  4. {
  5. private const int MAX_HP = 200;
  6. private List<Client> clientRoom = new List<Client>();
  7. private RoomState state = RoomState.WaitingJoin;
  8. private Server server;
  9. public Room(Server server) {}
  10. //等待加入
  11. public bool IsWaitingJoin(){}
  12. //加入用户
  13. public void AddClient(Client client){}
  14. //移除用户
  15. public void RemoveClient(Client client){}
  16. //获得房间的数据
  17. public string GetHouseOwnerData() {}
  18. //获得ID
  19. public int GetId(){}
  20. //获得房间的数据
  21. public String GetRoomData(){}
  22. //广播信息
  23. public void BroadcastMessage(Client excludeClient,ActionCode actionCode,string data){}
  24. //是否房主
  25. public bool IsHouseOwner(Client client){}
  26. //离开房间
  27. public void QuitRoom(Client client){}
  28. //关闭房间
  29. public void Close(){}
  30. //开始计时
  31. public void StartTimer(){}
  32. //持续时间
  33. private void RunTimer(){}
  34. //受到伤害
  35. public void TakeDamage(int damage,Client excludeClient){}
  36. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Common;
  7. using System.Threading;
  8. namespace GameServer.Servers
  9. {
  10. enum RoomState
  11. {
  12. WaitingJoin,
  13. WaitingBattle,
  14. Battle,
  15. End
  16. }
  17. class Room
  18. {
  19. private const int MAX_HP = 200;
  20. private List<Client> clientRoom = new List<Client>();
  21. private RoomState state = RoomState.WaitingJoin;
  22. private Server server;
  23. public Room(Server server)
  24. {
  25. this.server = server;
  26. }
  27. //等待加入
  28. public bool IsWaitingJoin()
  29. {
  30. return state == RoomState.WaitingJoin;
  31. }
  32. //加入用户
  33. public void AddClient(Client client)
  34. {
  35. client.HP = MAX_HP;
  36. clientRoom.Add(client);
  37. client.Room = this;
  38. if (clientRoom.Count>= 2)
  39. {
  40. state = RoomState.WaitingBattle;
  41. }
  42. }
  43. //移除用户
  44. public void RemoveClient(Client client)
  45. {
  46. client.Room = null;
  47. clientRoom.Remove(client);
  48. if (clientRoom.Count >= 2)
  49. {
  50. state = RoomState.WaitingBattle;
  51. }
  52. else
  53. {
  54. state = RoomState.WaitingJoin;
  55. }
  56. }
  57. //获得房间的数据
  58. public string GetHouseOwnerData()
  59. {
  60. return clientRoom[0].GetUserData();
  61. }
  62. //获得ID
  63. public int GetId()
  64. {
  65. if (clientRoom.Count > 0)
  66. {
  67. return clientRoom[0].GetUserId();
  68. }
  69. return -1;
  70. }
  71. //获得房间的数据
  72. public String GetRoomData()
  73. {
  74. StringBuilder sb = new StringBuilder();
  75. foreach(Client client in clientRoom)
  76. {
  77. sb.Append(client.GetUserData() + "|");
  78. }
  79. if (sb.Length > 0)
  80. {
  81. sb.Remove(sb.Length - 1, 1);
  82. }
  83. return sb.ToString();
  84. }
  85. //广播信息
  86. public void BroadcastMessage(Client excludeClient,ActionCode actionCode,string data)
  87. {
  88. foreach(Client client in clientRoom)
  89. {
  90. if (client != excludeClient)
  91. {
  92. server.SendResponse(client, actionCode, data);
  93. }
  94. }
  95. }
  96. //是否房主
  97. public bool IsHouseOwner(Client client)
  98. {
  99. return client == clientRoom[0];
  100. }
  101. //离开房间
  102. public void QuitRoom(Client client)
  103. {
  104. if (client == clientRoom[0])
  105. {
  106. Close();
  107. }
  108. else
  109. clientRoom.Remove(client);
  110. }
  111. //关闭房间
  112. public void Close()
  113. {
  114. foreach(Client client in clientRoom)
  115. {
  116. client.Room = null;
  117. }
  118. server.RemoveRoom(this);
  119. }
  120. //开始计时
  121. public void StartTimer()
  122. {
  123. new Thread(RunTimer).Start();
  124. }
  125. //持续时间
  126. private void RunTimer()
  127. {
  128. Thread.Sleep(1000);
  129. for (int i = 3; i > 0; i--)
  130. {
  131. BroadcastMessage(null, ActionCode.ShowTimer, i.ToString());
  132. Thread.Sleep(1000);
  133. }
  134. BroadcastMessage(null, ActionCode.StartPlay, "r");
  135. }
  136. //受到伤害
  137. public void TakeDamage(int damage,Client excludeClient)
  138. {
  139. bool isDie = false;
  140. foreach (Client client in clientRoom)
  141. {
  142. if (client != excludeClient)
  143. {
  144. if (client.TakeDamage(damage))
  145. {
  146. isDie = true;
  147. }
  148. }
  149. }
  150. if (isDie == false) return;
  151. //如果其中一个角色死亡,要结束游戏
  152. foreach (Client client in clientRoom)
  153. {
  154. if (client.IsDie())
  155. {
  156. client.UpdateResult(false);
  157. client.Send(ActionCode.GameOver, ((int)ReturnCode.Fail).ToString());
  158. }
  159. else
  160. {
  161. client.UpdateResult(true);
  162. client.Send(ActionCode.GameOver, ((int)ReturnCode.Success).ToString());
  163. }
  164. }
  165. Close();
  166. }
  167. }
  168. }
Server - Room.cs - 房间