~ruther/NosSmooth

092afaf47312b7a5ac76919c9ce4825c420c00a1 — František Boháček 3 years ago 3d723b7
feat: add fc packet
A Core/NosSmooth.Packets/Enums/Act4Mode.cs => Core/NosSmooth.Packets/Enums/Act4Mode.cs +33 -0
@@ 0,0 1,33 @@
//
//  Act4Mode.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.

namespace NosSmooth.Packets.Enums;

/// <summary>
/// The act4 mode.
/// </summary>
public enum Act4Mode
{
    /// <summary>
    /// There is nothing currently running, percentage has to reach 100 % for Mukraju to spawn.
    /// </summary>
    None = 0,

    /// <summary>
    /// Mukraju has been spawned and has to be killed in order to start raid.
    /// </summary>
    Mukraju = 1,

    /// <summary>
    /// Unknown mode.
    /// </summary>
    Unknown = 2,

    /// <summary>
    /// Raid is in progress.
    /// </summary>
    Raid = 3,
}
\ No newline at end of file

A Core/NosSmooth.Packets/Enums/FactionType.cs => Core/NosSmooth.Packets/Enums/FactionType.cs +28 -0
@@ 0,0 1,28 @@
//
//  FactionType.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.

namespace NosSmooth.Packets.Enums;

/// <summary>
/// Faction of an entity.
/// </summary>
public enum FactionType
{
    /// <summary>
    /// No faction.
    /// </summary>
    Neutral = 0,

    /// <summary>
    /// Angel faction.
    /// </summary>
    Angel = 1,

    /// <summary>
    /// Demon faction.
    /// </summary>
    Demon = 2
}
\ No newline at end of file

A Core/NosSmooth.Packets/Packets/Server/Act4/FcPacket.cs => Core/NosSmooth.Packets/Packets/Server/Act4/FcPacket.cs +37 -0
@@ 0,0 1,37 @@
//
//  FcPacket.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 NosSmooth.Packets.Attributes;
using NosSmooth.Packets.Enums;

namespace NosSmooth.Packets.Packets.Server.Act4;

/// <summary>
/// Contains the state of the act4.
/// </summary>
/// <remarks>
/// Sent only in act4. Receiving the packet
/// will make status bar appear in the game.
/// The packet contains all information about
/// the current status such as percentage till Mukraju,
/// type of raid etc.
/// </remarks>
/// <param name="Faction">The faction of the player playing.</param>
/// <param name="MinutesUntilReset">The minutes it will take to reset.</param>
/// <param name="AngelState">The status of angel faction, see <see cref="FcSubPacket"/>.</param>
/// <param name="DemonState">The status of demon faction, see <see cref="FcSubPacket"/>.</param>
[PacketHeader("fc", PacketSource.Server)]
[GenerateSerializer]
public record FcPacket(
    [PacketIndex(0)]
    FactionType Faction,
    [PacketIndex(1)]
    long MinutesUntilReset,
    [PacketIndex(2, InnerSeparator = ' ')]
    FcSubPacket AngelState,
    [PacketIndex(3, InnerSeparator = ' ')]
    FcSubPacket DemonState
) : IPacket;
\ No newline at end of file

A Core/NosSmooth.Packets/Packets/Server/Act4/FcSubPacket.cs => Core/NosSmooth.Packets/Packets/Server/Act4/FcSubPacket.cs +47 -0
@@ 0,0 1,47 @@
//
//  FcSubPacket.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 NosSmooth.Packets.Attributes;
using NosSmooth.Packets.Enums;

namespace NosSmooth.Packets.Packets.Server.Act4;

/// <summary>
/// Sub packet of <see cref="FcPacket"/>, containing
/// either the state of angels or demons
/// </summary>
/// <param name="Percentage">The percentage of the faction.</param>
/// <param name="Mode">The mode </param>
/// <param name="CurrentTime">The time the raid has been open for so far.</param>
/// <param name="TotalTime">The total time the raid will be open for.</param>
/// <param name="IsMorcos">Whether the current raid is Marcos.</param>
/// <param name="IsHatus">Whether the current raid is Hatus.</param>
/// <param name="IsCalvina">Whether the current raid is Calvina.</param>
/// <param name="IsBerios">Whether the current raid is Berios.</param>
/// <param name="Unknown">Unknown value, seems to be always 0.</param>
[GenerateSerializer]
[PacketHeader(null, PacketSource.Server)]
public record FcSubPacket
(
    [PacketIndex(0)]
    short Percentage,
    [PacketIndex(1)]
    Act4Mode Mode,
    [PacketIndex(2)]
    long CurrentTime,
    [PacketIndex(3)]
    long TotalTime,
    [PacketIndex(4)]
    bool IsMorcos,
    [PacketIndex(5)]
    bool IsHatus,
    [PacketIndex(6)]
    bool IsCalvina,
    [PacketIndex(7)]
    bool IsBerios,
    [PacketIndex(8)]
    byte Unknown
) : IPacket;
\ No newline at end of file

A Tests/NosSmooth.Packets.Tests/Converters/Packets/FcPacketConverterTests.cs => Tests/NosSmooth.Packets.Tests/Converters/Packets/FcPacketConverterTests.cs +72 -0
@@ 0,0 1,72 @@
//
//  FcPacketConverterTests.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 NosSmooth.Packets.Attributes;
using NosSmooth.Packets.Converters;
using NosSmooth.Packets.Enums;
using NosSmooth.Packets.Extensions;
using NosSmooth.Packets.Packets.Server.Act4;
using Xunit;

namespace NosSmooth.Packets.Tests.Converters.Packets;

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

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

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

    /// <summary>
    /// Tests that the serialization runs correctly.
    /// </summary>
    [Fact]
    public void Converter_Serialization_SerializesCorrectly()
    {
        var packet = new FcPacket(
            FactionType.Angel,
            15122,
            new FcSubPacket(76, Act4Mode.None, 0, 0, false, false, false, false, 0),
            new FcSubPacket(100, Act4Mode.Raid, 10, 1000, true, false, false, false, 0)
        );
        var packetResult = _packetSerializer.Serialize(packet);
        Assert.True(packetResult.IsSuccess);

        Assert.Equal("fc 1 15122 76 0 0 0 0 0 0 0 0 100 3 10 1000 1 0 0 0 0", packetResult.Entity);
    }

    /// <summary>
    /// Tests that the deserialization runs correctly.
    /// </summary>
    [Fact]
    public void Converter_Deserialization_DeserializesCorrectly()
    {
        var packetResult = _packetSerializer.Deserialize("fc 1 15122 76 0 0 0 0 0 0 0 0 100 3 10 1000 1 0 0 0 0", PacketSource.Server);
        Assert.True(packetResult.IsSuccess);

        var expectedPacket = new FcPacket(
            FactionType.Angel,
            15122,
            new FcSubPacket(76, Act4Mode.None, 0, 0, false, false, false, false, 0),
            new FcSubPacket(100, Act4Mode.Raid, 10, 1000, true, false, false, false, 0)
        );
        Assert.Equal(expectedPacket, packetResult.Entity);
    }
}
\ No newline at end of file

Do not follow this link