~ruther/NosSmooth

ref: 1c72fa321b5e1dec1e3f9b8bf1d9c85c7f7ec983 NosSmooth/Core/NosSmooth.Game/Apis/Unsafe/UnsafeInventoryApi.cs -rw-r--r-- 5.2 KiB
1c72fa32 — Rutherther Merge pull request #55 from Rutherther/feat/contracts 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
//  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;

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

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

    /// <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 Task<Result> DropItemAsync
    (
        BagType bag,
        short slot,
        short amount,
        CancellationToken ct = default
    )
        => _client.SendPacketAsync(new PutPacket(bag, slot, amount), ct);

    /// <summary>
    /// Creates a contract for dropping an item.
    /// Returns the ground item that was thrown on the ground.
    /// </summary>
    /// <param name="bag">The inventory bag.</param>
    /// <param name="slot">The inventory slot.</param>
    /// <param name="amount">The amount to drop.</param>
    /// <returns>A contract representing the drop operation.</returns>
    public IContract<GroundItem, DefaultStates> ContractDropItem
    (
        BagType bag,
        InventorySlot slot,
        short amount
    )
    {
        // TODO: confirm dialog.
        return new ContractBuilder<GroundItem, DefaultStates, NoErrors>(_contractor, DefaultStates.None)
            .SetMoveAction
            (
                DefaultStates.None,
                async (_, ct) =>
                {
                    await DropItemAsync(bag, slot.Slot, amount, ct);
                    return true;
                },
                DefaultStates.Requested
            )
            .SetMoveFilter<ItemDroppedEvent>
            (
                DefaultStates.Requested,
                data => data.Item.Amount == amount && data.Item.VNum == slot.Item?.ItemVNum,
                DefaultStates.ResponseObtained
            )
            .SetFillData<ItemDroppedEvent>
            (
                DefaultStates.Requested,
                data => data.Item
            )
            .Build();
    }

    /// <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 Task<Result> MoveItemAsync
    (
        BagType bag,
        short sourceSlot,
        short destinationSlot,
        short amount,
        CancellationToken ct = default
    )
        => 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 Task<Result> 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);
    }
}
Do not follow this link