using ET; using ET.EventType; using System; using System.Collections.Generic; using System.Text; namespace ET { public class GateUserComponentAwakeSystem : AwakeSystem { public override void Awake(GateUserComponent self) { self.Awake(); } } public static class GateUserComponentSystem { /// /// 查询缓存或数据库 /// /// /// /// public static async ETTask Query(this GateUserComponent self, long id) { User player = self.Get(id); if (player == null) player = await self.QueryUserByCache(id); if (player == null) { Log.Error($"[ERROR] user == null where id = {id}"); return null; } return player; } private static async ETTask QueryUserByCache(this GateUserComponent self,long id) { long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(self.DomainZone(), SceneType.UserCache.ToString()).SceneId; U2M_GetComponent getComponent= (U2M_GetComponent) await MessageHelper.CallActor(mapInstanceId, new M2U_GetComponent() { UserId = id, type = nameof (User) }); if (getComponent.component == null) return null; User user = getComponent.component as User; return user; } #region 登录 注册 //用户登陆 public static async ETTask<(Account, string)> Login(this GateUserComponent self, string ip, string dataStr, LoginType loginType) { int forbiddenCount = await AccountHelper.GetFirbiddenIpCount(ip); if (forbiddenCount > 0) { return (null, "您的帐号已被停封,请联系客服"); } Account accountInfo = null; switch (loginType) { case LoginType.Editor: case LoginType.Tourist: case LoginType.Voucher://检验凭证 accountInfo = await self.VoucherLogin(dataStr); break; default: Log.Error("不存在的登陆方式:" + loginType); break; } //更新最后登录时间 if (accountInfo != null) { accountInfo.LastLoginTime = TimeHelper.ClientNow(); accountInfo.LastIp = ip; accountInfo.HistoryIpList ??= new List(); if (!accountInfo.HistoryIpList.Contains(ip)) { accountInfo.HistoryIpList.Add(ip); } await DBComponent.Instance.Save(accountInfo); } if (accountInfo == null) return (null, "帐号或者密码错误"); return (accountInfo, string.Empty); } //用户注册 public static async ETTask<(Account, string)> Register(this GateUserComponent self, string ip, string dataStr,string identifyStr) { int forbiddenCount = await AccountHelper.GetFirbiddenIpCount(ip); if (forbiddenCount > 0) { return (null, "您的帐号已被停封,请联系客服"); } if (!AccountHelper.CanRegister(identifyStr)) { return (null, "今日注册数达到上限"); } string[] userIdPassword = dataStr.Split('|'); if (userIdPassword.Length != 2) { return (null, "帐号或者密码错误"); } Account accountInfo = null; List accountInfos = await DBComponent.Instance.Query(t => t.Username == userIdPassword[0]); if (accountInfos.Count == 0) { accountInfo = await AccountFactory.EditRegisterCreatUser(self.DomainScene(), userIdPassword[0], userIdPassword[1]); accountInfos.Add(accountInfo); accountInfo.LastLoginTime = TimeHelper.ClientNow(); accountInfo.CreateIp = identifyStr; accountInfo.LastIp = ip; accountInfo.HistoryIpList ??= new List(); if (!accountInfo.HistoryIpList.Contains(ip)) { accountInfo.HistoryIpList.Add(ip); } await DBComponent.Instance.Save(accountInfo); } if (accountInfo == null) return (null, "帐号已被注册"); return (accountInfo, string.Empty); } //凭证登陆 就是 userId'|'密码 public static async ETTask VoucherLogin(this GateUserComponent self, string userIdAndPassword) { string[] userIdPassword = userIdAndPassword.Split('|'); if (userIdPassword.Length != 2) { return null; } try { string queryUserId = userIdPassword[0]; List accountInfos = await DBComponent.Instance.Query(AccountInfo => AccountInfo.Username == queryUserId && AccountInfo.Pwd == userIdPassword[1]); if (accountInfos.Count > 0) { return accountInfos[0]; } } catch (Exception e) { Log.Error(e); } return null; } #endregion public static async ETTask Save(this GateUserComponent self, User player) { long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(self.DomainZone(), SceneType.UserCache.ToString()).SceneId; MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = player.Id, type = nameof (User), component =player }); return player; } /// /// 玩家上线事件 /// /// /// /// /// public static async ETTask OnLine(this GateUserComponent self, Session session, long userId, string platform) { User user = await self.Query(userId); if (user == null) { return null; } Log.Info($"************\n【上线】【{platform}】玩家Id=【{user.Id}】 Name=【{user.NickName}】上线了\n***********"); OnLineComponent.Instance.OnLine(userId, session); long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(session.DomainZone(), "Map").SceneId; MessageHelper.SendActor(mapInstanceId, new G2M_UserOnLine() { Id = user.Id}); //记录玩家信息 self.idPlayers[user.Id]=user; return user; } /// /// 玩家下线 /// /// /// public static async ETTask OffLine(this GateUserComponent self, User user, Session session) { await ETTask.CompletedTask; if (user != null) { Log.Info($"************\n【离线】玩家Id=【{user.Id}】 Name=【{user.NickName}】下线了\n***********"); OnLineComponent.Instance.OffLine(user.Id); long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(session.DomainZone(), "Map").SceneId; MessageHelper.SendActor(mapInstanceId, new G2M_UserOffLine { Id =user.Id}); bool ret = self.Remove(user.Id); if (!ret) Log.Error($"玩家Id=【{user.Id}】 Name=【{user.NickName}】不在GateUserComponent中"); } } } }