Skip to content

Commit

Permalink
Closes #3378
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Feb 9, 2025
1 parent db70633 commit 62ce58e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ArchiSteamFarm/IPC/Controllers/Api/BotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using ArchiSteamFarm.IPC.Responses;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Steam.Data;
using ArchiSteamFarm.Steam.Storage;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -277,6 +278,57 @@ public async Task<ActionResult<GenericResponse>> InputPost(string botNames, [Fro
return Ok(results.All(static result => result) ? new GenericResponse(true) : new GenericResponse(false, Strings.WarningFailed));
}

[EndpointSummary("Fetches inventory of given bots")]
[HttpGet("{botNames:required}/Inventory/{appID}/{contextID}")]
[ProducesResponseType<GenericResponse<IReadOnlyDictionary<string, BotInventoryResponse>>>((int) HttpStatusCode.OK)]
[ProducesResponseType<GenericResponse>((int) HttpStatusCode.BadRequest)]
public async Task<ActionResult<GenericResponse>> InventoryGet(string botNames, uint appID, ulong contextID) {
ArgumentException.ThrowIfNullOrEmpty(botNames);

if (appID == 0) {
return BadRequest(new GenericResponse(false, Strings.FormatErrorIsInvalid(nameof(appID))));
}

if (contextID == 0) {
return BadRequest(new GenericResponse(false, Strings.FormatErrorIsInvalid(nameof(contextID))));
}

HashSet<Bot>? bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0)) {
return BadRequest(new GenericResponse(false, Strings.FormatBotNotFound(botNames)));
}

IList<(HashSet<Asset>? Result, string Message)> results = await Utilities.InParallel(bots.Select(bot => bot.Actions.GetInventory(appID, contextID))).ConfigureAwait(false);

Dictionary<string, BotInventoryResponse> result = new(bots.Count, Bot.BotsComparer);

foreach (Bot bot in bots) {
(HashSet<Asset>? inventory, _) = results[result.Count];

if (inventory == null) {
result[bot.BotName] = new BotInventoryResponse();

continue;
}

HashSet<CEcon_Asset> assets = new(inventory.Count);
HashSet<CEconItem_Description> descriptions = [];

foreach (Asset asset in inventory) {
assets.Add(asset.Body);

if (asset.Description != null) {
descriptions.Add(asset.Description.Body);
}
}

result[bot.BotName] = new BotInventoryResponse(assets, descriptions);
}

return Ok(new GenericResponse<IReadOnlyDictionary<string, BotInventoryResponse>>(result));
}

[EndpointSummary("Pauses given bots")]
[HttpPost("{botNames:required}/Pause")]
[ProducesResponseType<GenericResponse>((int) HttpStatusCode.OK)]
Expand Down
45 changes: 45 additions & 0 deletions ArchiSteamFarm/IPC/Responses/BotInventoryResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2025 Łukasz "JustArchi" Domeradzki
// Contact: [email protected]
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Text.Json.Serialization;
using SteamKit2.Internal;

namespace ArchiSteamFarm.IPC.Responses;

public sealed class BotInventoryResponse {
[Description("Inventory assetsr")]
[JsonInclude]
public ImmutableHashSet<CEcon_Asset>? Assets { get; private init; }

[Description("Descriptions of the inventory assets")]
[JsonInclude]
public ImmutableHashSet<CEconItem_Description>? Descriptions { get; private init; }

internal BotInventoryResponse(IEnumerable<CEcon_Asset>? assets = null, IEnumerable<CEconItem_Description>? descriptions = null) {
Assets = assets?.ToImmutableHashSet();
Descriptions = descriptions?.ToImmutableHashSet();
}
}
28 changes: 28 additions & 0 deletions ArchiSteamFarm/Steam/Interaction/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using ArchiSteamFarm.Plugins.Interfaces;
using ArchiSteamFarm.Steam.Data;
using ArchiSteamFarm.Steam.Exchange;
using ArchiSteamFarm.Steam.Integration;
using ArchiSteamFarm.Steam.Storage;
using ArchiSteamFarm.Storage;
using ArchiSteamFarm.Web;
Expand Down Expand Up @@ -172,6 +173,33 @@ public ulong GetFirstSteamMasterID() {
return (steamOwnerID > 0) && new SteamID(steamOwnerID).IsIndividualAccount ? steamOwnerID : 0;
}

/// <remarks>This action should be used if you require full inventory exclusively, otherwise consider calling <see cref="ArchiHandler.GetMyInventoryAsync" /> instead.</remarks>
[PublicAPI]
public async Task<(HashSet<Asset>? Result, string Message)> GetInventory(uint appID = Asset.SteamAppID, ulong contextID = Asset.SteamCommunityContextID, Func<Asset, bool>? filterFunction = null) {
ArgumentOutOfRangeException.ThrowIfZero(appID);
ArgumentOutOfRangeException.ThrowIfZero(contextID);

if (!Bot.IsConnectedAndLoggedOn) {
return (null, Strings.BotNotConnected);
}

filterFunction ??= static _ => true;

using (await GetTradingLock().ConfigureAwait(false)) {
try {
return (await Bot.ArchiHandler.GetMyInventoryAsync(appID, contextID).Where(item => filterFunction(item)).ToHashSetAsync().ConfigureAwait(false), Strings.Success);
} catch (TimeoutException e) {
Bot.ArchiLogger.LogGenericWarningException(e);

return (null, Strings.FormatWarningFailedWithError(e.Message));
} catch (Exception e) {
Bot.ArchiLogger.LogGenericException(e);

return (null, Strings.FormatWarningFailedWithError(e.Message));
}
}
}

[PublicAPI]
public async Task<Dictionary<uint, LoyaltyRewardDefinition>?> GetRewardItems(IReadOnlyCollection<uint> definitionIDs) {
if ((definitionIDs == null) || (definitionIDs.Count == 0)) {
Expand Down

0 comments on commit 62ce58e

Please sign in to comment.