feat: implement character deletion

This commit is contained in:
Timothy Schenk 2023-11-16 17:55:36 +01:00
parent 87955f3ba3
commit 16673ef3c0
6 changed files with 92 additions and 1 deletions

View file

@ -0,0 +1,41 @@
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)
{
using var authSession = session as AuthSession;
var response = new CharacterDeleteResponsePacket { Response = CharacterDeletionResponse.MakeClientStuck };
if (authSession == null)
{
return;
}
var character = await _wonderkingContext.Characters.FirstOrDefaultAsync(x => x.Name == packet.Name)
.ConfigureAwait(false);
if (character == null)
{
authSession.Send(response);
return;
}
_wonderkingContext.Characters.Remove(character);
await _wonderkingContext.SaveChangesAsync().ConfigureAwait(false);
response.Response = CharacterDeletionResponse.Ok;
authSession.Send(response);
}
}

View file

@ -23,7 +23,6 @@ var loggerFactory = LoggerFactory.Create(loggingBuilder =>
loggingBuilder.AddFile("Logs/Server-{Date}.json.log", LogLevel.Trace, isJson: true); loggingBuilder.AddFile("Logs/Server-{Date}.json.log", LogLevel.Trace, isJson: true);
loggingBuilder.AddConsole(); loggingBuilder.AddConsole();
}); });
builder.Services.AddEntityFrameworkNpgsql();
builder.Services.AddDbContextPool<WonderkingContext>(o => builder.Services.AddDbContextPool<WonderkingContext>(o =>
{ {
using var configuration = builder.Configuration; using var configuration = builder.Configuration;

View file

@ -0,0 +1,24 @@
using System.Text;
namespace Wonderking.Packets.Incoming;
[PacketId(OperationCode.CharacterDeletion)]
public class CharacterDeletePacket : IPacket
{
public byte Slot { get; set; }
public string Name { get; set; }
public uint Unknown { get; set; }
public void Deserialize(byte[] data)
{
Span<byte> span = data;
Slot = span[0];
Name = Encoding.ASCII.GetString(span.Slice(1, 20)).TrimEnd('\0').TrimEnd('\n').TrimEnd('\0');
Unknown = BitConverter.ToUInt32(span.Slice(21, 4));
}
public byte[] Serialize()
{
throw new NotSupportedException();
}
}

View file

@ -11,6 +11,7 @@ public enum OperationCode : ushort
CharacterCreation = 15, CharacterCreation = 15,
CharacterCreationResponse = 15, CharacterCreationResponse = 15,
CharacterDeletion = 16, CharacterDeletion = 16,
CharacterDeletionResponse = 16,
CharacterSelection = 17, CharacterSelection = 17,
CharacterSelectionSetGuildName = 19, CharacterSelectionSetGuildName = 19,
} }

View file

@ -0,0 +1,19 @@
namespace Wonderking.Packets.Outgoing;
[PacketId(OperationCode.CharacterDeletionResponse)]
public class CharacterDeleteResponsePacket : IPacket
{
public required CharacterDeletionResponse Response { get; set; }
public void Deserialize(byte[] data)
{
throw new NotImplementedException();
}
public byte[] Serialize()
{
Span<byte> data = stackalloc byte[1];
data[0] = (byte)Response;
return data.ToArray();
}
}

View file

@ -0,0 +1,7 @@
namespace Wonderking.Packets.Outgoing;
public enum CharacterDeletionResponse : byte
{
Ok = 0,
MakeClientStuck = 1,
}