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); } }
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(); }
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); } }
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); } }
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); } }
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); } }
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 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); } }
protected override async ETTask Run(Session session, Logout_C2R message) { //玩家退出登录 await RealmHelper.KickOutPlayer(message.UserId, false); }