public static async ETVoid EnterMapAsync() { try { // 加载Unit资源 ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent <ResourcesComponent>(); await resourcesComponent.LoadBundleAsync($"player.unity3d"); // 加载场景资源 await ETModel.Game.Scene.GetComponent <ResourcesComponent>().LoadBundleAsync("map.unity3d"); // 切换到map场景 using (SceneChangeComponent sceneChangeComponent = ETModel.Game.Scene.AddComponent <SceneChangeComponent>()) { await sceneChangeComponent.ChangeSceneAsync(SceneType.Map); } Game.Scene.AddComponent <OperaComponent>(); Game.Scene.AddComponent <PlatformComponent>(); Player player = PlayerFactory.Create(IdGenerater.GenerateId()); PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; Game.EventSystem.Run(EventIdType.EnterMapFinish); } catch (Exception e) { Log.Error(e); } }
public static void Awake(this PlayerComponent self) { var proxy = Game.Scene.GetComponent <CacheProxyComponent>(); self.MemorySync = proxy.GetMemorySyncSolver <Player>(); self.MemorySync.onRefresh += self.OnRefresh; //self.Test(); }
public async void OnLogin() { try { IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address); string text = this.account.GetComponent <InputField>().text; // 创建一个ETModel层的Session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = ComponentFactory.Create <Session, ETModel.Session>(session); R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = text, Password = "******" }); realmSession.Dispose(); connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); UserInfo userInfor = (UserInfo)await SessionComponent.Instance.Session.Call(new UserInfor() { }); //加载心跳组件 Game.Scene.GetComponent <SessionComponent>().Session.AddComponent <HeartBeatComponent>(); Log.Info("登陆gate成功!"); Log.Info("请求信息gate成功!" + " COIN:" + userInfor.Coin + " EXP:" + userInfor.Exp); // 创建Player Player player = ETModel.ComponentFactory.CreateWithId <Player>(g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; playerComponent.Add(player); //Log.Info("Login Player.Unitid"+ player.UnitId.ToString()); //Log.Info("Login Player.id" + player.Id.ToString()); Game.Scene.GetComponent <UIComponent>().Create(UIType.EvoUILobby); Game.Scene.GetComponent <UIComponent>().Remove(UIType.UILogin); } catch (Exception e) { Log.Error(e); } }
public static void Destroy(this PlayerComponent self) { // 預防遞歸卡死 if (self.IsDisposed) { return; } // 非擁有者請勿操作Dispose self.MemorySync.Dispose(); self.Dispose(); }
public async void OnLogin() { try { IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address); string text = this.account.GetComponent <InputField>().text; // 创建一个ETModel层的Session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = ComponentFactory.Create <Session, ETModel.Session>(session); R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = text, Password = "******" }); realmSession.Dispose(); connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.ComponentFactory.CreateWithId <Player>(g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; this.tokenSource.Cancel(); Game.Scene.GetComponent <UIComponent>().Create(UIType.UILobby); Game.Scene.GetComponent <UIComponent>().Remove(UIType.UILogin); // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); } catch (Exception e) { Log.Error(e); } }
//不懂TODO public static async ETVoid OnLoginAsync(string username, string password) { try { // 创建一个ETModel层的Session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = ComponentFactory.Create <Session, ETModel.Session>(session); //给realm服务器发送账号密码进行登录 返回过来一个gate服务器地址和一个key R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = username, Password = password }); realmSession.Dispose(); Log.Debug("r2CLogin.Address:" + r2CLogin.Address); #region 热更层和Model层分别都连接gate服务器地址 SessionComponent.Instance.Session.Call可以分别给的两个层的发消息 // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession); #endregion G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.ComponentFactory.CreateWithId <Player>(g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; //消息机制调用 Game.EventSystem.Run(EventIdType.UILoginPanelFinish); // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); } catch (Exception e) { Log.Error(e); } }
public static async ETVoid OnLoginAsync(Entity domain, string account) { try { // 创建一个ETModel层的Session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = EntityFactory.Create <Session, ETModel.Session>(domain, session); if (account == "") { account = IdGenerater.GenerateId().ToString(); } R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = account, Password = "******" }); realmSession.Dispose(); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = EntityFactory.Create <Session, ETModel.Session>(Game.Scene, gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.EntityFactory.CreateWithId <Player>(ETModel.Game.Scene, g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; Game.EventSystem.Run(EventIdType.LoginFinish); // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); } catch (Exception e) { Log.Error(e); } }
protected override async ETTask Run(Player entity, Actor_PlayerNetSyncToCline message) { G2C_OtherPlayerPosition packge = new G2C_OtherPlayerPosition(); packge.DirAccount.Add(message.DirAccount); packge.PositionX.Add(message.PositionX); packge.PositionY.Add(message.PositionY); packge.PositionZ.Add(message.PositionZ); packge.RotationX.Add(message.RotationX); packge.RotationY.Add(message.RotationY); packge.RotationZ.Add(message.RotationZ); packge.RotationW.Add(message.RotationW); packge.VelocityX.Add(message.VelocityX); packge.VelocityY.Add(message.VelocityY); packge.VelocityZ.Add(message.VelocityZ); //Log.Info("时间: " + TimeHelper.ClientNow()); packge.ServerTime = TimeHelper.ClientNow(); packge.Fire.Add(message.Fire); packge.Bullets.Add(message.Bullets); //Log.Error("场景子弹数量: " + message.Bullets.Count); try { entity.session.Send(packge); } catch (Exception e) { //说明有人掉线了 if (playerComponent == null) { playerComponent = Game.Scene.GetComponent <PlayerComponent>(); } //给其它玩家广播这个玩家掉线的信息 playerComponent.removeOnePlayerLink(entity.Account).Coroutine(); Log.Error("一名玩家离线了: " + entity.Account); } await ETTask.CompletedTask; }
public static async ETVoid OnLoginAsync(string account) { try { //创建一个会话实体session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address); //热更层也创建一个Seesion,将Model层的session传递过去 //热更层的Seesion创建后,会调用Awake方法,在内部关联了Model层的session //以后调用热更层的Seesion 就是调用间接的调用了主工程的 Seesion Session realmSession = ComponentFactory.Create <Session, ETModel.Session>(session); //await等待服务器响应 r2CLogin这个是响应后 解包->反序列化得到的对象 里面已经包含服务器发送过来的数据 R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = account, Password = "******" }); realmSession.Dispose(); //服务器返回了网关地址 //那么就根据网关地址创建一个新的Session 连接到网关去 ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); //在Scene实体中添加SessionComponent组件 并且缓存Session对象 以后就可以直接获取来发送消息 ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; //这里跟上面逻辑一样,创建热更层的Session,关联到主工程 Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.ComponentFactory.CreateWithId <Player>(g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; Game.EventSystem.Run(EventIdType.LoginFinish); // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); } catch (Exception e) { Log.Error(e); } }
//不懂TODO public static async ETVoid OnLoginAsync(string account) { try { // 创建一个ETModel层的Session 建立服务器连接 ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = ComponentFactory.Create <Session, ETModel.Session>(session); R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = account, Password = "******" }); realmSession.Dispose(); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 Log.Debug("r2CLogin.Address:" + r2CLogin.Address); ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.ComponentFactory.CreateWithId <Player>(g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; //消息机制调用 Game.EventSystem.Run(EventIdType.LoginFinish); // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); } catch (Exception e) { Log.Error(e); } }
public static Player Create(long id) { ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent <ResourcesComponent>(); GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset("Player.unity3d", "Player"); GameObject prefab = bundleGameObject.Get <GameObject>("Cube"); GameObject go = UnityEngine.Object.Instantiate(prefab); Player player = ComponentFactory.CreateWithId <Player>(id, go); PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>(); player.AddComponent <MoveComponent>(); player.AddComponent <TurnComponent>(); playerComponent.Add(player); return(player); }
/// <summary> /// 先向 BD 服务器 初始化小怪数据 /// </summary> public static async ETTask <List <Player> > GetPlayerByIds(this PlayerComponent self, List <long> ids) { DBProxyComponent dBProxy = Game.Scene.GetComponent <DBProxyComponent>(); List <ComponentWithId> playerdbs = await dBProxy.Query <Playerdb>(ids); foreach (Playerdb tem in playerdbs) { Player player = tem.ToPlayer(); /// 然后向 DB 服务器 小怪数据放入字典 self.Add(player); } Console.WriteLine(" PlayerComponentHelper-28: " + " BD服务器,Player数量: " + Game.Scene.GetComponent <PlayerComponent>().Count); return(self.GetAll().ToList()); }
public async void OnLogin() { SessionWrap sessionWrap = null; try { IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address); string text = this.account.GetComponent <InputField>().text; Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); sessionWrap = new SessionWrap(session); R2C_Login r2CLogin = (R2C_Login)await sessionWrap.Call(new C2R_Login() { Account = text, Password = "******" }); sessionWrap.Dispose(); connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address); Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); Game.Scene.AddComponent <SessionWrapComponent>().Session = new SessionWrap(gateSession); ETModel.Game.Scene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionWrapComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.ComponentFactory.CreateWithId <Player>(g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; Game.Scene.GetComponent <UIComponent>().Create(UIType.UILobby); Game.Scene.GetComponent <UIComponent>().Remove(UIType.UILogin); } catch (Exception e) { sessionWrap?.Dispose(); Log.Error(e); } }
public static async ETVoid Login(string account) { // 创建一个ETModel层的Session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = EntityFactory.Create <Session, ETModel.Session>(Game.Scene, session); R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = account, Password = "******" }); realmSession.Dispose(); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = EntityFactory.Create <Session, ETModel.Session>(Game.Scene, gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.EntityFactory.CreateWithId <Player>(ETModel.Game.Scene, g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); // 逻辑层不应该去调用UI,逻辑层只关心逻辑并且抛出事件,由UI层自己去订阅事件,而且注意事件名字 // 很多人容易把这个事件取名成LoginFinishiCreateLobbyUI,这是不对的,事件抛出去不可能知道谁订阅了这个事件, // 也不会知道别人订阅这个事件是干什么的,这里只知道我Login Finish Game.EventSystem.Run(EventIdType.LoginFinish); }
public override void Dispose() { if (this.IsDisposed) { return; } base.Dispose(); //移除连接组件 ETModel.Game.Scene.RemoveComponent <SessionComponent>(); Game.Scene.RemoveComponent <SessionWrapComponent>(); //释放本地玩家对象 PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); if (playerComponent.MyPlayer != null) { playerComponent.MyPlayer.Dispose(); playerComponent.MyPlayer = null; } UIComponent uiComponent = Game.Scene.GetComponent <UIComponent>(); //游戏关闭,不用回到登录界面 if (uiComponent == null || uiComponent.IsDisposed) { return; } //UI uiLogin = uiComponent.Create(UIType.UILogin); //uiLogin.GetComponent<UILoginComponent>().SetPrompt("连接断开"); if (uiComponent.Get(UIType.UILobby) != null) { uiComponent.Remove(UIType.UILobby); } else if (uiComponent.Get(UIType.UIRoom) != null) { uiComponent.Remove(UIType.UIRoom); } }
protected override async ETTask Run(Session session, C2G_GetOtherPlayer message) { //获取内网发送组件 IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent <InnerConfig>().IPEndPoint; Session mapSession = Game.Scene.GetComponent <NetInnerComponent>().Get(mapAddress); M2G_GetAllMapUnitExcept m2GGetAllMapUnitExcept = (M2G_GetAllMapUnitExcept)await mapSession.Call(new G2M_GetAllMapUnitExcept() { Account = message.Account }); if (m2GGetAllMapUnitExcept.Accounts.Count > 0) { PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>(); ActorMessageSenderComponent actorSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>(); for (int i = 0; i < m2GGetAllMapUnitExcept.Accounts.Count; i++) { if (playerComponent.AccountHaveBeCreated(m2GGetAllMapUnitExcept.Accounts[i])) { Player player = playerComponent.getPlayerByAccount(m2GGetAllMapUnitExcept.Accounts[i]); ActorMessageSender actorMessageSender = actorSenderComponent.Get(player.MapInstanceId); Actor_PlayerInitPositionResponse actor_PlayerInitPositionResponse = (Actor_PlayerInitPositionResponse)await actorMessageSender.Call(new Actor_PlayerInitPositionRequest()); session.Send(new G2C_OtherPlayerEnterMap() { Account = player.Account, PositionX = actor_PlayerInitPositionResponse.PositionX, PositionY = actor_PlayerInitPositionResponse.PositionY, PositionZ = actor_PlayerInitPositionResponse.PositionZ }); } } } await ETTask.CompletedTask; }
protected override async ETTask Run(Session session, C2G_Login request, G2C_Login response, Action reply) { Log.Info("玩家请求登录:" + request.Account + "玩家输入密码:" + request.Password); DBProxyComponent dBProxy = Game.Scene.GetComponent <DBProxyComponent>(); List <ComponentWithId> count = await dBProxy.Query <PlayerInfoDB>(PlayerInfoDB => PlayerInfoDB.account == request.Account); if (count.Count > 0) { if (count.Count == 1) { //取得该条数据 PlayerInfoDB info = await dBProxy.Query <PlayerInfoDB>(count[0].Id); //验证密码 if (info.pwd == request.Password) { Log.Info("密码正确,允许登录"); response.LoginFail = true; PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>(); Player loginPlayer; //查看玩家是否已经登录创建过 if (playerComponent.AccountHaveBeCreated(request.Account)) { Log.Info("玩家被顶号: " + request.Account); //获取之前已经创建好的Player实体 loginPlayer = playerComponent.getPlayerByAccount(request.Account); try { //给被顶号的人发送被顶号的信息 loginPlayer.session.Send(new G2C_PlayerBackLogin() { NetMessage = "此账号在其它地方被登录" }); } catch { Log.Info("发送顶号信息失败:" + request.Account); } //给其它玩家广播这个玩家掉线的信息 playerComponent.PlayerBackLogin(request.Account).Coroutine(); } else { //创建登录玩家的实体 loginPlayer = PlayerFactory.Create(request.Account); //向玩家管理组件里添加玩家的信息 playerComponent.addPlayerToDict(request.Account, loginPlayer); } //对玩家的session进行记录 loginPlayer.session = session; session.AddComponent <SessionPlayerComponent>().Player = loginPlayer; } else { Log.Info("密码错误"); response.LoginFail = false; } } else { Log.Error("账号重复了: " + count.Count); response.LoginFail = false; } } else { response.LoginFail = false; } reply(); await ETTask.CompletedTask; }
public static async ETVoid OnLoginAsync(string account, string password) { try { // 显示加载UI ETModel.Game.EventSystem.Run(ETModel.EventIdType.ShowLoadingUI); // 如果正在登录,就驳回登录请求,为了双重保险,点下登录按钮后,收到服务端响应之前将不能再点击 if (isLogining) { FinalRun(); return; } isLogining = true; if (account == "" || password == "") { Game.EventSystem.Run(EventIdType.ShowLoginInfo, "账号或密码不能为空"); FinalRun(); return; } // 创建一个ETModel层的Session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>() .Create(GlobalConfigComponent.Instance.GlobalProto.Address); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = ComponentFactory.Create <Session, ETModel.Session>(session); Game.EventSystem.Run(EventIdType.ShowLoginInfo, "登陆中。。。"); // 发送登录请求,账号,密码均为传来的参数 R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = account, Password = password }); if (r2CLogin.Error == ErrorCode.ERR_LoginError) { Game.EventSystem.Run(EventIdType.ShowLoginInfo, "登录失败,账号或密码错误"); FinalRun(); return; } //释放realmSession realmSession.Dispose(); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession); // 增加客户端断线处理组件 Game.Scene.GetComponent <SessionComponent>().Session.AddComponent <SessionOfflineComponent>(); // 增加心跳组件 ETModel.Game.Scene.GetComponent <ETModel.SessionComponent>().Session.AddComponent <HeartBeatComponent>(); await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Game.EventSystem.Run(EventIdType.ShowLoginInfo, "登录成功"); // 创建Player(抽象化的玩家),这里的id是数据库里的账号id Player player = ETModel.ComponentFactory.CreateWithId <Player>(r2CLogin.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; // 登录完成 Game.EventSystem.Run(EventIdType.LoginFinish); // 测试消息有成员是class类型 // G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo) await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); // Debug.Log("测试玩家信息为" + g2CPlayerInfo.Message); FinalRun(); } catch (Exception e) { Log.Error(e); FinalRun(); } }
public void Awake() { Instance = this; }
public async void OnLogin() { try { // IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address); // 创建一个ETModel层的Session ETModel.Session session = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); Session realmSession = ComponentFactory.Create <Session, ETModel.Session>(session); /* * 请求登录 */ R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = this.account.GetComponent <InputField>().text, Password = this.password.GetComponent <InputField>().text } ); realmSession.Dispose(); /* * 处理登录结果 */ if (r2CLogin.Error == ErrorCode.ERR_Success) { connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中"111.230.133.20:10002" ETModel.Session gateSession = ETModel.Game.Scene.GetComponent <NetOuterComponent>().Create(connetEndPoint); ETModel.Game.Scene.AddComponent <ETModel.SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); Log.Info("登陆gate成功!"); // 创建Player Player player = ETModel.ComponentFactory.CreateWithId <Player>(g2CLoginGate.PlayerId); PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; Game.Scene.GetComponent <UIComponent>().Create(UIType.UILobby); Game.Scene.GetComponent <UIComponent>().Remove(UIType.UILogin); } else if (r2CLogin.Error == ErrorCode.ERR_AccountInvaild) { Game.Scene.GetComponent <UIAlertsGrayComponent>().CreateAlertWin("账号中有不法字符~", onAlertCbLoginFail, "好的~"); } else if (r2CLogin.Error == ErrorCode.ERR_PasswordInvaild) { Game.Scene.GetComponent <UIAlertsGrayComponent>().CreateAlertWin("密码中有不法字符~", onAlertCbLoginFail, "好的~"); } else if (r2CLogin.Error == ErrorCode.ERR_PasswordIncorrect) { Game.Scene.GetComponent <UIAlertsGrayComponent>().CreateAlertWin("密码不正确~", onAlertCbLoginFail, "好的~"); } else if (r2CLogin.Error == ErrorCode.ERR_AccountNotExist) { UIAlertsGrayComponent l = Game.Scene.GetComponent <UIAlertsGrayComponent>(); Game.Scene.GetComponent <UIAlertsGrayComponent>().CreateAlertWin("账号不存在~", onAlertCbLoginFail, "好的~"); } else if (r2CLogin.Error == ErrorCode.ERR_DataBaseRead) { Game.Scene.GetComponent <UIAlertsGrayComponent>().CreateAlertWin("网络异常~", onAlertCbLoginFail, "好的~"); } } catch (Exception e) { Log.Error(e); } }
//public static void Test(this PlayerComponent self) //{ // List<long> uids = new List<long> // { // 385283104112654L, // 385284038459567L, // 385284160618576L, // 385284163240049L, // 385284180541703L, // 385284256104497L, // 385284340318336L // }; // for(int i = 0; i < 1000; i++) // { // var rnd = RandomHelper.RandomNumber(0, uids.Count); // var uid = uids[rnd]; // self.Run(uid); // } //} //public static async void Run(this PlayerComponent self, long uid) //{ // System.Console.WriteLine($"uid:{uid}"); // //ComponentWithId entity = await Game.Scene.GetComponent<CacheProxyComponent>().QueryById<ComponentWithId>("player", uid); // //System.Console.WriteLine($"uid:{uid}:QueryById"); // var player = ComponentFactory.CreateWithId<Player>(uid); // await self.Add(player); // System.Console.WriteLine($"uid:{uid}:Add"); // player.gateSessionActorId = 0; // player.SetOnline(true); // // 隨機配給MapServerID // var startComponent = Game.Scene.GetComponent<StartConfigComponent>(); // player.mapServerId = RandomHelper.RandomNumber(0, startComponent.MapConfigs.Count); // await self.Update(player); // System.Console.WriteLine($"uid:{uid}:Update"); // await self.Remove(uid); // System.Console.WriteLine($"uid:{uid}:Remove"); //} public static void OnRefresh(this PlayerComponent self, long?id) { }
public static async ETTask Update(this PlayerComponent self, Player player) { await self.MemorySync.Update(player); }
protected override async ETTask Run(Session session, C2G_RequestEnterMap request, G2C_RequestEnterMap response, Action reply) { Log.Info("玩家:" + request.Account + "请求进入Map"); //获取玩家 PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>(); Player player = playerComponent.getPlayerByAccount(request.Account); if (player != null) { //获取内网发送组件 IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent <InnerConfig>().IPEndPoint; Session mapSession = Game.Scene.GetComponent <NetInnerComponent>().Get(mapAddress); M2G_EnterWorld m2GEnterWorld = (M2G_EnterWorld)await mapSession.Call(new G2M_EnterWorld() { Account = request.Account, PlayerGateInstanceId = player.InstanceId }); player.MapInstanceId = m2GEnterWorld.PlayerMapInstanceId; //获取角色出生位置 ActorMessageSenderComponent actorSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>(); ActorMessageSender actorMessageSender = actorSenderComponent.Get(player.MapInstanceId); Actor_PlayerInitPositionResponse actor_PlayerInitPositionResponse = (Actor_PlayerInitPositionResponse)await actorMessageSender.Call(new Actor_PlayerInitPositionRequest()); Log.Info("获取角色初始位置:" + actor_PlayerInitPositionResponse.PositionX + " || " + actor_PlayerInitPositionResponse.PositionY + " || " + actor_PlayerInitPositionResponse.PositionZ); response.PositionX = actor_PlayerInitPositionResponse.PositionX; response.PositionY = actor_PlayerInitPositionResponse.PositionY; response.PositionZ = actor_PlayerInitPositionResponse.PositionZ; reply(); Log.Info("同意进入Map"); //向其它玩家发送自己登录的信息 List <Player> players = playerComponent.getOtherPlayerIgnoreAccount(request.Account); if (players != null) { for (int i = 0; i < players.Count; i++) { players[i].session.Send(new G2C_OtherPlayerEnterMap() { Account = request.Account, PositionX = actor_PlayerInitPositionResponse.PositionX, PositionY = actor_PlayerInitPositionResponse.PositionY, PositionZ = actor_PlayerInitPositionResponse.PositionZ }); } } } else { Log.Info("玩家超时:" + request.Account); session.Send(new G2C_PlayerBackLogin() { NetMessage = "游戏超时: " + request.Account }); } await ETTask.CompletedTask; }
public static async ETTask Remove(this PlayerComponent self, long uid) { await self.MemorySync.Delete <Player>(uid); }