mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
注册登录Demo
注册登录Demo
This commit is contained in:
19
Assets/GameScripts/DotNet/Logic/ErrorCode.cs
Normal file
19
Assets/GameScripts/DotNet/Logic/ErrorCode.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
3
Assets/GameScripts/DotNet/Logic/ErrorCode.cs.meta
Normal file
3
Assets/GameScripts/DotNet/Logic/ErrorCode.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6977010e71da47eaaf96a40a1db7c021
|
||||
timeCreated: 1689435709
|
@@ -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<int, string> ErrCodeTextMap = new Dictionary<int, string>
|
||||
{
|
||||
{
|
||||
ErrorCode.ERR_AccountAlreadyRegisted, "账户已经被注册了"
|
||||
},
|
||||
{
|
||||
ErrorCode.ERR_AccountOrPasswordError, "账户或者密码错误"
|
||||
},
|
||||
{
|
||||
ErrorCode.ERR_UserNotOnline, "用户当前不在线"
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
@@ -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
|
||||
|
@@ -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
|
||||
/// <summary>
|
||||
/// 登录消息回调。
|
||||
/// </summary>
|
||||
@@ -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
|
||||
/// <summary>
|
||||
/// 注册消息回调。
|
||||
/// </summary>
|
||||
/// <param name="response">网络回复消息包。</param>
|
||||
public void OnRegisterRes(IResponse response)
|
||||
{
|
||||
if (NetworkUtils.CheckError(response))
|
||||
{
|
||||
return;
|
||||
}
|
||||
H_G2C_RegisterResponse ret = (H_G2C_RegisterResponse)response;
|
||||
Log.Debug(ret.ToJson());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册消息请求。
|
||||
/// </summary>
|
||||
/// <param name="userName">用户名。</param>
|
||||
/// <param name="passWord">用户密码。</param>
|
||||
public void DoRegisterReq(string userName,string passWord)
|
||||
{
|
||||
H_C2G_RegisterRequest registerQuest =new H_C2G_RegisterRequest()
|
||||
{
|
||||
UserName = userName,
|
||||
Password = passWord
|
||||
};
|
||||
GameClient.Instance.Send(registerQuest);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@@ -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; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 客户端发送消息请求注册账户
|
||||
/// </summary>
|
||||
[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; }
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
20
DotNet/Config/ProtoBuf/proto_csOuterMessage.proto
Normal file
20
DotNet/Config/ProtoBuf/proto_csOuterMessage.proto
Normal file
@@ -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;
|
||||
}
|
@@ -1,5 +1,3 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
// 生成器自动生成,请不要手动编辑。
|
||||
|
@@ -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; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 客户端发送消息请求注册账户
|
||||
/// </summary>
|
||||
[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; }
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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<H_C2G_LoginRequest,H_G2C_LoginResponse>
|
||||
{
|
||||
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
|
@@ -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<H_C2G_LoginRequest, H_G2C_LoginResponse>
|
||||
{
|
||||
protected override async FTask Run(Session session, H_C2G_LoginRequest request, H_G2C_LoginResponse response, Action reply)
|
||||
{
|
||||
IDateBase db = session.Scene.World.DateBase;
|
||||
List<AccountInfo> result = await db.Query<AccountInfo>(
|
||||
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
|
@@ -0,0 +1,50 @@
|
||||
using TEngine.Core.Network;
|
||||
using TEngine.Core;
|
||||
using TEngine.Core.DataBase;
|
||||
|
||||
namespace TEngine.Logic
|
||||
{
|
||||
public class H_C2G_RegisterRequestHandler: MessageRPC<H_C2G_RegisterRequest, H_G2C_RegisterResponse>
|
||||
{
|
||||
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<AccountInfo> result = !isSDKRegister ?
|
||||
await db.Query<AccountInfo>(t=>t.UserName == request.UserName) :
|
||||
await db.Query<AccountInfo>(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<AccountInfo>(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<uint> GeneratorUID(IDateBase db)
|
||||
{
|
||||
var ret = await db.First<AccountInfo>(t=>t.UID != 0);
|
||||
if (ret == null)
|
||||
{
|
||||
return 100000;
|
||||
}
|
||||
return ret.UID + 1;
|
||||
}
|
||||
}
|
||||
}
|
32
DotNet/Logic/src/Model/AccountInfo.cs
Normal file
32
DotNet/Logic/src/Model/AccountInfo.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
namespace TEngine.Logic;
|
||||
|
||||
/// <summary>
|
||||
/// 账号信息
|
||||
/// </summary>
|
||||
public class AccountInfo : Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户唯一ID。
|
||||
/// </summary>
|
||||
public uint UID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户名。
|
||||
/// </summary>
|
||||
public string UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 密码。
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 渠道唯一ID。
|
||||
/// </summary>
|
||||
public uint SDKUID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否禁用账号。
|
||||
/// </summary>
|
||||
public bool Forbid { get; set; }
|
||||
}
|
38
DotNet/Logic/src/Model/UserInfo.cs
Normal file
38
DotNet/Logic/src/Model/UserInfo.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace TEngine.Logic;
|
||||
|
||||
public class UserInfoAwakeSystem : AwakeSystem<UserInfo>
|
||||
{
|
||||
protected override void Awake(UserInfo self)
|
||||
{
|
||||
self.Awake();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 角色信息。
|
||||
/// </summary>
|
||||
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<Ca>
|
||||
public void Awake()
|
||||
{
|
||||
UserName = string.Empty;
|
||||
Level = 1;
|
||||
Money = 10000;
|
||||
LastPlay = 0;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user