Пример #1
0
        protected override async ETTask Run(Session session, C2G_DLoginGate request, G2C_DLoginGate response, Action reply)
        {
            Scene  scene   = session.DomainScene();
            string account = scene.GetComponent <DGateSessionKeyComponent>().Get(request.Key);

            if (account == null)
            {
                response.Error   = ErrorCode.ERR_ConnectGateKeyError;
                response.Message = "Gate key验证失败!";
                reply();

                return;
            }

            DBComponent db     = session.Domain.GetComponent <DBComponent>();
            DPlayer     player = await PlayerDBHelper.GetPlayerFromDB(db, account);

            scene.GetComponent <DPlayerComponent>().Add(player);

            session.AddComponent <DSessionPlayerComponent>().Player = player;
            session.AddComponent <MailBoxComponent, MailboxType>(MailboxType.GateSession);

            response.PlayerId = player.Id;
            reply();
        }
Пример #2
0
        public static async ETTask <long> Remove <T>(this DBComponent self, long taskId, long id, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
            {
                DeleteResult result = await self.GetCollection <T>(collection).DeleteOneAsync(d => d.Id == id);

                return(result.DeletedCount);
            }
        }
Пример #3
0
        public static async ETTask <long> Remove <T>(this DBComponent self, Expression <Func <T, bool> > filter, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
            {
                DeleteResult result = await self.GetCollection <T>(collection).DeleteManyAsync(filter);

                return(result.DeletedCount);
            }
        }
Пример #4
0
        public static async ETTask <T> Query <T>(this DBComponent self, long id, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
            {
                IAsyncCursor <T> cursor = await self.GetCollection <T>(collection).FindAsync(d => d.Id == id);

                return(await cursor.FirstOrDefaultAsync());
            }
        }
Пример #5
0
        public static async ETTask <T> QueryOne <T>(this DBComponent self, Expression <Func <T, bool> > filter, string collection = null)
            where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
            {
                IAsyncCursor <T> cursor = await self.GetCollection <T>(collection).FindAsync(filter);

                return(await cursor.FirstOrDefaultAsync());
            }
        }
Пример #6
0
        public static async ETTask <List <T> > Query <T>(this DBComponent self, long taskId, Expression <Func <T, bool> > filter, string collection = null)
            where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
            {
                IAsyncCursor <T> cursor = await self.GetCollection <T>(collection).FindAsync(filter);

                return(await cursor.ToListAsync());
            }
        }
Пример #7
0
        public static async ETTask <List <T> > QueryJson <T>(this DBComponent self, long taskId, string json, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
            {
                FilterDefinition <T> filterDefinition = new JsonFilterDefinition <T>(json);
                IAsyncCursor <T>     cursor           = await self.GetCollection <T>(collection).FindAsync(filterDefinition);

                return(await cursor.ToListAsync());
            }
        }
Пример #8
0
        public static async ETTask SaveNotWait <T>(this DBComponent self, T entity, long taskId = 0, string collection = null) where T : Entity
        {
            if (taskId == 0)
            {
                await self.Save(entity, collection);

                return;
            }

            await self.Save(taskId, entity, collection);
        }
Пример #9
0
        public static async ETTask InsertBatch <T>(this DBComponent self, IEnumerable <T> list, string collection = null) where T : Entity
        {
            if (collection == null)
            {
                collection = typeof(T).Name;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
            {
                await self.GetCollection(collection).InsertManyAsync(list);
            }
        }
Пример #10
0
        protected override async ETTask Run(Scene scene, G2M_CreateGamer request, M2G_CreateGamer response, Action reply)
        {
            DBComponent db    = scene.GetComponent <DBComponent>();
            Gamer       gamer = await GamerDBHelper.GetGamerFromDB(scene.Domain, db, request.GamerId);

            gamer.AddComponent <MailBoxComponent>();
            await gamer.AddLocation();

            gamer.AddComponent <GamerGateComponent, long>(request.GateSessionId);
            scene.GetComponent <GamerComponent>().Add(gamer);

            response.SelfGamer = GamerHelper.CreateGamerInfo(gamer);
            reply();
        }
Пример #11
0
        protected override async ETTask Run(Scene scene, R2G_CreateAccount request, G2R_CreateAccount response, Action reply)
        {
            // 1 首次创建Gamer对象
            Gamer gamer = GamerHelper.InitGamer(scene.Domain, request.Account);

            // 2 存出Gamer对象到DB
            DBComponent db = scene.GetComponent <DBComponent>();
            await GamerDBHelper.AddGamerToDB(db, gamer);

            // 3 创建Player对象
            DPlayer player = PlayerHelper.InitPlayer(request.Account, request.Password, gamer.Id);
            await PlayerDBHelper.AddPlayerToDB(db, player);

            reply();
        }
Пример #12
0
        public static async ETTask <DPlayer> GetPlayerFromDB(DBComponent db, string account)
        {
            DPlayer        player;
            List <DPlayer> playerList = await db.Query <DPlayer>(DPlayer => DPlayer.Account == account);

            if (playerList == null || playerList.Count == 0)
            {
                player = null;
            }
            else
            {
                player = playerList[0];
            }

            return(player);
        }
Пример #13
0
        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);
        }
