Эх сурвалжийг харах

【增加】房间号生成逻辑,创建房间协议调整

johnclot69 1 жил өмнө
parent
commit
bb956c4714

+ 8 - 0
Config/Proto/CommonProto_CS_10001.proto

@@ -27,4 +27,12 @@ message PlayerInfo
   int64 exp = 4;
   int32 level = 5;
   int32 vip = 6;
+}
+
+message RoomInfo
+{
+	string RoomId = 1;	// 房间号
+	int32 Type = 2;		// 房间玩法类型 1:麻将 2:斗地主
+	int64 OwnerId = 3;	// 房主playerId
+	repeated PlayerInfo PlayerList = 4;		// 房间玩家列表
 }

+ 1 - 2
Config/Proto/OuterMessage_C_30001.proto

@@ -166,8 +166,7 @@ message G2C_CreatRoom // IResponse
 	int32 RpcId = 1;
 	int32 Error = 2;
 	string Message = 3;
-	int64 OwnerId = 4;		// 房主id
-	repeated PlayerInfo PlayerList = 5;		// 房间玩家列表
+	RoomInfo Info = 4;	// 房间信息
 }
 
 message G2C_TestHotfixMessage // IMessage

+ 0 - 27
DotNet/Hotfix/Helper/PlayerHelper.cs

@@ -1,27 +0,0 @@
-namespace ET.Server
-{
-    /// <summary>
-    /// 玩家工具类
-    /// </summary>
-    public static class PlayerHelper
-    {
-        /// <summary>
-        /// 玩家信息转proto
-        /// </summary>
-        /// <param name="player"></param>
-        /// <returns></returns>
-        public static PlayerInfo PlayerToProto(Player player)
-        {
-            return new PlayerInfo()
-            {
-                id = player.Id,
-                name = player.Name,
-                sex = player.Sex,
-                exp = player.Exp,
-                level = player.Level,
-                vip = 0
-            };
-        }
-
-    }
-}

+ 49 - 0
DotNet/Hotfix/Helper/ProtoHelper.cs

@@ -0,0 +1,49 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace ET.Server
+{
+    /// <summary>
+    /// Proto工具类
+    /// </summary>
+    public static class ProtoHelper
+    {
+        /// <summary>
+        /// 玩家信息转proto
+        /// </summary>
+        /// <param name="player"></param>
+        /// <returns></returns>
+        public static PlayerInfo PlayerToProto(Player player)
+        {
+            return new PlayerInfo()
+            {
+                id = player.Id,
+                name = player.Name,
+                sex = player.Sex,
+                exp = player.Exp,
+                level = player.Level,
+                vip = 0
+            };
+        }
+
+        /// <summary>
+        /// 房间信息转proto
+        /// </summary>
+        /// <param name="room"></param>
+        /// <returns></returns>
+        public static RoomInfo RoomToProto(Room room)
+        {
+            RoomInfo info = new RoomInfo();
+            info.RoomId = room.RoomId;
+            info.Type = room.Type;
+            info.OwnerId = room.OwnerId;
+            info.PlayerList = new List<PlayerInfo>();
+            foreach (Player p in room.Players.Where(p => p != null))
+            {
+                info.PlayerList.Add(ProtoHelper.PlayerToProto(p));
+            }
+            return info;
+        }
+
+    }
+}

+ 5 - 5
DotNet/Hotfix/Scenes/Game/GameRoomComponentSystem.cs

@@ -7,18 +7,18 @@ public static class GameRoomComponentSystem
 {
     public static void Add(this GameRoomComponent self, Room room)
     {
-        self.idRooms.Add(room.Id, room);
+        self.idRooms.Add(room.RoomId, room);
     }
 
-    public static Room Get(this GameRoomComponent self, long id)
+    public static Room Get(this GameRoomComponent self, string roomId)
     {
-        self.idRooms.TryGetValue(id, out Room room);
+        self.idRooms.TryGetValue(roomId, out Room room);
         return room;
     }
 
-    public static void Remove(this GameRoomComponent self, long id)
+    public static void Remove(this GameRoomComponent self, string roomId)
     {
-        self.idRooms.Remove(id);
+        self.idRooms.Remove(roomId);
     }
 
     public static Room[] GetAll(this GameRoomComponent self)

+ 1 - 6
DotNet/Hotfix/Scenes/Game/Handler/C2G_CreatRoomHandler.cs

@@ -44,12 +44,7 @@ namespace ET.Server
             }
             
             // 返回房间数据
