~ruther/NosSmooth

4098fc38ac0b183a2e5a49ce72fb4be174da361f — Rutherther 2 years ago cc051e9
feat(game): add inventory api
A Core/NosSmooth.Game/Apis/NostaleInventoryPacketApi.cs => Core/NosSmooth.Game/Apis/NostaleInventoryPacketApi.cs +101 -0
@@ 0,0 1,101 @@
//
//  NostaleInventoryPacketApi.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.Core.Client;
using NosSmooth.Packets.Client.Inventory;
using NosSmooth.Packets.Enums.Inventory;
using Remora.Results;

namespace NosSmooth.Game.Apis;

/// <summary>
/// Packet api for managing items in inventory.
/// </summary>
public class NostaleInventoryPacketApi
{
    private readonly INostaleClient _client;

    /// <summary>
    /// Initializes a new instance of the <see cref="NostaleInventoryPacketApi"/> class.
    /// </summary>
    /// <param name="client">The nostale client.</param>
    public NostaleInventoryPacketApi(INostaleClient client)
    {
        _client = client;
    }

    /// <summary>
    /// Drop the given item.
    /// </summary>
    /// <param name="bag">The bag where the item is located.</param>
    /// <param name="slot">The slot the item is at.</param>
    /// <param name="amount">The amount to drop.</param>
    /// <param name="ct">The cancellation token used for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public async Task<Result> DropItemAsync
    (
        BagType bag,
        short slot,
        short amount,
        CancellationToken ct = default
    )
        => await _client.SendPacketAsync(new PutPacket(bag, slot, amount), ct);

    /// <summary>
    /// Move the given item within one bag.
    /// </summary>
    /// <param name="bag">The bag the item is in.</param>
    /// <param name="sourceSlot">The source slot to move the item from.</param>
    /// <param name="destinationSlot">The destination slot to move the item to.</param>
    /// <param name="amount">The amount to move.</param>
    /// <param name="ct">The cancellation token used for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public async Task<Result> MoveItemAsync
    (
        BagType bag,
        short sourceSlot,
        short destinationSlot,
        short amount,
        CancellationToken ct = default
    )
        => await MoveItemAsync
        (
            bag,
            sourceSlot,
            bag,
            destinationSlot,
            amount,
            ct
        );

    /// <summary>
    /// Move an item from the given source bag and slot to the given destination bag and slot.
    /// </summary>
    /// <param name="sourceBag">The bag the item is in.</param>
    /// <param name="sourceSlot">The source slot to move the item from.</param>
    /// <param name="destinationBag">The destination bag to move the item to.</param>
    /// <param name="destinationSlot">The destination slot to move the item to.</param>
    /// <param name="amount">The amount to move.</param>
    /// <param name="ct">The cancellation token used for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public async Task<Result> MoveItemAsync
    (
        BagType sourceBag,
        short sourceSlot,
        BagType destinationBag,
        short destinationSlot,
        short amount,
        CancellationToken ct = default
    )
    {
        if (sourceBag == destinationBag)
        {
            return await _client.SendPacketAsync(new MviPacket(sourceBag, sourceSlot, amount, destinationSlot), ct);
        }

        return await _client.SendPacketAsync(new MvePacket(sourceBag, sourceSlot, destinationBag, destinationSlot), ct);
    }
}
\ No newline at end of file

M Core/NosSmooth.Game/Data/Info/Position.cs => Core/NosSmooth.Game/Data/Info/Position.cs +4 -0
@@ 89,4 89,8 @@ public record struct Position(short X, short Y)
    {
        return new Position((short)(left.X - right.X), (short)(left.Y - right.Y));
    }

    /// <inheritdoc />
    public override string ToString()
        => $"{X}, {Y}";
}
\ No newline at end of file

A Core/NosSmooth.Game/Errors/NotInRangeError.cs => Core/NosSmooth.Game/Errors/NotInRangeError.cs +18 -0
@@ 0,0 1,18 @@
//
//  NotInRangeError.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.Game.Data.Info;
using Remora.Results;

namespace NosSmooth.Game.Errors;

public record NotInRangeError
(
    string Entity,
    Position EntityPosition,
    Position TargetPosition,
    short Range
) : ResultError($"Entity {Entity} ({EntityPosition}) is not in range ({Range}) of ({TargetPosition}).");
\ No newline at end of file

A Core/NosSmooth.Game/Errors/NotInitializedError.cs => Core/NosSmooth.Game/Errors/NotInitializedError.cs +15 -0
@@ 0,0 1,15 @@
//
//  NotInitializedError.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 Remora.Results;

namespace NosSmooth.Game.Errors;

/// <summary>
/// A game field that had to be accessed was not initialized.
/// </summary>
/// <param name="Field">The field.</param>
public record NotInitializedError(string Field) : ResultError($"The {Field} is not initialized.");
\ No newline at end of file

M Core/NosSmooth.Game/NosSmooth.Game.csproj => Core/NosSmooth.Game/NosSmooth.Game.csproj +0 -4
@@ 16,10 16,6 @@ Move Friends, Group, Family, Inventory to Game</PackageReleaseNotes>
    </PropertyGroup>

    <ItemGroup>
      <Folder Include="Apis" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\..\Data\NosSmooth.Data.Abstractions\NosSmooth.Data.Abstractions.csproj" />
      <ProjectReference Include="..\NosSmooth.Core\NosSmooth.Core.csproj" />
    </ItemGroup>

Do not follow this link