diff --git a/Assets/GameScripts/DotNet/Logic/ErrorCode.cs b/Assets/GameScripts/DotNet/Logic/ErrorCode.cs new file mode 100644 index 00000000..0e8125d0 --- /dev/null +++ b/Assets/GameScripts/DotNet/Logic/ErrorCode.cs @@ -0,0 +1,19 @@ +namespace TEngine.Logic +{ + public static partial class ErrorCode + { + public const int ERR_SignError = 10000; + public const int ERR_Disconnect = 210000; + public const int ERR_LoginError = 210005; + + //300000开始自定义错误 + public const int ERR_AccountAlreadyRegisted = 300001; + public const int ERR_AccountOrPasswordError = 300002; + public const int ERR_UserNotOnline = 300003; + public const int ERR_ConnectGateKeyError = 300004; + public const int ERR_CreateNewCharacter = 300007; + public const int ERR_CannotCreateMoreCharacter = 300008; + public const int ERR_CharacterAlreadyRegisted = 300009; + public const int ERR_AccountIsForbid = 300010; + } +} \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Logic/ErrorCode.cs.meta b/Assets/GameScripts/DotNet/Logic/ErrorCode.cs.meta new file mode 100644 index 00000000..c1abaff7 --- /dev/null +++ b/Assets/GameScripts/DotNet/Logic/ErrorCode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6977010e71da47eaaf96a40a1db7c021 +timeCreated: 1689435709 \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs b/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs index 7d48efaf..affaf98c 100644 --- a/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs +++ b/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs @@ -1,4 +1,7 @@ -using TEngine.Core.Network; +using System.Collections.Generic; +using TEngine; +using TEngine.Core.Network; +using TEngine.Logic; namespace GameLogic { @@ -21,8 +24,27 @@ namespace GameLogic else { hasError = response.ErrorCode != 0; + if (ErrCodeTextMap.TryGetValue(response.ErrorCode,out var ret)) + { + Log.Error(ret); + } } + return hasError; } + + //Remark 这里图方便注册错误码文本,正常应该走文本配置表。 + public static Dictionary ErrCodeTextMap = new Dictionary + { + { + ErrorCode.ERR_AccountAlreadyRegisted, "账户已经被注册了" + }, + { + ErrorCode.ERR_AccountOrPasswordError, "账户或者密码错误" + }, + { + ErrorCode.ERR_UserNotOnline, "用户当前不在线" + }, + }; } } \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs index 8fd40331..b1f4816e 100644 --- a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs +++ b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs @@ -63,6 +63,18 @@ class NetWorkDemoUI : UIWindow private void OnClickRegisterBtn() { + if (GameClient.Instance.Status == GameClientStatus.StatusInit) + { + Log.Info("没有连接到服务器、请先点击连接到服务器按钮在进行此操作"); + return; + } + + if (string.IsNullOrEmpty(m_inputName.text) || string.IsNullOrEmpty(m_inputPassWord.text)) + { + Log.Info("请输入账号和密码"); + return; + } + PlayerNetSys.Instance.DoRegisterReq(m_inputName.text,m_inputPassWord.text); } #endregion diff --git a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs index fa508627..62ed6ec0 100644 --- a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs +++ b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs @@ -2,6 +2,7 @@ using TEngine; using TEngine.Core; using TEngine.Core.Network; +using UnityEngine; namespace GameLogic { @@ -18,8 +19,11 @@ namespace GameLogic base.Init(); //注册登录消息回调。 GameClient.Instance.RegisterMsgHandler(OuterOpcode.H_G2C_LoginResponse,OnLoginRes); + //注册注册账号消息回调。 + GameClient.Instance.RegisterMsgHandler(OuterOpcode.H_G2C_RegisterResponse,OnRegisterRes); } + #region Login /// /// 登录消息回调。 /// @@ -28,6 +32,7 @@ namespace GameLogic { if (NetworkUtils.CheckError(response)) { + Debug.Log("登录失败!"); GameClient.Instance.Status = GameClientStatus.StatusConnected; return; } @@ -56,5 +61,43 @@ namespace GameLogic GameClient.Instance.Send(loginRequest); GameClient.Instance.Status = GameClientStatus.StatusLogin; } + + + #endregion + + + #region Register + /// + /// 注册消息回调。 + /// + /// 网络回复消息包。 + public void OnRegisterRes(IResponse response) + { + if (NetworkUtils.CheckError(response)) + { + return; + } + H_G2C_RegisterResponse ret = (H_G2C_RegisterResponse)response; + Log.Debug(ret.ToJson()); + } + + /// + /// 注册消息请求。 + /// + /// 用户名。 + /// 用户密码。 + public void DoRegisterReq(string userName,string passWord) + { + H_C2G_RegisterRequest registerQuest =new H_C2G_RegisterRequest() + { + UserName = userName, + Password = passWord + }; + GameClient.Instance.Send(registerQuest); + } + + + #endregion + } } \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterMessage.cs b/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterMessage.cs index 9e801a50..7ff55bd3 100644 --- a/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterMessage.cs +++ b/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterMessage.cs @@ -152,6 +152,33 @@ namespace TEngine [ProtoMember(91, IsRequired = true)] public int ErrorCode { get; set; } [ProtoMember(1)] + public uint UID { get; set; } + [ProtoMember(2)] public string Text { get; set; } } + /// + /// 客户端发送消息请求注册账户 + /// + [ProtoContract] + public partial class H_C2G_RegisterRequest : AProto, IRequest + { + [ProtoIgnore] + public H_G2C_RegisterResponse ResponseType { get; set; } + public uint OpCode() { return OuterOpcode.H_C2G_RegisterRequest; } + [ProtoMember(1)] + public string UserName { get; set; } + [ProtoMember(2)] + public string Password { get; set; } + [ProtoMember(3)] + public uint SDKUID { get; set; } + } + [ProtoContract] + public partial class H_G2C_RegisterResponse : AProto, IResponse + { + public uint OpCode() { return OuterOpcode.H_G2C_RegisterResponse; } + [ProtoMember(91, IsRequired = true)] + public int ErrorCode { get; set; } + [ProtoMember(1)] + public uint UID { get; set; } + } } diff --git a/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterOpcode.cs b/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterOpcode.cs index 0aaa766c..b9668041 100644 --- a/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterOpcode.cs +++ b/Assets/GameScripts/HotFix/GameProto/GameProtocol/OuterOpcode.cs @@ -16,5 +16,7 @@ namespace TEngine public const int H_M2C_ReceiveAddressMessageToServer = 190000003; public const int H_C2G_LoginRequest = 110000003; public const int H_G2C_LoginResponse = 160000003; + public const int H_C2G_RegisterRequest = 110000004; + public const int H_G2C_RegisterResponse = 160000004; } } diff --git a/DotNet/Config/ProtoBuf/OuterMessage.proto b/DotNet/Config/ProtoBuf/OuterMessage.proto index dc9cfafd..dc11d1b8 100644 --- a/DotNet/Config/ProtoBuf/OuterMessage.proto +++ b/DotNet/Config/ProtoBuf/OuterMessage.proto @@ -65,5 +65,17 @@ message H_C2G_LoginRequest // IRequest,H_G2C_LoginResponse } message H_G2C_LoginResponse // IResponse { - string Text = 1; + uint UID = 1; + string Text = 2; +} +/// 客户端发送消息请求注册账户 +message H_C2G_RegisterRequest // IRequest,H_G2C_RegisterResponse +{ + string UserName = 1; + string Password = 2; + uint SDKUID = 3; +} +message H_G2C_RegisterResponse // IResponse +{ + uint UID = 1; } \ No newline at end of file diff --git a/DotNet/Config/ProtoBuf/proto_csOuterMessage.proto b/DotNet/Config/ProtoBuf/proto_csOuterMessage.proto new file mode 100644 index 00000000..e9bc32e3 --- /dev/null +++ b/DotNet/Config/ProtoBuf/proto_csOuterMessage.proto @@ -0,0 +1,20 @@ +syntax = "proto3"; +package TEngine.Network.Message; +import "google/protobuf/struct.proto"; + + +message AccountInfo +{ + int64 UnitId = 1; + int64 UserId = 2; + int64 CharaId = 3; + repeated int32 Ks = 4; + repeated int64 Vs = 5; +} + +message ItemInfo +{ + int32 ItemGId = 1; + int32 ItemId = 2; + int32 Count = 3; +} \ No newline at end of file diff --git a/DotNet/Logic/src/Generate/CustomExport/SceneType.cs b/DotNet/Logic/src/Generate/CustomExport/SceneType.cs index 7f60d994..e4ccaa18 100644 --- a/DotNet/Logic/src/Generate/CustomExport/SceneType.cs +++ b/DotNet/Logic/src/Generate/CustomExport/SceneType.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; - namespace TEngine { // 生成器自动生成,请不要手动编辑。 diff --git a/DotNet/Logic/src/Generate/NetworkProtocol/OuterMessage.cs b/DotNet/Logic/src/Generate/NetworkProtocol/OuterMessage.cs index 1e8234f9..9c39e3b7 100644 --- a/DotNet/Logic/src/Generate/NetworkProtocol/OuterMessage.cs +++ b/DotNet/Logic/src/Generate/NetworkProtocol/OuterMessage.cs @@ -152,6 +152,33 @@ namespace TEngine [ProtoMember(91, IsRequired = true)] public int ErrorCode { get; set; } [ProtoMember(1)] + public uint UID { get; set; } + [ProtoMember(2)] public string Text { get; set; } } + /// + /// 客户端发送消息请求注册账户 + /// + [ProtoContract] + public partial class H_C2G_RegisterRequest : AProto, IRequest + { + [ProtoIgnore] + public H_G2C_RegisterResponse ResponseType { get; set; } + public uint OpCode() { return OuterOpcode.H_C2G_RegisterRequest; } + [ProtoMember(1)] + public string UserName { get; set; } + [ProtoMember(2)] + public string Password { get; set; } + [ProtoMember(3)] + public uint SDKUID { get; set; } + } + [ProtoContract] + public partial class H_G2C_RegisterResponse : AProto, IResponse + { + public uint OpCode() { return OuterOpcode.H_G2C_RegisterResponse; } + [ProtoMember(91, IsRequired = true)] + public int ErrorCode { get; set; } + [ProtoMember(1)] + public uint UID { get; set; } + } } diff --git a/DotNet/Logic/src/Generate/NetworkProtocol/OuterOpcode.cs b/DotNet/Logic/src/Generate/NetworkProtocol/OuterOpcode.cs index 0aaa766c..b9668041 100644 --- a/DotNet/Logic/src/Generate/NetworkProtocol/OuterOpcode.cs +++ b/DotNet/Logic/src/Generate/NetworkProtocol/OuterOpcode.cs @@ -16,5 +16,7 @@ namespace TEngine public const int H_M2C_ReceiveAddressMessageToServer = 190000003; public const int H_C2G_LoginRequest = 110000003; public const int H_G2C_LoginResponse = 160000003; + public const int H_C2G_RegisterRequest = 110000004; + public const int H_G2C_RegisterResponse = 160000004; } } diff --git a/DotNet/Logic/src/Handler/H_C2G_LoginRequestHandler.cs b/DotNet/Logic/src/Handler/H_C2G_LoginRequestHandler.cs deleted file mode 100644 index 47903125..00000000 --- a/DotNet/Logic/src/Handler/H_C2G_LoginRequestHandler.cs +++ /dev/null @@ -1,18 +0,0 @@ -#if TENGINE_NET -using System; -using TEngine.Core.Network; -using TEngine.Core; - -namespace TEngine.Logic -{ - public class H_C2G_LoginRequestHandler : MessageRPC - { - protected override async FTask Run(Session session, H_C2G_LoginRequest request, H_G2C_LoginResponse response, Action reply) - { - Log.Debug($"收到请求登录的消息 request:{request.ToJson()}"); - response.Text = "登录成功"; - await FTask.CompletedTask; - } - } -} -#endif \ No newline at end of file diff --git a/DotNet/Logic/src/Handler/UserHandler/H_C2G_LoginRequestHandler.cs b/DotNet/Logic/src/Handler/UserHandler/H_C2G_LoginRequestHandler.cs new file mode 100644 index 00000000..77a94f9d --- /dev/null +++ b/DotNet/Logic/src/Handler/UserHandler/H_C2G_LoginRequestHandler.cs @@ -0,0 +1,39 @@ +#if TENGINE_NET +using System; +using TEngine.Core.Network; +using TEngine.Core; +using TEngine.Core.DataBase; + +namespace TEngine.Logic +{ + public class H_C2G_LoginRequestHandler: MessageRPC + { + protected override async FTask Run(Session session, H_C2G_LoginRequest request, H_G2C_LoginResponse response, Action reply) + { + IDateBase db = session.Scene.World.DateBase; + List result = await db.Query( + t=>t.UserName == request.UserName && + t.Password == request.Password); + + if (result.Count < 1) + { + response.ErrorCode = ErrorCode.ERR_AccountOrPasswordError; + reply(); + return; + } + + if (result[0].Forbid) + { + response.ErrorCode = ErrorCode.ERR_AccountIsForbid; + reply(); + return; + } + + Log.Debug($"收到请求登录的消息 request:{request.ToJson()}"); + response.Text = "登录成功"; + response.UID = result[0].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 new file mode 100644 index 00000000..c8748487 --- /dev/null +++ b/DotNet/Logic/src/Handler/UserHandler/H_C2G_RegisterRequestHandler.cs @@ -0,0 +1,50 @@ +using TEngine.Core.Network; +using TEngine.Core; +using TEngine.Core.DataBase; + +namespace TEngine.Logic +{ + public class H_C2G_RegisterRequestHandler: MessageRPC + { + protected override async FTask Run(Session session, H_C2G_RegisterRequest request, H_G2C_RegisterResponse response, Action reply) + { + IDateBase db = session.Scene.World.DateBase; + bool isSDKRegister = request.SDKUID != 0; + + List result = !isSDKRegister ? + await db.Query(t=>t.UserName == request.UserName) : + await db.Query(t=>t.SDKUID == request.SDKUID) ; + + if (result.Count > 0) + { + response.ErrorCode = ErrorCode.ERR_AccountAlreadyRegisted; + reply(); + return; + } + + uint uid = await GeneratorUID(db); + + AccountInfo accountInfo = Entity.Create(session.Scene); + accountInfo.UserName = request.UserName; + accountInfo.Password = request.Password; + accountInfo.SDKUID = request.SDKUID; + accountInfo.UID = uid; + + db.Save(accountInfo); + + Log.Debug($"收到注册的消息 request:{request.ToJson()}"); + response.UID = uid; + await FTask.CompletedTask; + } + + public async FTask GeneratorUID(IDateBase db) + { + var ret = await db.First(t=>t.UID != 0); + if (ret == null) + { + return 100000; + } + return ret.UID + 1; + } + } +} \ No newline at end of file diff --git a/DotNet/Logic/src/Model/AccountInfo.cs b/DotNet/Logic/src/Model/AccountInfo.cs new file mode 100644 index 00000000..6141ce73 --- /dev/null +++ b/DotNet/Logic/src/Model/AccountInfo.cs @@ -0,0 +1,32 @@ +namespace TEngine.Logic; + +/// +/// 账号信息 +/// +public class AccountInfo : Entity +{ + /// + /// 用户唯一ID。 + /// + public uint UID { get; set; } + + /// + /// 用户名。 + /// + public string UserName { get; set; } + + /// + /// 密码。 + /// + public string Password { get; set; } + + /// + /// 渠道唯一ID。 + /// + public uint SDKUID { get; set; } + + /// + /// 是否禁用账号。 + /// + public bool Forbid { get; set; } +} \ No newline at end of file diff --git a/DotNet/Logic/src/Model/UserInfo.cs b/DotNet/Logic/src/Model/UserInfo.cs new file mode 100644 index 00000000..be29ebea --- /dev/null +++ b/DotNet/Logic/src/Model/UserInfo.cs @@ -0,0 +1,38 @@ +namespace TEngine.Logic; + +public class UserInfoAwakeSystem : AwakeSystem +{ + protected override void Awake(UserInfo self) + { + self.Awake(); + } +} + +/// +/// 角色信息。 +/// +public class UserInfo : Entity +{ + //昵称 + public string UserName { get; set; } + + //等级 + public int Level { get; set; } + + //余额 + public long Money { get; set; } + + + //上次游戏角色序列 1/2/3 + public int LastPlay { get; set; } + + //public List + public void Awake() + { + UserName = string.Empty; + Level = 1; + Money = 10000; + LastPlay = 0; + } + +} \ No newline at end of file