2023-11-20 19:58:30 +01:00
|
|
|
// // Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
|
|
|
|
|
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)
|
|
|
|
{
|
2023-11-19 15:07:14 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-16 21:01:55 +01:00
|
|
|
var character = await _wonderkingContext.Characters.FirstOrDefaultAsync(x => x.Name == packet.Name &&
|
|
|
|
x.Account.Id == authSession.AccountId)
|
2023-11-16 23:51:30 +01:00
|
|
|
.ConfigureAwait(true);
|
|
|
|
var response = new CharacterDeleteResponsePacket { IsDeleted = 0 };
|
2023-11-16 17:55:36 +01:00
|
|
|
if (character == null)
|
|
|
|
{
|
2023-11-19 15:05:48 +01:00
|
|
|
await authSession.SendAsync(response).ConfigureAwait(false);
|
2023-11-16 17:55:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-19 15:08:17 +01:00
|
|
|
_wonderkingContext.Characters.Remove(character);
|
|
|
|
await _wonderkingContext.SaveChangesAsync().ConfigureAwait(false);
|
2023-11-16 17:55:36 +01:00
|
|
|
|
2023-11-19 15:05:48 +01:00
|
|
|
await authSession.SendAsync(response).ConfigureAwait(false);
|
2023-11-16 17:55:36 +01:00
|
|
|
}
|
|
|
|
}
|