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 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 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 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); } }
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); } }