-            response.OwnerId = room.OwnerId;
-            response.PlayerList = new List<PlayerInfo>();
-            foreach (Player p in room.Players.Where(p => p != null))
-            {
-                response.PlayerList.Add(PlayerHelper.PlayerToProto(p));
-            }
+            response.Info = ProtoHelper.RoomToProto(room);
         
             await ETTask.CompletedTask;
         }

+ 1 - 1
DotNet/Hotfix/Scenes/Game/Handler/C2G_LoginGameHandler.cs

@@ -37,7 +37,7 @@ namespace ET.Server
 			session.AddComponent<SessionPlayerComponent>().PlayerId = player.Id;
 			session.AddComponent<MailBoxComponent, MailboxType>(MailboxType.GameSession);
 
-			response.Player = PlayerHelper.PlayerToProto(player);
+			response.Player = ProtoHelper.PlayerToProto(player);
 			response.IsInRoom = player.RoomID > 0;
 			reply();
 			await ETTask.CompletedTask;

+ 2 - 1
DotNet/Hotfix/Scenes/Game/Room/RoomSystem.cs

@@ -11,6 +11,7 @@ public static class RoomSystem
         protected override void Awake(Room self, Player owner)
         {
             Log.Info($"创建房间实体...");
+            self.RoomId = RandomGenerator.RandRoomId();
             self.Type = 2;
             self.OwnerId = owner.Id;
             self.Players = new List<Player>();
@@ -26,7 +27,7 @@ public static class RoomSystem
             {
                 Log.Info($"销毁房间实体...");
                 // 移除本地房间数据
-                self.DomainScene().GetComponent<GameRoomComponent>()?.Remove(self.Id);
+                self.DomainScene().GetComponent<GameRoomComponent>()?.Remove(self.RoomId);
             }
         }
     }

+ 19 - 0
DotNet/Model/Generate/Message/CommonProto_CS_10001.cs

@@ -69,10 +69,29 @@ namespace ET
 
 	}
 
+	[Message(CommonProto.RoomInfo)]
+	[ProtoContract]
+	public partial class RoomInfo: ProtoObject
+	{
+		[ProtoMember(1)]
+		public string RoomId { get; set; }
+
+		[ProtoMember(2)]
+		public int Type { get; set; }
+
+		[ProtoMember(3)]
+		public long OwnerId { get; set; }
+
+		[ProtoMember(4)]
+		public List<PlayerInfo> PlayerList { get; set; }
+
+	}
+
 	public static class CommonProto
 	{
 		 public const ushort MoveInfo = 10002;
 		 public const ushort UnitInfo = 10003;
 		 public const ushort PlayerInfo = 10004;
+		 public const ushort RoomInfo = 10005;
 	}
 }

+ 1 - 4
DotNet/Model/Generate/Message/OuterMessage_C_30001.cs

@@ -338,10 +338,7 @@ namespace ET
 		public string Message { get; set; }
 
 		[ProtoMember(4)]
-		public long OwnerId { get; set; }
-
-		[ProtoMember(5)]
-		public List<PlayerInfo> PlayerList { get; set; }
+		public RoomInfo Info { get; set; }
 
 	}
 

+ 2 - 2
DotNet/Model/Scenes/Game/GameRoomComponent.cs

@@ -8,6 +8,6 @@ namespace ET.Server;
 [ComponentOf(typeof(Scene))]
 public class GameRoomComponent : Entity, IAwake, IDestroy
 {
-    /** 房间集合 **/
-    public readonly Dictionary<long, Room> idRooms = new Dictionary<long, Room>();
+    /** 房间集合 <key:房间号, value:房间实例> **/
+    public readonly Dictionary<string, Room> idRooms = new Dictionary<string, Room>();
 }

+ 4 - 1
DotNet/Model/Scenes/Game/Room/Room.cs

