protected override async void Run(Session session, R2G_PlayerKickOut_Req message, Action <G2R_PlayerKickOut_Res> reply)
        {
            G2R_PlayerKickOut_Res response = new G2R_PlayerKickOut_Res();

            try
            {
                ETModel.UserInfo userInfo = Game.Scene.GetComponent <PlayerManagerComponent>().Get(message.UserID);

                RealmHelper.KickNotification(message.UserID);

                await Task.Delay(1000);

                long userSessionId = userInfo.GetComponent <UnitGateComponent>().GateSessionActorId;

                Game.Scene.GetComponent <NetOuterComponent>().Remove(userSessionId);

                Log.Info($"将玩家{message.UserID}连接断开");

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
        protected override async ETTask Run(Session session, Login_C2R request, Login_R2C response, Action reply)
        {
            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();
                //有多种操作数据库的方式 Json字符串模式可以提供多个条件/ID查找模式可以以Entity.Id查找:
                //UserInfo userInfo = await dbProxy.Query<UserInfo>(gamer.UserID, false);
                //先声明一个数据库操作Entity对象AccountInfo

                //验证请求的账号和密码
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>($"{{Account:'{request.Account}',Password:'******'}}");

                if (result.Count != 1)
                {
                    response.Error = ErrorCode.ERR_AccountOrPasswordError;
                    reply();
                    return;
                }

                //已验证通过,可能存在其它地方有登录,要先踢下线
                AccountInfo account = (AccountInfo)result[0];
                await RealmHelper.KickOutPlayer(account.Id);

                int         GateAppId;
                StartConfig config;
                //获取账号所在区服的AppId 索取登陆Key
                if (StartConfigComponent.Instance.GateConfigs.Count == 1)
                { //只有一个Gate服务器时当作AllServer配置处理
                    config = StartConfigComponent.Instance.StartConfig;
                }
                else
                { //有多个Gate服务器时当作分布式配置处理
                    GateAppId = RealmHelper.GetGateAppIdFromUserId(account.Id);
                    config    = StartConfigComponent.Instance.GateConfigs[GateAppId - 1];
                }
                IPEndPoint      innerAddress   = config.GetComponent <InnerConfig>().IPEndPoint;
                Session         gateSession    = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress);
                GetLoginKey_G2R g2RGetLoginKey = (GetLoginKey_G2R)await gateSession.Call(new GetLoginKey_R2G()
                {
                    UserId = account.Id
                });

                // *** 分配网关地址 *** //
                //如果有多台网关服务器,那就应该在realm上添加GateManagerComponent
                //可以管理所有在线的网关服务器,接收网关的负载状态,前端也可以向realm获取网关的负载状态
                //可以根据网关服务器的负载分配网关地址给客户端,也可以随机分配,也可以指定分配
                string outerAddress = config.GetComponent <OuterConfig>().Address2;

                response.GateAddress  = outerAddress;
                response.GateLoginKey = g2RGetLoginKey.GateLoginKey;
                reply();
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #3
0
        protected override async ETTask Run(Session session, G2R_PlayerOnline message, R2G_PlayerOnline response, Action reply)
        {
            //Log.Info("将已在线玩家踢下线");
            await RealmHelper.KickOutPlayer(message.PlayerIdInDB, PlayerOfflineTypes.SamePlayerLogin);

            //Log.Info("玩家上线");
            Game.Scene.GetComponent <OnlineComponent>().Add(message.PlayerIdInDB, message.PlayerIdInPlayerComponent, message.GateAppID);
            //Log.Info($"玩家{message.playerAccount}上线");
            reply();
        }
Exemple #4
0
        protected override async void Run(Session session, C2R_RBLogin message, Action <R2C_RBLogin> reply)
        {
            R2C_RBLogin response = new R2C_RBLogin();

            try
            {
                //数据库操作对象
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                Log.Info($"登录请求:{{Account:'{message.Account}',Password:'******'}}");
                //验证账号密码是否正确
                var result = await dbProxy.Query <DB_Account>((p) => p.Account == message.Account);

                //查无此账号
                if (result.Count == 0)
                {
                    response.Error = ProtocolErrorCode.ERR_LoginError;
                    reply(response);
                    return;
                }

                //密码错误
                DB_Account account = result[0] as DB_Account;
                if (account.Password != message.Password)
                {
                    response.Error = ProtocolErrorCode.ERR_LoginError;
                    reply(response);
                    return;
                }

                Log.Info($"账号登录成功{MongoHelper.ToJson(account)}");

                //将已在线玩家踢下线
                await RealmHelper.KickOutPlayer(account.Id);

                //随机分配网关服务器
                StartConfig gateConfig  = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                Session     gateSession = Game.Scene.GetComponent <NetInnerComponent>().Get(gateConfig.GetComponent <InnerConfig>().IPEndPoint);

                //请求登录Gate服务器密匙
                G2R_RBGetLoginKey getLoginKey = await gateSession.Call(new R2G_RBGetLoginKey()
                {
                    userId = account.Id
                }) as G2R_RBGetLoginKey;

                response.Key     = getLoginKey.Key;
                response.Address = gateConfig.GetComponent <OuterConfig>().IPEndPoint2.ToString();
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #5
0
        protected override async void Run(Session session, A0002_Login_C2R message, Action <A0002_Login_R2C> reply)
        {
            A0002_Login_R2C response = new A0002_Login_R2C();

            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();
                //有多种操作数据库的方式 Json字符串模式可以提供多个条件/ID查找模式可以以Entity.Id查找:
                //UserInfo userInfo = await dbProxy.Query<UserInfo>(gamer.UserID, false);
                //先声明一个数据库操作Entity对象AccountInfo

                //验证假定的账号和密码
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>($"{{Account:'{message.Account}',Password:'******'}}");

                if (result.Count != 1)
                {
                    response.Error = ErrorCode.ERR_AccountOrPasswordError;
                    reply(response);
                    return;
                }

                AccountInfo account = (AccountInfo)result[0];
                await RealmHelper.KickOutPlayer(account.Id);

                int         GateAppId;
                StartConfig config;
                //获取账号所在区服的AppId 索取登陆Key
                if (StartConfigComponent.Instance.GateConfigs.Count == 1)
                { //只有一个Gate服务器时当作AllServer配置处理
                    config = StartConfigComponent.Instance.StartConfig;
                }
                else
                { //有多个Gate服务器时当作分布式配置处理
                    GateAppId = RealmHelper.GetGateAppIdFromUserId(account.Id);
                    config    = StartConfigComponent.Instance.GateConfigs[GateAppId - 1];
                }
                IPEndPoint innerAddress = config.GetComponent <InnerConfig>().IPEndPoint;
                Session    gateSession  = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress);
                string     outerAddress = config.GetComponent <OuterConfig>().Address2;

                A0006_GetLoginKey_G2R g2RGetLoginKey = (A0006_GetLoginKey_G2R)await gateSession.Call(new A0006_GetLoginKey_R2G()
                {
                    UserID = account.Id
                });

                response.GateAddress  = outerAddress;
                response.GateLoginKey = g2RGetLoginKey.GateLoginKey;
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #6
0
        protected override async ETTask Run(Session session, A0002_Login_C2R request, A0002_Login_R2C response, Action reply)
        {
            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();
                //验证提交来的的账号和密码
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>($"{{Account:'{request.Account}',Password:'******'}}");

                if (result.Count != 1)
                {
                    response.Error = ErrorCode.ERR_AccountOrPasswordError;
                    reply();
                    return;
                }
                //已验证通过,可能存在其它地方有登录,先踢下线
                AccountInfo account = (AccountInfo)result[0];
                await RealmHelper.KickOutPlayer(account.Id);

                int         GateAppId;
                StartConfig config;
                //获取账号所在区服的AppId 索取登陆Key
                if (StartConfigComponent.Instance.GateConfigs.Count == 1)
                { //只有一个Gate服务器时当作AllServer配置处理
                    config = StartConfigComponent.Instance.StartConfig;
                }
                else
                { //有多个Gate服务器时当作分布式配置处理
                    //查询账号所在区号
                    GateAppId = RealmHelper.GetGateAppIdFromUserId(account.Id);
                    //对应区号处理
                    config = StartConfigComponent.Instance.GateConfigs[GateAppId - 1];
                }
                IPEndPoint innerAddress = config.GetComponent <InnerConfig>().IPEndPoint;
                Session    gateSession  = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress);
                string     outerAddress = config.GetComponent <OuterConfig>().Address2;

                A0006_GetLoginKey_G2R g2RGetLoginKey = (A0006_GetLoginKey_G2R)await gateSession.Call(new A0006_GetLoginKey_R2G()
                {
                    UserID = account.Id
                });

                response.GateAddress  = outerAddress;
                response.GateLoginKey = g2RGetLoginKey.GateLoginKey;
                reply();
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #7
0
        protected override async ETTask Run(Session session, A0001_Register_C2R request, A0001_Register_R2C response, Action reply)
        {
            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                //验证假定的账号和密码
                if (request.Account.Length <= 0 || request.Password.Length <= 0)
                {
                    Log.Error("账号密码为空 account:" + request.Account + "passwd:" + request.Password);
                    reply();
                    return;
                }
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>($"{{Account:'{request.Account}'}}");

                if (result.Count == 1)
                {
                    response.Error = ErrorCode.ERR_AccountAlreadyRegisted;
                    reply();
                    return;
                }
                else if (result.Count > 1)
                {
                    response.Error = ErrorCode.ERR_RepeatedAccountExist;
                    Log.Error("出现重复账号:" + request.Account);
                    reply();
                    return;
                }
                //生成玩家帐号 这里随机生成区号
                AccountInfo newAccount = ComponentFactory.CreateWithId <AccountInfo>(RealmHelper.GenerateId());
                newAccount.Account  = request.Account;
                newAccount.Password = request.Password;
                await dbProxy.Save(newAccount);

                //生成玩家的用户信息 用户名在消息中提供
                UserInfo newUser = ComponentFactory.CreateWithId <UserInfo, string>(newAccount.Id, request.Account);
                await dbProxy.Save(newUser);

                reply();

                await ETTask.CompletedTask;
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #8
0
        protected override async void Run(Session session, C2R_CowCowLogin message, Action <R2C_CowCowLogin> reply)
        {
            R2C_CowCowLogin response = new R2C_CowCowLogin();

            try
            {
                DBProxyComponent db = Game.Scene.GetComponent <DBProxyComponent>();
                var account         = await db.Query <Accounts>(_account => _account.Account == message.Account && _account.Password == message.Password);

                if (account.Count == 0)
                {
                    response.Error   = ErrorCode.ERR_LoginError;
                    response.Message = "用户名不存在!";
                    reply(response);
                    return;
                }
                Log.WriteLine($"UserName:{message.Account},Password:{message.Password}");

                Accounts accounts = (Accounts)account[0];
                //将已在线玩家踢下线
                await RealmHelper.KickOut(accounts.Id);

                //随机分配网管服务器
                StartConfig gateConfig  = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                Session     gateSession = Game.Scene.GetComponent <NetInnerComponent>().Get(gateConfig.GetComponent <InnerConfig>().IPEndPoint);

                //请求登录gate服务器密匙
                G2R_CowCowGetLoginKey g2R_GetLoginKey = (G2R_CowCowGetLoginKey)await gateSession.Call(new R2G_CowCowGetLoginKey()
                {
                    UserID = accounts.Id
                });

                response.Key     = g2R_GetLoginKey.Key;
                response.Address = gateConfig.GetComponent <OuterConfig>().Address2;
                response.Error   = 0;
                response.Message = "恭喜您登录成功";

                reply(response);
            }
            catch (Exception e)
            {
                response.Error   = ErrorCode.ERR_LoginError;
                response.Message = "登录失败!";
                ReplyError(response, e, reply);
            }
        }
        protected override async void Run(Session session, C2R_Login_Req message, Action <R2C_Login_Ack> reply)
        {
            R2C_Login_Ack response = new R2C_Login_Ack();

            try
            {
                //数据库操作对象
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                Log.Info($"登录请求:{{Account:'{message.Account}',Password:'******'}}");
                //验证账号密码是否正确
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>(_account => _account.Account == message.Account && _account.Password == message.Password);

                if (result.Count == 0)
                {
                    response.Error = ErrorCode.ERR_LoginError;
                    reply(response);
                    return;
                }

                AccountInfo account = result[0] as AccountInfo;
                Log.Info($"账号登录成功{MongoHelper.ToJson(account)}");

                //将已在线玩家踢下线
                await RealmHelper.KickOutPlayer(account.Id);

                //随机分配网关服务器
                StartConfig gateConfig  = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                Session     gateSession = Game.Scene.GetComponent <NetInnerComponent>().Get(gateConfig.GetComponent <InnerConfig>().IPEndPoint);

                //请求登录Gate服务器密匙
                G2R_GetLoginKey_Ack getLoginKey_Ack = await gateSession.Call(new R2G_GetLoginKey_Req()
                {
                    UserID = account.Id
                }) as G2R_GetLoginKey_Ack;

                response.Key     = getLoginKey_Ack.Key;
                response.Address = gateConfig.GetComponent <OuterConfig>().Address2;
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #10
0
        protected override async ETTask Run(Session session, Register_C2R request, Register_R2C response, Action reply)
        {
            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                //验证假定的账号和密码
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>($"{{Account:'{request.Account}'}}");

                if (result.Count >= 1)
                {
                    response.Error = MMOErrorCode.ERR_AccountAlreadyRegisted;
                    reply();
                    return;
                }

                // 前端请求有分区号,生成指定分区账号ID,否则生成随机分区账号ID
                AccountInfo newAccount;
                if (request.Partition > 0)
                {
                    newAccount = ComponentFactory.CreateWithId <AccountInfo>(RealmHelper.GenerateId(request.Partition));
                }
                else
                {
                    newAccount = ComponentFactory.CreateWithId <AccountInfo>(RealmHelper.GenerateId());
                };

                newAccount.Account  = request.Account;
                newAccount.Password = request.Password;
                await dbProxy.Save(newAccount);

                // 生成玩家的用户信息
                UserInfo newUser = ComponentFactory.CreateWithId <UserInfo, string>(newAccount.Id, request.Account);
                await dbProxy.Save(newUser);

                reply();

                await ETTask.CompletedTask;
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #11
0
        protected override async ETTask Run(Session session, A0001_Register_C2R request, A0001_Register_R2C response, Action reply)
        {
            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                //验证假定的账号和密码
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>($"{{Account:'{request.Account}'}}");

                if (result.Count == 1)
                {
                    response.Error = ErrorCode.ERR_AccountAlreadyRegisted;
                    reply();
                    return;
                }
                else if (result.Count > 1)
                {
                    response.Error = ErrorCode.ERR_RepeatedAccountExist;
                    Log.Error("出现重复账号:" + request.Account);
                    reply();
                    return;
                }

                //随机生成区号做为主键和前端输入的账号和密码在AccountInfo里存储一个newAccount
                AccountInfo newAccount = ComponentFactory.CreateWithId <AccountInfo>(RealmHelper.GenerateId());
                newAccount.Account  = request.Account;
                newAccount.Password = request.Password;
                await dbProxy.Save(newAccount);

                //以newAccount.Id(区号)为主键在UserInfo存入Id、Phone等数据
                UserInfo newUser = ComponentFactory.CreateWithId <UserInfo, string>(newAccount.Id, request.Account);
                await dbProxy.Save(newUser);

                reply();

                await ETTask.CompletedTask;
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
        protected override async void Run(Session session, A0001_Register_C2R message, Action <A0001_Register_R2C> reply)
        {
            A0001_Register_R2C response = new A0001_Register_R2C();

            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                //验证假定的账号和密码
                List <ComponentWithId> result = await dbProxy.Query <AccountInfo>($"{{Account:'{message.Account}'}}");

                if (result.Count == 1)
                {
                    response.Error = ErrorCode.ERR_AccountAlreadyRegisted;
                    reply(response);
                    return;
                }
                else if (result.Count > 1)
                {
                    response.Error = ErrorCode.ERR_RepeatedAccountExist;
                    Log.Error("出现重复账号:" + message.Account);
                    reply(response);
                    return;
                }

                //生成玩家帐号 这里随机生成区号
                AccountInfo newAccount = ComponentFactory.CreateWithId <AccountInfo>(RealmHelper.GenerateId());
                newAccount.Account  = message.Account;
                newAccount.Password = message.Password;
                await dbProxy.Save(newAccount);

                //生成玩家的用户信息 用户名在消息中提供
                UserInfo newUser = ComponentFactory.CreateWithId <UserInfo, string>(newAccount.Id, message.Account);
                await dbProxy.Save(newUser);

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #13
0
        protected override async void Run(Session session, G2R_PlayerOnline_Req message, Action <R2G_PlayerOnline_Res> reply)
        {
            R2G_PlayerOnline_Res response = new R2G_PlayerOnline_Res();

            try
            {
                OnlineComponent onlineComponent = Game.Scene.GetComponent <OnlineComponent>();

                await RealmHelper.KickOutPlayer(message.UserID);

                onlineComponent.Add(message.UserID, message.GateAppID);

                Log.Info($"玩家{message.UserID}上线");

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
        protected override async void Run(Session session, G2R_PlayerOnline message, Action <R2G_PlayerOnline> reply)
        {
            R2G_PlayerOnline response = new R2G_PlayerOnline();

            try
            {
                OnlineComponent onlineComponent = Game.Scene.GetComponent <OnlineComponent>();

                //将已在线玩家踢下线
                await RealmHelper.KickOutPlayer(message.UserId);

                //玩家上线
                onlineComponent.Add(message.UserId, message.GateAppId);
                Log.Info($"玩家{message.UserId}上线");

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
 protected override async ETTask Run(Session session, Logout_C2R message)
 {
     //玩家退出登录
     await RealmHelper.KickOutPlayer(message.UserId, false);
 }
        protected override async void Run(Session session, C2R_CowCowRegister message, Action <R2C_CowCowRegister> reply)
        {
            R2C_CowCowRegister response = new R2C_CowCowRegister();

            try
            {
                DBProxyComponent db = Game.Scene.GetComponent <DBProxyComponent>();

                //查询账号是否存在
                var accounts = await db.Query <Accounts>(_account => _account.Account == message.Account);

                if (accounts.Count > 0)
                {
                    response.Error = ErrorCode.ERR_AccountAlreadyRegister;
                    reply(response);
                    return;
                }

                //新建账号,之后如果操作用户信息,通过唯一ID查找并修改或删除等
                Accounts newAccounts = ComponentFactory.CreateWithId <Accounts>(IdGenerater.GenerateId());
                newAccounts.Account   = message.Account;
                newAccounts.Password  = message.Password;
                newAccounts.LoginTime = DateTime.Now;

                Log.WriteLine($"注册新账号:{newAccounts.Account},密码:{message.Password}");

                //新建用户信息
                UserInfo newUser = ComponentFactory.CreateWithId <UserInfo>(newAccounts.Id);
                newUser.NickName     = $"用户{message.Account}";
                newUser.HeadIcon     = message.Account;
                newUser.Sex          = 0;
                newUser.Diamond      = 10000;
                newUser.RegisterTime = DateTime.Now;

                await db.Save(newAccounts);

                await db.Save(newUser);

                await RealmHelper.KickOut(newAccounts.Id);

                //随机分配网管服务器
                StartConfig gateConfig  = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                Session     gateSession = Game.Scene.GetComponent <NetInnerComponent>().Get(gateConfig.GetComponent <InnerConfig>().IPEndPoint);

                //请求登录gate服务器密匙
                G2R_CowCowGetLoginKey g2R_GetLoginKey = (G2R_CowCowGetLoginKey)await gateSession.Call(new R2G_CowCowGetLoginKey()
                {
                    UserID = newAccounts.Id
                });

                response.Key     = g2R_GetLoginKey.Key;
                response.Address = gateConfig.GetComponent <OuterConfig>().Address2;
                response.Error   = 0;
                response.Message = "登录成功";
                reply(response);
            }
            catch (Exception e)
            {
                response.Error   = ErrorCode.ERR_AccountAlreadyRegister;
                response.Message = "注册失败!";
                ReplyError(response, e, reply);
            }
        }
Exemple #17
0
        protected override async ETTask Run(Session session, CreateNewCharacter_C2G request, CharacterMessage_G2C response, Action reply)
        {
            try
            {
                //验证Session
                if (!GateHelper.SignSession(session))
                {
                    response.Error = MMOErrorCode.ERR_UserNotOnline;
                    reply();
                    return;
                }

                // 限制最多可创建多少角色处理
                int max = request.Max;
                if (max == 0)
                {
                    max = 100;
                }
                if (request.Index > max)
                {
                    response.Error = MMOErrorCode.ERR_CannotCreateMoreCharacter;
                    reply();
                    return;
                }

                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                //获取玩家对象
                User user = session.GetComponent <SessionUserComponent>().User;

                //获取玩家所在大区编号
                UserInfo userInfo = await dbProxy.Query <UserInfo>(user.UserId);

                int GateAppId = RealmHelper.GetGateAppIdFromUserId(userInfo.Id);
                userInfo.LastPlay = request.Index;

                //检查角色名是否可用
                //会得到全部大区的同名角色 需遍历排除
                List <ComponentWithId> result = await dbProxy.Query <Character>($"{{Name:'{request.Name}'}}");

                foreach (var a in result)
                {
                    if (RealmHelper.GetGateAppIdFromUserId(((Character)a).UserId) == GateAppId)
                    {
                        //出现同名角色
                        response.Error = MMOErrorCode.ERR_CreateNewCharacter;
                        reply();
                        return;
                    }
                }

                //新建角色数据
                Character character = ComponentFactory.Create <Character, string, long>(request.Name, userInfo.Id);
                character.Race   = request.Race;
                character.Class  = request.Class;
                character.Name   = request.Name;
                character.Level  = 1;
                character.Map    = 1001;
                character.Region = 03;
                character.X      = request.X;
                character.Y      = request.Y;
                character.Z      = request.Z;
                character.Money  = 0;
                character.Mail   = 0;
                character.Index  = request.Index;

                //构建同样的返回数据,减少前端再查询一次此角色数据
                response.Character = GateHelper.CharacterInfoByData(character, false);

                //新角色默认装备
                List <Component> equipInfo = await dbProxy.Query2 <GlobalInfo>($"{{Type:'{request.Class}Equip'}}");

                foreach (GlobalInfo row in equipInfo)
                {
                    character.Equipments          = row.Equipments;
                    response.Character.Equipments = To.RepeatedField <EquipInfo>(row.Equipments);
                }

                //存储数据
                await dbProxy.Save(character);

                await dbProxy.Save(userInfo);

                reply();
                await ETTask.CompletedTask;
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #18
0
        protected override async void Run(Session session, C2R_Login message, Action <R2C_Login> reply)
        {
            R2C_Login response = new R2C_Login();

            try
            {
                if (message.Password != "VisitorPassword")
                {
                    response.Error = ErrorCode.ERR_AccountOrPasswordError;
                    reply(response);
                    return;
                }

                //数据库操作对象
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                Log.Info($"登录请求:{{Account:'{message.Account}',Password:'******'}}");
                //验证账号密码是否正确
                List <AccountInfo> result = await dbProxy.QueryJson <AccountInfo>($"{{Account:'{message.Account}',Password:'******'}}");

                AccountInfo account;
                if (result.Count == 0)
                {
                    //新建账号
                    account          = ComponentFactory.CreateWithId <AccountInfo>(IdGenerater.GenerateId());
                    account.Account  = message.Account;
                    account.Password = message.Password;

                    Log.Info($"注册新账号:{MongoHelper.ToJson(account)}");

                    //新建用户信息
                    UserInfo newUser = ComponentFactory.CreateWithId <UserInfo>(account.Id);
                    newUser.NickName = $"用户{message.Account.Substring(0, 4)}";
                    BaseConfig baseConfig = Game.Scene.GetComponent <ConfigComponent>().Get <BaseConfig>(1);
                    newUser.Gold = baseConfig.Value1;

                    //保存到数据库
                    await dbProxy.Save(account);

                    await dbProxy.Save(newUser, false);
                }
                else
                {
                    account = result[0];
                }
                Log.Info($"账号登录成功{MongoHelper.ToJson(account)}");

                //将已在线玩家踢下线
                await RealmHelper.KickOutPlayer(account.Id);

                // 随机分配一个Gate
                StartConfig config = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                //Log.Debug($"gate address: {MongoHelper.ToJson(config)}");
                IPEndPoint innerAddress = config.GetComponent <InnerConfig>().IPEndPoint;
                Session    gateSession  = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress);

                // 向gate请求一个key,客户端可以拿着这个key连接gate
                G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey()
                {
                    UserId = account.Id
                });

                string outerAddress = config.GetComponent <OuterConfig>().IPEndPoint2.ToString();

                response.Address = outerAddress;
                response.Key     = g2RGetLoginKey.Key;
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #19
0
        protected override async void Run(Session session, A0009_CreateNewCharacter_C2G message, Action <A0009_CreateNewCharacter_G2C> reply)
        {
            A0009_CreateNewCharacter_G2C response = new A0009_CreateNewCharacter_G2C();

            try
            {
                //验证Session
                if (!GateHelper.SignSession(session))
                {
                    response.Error = ErrorCode.ERR_UserNotOnline;
                    reply(response);
                    return;
                }

                //获取玩家对象
                User user = session.GetComponent <SessionUserComponent>().User;

                //获取玩家所在大区编号
                DBProxyComponent dbProxy  = Game.Scene.GetComponent <DBProxyComponent>();
                UserInfo         userInfo = await dbProxy.Query <UserInfo>(user.UserID);

                int GateAppId = RealmHelper.GetGateAppIdFromUserId(userInfo.Id);

                //检查角色名是否可用
                //会得到全部大区的同名角色 需遍历排除
                List <ComponentWithId> result = await dbProxy.Query <Character>($"{{Name:'{message.Name}'}}");

                foreach (var a in result)
                {
                    if (RealmHelper.GetGateAppIdFromUserId(((Character)a).UserID) == GateAppId)
                    {
                        //出现同名角色
                        response.Error = ErrorCode.ERR_CreateNewCharacter;
                        reply(response);
                        return;
                    }
                }

                //检查玩家是否有资格创建新角色
                bool canCreate     = false;
                int  characterSeat = message.Seat; //玩家请求创建的角色位置
                switch (characterSeat)
                {
                case 1:
                    if (userInfo.CharacterID1 == 0)
                    {
                        canCreate = true;
                    }
                    break;

                case 2:
                    if (userInfo.CharacterID2 == 0)
                    {
                        canCreate = true;
                    }
                    break;

                case 3:
                    if (userInfo.CharacterID3 == 0)
                    {
                        canCreate = true;
                    }
                    break;

                default:
                    break;
                }

                //判定为无法创建角色时返回错误消息
                if (!canCreate)
                {
                    //理应不该出现这个错误
                    //当玩家位置满时 点击创建角色按钮应有提示 无法进入创建角色界面
                    Log.Error("玩家当前位置已有角色");
                    response.Error = ErrorCode.ERR_CreateNewCharacter;
                    reply(response);
                    return;
                }

                //新建角色数据 角色可以通过UserID来识别区号 可以不使用CreateWithId方法
                Character character = ComponentFactory.CreateWithId <Character, long>(RealmHelper.GenerateId(), userInfo.Id);
                character.Name     = message.Name;
                character.Level    = 1;
                character.Career   = message.Career;
                character.Pet      = PetType.NonePet;
                character.Skeleton = message.Skeleton;
                switch (character.Career) //初始装备是绑定职业的
                {
                case CareerType.Warror:
                    character.Weapon = WeaponType.Sword;
                    character.Head   = HeadType.Head1;
                    character.Chest  = ChestType.Chest1;
                    character.Hand   = HandType.Hand1;
                    character.Feet   = FeetType.Feet1;
                    break;

                case CareerType.Mage:
                    character.Weapon = WeaponType.Wand;
                    character.Head   = HeadType.Head2;
                    character.Chest  = ChestType.Chest2;
                    character.Hand   = HandType.Hand2;
                    character.Feet   = FeetType.Feet2;
                    break;
                }
                character.Region = RegionType.Village; //初始地图为村庄
                character.X      = 1;                  //设置初始坐标
                character.Y      = 2;
                character.Z      = 3;
                character.Money  = 10;
                character.Mail   = 0;

                //存储数据
                switch (characterSeat)
                {
                case 1:
                    userInfo.CharacterID1 = character.Id;
                    userInfo.LastPlay     = 1;
                    break;

                case 2:
                    userInfo.CharacterID2 = character.Id;
                    userInfo.LastPlay     = 2;
                    break;

                case 3:
                    userInfo.CharacterID3 = character.Id;
                    userInfo.LastPlay     = 3;
                    break;

                default:
                    throw new Exception($"创建新角色错误:{userInfo.Id}");
                }

                await dbProxy.Save(character);

                await dbProxy.Save(userInfo);

                Log.Debug($"新增一个角色{character.Id}");
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }