feat: implement character deletion
This commit is contained in:
parent
87955f3ba3
commit
16673ef3c0
6 changed files with 92 additions and 1 deletions
41
Server/PacketHandlers/CharacterDeletionHandler.cs
Normal file
41
Server/PacketHandlers/CharacterDeletionHandler.cs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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<WonderkingContext>(o =>
|
||||
{
|
||||
using var configuration = builder.Configuration;
|
||||
|
|
24
Wonderking/Packets/Incoming/CharacterDeletePacket.cs
Normal file
24
Wonderking/Packets/Incoming/CharacterDeletePacket.cs
Normal 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();
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ public enum OperationCode : ushort
|
|||
CharacterCreation = 15,
|
||||
CharacterCreationResponse = 15,
|
||||
CharacterDeletion = 16,
|
||||
CharacterDeletionResponse = 16,
|
||||
CharacterSelection = 17,
|
||||
CharacterSelectionSetGuildName = 19,
|
||||
}
|
||||
|
|
19
Wonderking/Packets/Outgoing/CharacterDeleteResponsePacket.cs
Normal file
19
Wonderking/Packets/Outgoing/CharacterDeleteResponsePacket.cs
Normal 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();
|
||||
}
|
||||
}
|
7
Wonderking/Packets/Outgoing/CharacterDeletionResponse.cs
Normal file
7
Wonderking/Packets/Outgoing/CharacterDeletionResponse.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Wonderking.Packets.Outgoing;
|
||||
|
||||
public enum CharacterDeletionResponse : byte
|
||||
{
|
||||
Ok = 0,
|
||||
MakeClientStuck = 1,
|
||||
}
|
Loading…
Reference in a new issue