@@ -8,10 +8,13 @@ namespace ET.Server;
 [ChildOf(typeof(GameRoomComponent))]
 public class Room : Entity, IAwake<Player>, IDestroy
 {
+    /** 房间号 **/
+    public string RoomId { get; set; }
+
     /** 房间玩法类型 1:麻将 2:斗地主 **/
     public int Type { get; set; }
     
-    /** 房主id **/
+    /** 房主playerId **/
     public long OwnerId { get; set; }
 
     /** 房间玩家集合 **/

+ 19 - 0
Unity/Assets/Scripts/Codes/Model/Generate/Client/Message/CommonProto_CS_10001.cs

@@ -69,10 +69,29 @@ namespace ET
 
 	}
 
+	[Message(CommonProto.RoomInfo)]
+	[ProtoContract]
+	public partial class RoomInfo: ProtoObject
+	{
+		[ProtoMember(1)]
+		public string RoomId { get; set; }
+
+		[ProtoMember(2)]
+		public int Type { get; set; }
+
+		[ProtoMember(3)]
+		public long OwnerId { get; set; }
+
+		[ProtoMember(4)]
+		public List<PlayerInfo> PlayerList { get; set; }
+
+	}
+
 	public static class CommonProto
 	{
 		 public const ushort MoveInfo = 10002;
 		 public const ushort UnitInfo = 10003;
 		 public const ushort PlayerInfo = 10004;
+		 public const ushort RoomInfo = 10005;
 	}
 }

+ 1 - 4
Unity/Assets/Scripts/Codes/Model/Generate/Client/Message/OuterMessage_C_30001.cs

@@ -338,10 +338,7 @@ namespace ET
 		public string Message { get; set; }
 
 		[ProtoMember(4)]
-		public long OwnerId { get; set; }
-
-		[ProtoMember(5)]
-		public List<PlayerInfo> PlayerList { get; set; }
+		public RoomInfo Info { get; set; }
 
 	}
 

+ 19 - 0
Unity/Assets/Scripts/Codes/Model/Generate/ClientServer/Message/CommonProto_CS_10001.cs

@@ -69,10 +69,29 @@ namespace ET
 
 	}
 
+	[Message(CommonProto.RoomInfo)]
+	[ProtoContract]
+	public partial class RoomInfo: ProtoObject
+	{
+		[ProtoMember(1)]
+		public string RoomId { get; set; }
+
+		[ProtoMember(2)]
+		public int Type { get; set; }
+
+		[ProtoMember(3)]
+		public long OwnerId { get; set; }
+
+		[ProtoMember(4)]
+		public List<PlayerInfo> PlayerList { get; set; }
+
+	}
+
 	public static class CommonProto
 	{
 		 public const ushort MoveInfo = 10002;
 		 public const ushort UnitInfo = 10003;
 		 public const ushort PlayerInfo = 10004;
+		 public const ushort RoomInfo = 10005;
 	}
 }

+ 1 - 4
Unity/Assets/Scripts/Codes/Model/Generate/ClientServer/Message/OuterMessage_C_30001.cs

@@ -338,10 +338,7 @@ namespace ET
 		public string Message { get; set; }
 
 		[ProtoMember(4)]
-		public long OwnerId { get; set; }
-
-		[ProtoMember(5)]
-		public List<PlayerInfo> PlayerList { get; set; }
+		public RoomInfo Info { get; set; }
 
 	}
 

+ 16 - 0
Unity/Assets/Scripts/Core/Helper/RandomGenerator.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using Random = System.Random;
+using System.Security.Cryptography;
 
 namespace ET
 {
@@ -92,5 +93,20 @@ namespace ET
             int a = RandomNumber(0, 1000000);
             return a / 1000000f;
         }
+
+        /// <summary>
+        /// 随机一个6位数房间id
+        /// </summary>
+        /// <returns></returns>
+        public static string RandRoomId()
+        {
+            byte[] randomNumber = new byte[4];
+            using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
+            {
+                rng.GetBytes(randomNumber);
+                uint value = BitConverter.ToUInt32(randomNumber, 0) % 900000 + 100000; // 确保是六位数
+                return value.ToString();
+            }
+        }
     }
 }