continuity/Continuity.AuthServer/PacketHandlers/CharacterDeletionHandler.cs

44 lines
1.3 KiB
C#
Raw Permalink Normal View History

2023-11-21 20:37:50 +00:00
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
2023-11-20 18:58:30 +00:00
using Continuity.AuthServer.DB;
2023-11-16 16:55:36 +00:00
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Continuity.AuthServer.PacketHandlers;
2023-11-16 16:55:36 +00:00
public class CharacterDeletionHandler : IPacketHandler<CharacterDeletePacket>
{
private readonly WonderkingContext _wonderkingContext;
public CharacterDeletionHandler(WonderkingContext wonderkingContext)
{
_wonderkingContext = wonderkingContext;
}
public async Task HandleAsync(CharacterDeletePacket packet, TcpSession session)
{
if (session is not AuthSession authSession)
2023-11-16 16:55:36 +00:00
{
2023-11-16 22:51:30 +00:00
session.Disconnect();
2023-11-16 16:55:36 +00:00
return;
}
var character = await _wonderkingContext.Characters.FirstOrDefaultAsync(x => x.Name == packet.Name &&
2023-11-25 16:59:17 +00:00
x.Account.Id == authSession.AccountId);
2023-11-25 16:57:15 +00:00
var response = new CharacterDeleteResponsePacket { HasToBeZero = 0 };
2023-11-16 16:55:36 +00:00
if (character == null)
{
2023-11-21 20:36:05 +00:00
await authSession.SendAsync(response);
2023-11-16 16:55:36 +00:00
return;
}
2023-11-19 14:08:17 +00:00
_wonderkingContext.Characters.Remove(character);
2023-11-21 20:36:05 +00:00
await _wonderkingContext.SaveChangesAsync();
2023-11-16 16:55:36 +00:00
2023-11-21 20:36:05 +00:00
await authSession.SendAsync(response);
2023-11-16 16:55:36 +00:00
}
}