continuity/Continuity.AuthServer/PacketHandlers/CharacterDeletionHandler.cs

44 lines
1.4 KiB
C#

// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
using Continuity.AuthServer.DB;
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using Rai.PacketMediator;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Continuity.AuthServer.PacketHandlers;
public class CharacterDeletionHandler : IPacketHandler<CharacterDeletePacket, TcpSession>
{
private readonly WonderkingContext _wonderkingContext;
public CharacterDeletionHandler(WonderkingContext wonderkingContext)
{
_wonderkingContext = wonderkingContext;
}
public async Task HandleAsync(CharacterDeletePacket packet, TcpSession session, CancellationToken cancellationToken)
{
if (session is not AuthSession authSession)
{
session.Disconnect();
return;
}
var character = await _wonderkingContext.Characters.FirstOrDefaultAsync(x => x.Name == packet.Name &&
x.Account.Id == authSession.AccountId, cancellationToken: cancellationToken);
var response = new CharacterDeleteResponsePacket { HasToBeZero = 0 };
if (character == null)
{
await authSession.SendAsync(response);
return;
}
_wonderkingContext.Characters.Remove(character);
await _wonderkingContext.SaveChangesAsync(cancellationToken);
await authSession.SendAsync(response);
}
}