From 16673ef3c001f8133ebf5d5d0a34f11bb1839f11 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Thu, 16 Nov 2023 17:55:36 +0100 Subject: [PATCH] feat: implement character deletion --- .../CharacterDeletionHandler.cs | 41 +++++++++++++++++++ Server/Program.cs | 1 - .../Packets/Incoming/CharacterDeletePacket.cs | 24 +++++++++++ Wonderking/Packets/OperationCode.cs | 1 + .../Outgoing/CharacterDeleteResponsePacket.cs | 19 +++++++++ .../Outgoing/CharacterDeletionResponse.cs | 7 ++++ 6 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 Server/PacketHandlers/CharacterDeletionHandler.cs create mode 100644 Wonderking/Packets/Incoming/CharacterDeletePacket.cs create mode 100644 Wonderking/Packets/Outgoing/CharacterDeleteResponsePacket.cs create mode 100644 Wonderking/Packets/Outgoing/CharacterDeletionResponse.cs diff --git a/Server/PacketHandlers/CharacterDeletionHandler.cs b/Server/PacketHandlers/CharacterDeletionHandler.cs new file mode 100644 index 0000000..8a43182 --- /dev/null +++ b/Server/PacketHandlers/CharacterDeletionHandler.cs @@ -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 +{ + 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); + } +} diff --git a/Server/Program.cs b/Server/Program.cs index 3a3659d..cdd05d0 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -23,7 +23,6 @@ var loggerFactory = LoggerFactory.Create(loggingBuilder => loggingBuilder.AddFile("Logs/Server-{Date}.json.log", LogLevel.Trace, isJson: true); loggingBuilder.AddConsole(); }); -builder.Services.AddEntityFrameworkNpgsql(); builder.Services.AddDbContextPool(o => { using var configuration = builder.Configuration; diff --git a/Wonderking/Packets/Incoming/CharacterDeletePacket.cs b/Wonderking/Packets/Incoming/CharacterDeletePacket.cs new file mode 100644 index 0000000..545c524 --- /dev/null +++ b/Wonderking/Packets/Incoming/CharacterDeletePacket.cs @@ -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 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(); + } +} diff --git a/Wonderking/Packets/OperationCode.cs b/Wonderking/Packets/OperationCode.cs index 28967dd..d7beb64 100644 --- a/Wonderking/Packets/OperationCode.cs +++ b/Wonderking/Packets/OperationCode.cs @@ -11,6 +11,7 @@ public enum OperationCode : ushort CharacterCreation = 15, CharacterCreationResponse = 15, CharacterDeletion = 16, + CharacterDeletionResponse = 16, CharacterSelection = 17, CharacterSelectionSetGuildName = 19, } diff --git a/Wonderking/Packets/Outgoing/CharacterDeleteResponsePacket.cs b/Wonderking/Packets/Outgoing/CharacterDeleteResponsePacket.cs new file mode 100644 index 0000000..72392c3 --- /dev/null +++ b/Wonderking/Packets/Outgoing/CharacterDeleteResponsePacket.cs @@ -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 data = stackalloc byte[1]; + data[0] = (byte)Response; + return data.ToArray(); + } +} diff --git a/Wonderking/Packets/Outgoing/CharacterDeletionResponse.cs b/Wonderking/Packets/Outgoing/CharacterDeletionResponse.cs new file mode 100644 index 0000000..8676416 --- /dev/null +++ b/Wonderking/Packets/Outgoing/CharacterDeletionResponse.cs @@ -0,0 +1,7 @@ +namespace Wonderking.Packets.Outgoing; + +public enum CharacterDeletionResponse : byte +{ + Ok = 0, + MakeClientStuck = 1, +}