// // UnsafeInventoryApi.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.Data; using NosSmooth.Core.Client; using NosSmooth.Core.Contracts; using NosSmooth.Game.Data.Entities; using NosSmooth.Game.Data.Inventory; using NosSmooth.Game.Events.Entities; using NosSmooth.Packets.Client.Inventory; using NosSmooth.Packets.Enums.Inventory; using NosSmooth.Packets.Server.Maps; using OneOf.Types; using Remora.Results; namespace NosSmooth.Game.Apis.Unsafe; /// /// Packet api for managing items in inventory. /// public class UnsafeInventoryApi { private readonly INostaleClient _client; private readonly Contractor _contractor; /// /// Initializes a new instance of the class. /// /// The nostale client. /// The contractor. public UnsafeInventoryApi(INostaleClient client, Contractor contractor) { _client = client; _contractor = contractor; } /// /// Drop the given item. /// /// The bag where the item is located. /// The slot the item is at. /// The amount to drop. /// The cancellation token used for cancelling the operation. /// A result that may or may not have succeeded. public Task DropItemAsync ( BagType bag, short slot, short amount, CancellationToken ct = default ) => _client.SendPacketAsync(new PutPacket(bag, slot, amount), ct); /// /// Creates a contract for dropping an item. /// Returns the ground item that was thrown on the ground. /// /// The inventory bag. /// The inventory slot. /// The amount to drop. /// A contract representing the drop operation. public IContract ContractDropItem ( BagType bag, InventorySlot slot, short amount ) { // TODO: confirm dialog. return new ContractBuilder(_contractor, DefaultStates.None) .SetMoveAction ( DefaultStates.None, async (_, ct) => { await DropItemAsync(bag, slot.Slot, amount, ct); return true; }, DefaultStates.Requested ) .SetMoveFilter ( DefaultStates.Requested, data => data.Item.Amount == amount && data.Item.VNum == slot.Item?.ItemVNum, DefaultStates.ResponseObtained ) .SetFillData ( DefaultStates.Requested, data => data.Item ) .Build(); } /// /// Move the given item within one bag. /// /// The bag the item is in. /// The source slot to move the item from. /// The destination slot to move the item to. /// The amount to move. /// The cancellation token used for cancelling the operation. /// A result that may or may not have succeeded. public Task MoveItemAsync ( BagType bag, short sourceSlot, short destinationSlot, short amount, CancellationToken ct = default ) => MoveItemAsync ( bag, sourceSlot, bag, destinationSlot, amount, ct ); /// /// Move an item from the given source bag and slot to the given destination bag and slot. /// /// The bag the item is in. /// The source slot to move the item from. /// The destination bag to move the item to. /// The destination slot to move the item to. /// The amount to move. /// The cancellation token used for cancelling the operation. /// A result that may or may not have succeeded. public Task MoveItemAsync ( BagType sourceBag, short sourceSlot, BagType destinationBag, short destinationSlot, short amount, CancellationToken ct = default ) { if (sourceBag == destinationBag) { return _client.SendPacketAsync(new MviPacket(sourceBag, sourceSlot, amount, destinationSlot), ct); } return _client.SendPacketAsync(new MvePacket(sourceBag, sourceSlot, destinationBag, destinationSlot), ct); } }