using Fantasy; using Fantasy.Async; using Fantasy.Entitas; using Fantasy.Entitas.Interface; using Fantasy.Helper; using Fantasy.Platform.Net; namespace Hotfix; public static class AuthenticationComponentSystem { #region 注册 /// /// 注册 /// /// /// /// /// /// public static async FTask Register(this AuthenticationComponent self,string username, string password,string source) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return GameErrorCode.UsernameOrPasswordCannotBeEmpty; } var position = HashCodeHelper.MurmurHash3(username) % self.AuthenticationCount; if (position != self.Position) { return GameErrorCode.UnauthorizedServer; } var scene = self.Scene; var database = scene.World.DataBase; var userCode = (long)username.GetHashCode(); using (await scene.CoroutineLockComponent.Wait((int)ELockType.AuthenticationRegister,userCode)) { if (self.RegisterAccounts.ContainsKey(userCode)) { Log.Debug($"Authentication:RegisterAccount Fail(注册失败),username:{username} 已存在缓存中"); return GameErrorCode.UserAlreadyExists; } var isExist = await database.Exist(x=>x.Username == username); if (isExist) { Log.Debug($"Authentication:RegisterAccount Fail(注册失败),username:{username} 已存在数据库中"); return GameErrorCode.UserAlreadyExists; } var account = Entity.Create(scene,true,false); account.Username = username; account.Password = password; account.CreateTime = DateTime.Now.Ticks; var timerOutCom = account.AddComponent(); timerOutCom.TimeOut(5000); self.RegisterAccounts.Add(userCode,account); database.Save(account); Log.Debug($"Authentication:RegisterAccount Success(注册成功),username:{username},password:{password},source:{source},Position:{self.Scene.SceneConfigId}"); return GameErrorCode.Success; } } /// /// 移除注册缓存 /// /// /// /// public static void RemoveRegisterCacheAccount(this AuthenticationComponent self,long userCode,bool isDispose) { if (!self.RegisterAccounts.Remove(userCode,out var account)) { return; } if (isDispose) { Log.Debug($"Authentication:账号{ account.Username}超时,移除缓存"); account.Dispose(); } } public static async FTask<(uint error,long accountId)> Login(this AuthenticationComponent self,string username, string password,string source) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return (GameErrorCode.UsernameOrPasswordCannotBeEmpty,0); } var position = HashCodeHelper.MurmurHash3(username) % self.AuthenticationCount; if (position != self.Position) { return (GameErrorCode.UnauthorizedServer,0); } var scene = self.Scene; var database = scene.World.DataBase; var userCode = (long)username.GetHashCode(); using (await scene.CoroutineLockComponent.Wait((int)ELockType.AuthenticationLogin,userCode)) { (uint,long) result = (0,0); if (self.LoginAccounts.TryGetValue(username,out var accountInfoCache)) { var accountCache = accountInfoCache.GetComponent(); Log.Debug($"Authentication:Login 缓存中已存在,username:{username},password:{password},source:{source} 缓存中查看,Position:{self.Scene.SceneConfigId}"); return accountCache != null ? (GameErrorCode.UserAlreadyExists,accountCache.Id):(GameErrorCode.UserNotFoundOrPasswordIsNotCorrect,0); } var account = await database.First(x=>x.Username == username && x.Password == password); if (account == null) { result = (GameErrorCode.UserNotFoundOrPasswordIsNotCorrect, 0); Log.Debug($"Authentication:Login 失败(账号不存在或者密码不正确),username:{username},password:{password},source:{source} 数据库中查看,Position:{self.Scene.SceneConfigId}"); } var accountInfo = Entity.Create(scene,true,false); if (account !=null) { account.Deserialize(scene); accountInfo.AddComponent(account); result = (GameErrorCode.Success,account.Id); Log.Debug($"Authentication: Login Success(登录成功),username:{username},password:{password},source:{source},Position:{self.Scene.SceneConfigId}"); } var timerOutCom = accountInfo.AddComponent(); timerOutCom.Key = username; timerOutCom.TimeOut(6000); self.LoginAccounts.Add(username,accountInfo); return result; } } public static void RemoveCacheAccountInfo(this AuthenticationComponent self,string key, bool isDispose) { if (!self.LoginAccounts.Remove(key,out var accountCacheInfo)) { return; } if (isDispose) { Log.Debug($"Authentication:Login:username:{key} 用户移除成功 从缓存中"); accountCacheInfo.Dispose(); } } public static void UpdatePosition(this AuthenticationComponent self) { var authenticationScenes = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Authentication); var sceneConfig = SceneConfigData.Instance.Get(self.Scene.SceneConfigId); self.Position = authenticationScenes.IndexOf(sceneConfig); self.AuthenticationCount = authenticationScenes.Count; } #endregion } public class AuthenticationComponentAwakeSystem : AwakeSystem { protected override void Awake(AuthenticationComponent self) { self.UpdatePosition(); } } public class AuthenticationComponentDestroySystem : DestroySystem { protected override void Destroy(AuthenticationComponent self) { foreach (var account in self.RegisterAccounts.Values.ToArray()) { account.Dispose(); } self.RegisterAccounts.Clear(); } }