Просмотр исходного кода

DBComponent不应该作为一个单例,因为每个区都可能有自己的数据库,所以应该是一个区一个DBComponent
增加DBManagerComponent用来管理所有的DBComponent

tanghai 4 лет назад
Родитель
Сommit
ffa5299d50

+ 2 - 20
Server/Hotfix/Module/DB/DBComponentSystem.cs

@@ -5,28 +5,12 @@ using MongoDB.Driver;
 
 namespace ET
 {
-	public class DBComponentAwakeSystem : AwakeSystem<DBComponent, string, string>
+	public class DBComponentAwakeSystem : AwakeSystem<DBComponent, string, string, int>
 	{
-		public override void Awake(DBComponent self, string dbConnection, string dbName)
+		public override void Awake(DBComponent self, string dbConnection, string dbName, int zone)
 		{
 			self.mongoClient = new MongoClient(dbConnection);
 			self.database = self.mongoClient.GetDatabase(dbName);
-			
-			self.Transfers.Clear();
-			foreach (Type type in Game.EventSystem.GetTypes().Values)
-			{
-				if (type == typeof (IDBCollection))
-				{
-					continue;
-				}
-				if (!typeof(IDBCollection).IsAssignableFrom(type))
-				{
-					continue;
-				}
-				self.Transfers.Add(type.Name);
-			}
-			
-			DBComponent.Instance = self;
 		}
 	}
 	
@@ -34,8 +18,6 @@ namespace ET
     {
         public override void Destroy(DBComponent self)
         {
-	        DBComponent.Instance = null;
-	        self.Transfers.Clear();
         }
     }
 	

+ 44 - 0
Server/Hotfix/Module/DB/DBManagerComponentSystem.cs

@@ -0,0 +1,44 @@
+using System;
+
+namespace ET
+{
+    public static class DBManagerComponentSystem
+    {
+        [ObjectSystem]
+        public class DBManagerComponentAwakeSystem: AwakeSystem<DBManagerComponent>
+        {
+            public override void Awake(DBManagerComponent self)
+            {
+                DBManagerComponent.Instance = self;
+            }
+        }
+
+        [ObjectSystem]
+        public class DBManagerComponentDestroySystem: DestroySystem<DBManagerComponent>
+        {
+            public override void Destroy(DBManagerComponent self)
+            {
+                DBManagerComponent.Instance = null;
+            }
+        }
+        
+        public static DBComponent GetZoneDB(this DBManagerComponent self, int zone)
+        {
+            DBComponent dbComponent = self.DBComponents[zone];
+            if (dbComponent != null)
+            {
+                return dbComponent;
+            }
+
+            StartZoneConfig startZoneConfig = StartZoneConfigCategory.Instance.Get(zone);
+            if (startZoneConfig.DBConnection == "")
+            {
+                throw new Exception($"zone: {zone} not found mongo connect string");
+            }
+
+            dbComponent = self.AddChild<DBComponent, string, string, int>(startZoneConfig.DBConnection, startZoneConfig.DBName, zone);
+            self.DBComponents[zone] = dbComponent;
+            return dbComponent;
+        }
+    }
+}

+ 22 - 28
Server/Model/Module/DB/DBComponent.cs

@@ -1,31 +1,25 @@
-using System.Collections.Generic;
-using System.Text;
-using MongoDB.Driver;
+using MongoDB.Driver;
 
 namespace ET
 {
-	/// <summary>
-	/// 用来缓存数据
-	/// </summary>
-	public class DBComponent : Entity, IAwake<string, string>, IDestroy
-	{
-		public static DBComponent Instance;
-		
-		public List<string> Transfers = new List<string>();
-		
-		public const int TaskCount = 32;
-		
-		public MongoClient mongoClient;
-		public IMongoDatabase database;
-		
-		public IMongoCollection<T> GetCollection<T>(string collection=null)
-		{
-			return this.database.GetCollection<T>(collection ?? typeof (T).Name);
-		} 
-		
-		public IMongoCollection<Entity> GetCollection(string name)
-		{
-			return this.database.GetCollection<Entity>(name);
-		}
-	}
-}
+    /// <summary>
+    /// 用来缓存数据
+    /// </summary>
+    public class DBComponent: Entity, IAwake<string, string, int>, IDestroy
+    {
+        public const int TaskCount = 32;
+
+        public MongoClient mongoClient;
+        public IMongoDatabase database;
+
+        public IMongoCollection<T> GetCollection<T>(string collection = null)
+        {
+            return this.database.GetCollection<T>(collection ?? typeof (T).Name);
+        }
+
+        public IMongoCollection<Entity> GetCollection(string name)
+        {
+            return this.database.GetCollection<Entity>(name);
+        }
+    }
+}

+ 9 - 0
Server/Model/Module/DB/DBManagerComponent.cs

@@ -0,0 +1,9 @@
+namespace ET
+{
+    public class DBManagerComponent: Entity, IAwake, IDestroy
+    {
+        public static DBManagerComponent Instance;
+        
+        public DBComponent[] DBComponents = new DBComponent[IdGenerater.MaxZone];
+    }
+}