continuity/Server/PacketHandlers/CharacterDeletionHandler.cs

44 lines
1.3 KiB
C#
Raw Normal View History

2023-11-21 21:37:50 +01:00
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
2023-11-20 19:58:30 +01:00
2023-11-16 17:55:36 +01:00
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using Server.DB;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Server.PacketHandlers;
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 17:55:36 +01:00
{
2023-11-16 23:51:30 +01:00
session.Disconnect();
2023-11-16 17:55:36 +01:00
return;
}
var character = await _wonderkingContext.Characters.FirstOrDefaultAsync(x => x.Name == packet.Name &&
2023-11-25 17:59:17 +01:00
x.Account.Id == authSession.AccountId);
2023-11-25 17:57:15 +01:00
var response = new CharacterDeleteResponsePacket { HasToBeZero = 0 };
2023-11-16 17:55:36 +01:00
if (character == null)
{
2023-11-21 21:36:05 +01:00
await authSession.SendAsync(response);
2023-11-16 17:55:36 +01:00
return;
}
2023-11-19 15:08:17 +01:00
_wonderkingContext.Characters.Remove(character);
2023-11-21 21:36:05 +01:00
await _wonderkingContext.SaveChangesAsync();
2023-11-16 17:55:36 +01:00
2023-11-21 21:36:05 +01:00
await authSession.SendAsync(response);
2023-11-16 17:55:36 +01:00
}
}