Esempio n. 1
0
    /// <inheritdoc/>
    public async ValueTask <bool> TryTakeAllAsync(IStorage anotherStorage)
    {
        var result = true;

        var itemsWithSlots = anotherStorage.Items.Select(item => (item.ItemSlot, Item: item)).ToList();

        try
        {
            foreach (var item in itemsWithSlots)
            {
                await anotherStorage.RemoveItemAsync(item.Item).ConfigureAwait(false);

                if (!await this.AddItemAsync(item.Item).ConfigureAwait(false))
                {
                    result = false;
                }
            }
        }
        finally
        {
            if (!result)
            {
                foreach (var item in itemsWithSlots)
                {
                    await this.RemoveItemAsync(item.Item).ConfigureAwait(false);

                    await anotherStorage.AddItemAsync(item.ItemSlot, item.Item).ConfigureAwait(false);
                }
            }
        }

        return(result);
    }