continuity/Server/PacketHandlers/CharacterNameCheckHandler.cs

29 lines
954 B
C#

// // Copyright (c) 2023 Timothy Schenk.Subject to the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using Server.DB;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Server.PacketHandlers;
public class CharacterNameCheckHandler : IPacketHandler<CharacterNameCheckPacket>
{
private readonly WonderkingContext _wonderkingContext;
public CharacterNameCheckHandler(WonderkingContext wonderkingContext)
{
_wonderkingContext = wonderkingContext;
}
public async Task HandleAsync(CharacterNameCheckPacket packet, TcpSession session)
{
var isTaken = await _wonderkingContext.Characters.AnyAsync(c => c.Name == packet.Name);
var responsePacket = new CharacterNameCheckPacketResponse { IsTaken = isTaken };
if (session is AuthSession authSession)
{
await authSession.SendAsync(responsePacket);
}
}
}