~ruther/NosSmooth

d610c7b531a24406a55ee6beb4eae2ecd1198596 — František Boháček 3 years ago bf01b2a
feat: add pinit packet
A Core/NosSmooth.Packets/Packets/Server/Groups/PinitPacket.cs => Core/NosSmooth.Packets/Packets/Server/Groups/PinitPacket.cs +27 -0
@@ 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;

/// <summary>
/// Sent for initialization of pets and groups.
/// </summary>
/// <remarks>
/// Contains pet and group information.
/// </remarks>
/// <param name="GroupSize"></param>
/// <param name="PinitSubPackets"></param>
[GenerateSerializer]
[PacketHeader("pinit", PacketSource.Server)]
public record PinitPacket
(
    [PacketIndex(0)] int GroupSize,
    [PacketListIndex(1, ListSeparator = ' ', InnerSeparator = '|')]
    IReadOnlyList<PinitSubPacket> PinitSubPackets
) : IPacket;
\ No newline at end of file

A Core/NosSmooth.Packets/Packets/Server/Groups/PinitSubPacket.cs => Core/NosSmooth.Packets/Packets/Server/Groups/PinitSubPacket.cs +48 -0
@@ 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;

/// <summary>
/// Sub packet of <see cref="PinitSubPacket"/> containing information
/// about one of the group members.
/// </summary>
/// <param name="EntityType">The type of the entity.</param>
/// <param name="EntityId">The id of the entity.</param>
/// <param name="GroupPosition">The position in the group.</param>
/// <param name="Level">The level of the entity.</param>
/// <param name="Name">The name of the entity.</param>
/// <param name="Unknown">Unknown.</param>
/// <param name="VNum">The VNum of the pet for pets.</param>
/// <param name="Race">The race of the entity.</param>
/// <param name="Morph">The morph of the entity.</param>
/// <param name="HeroLevel">The hero level of the entity.</param>
[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

A Tests/NosSmooth.Packets.Tests/Converters/Packets/PinitPacketConverterTest.cs => Tests/NosSmooth.Packets.Tests/Converters/Packets/PinitPacketConverterTest.cs +72 -0
@@ 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;

/// <summary>
/// Tests <see cref="PinitPacketConverter"/>.
/// </summary>
public class PinitPacketConverterTest
{
    private readonly IPacketSerializer _packetSerializer;

    /// <summary>
    /// Initializes a new instance of the <see cref="PinitPacketConverterTest"/> class.
    /// </summary>
    public PinitPacketConverterTest()
    {
        var provider = new ServiceCollection()
            .AddSingleton<TypeConverterRepository>()
            .AddPacketSerialization()
            .BuildServiceProvider();

        _packetSerializer = provider.GetRequiredService<IPacketSerializer>();
    }

    /// <summary>
    /// Tests that the converter serializes mv packet correctly.
    /// </summary>
    [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);
    }

    /// <summary>
    /// Tests that the converter serializes mv packet correctly.
    /// </summary>
    [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

Do not follow this link