Пример #14
0
        public static async ETTask Save <T>(this DBComponent self, long taskId, T entity, string collection = null) where T : Entity
        {
            if (entity == null)
            {
                Log.Error($"save entity is null: {typeof (T).Name}");

                return;
            }

            if (collection == null)
            {
                collection = entity.GetType().Name;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
            {
                await self.GetCollection(collection).ReplaceOneAsync(d => d.Id == entity.Id, entity, new ReplaceOptions {
                    IsUpsert = true
                });
            }
        }
Пример #15
0
        protected override async ETTask Run(Session session, C2R_DLogin request, R2C_DLogin response, Action reply)
        {
            // 随机分配一个Gate
            StartSceneConfig config = DRealmGateAddressHelper.GetGate(session.DomainZone());

            // 账号验证
            DBComponent db     = session.Domain.GetComponent <DBComponent>();
            DPlayer     player = await PlayerDBHelper.GetPlayerFromDB(db, request.Account);

            if (player == null)
            {
                // DB无Player对象,创建对象
                await ActorMessageSenderComponent.Instance.Call(
                    config.SceneId, new R2G_CreateAccount()
                {
                    Account = request.Account, Password = request.Password
                });
            }
            else if (!player.PassWord.Equals(request.Password))
            {
                response.Error   = ErrorCode.ERR_ConnectPasswordError;
                response.Message = "Password Error!";

                reply();
                return;
            }

            // 向gate请求一个key,客户端可以拿着这个key连接gate
            G2R_DGetLoginKey g2RGetLoginKey = (G2R_DGetLoginKey)await ActorMessageSenderComponent.Instance.Call(
                config.SceneId, new R2G_DGetLoginKey()
            {
                Account = request.Account
            });

            response.Address = config.OuterIPPort.ToString();
            response.Key     = g2RGetLoginKey.Key;
            response.GateId  = g2RGetLoginKey.GateId;

            reply();
        }
Пример #16
0
        public static async ETTask Query(this DBComponent self, long id, List <string> collectionNames, List <Entity> result)
        {
            if (collectionNames == null || collectionNames.Count == 0)
            {
                return;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
            {
                foreach (string collectionName in collectionNames)
                {
                    IAsyncCursor <Entity> cursor = await self.GetCollection(collectionName).FindAsync(d => d.Id == id);

                    Entity e = await cursor.FirstOrDefaultAsync();

                    if (e == null)
                    {
                        continue;
                    }

                    result.Add(e);
                }
            }
        }
Пример #17
0
        public static async ETTask Save(this DBComponent self, long id, List <Entity> entities)
        {
            if (entities == null)
            {
                Log.Error($"save entity is null");
                return;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
            {
                foreach (Entity entity in entities)
                {
                    if (entity == null)
                    {
                        continue;
                    }

                    await self.GetCollection(entity.GetType().Name)
                    .ReplaceOneAsync(d => d.Id == entity.Id, entity, new ReplaceOptions {
                        IsUpsert = true
                    });
                }
            }
        }
Пример #18
0
 private static IMongoCollection <T> GetCollection <T>(this DBComponent self, string collection = null)
 {
     return(self.database.GetCollection <T>(collection ?? typeof(T).Name));
 }
Пример #19
0
        public static async ETTask AddPlayerToDB(DBComponent db, DPlayer player)
        {
            await db.Save(player);

            Log.Info($"ÐÂÔöÕ˺ţº{player.Account}£¬ÃÜÂ룺{player.PassWord}¡£");
        }
Пример #20
0
 private static IMongoCollection <Entity> GetCollection(this DBComponent self, string name)
 {
     return(self.database.GetCollection <Entity>(name));
 }