From d610c7b531a24406a55ee6beb4eae2ecd1198596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 30 Dec 2021 10:58:10 +0100 Subject: [PATCH] feat: add pinit packet --- .../Packets/Server/Groups/PinitPacket.cs | 27 +++++++ .../Packets/Server/Groups/PinitSubPacket.cs | 48 +++++++++++++ .../Packets/PinitPacketConverterTest.cs | 72 +++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 Core/NosSmooth.Packets/Packets/Server/Groups/PinitPacket.cs create mode 100644 Core/NosSmooth.Packets/Packets/Server/Groups/PinitSubPacket.cs create mode 100644 Tests/NosSmooth.Packets.Tests/Converters/Packets/PinitPacketConverterTest.cs diff --git a/Core/NosSmooth.Packets/Packets/Server/Groups/PinitPacket.cs b/Core/NosSmooth.Packets/Packets/Server/Groups/PinitPacket.cs new file mode 100644 index 0000000..e318b8b --- /dev/null +++ b/Core/NosSmooth.Packets/Packets/Server/Groups/PinitPacket.cs @@ -0,0 +1,27 @@ +// +// PinitPacket.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using NosSmooth.Packets.Attributes; + +namespace NosSmooth.Packets.Packets.Server.Groups; + +/// +/// Sent for initialization of pets and groups. +/// +/// +/// Contains pet and group information. +/// +/// +/// +[GenerateSerializer] +[PacketHeader("pinit", PacketSource.Server)] +public record PinitPacket +( + [PacketIndex(0)] int GroupSize, + [PacketListIndex(1, ListSeparator = ' ', InnerSeparator = '|')] + IReadOnlyList PinitSubPackets +) : IPacket; \ No newline at end of file diff --git a/Core/NosSmooth.Packets/Packets/Server/Groups/PinitSubPacket.cs b/Core/NosSmooth.Packets/Packets/Server/Groups/PinitSubPacket.cs new file mode 100644 index 0000000..4ed9a9a --- /dev/null +++ b/Core/NosSmooth.Packets/Packets/Server/Groups/PinitSubPacket.cs @@ -0,0 +1,48 @@ +// +// PinitSubPacket.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosCore.Shared.Enumerations; +using NosSmooth.Packets.Attributes; + +namespace NosSmooth.Packets.Packets.Server.Groups; + +/// +/// Sub packet of containing information +/// about one of the group members. +/// +/// The type of the entity. +/// The id of the entity. +/// The position in the group. +/// The level of the entity. +/// The name of the entity. +/// Unknown. +/// The VNum of the pet for pets. +/// The race of the entity. +/// The morph of the entity. +/// The hero level of the entity. +[GenerateSerializer] +[PacketHeader(null, PacketSource.Server)] +public record PinitSubPacket +( + [PacketIndex(0)] + VisualType EntityType, + [PacketIndex(1)] + long EntityId, + [PacketIndex(2)] + int GroupPosition, + [PacketIndex(3)] + byte Level, + [PacketIndex(4)] + string? Name, + [PacketIndex(5)] + int Unknown, + [PacketIndex(6)] + long VNum, + [PacketIndex(7)] + short Race, + [PacketIndex(8)] + short Morph +) : IPacket; \ No newline at end of file diff --git a/Tests/NosSmooth.Packets.Tests/Converters/Packets/PinitPacketConverterTest.cs b/Tests/NosSmooth.Packets.Tests/Converters/Packets/PinitPacketConverterTest.cs new file mode 100644 index 0000000..3c66989 --- /dev/null +++ b/Tests/NosSmooth.Packets.Tests/Converters/Packets/PinitPacketConverterTest.cs @@ -0,0 +1,72 @@ +// +// PinitPacketConverterTest.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.Extensions.DependencyInjection; +using NosCore.Shared.Enumerations; +using NosSmooth.Packets.Attributes; +using NosSmooth.Packets.Converters; +using NosSmooth.Packets.Extensions; +using NosSmooth.Packets.Packets.Server.Groups; +using NosSmooth.Packets.Packets.Server.Map; +using Xunit; + +namespace NosSmooth.Packets.Tests.Converters.Packets; + +/// +/// Tests . +/// +public class PinitPacketConverterTest +{ + private readonly IPacketSerializer _packetSerializer; + + /// + /// Initializes a new instance of the class. + /// + public PinitPacketConverterTest() + { + var provider = new ServiceCollection() + .AddSingleton() + .AddPacketSerialization() + .BuildServiceProvider(); + + _packetSerializer = provider.GetRequiredService(); + } + + /// + /// Tests that the converter serializes mv packet correctly. + /// + [Fact] + public void Converter_Serialization_SerializesCorrectly() + { + var packet = new PinitPacket(2, new[] + { + new PinitSubPacket(VisualType.Npc, 345377, 0, 83, "Kliff", -1, 319, 1, 0), + new PinitSubPacket(VisualType.Npc, 345384, 1, 83, "@", -1, 2105, 0, 0) + }); + var result = _packetSerializer.Serialize(packet); + Assert.True(result.IsSuccess); + + Assert.Equal("pinit 2 2|345377|0|83|Kliff|-1|319|1|0 2|345384|1|83|@|-1|2105|0|0", result.Entity); + } + + /// + /// Tests that the converter serializes mv packet correctly. + /// + [Fact] + public void Converter_Deserialization_DeserializesCorrectly() + { + var packetString = "pinit 2 2|345377|0|83|Kliff|-1|319|1|0 2|345384|1|83|@|-1|2105|0|0"; + var result = _packetSerializer.Deserialize(packetString, PacketSource.Server); + Assert.True(result.IsSuccess); + + var actualPacket = new PinitPacket(2, new[] + { + new PinitSubPacket(VisualType.Npc, 345377, 0, 83, "Kliff", -1, 319, 1, 0), + new PinitSubPacket(VisualType.Npc, 345384, 1, 83, "@", -1, 2105, 0, 0) + }); + Assert.Equal(result.Entity, actualPacket); + } +} \ No newline at end of file -- 2.49.0