continuity/Continuity.AuthServer/PacketHandlers/CharacterDeletionHandler.cs

35 lines
1.2 KiB
C#
Raw Normal View History

2024-02-07 16:40:36 +01:00
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
2023-11-20 19:58:30 +01:00
using Continuity.AuthServer.DB;
2023-11-16 17:55:36 +01:00
using Microsoft.EntityFrameworkCore;
2024-04-04 16:57:42 +02:00
using RaiNote.PacketMediator;
2023-11-16 17:55:36 +01:00
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Continuity.AuthServer.PacketHandlers;
2023-11-16 17:55:36 +01:00
public class CharacterDeletionHandler : IPacketHandler<CharacterDeletePacket, AuthSession> {
2023-11-16 17:55:36 +01:00
private readonly WonderkingContext _wonderkingContext;
public CharacterDeletionHandler(WonderkingContext wonderkingContext) {
2023-11-16 17:55:36 +01:00
_wonderkingContext = wonderkingContext;
}
2024-02-07 16:40:36 +01:00
public async Task HandleAsync(CharacterDeletePacket packet, AuthSession session,
CancellationToken cancellationToken) {
var character = await _wonderkingContext.Characters.FirstOrDefaultAsync(x => x.Name == packet.Name &&
2024-02-07 16:40:36 +01:00
x.Account.Id == session.AccountId, cancellationToken);
2023-11-25 17:59:17 +01:00
2023-11-25 17:57:15 +01:00
var response = new CharacterDeleteResponsePacket { HasToBeZero = 0 };
if (character == null) {
2024-02-05 18:12:12 +01:00
await session.SendAsync(response);
2023-11-16 17:55:36 +01:00
return;
}
2023-11-19 15:08:17 +01:00
_wonderkingContext.Characters.Remove(character);
2024-02-05 12:08:00 +01:00
await _wonderkingContext.SaveChangesAsync(cancellationToken);
2023-11-16 17:55:36 +01:00
2024-02-05 18:12:12 +01:00
await session.SendAsync(response);
2023-11-16 17:55:36 +01:00
}
}