From 9cd0bac81e7a9aecd11aff8b32350058bee6a73f Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Mon, 17 Jul 2023 01:00:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=99=BB=E5=BD=95=E6=B3=A8?= =?UTF-8?q?=E5=86=8CDemo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 完善登录注册Demo --- .../UserHandler/H_C2G_LoginRequestHandler.cs | 23 ++++++- .../H_C2G_RegisterRequestHandler.cs | 9 ++- DotNet/Logic/src/Model/AccountComponent.cs | 68 +++++++++++++++++++ DotNet/Logic/src/Model/AccountInfo.cs | 7 ++ .../Model/{UserInfo.cs => CharacterInfo.cs} | 6 +- DotNet/Logic/src/OnCreateScene.cs | 8 ++- 6 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 DotNet/Logic/src/Model/AccountComponent.cs rename DotNet/Logic/src/Model/{UserInfo.cs => CharacterInfo.cs} (76%) diff --git a/DotNet/Logic/src/Handler/UserHandler/H_C2G_LoginRequestHandler.cs b/DotNet/Logic/src/Handler/UserHandler/H_C2G_LoginRequestHandler.cs index 77a94f9d..9959c3c1 100644 --- a/DotNet/Logic/src/Handler/UserHandler/H_C2G_LoginRequestHandler.cs +++ b/DotNet/Logic/src/Handler/UserHandler/H_C2G_LoginRequestHandler.cs @@ -22,18 +22,35 @@ namespace TEngine.Logic return; } - if (result[0].Forbid) + AccountInfo account = result[0]; + + if (account.Forbid) { response.ErrorCode = ErrorCode.ERR_AccountIsForbid; reply(); return; } + AccountComponent accountComponent = session.Scene.GetComponent(); + if (accountComponent.Get(account.UID) != null) + { + response.ErrorCode = ErrorCode.ERR_LoginError; + reply(); + return; + } + else + { + var accountInfo = session.AddComponent(); + accountInfo.UID = account.UID; + accountInfo.SDKUID = account.SDKUID; + accountComponent.Add(account); + } + Log.Debug($"收到请求登录的消息 request:{request.ToJson()}"); response.Text = "登录成功"; - response.UID = result[0].UID; + response.UID = account.UID; await FTask.CompletedTask; - } + } } } #endif \ No newline at end of file diff --git a/DotNet/Logic/src/Handler/UserHandler/H_C2G_RegisterRequestHandler.cs b/DotNet/Logic/src/Handler/UserHandler/H_C2G_RegisterRequestHandler.cs index e572c285..cfd9ed39 100644 --- a/DotNet/Logic/src/Handler/UserHandler/H_C2G_RegisterRequestHandler.cs +++ b/DotNet/Logic/src/Handler/UserHandler/H_C2G_RegisterRequestHandler.cs @@ -15,12 +15,19 @@ namespace TEngine.Logic await db.Query(t=>t.UserName == request.UserName) : await db.Query(t=>t.SDKUID == request.SDKUID) ; - if (result.Count > 0) + if (result.Count == 1) { response.ErrorCode = ErrorCode.ERR_AccountAlreadyRegisted; reply(); return; } + else if (result.Count >= 1) + { + response.ErrorCode = ErrorCode.ERR_AccountAlreadyRegisted; + Log.Error("出现重复账号:" + request.UserName); + reply(); + return; + } uint uid = await GeneratorUID(db); diff --git a/DotNet/Logic/src/Model/AccountComponent.cs b/DotNet/Logic/src/Model/AccountComponent.cs new file mode 100644 index 00000000..94df269c --- /dev/null +++ b/DotNet/Logic/src/Model/AccountComponent.cs @@ -0,0 +1,68 @@ +namespace TEngine.Logic; + +/// +/// 场景账号管理组件。 +/// [Scene] +/// +public class AccountComponent:Entity, INotSupportedPool +{ + /// + /// 当前登陆的账号。 + /// + public readonly Dictionary accountInfoMap = new Dictionary(); + + /// + /// 获取账号信息。 + /// + /// + /// + public AccountInfo? Get(uint uid) + { + this.accountInfoMap.TryGetValue(uid, out AccountInfo? ret); + return ret; + } + + /// + /// 添加当前登录的账号。 + /// + /// 账号信息。 + /// + public bool Add(AccountInfo? accountInfo) + { + if (accountInfo == null) + { + return false; + } + + if (this.accountInfoMap.ContainsKey(accountInfo.UID)) + { + return false; + } + + this.accountInfoMap[accountInfo.UID] = accountInfo; + + return true; + } + + /// + /// 移除当前登录的账号。 + /// + /// 账号信息。 + /// + public bool Remove(AccountInfo? accountInfo) + { + if (accountInfo == null) + { + return false; + } + + if (!this.accountInfoMap.ContainsKey(accountInfo.UID)) + { + return false; + } + + this.accountInfoMap.Remove(accountInfo.UID); + + return true; + } +} \ No newline at end of file diff --git a/DotNet/Logic/src/Model/AccountInfo.cs b/DotNet/Logic/src/Model/AccountInfo.cs index 6141ce73..9379c9b0 100644 --- a/DotNet/Logic/src/Model/AccountInfo.cs +++ b/DotNet/Logic/src/Model/AccountInfo.cs @@ -29,4 +29,11 @@ public class AccountInfo : Entity /// 是否禁用账号。 /// public bool Forbid { get; set; } + + public override void Dispose() + { + Log.Debug($"UID:{this.UID}"); + this.Parent?.Scene?.GetComponent()?.Remove(this); + base.Dispose(); + } } \ No newline at end of file diff --git a/DotNet/Logic/src/Model/UserInfo.cs b/DotNet/Logic/src/Model/CharacterInfo.cs similarity index 76% rename from DotNet/Logic/src/Model/UserInfo.cs rename to DotNet/Logic/src/Model/CharacterInfo.cs index be29ebea..796b234e 100644 --- a/DotNet/Logic/src/Model/UserInfo.cs +++ b/DotNet/Logic/src/Model/CharacterInfo.cs @@ -1,8 +1,8 @@ namespace TEngine.Logic; -public class UserInfoAwakeSystem : AwakeSystem +public class CharacterInfoAwakeSystem : AwakeSystem { - protected override void Awake(UserInfo self) + protected override void Awake(CharacterInfo self) { self.Awake(); } @@ -11,7 +11,7 @@ public class UserInfoAwakeSystem : AwakeSystem /// /// 角色信息。 /// -public class UserInfo : Entity +public class CharacterInfo : Entity { //昵称 public string UserName { get; set; } diff --git a/DotNet/Logic/src/OnCreateScene.cs b/DotNet/Logic/src/OnCreateScene.cs index 1e774d9c..6f57f916 100644 --- a/DotNet/Logic/src/OnCreateScene.cs +++ b/DotNet/Logic/src/OnCreateScene.cs @@ -4,7 +4,8 @@ using TEngine.Core.Network; namespace TEngine.Logic; /// -/// 当Scene创建时需要干什么 +/// 场景创建回调。 +/// 常用于定义场景需要添加的组件。 /// public class OnCreateScene : AsyncEventSystem { @@ -23,6 +24,11 @@ public class OnCreateScene : AsyncEventSystem sceneConfigInfo.Scene.AddComponent(); break; } + case "Gate": + { + sceneConfigInfo.Scene.AddComponent(); + break; + } } Log.Info($"scene create: {self.SceneInfo.SceneType} {self.SceneInfo.Name} SceneId:{self.SceneInfo.Id} ServerId:{self.SceneInfo.RouteId} WorldId:{self.SceneInfo.WorldId}");