Supercell.GUT/Supercell.GUT.Server/Protocol/Handlers/AccountMessageHandler.cs

104 lines
3.2 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using Supercell.GUT.Logic.Message.Account;
using Supercell.GUT.Server.Database.Document;
using Supercell.GUT.Server.Document;
using Supercell.GUT.Server.Network.Connection;
using Supercell.GUT.Server.Protocol.Attributes;
namespace Supercell.GUT.Server.Protocol.Handlers;
[ServiceNode(1)]
internal class AccountMessageHandler : MessageHandlerBase
{
private readonly ILogger _logger;
private readonly ClientConnection _connection;
private readonly DocumentManager _documentManager;
public AccountMessageHandler(ClientConnection connection, ILogger<AccountMessageHandler> logger, DocumentManager documentManager)
{
_logger = logger;
_connection = connection;
_documentManager = documentManager;
}
[MessageHandler(10108)]
public async Task OnKeepAlive(KeepAliveMessage keepAliveMessage)
{
await Task.CompletedTask;
}
[MessageHandler(10105)]
public async Task OnStartSecureConnection(StartSecureConnectionMessage startSecureConnectionMessage)
{
string nonce = "nonce";
await _connection.SendMessage(new SecureConnectionOkMessage()
{
Nonce = nonce
});
_connection.SetNonce(nonce);
}
[MessageHandler(10103)]
public async Task OnCreateAccount(CreateAccountMessage createAccountMessage)
{
await _documentManager.CreateAccount();
AccountDocument? accountDocument = _documentManager.AccountDocument;
if (accountDocument == null)
{
await _connection.SendMessage(new CreateAccountFailedMessage()
{
ErrorCode = 1
});
return;
}
await _connection.SendMessage(new CreateAccountOkMessage()
{
AccountIdHigherInt = accountDocument.DocumentId.HigherInt,
AccountIdLowerInt = accountDocument.DocumentId.LowerInt,
SessionKey = accountDocument.SessionKey
});
}
[MessageHandler(10102)]
public async Task OnLoginUsingSession(LoginUsingSessionMessage loginUsingSessionMessage)
{
if (_documentManager.Id.LowerInt == 0)
_documentManager.SetDocumentId(new (loginUsingSessionMessage.AccountIdHigherInt, loginUsingSessionMessage.AccountIdLowerInt));
await _documentManager.EnsureAccountDocument();
AccountDocument? accountDocument = _documentManager.AccountDocument;
if (accountDocument == null)
{
await _connection.SendMessage(new LoginFailedMessage()
{
ErrorCode = 2
});
return;
}
else if (accountDocument.SessionKey != loginUsingSessionMessage.SessionKey)
{
await _connection.SendMessage(new LoginFailedMessage()
{
ErrorCode = 5
});
return;
}
await _connection.SendMessage(new LoginOkMessage()
{
PrimaryAvatarIdHigherInt = accountDocument.DocumentId.HigherInt,
PrimaryAvatarIdLowerInt = accountDocument.DocumentId.LowerInt,
Fingerprint = "fece25cfc941db1a5bb3e79d4e6a60c34659f49e"
});
}
}