注册登录Demo

注册登录Demo
This commit is contained in:
ALEXTANG
2023-07-16 00:57:20 +08:00
parent c178f8bb75
commit c6ecb48944
17 changed files with 350 additions and 22 deletions

View File

@@ -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;
}

View 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;
}

View File

@@ -1,5 +1,3 @@
using System.Collections.Generic;
namespace TEngine
{
// 生成器自动生成,请不要手动编辑。

View File

@@ -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; }
}
}

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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;
}
}
}

View 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; }
}

View 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;
}
}