• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

98 - Controller - RoomController.cs - 房间操作

作者:

贺及楼

成为作者

更新日期:2023-09-17 10:57:59

  1. class RoomController:BaseController
  2. {
  3. public RoomController(){}
  4. //新建房间
  5. public string CreateRoom(数据, 客户端, 系统){}
  6. //房间列表
  7. public string ListRoom(数据, 客户端, 系统){}
  8. //加入房间
  9. public string JoinRoom(数据, 客户端, 系统){}
  10. //离开房间
  11. public string QuitRoom(数据,客户端, 系统){}
  12. }
  1. class RoomController:BaseController
  2. {
  3. public RoomController(){}
  4. //新建房间
  5. public string CreateRoom(string data, Client client, Server server){}
  6. //房间列表
  7. public string ListRoom(string data, Client client, Server server){}
  8. //加入房间
  9. public string JoinRoom(string data, Client client, Server server){}
  10. //离开房间
  11. public string QuitRoom(string data, Client client, Server server){}
  12. }
  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 GameServer.Servers;
  8. namespace GameServer.Controller
  9. {
  10. class RoomController:BaseController
  11. {
  12. public RoomController()
  13. {
  14. requestCode = RequestCode.Room;
  15. }
  16. //新建房间
  17. public string CreateRoom(string data, Client client, Server server)
  18. {
  19. server.CreateRoom(client);
  20. return ((int)ReturnCode.Success).ToString()+","+ ((int)RoleType.Blue).ToString();
  21. }
  22. //房间列表
  23. public string ListRoom(string data, Client client, Server server)
  24. {
  25. StringBuilder sb = new StringBuilder();
  26. foreach(Room room in server.GetRoomList())
  27. {
  28. if (room.IsWaitingJoin())
  29. {
  30. sb.Append(room.GetHouseOwnerData()+"|");
  31. }
  32. }
  33. if (sb.Length == 0)
  34. {
  35. sb.Append("0");//没有房间返回0
  36. }
  37. else
  38. {
  39. sb.Remove(sb.Length - 1, 1);//去掉中杠
  40. }
  41. return sb.ToString();
  42. }
  43. //加入房间
  44. public string JoinRoom(string data, Client client, Server server)
  45. {
  46. int id = int.Parse(data);
  47. Room room = server.GetRoomById(id);
  48. if(room == null)
  49. {
  50. return ((int)ReturnCode.NotFound).ToString();
  51. }
  52. else if (room.IsWaitingJoin() == false)
  53. {
  54. return ((int)ReturnCode.Fail).ToString();
  55. }
  56. else
  57. {
  58. room.AddClient(client);
  59. string roomData = room.GetRoomData();//"returncode,roletype-id,username,tc,wc|id,username,tc,wc"
  60. room.BroadcastMessage(client, ActionCode.UpdateRoom, roomData);
  61. return ((int)ReturnCode.Success).ToString() + "," + ((int)RoleType.Red).ToString()+ "-" + roomData;
  62. }
  63. }
  64. //离开房间
  65. public string QuitRoom(string data, Client client, Server server)
  66. {
  67. bool isHouseOwner = client.IsHouseOwner();
  68. Room room = client.Room;
  69. if (isHouseOwner)
  70. {
  71. room.BroadcastMessage(client, ActionCode.QuitRoom, ((int)ReturnCode.Success).ToString());
  72. room.Close();
  73. return ((int)ReturnCode.Success).ToString();
  74. }
  75. else
  76. {
  77. client.Room.RemoveClient(client);
  78. room.BroadcastMessage(client, ActionCode.UpdateRoom, room.GetRoomData());
  79. return ((int)ReturnCode.Success).ToString();
  80. }
  81. }
  82. }
  83. }