Files
ArchiSteamFarm/ArchiSteamFarm/Trading.cs

717 lines
32 KiB
C#
Raw Normal View History

2019-02-16 17:34:17 +01:00
// _ _ _ ____ _ _____
2017-11-18 17:27:06 +01:00
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2019-01-14 19:11:17 +01:00
// |
// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki
2018-07-27 04:52:14 +02:00
// Contact: JustArchi@JustArchi.net
2019-01-14 19:11:17 +01:00
// |
2018-07-27 04:52:14 +02:00
// 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
2019-01-14 19:11:17 +01:00
// |
2018-07-27 04:52:14 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2019-01-14 19:11:17 +01:00
// |
2018-07-27 04:52:14 +02:00
// 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.
2015-10-28 19:21:27 +01:00
2016-04-12 19:12:45 +02:00
using System;
2015-10-28 19:21:27 +01:00
using System.Collections.Generic;
2019-04-10 22:26:49 +02:00
using System.Collections.Immutable;
2020-11-14 22:37:00 +01:00
using System.ComponentModel;
using System.Globalization;
2016-05-13 06:32:42 +02:00
using System.Linq;
using System.Net.Http;
2015-10-25 06:16:50 +01:00
using System.Threading;
using System.Threading.Tasks;
2018-09-08 01:03:55 +02:00
using ArchiSteamFarm.Collections;
2017-12-14 08:23:17 +01:00
using ArchiSteamFarm.Json;
2017-01-06 15:32:12 +01:00
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Plugins;
using JetBrains.Annotations;
2019-12-29 19:21:31 +01:00
using SteamKit2;
2015-10-25 06:16:50 +01:00
namespace ArchiSteamFarm {
public sealed class Trading : IDisposable {
2020-02-24 21:11:54 +01:00
internal const byte MaxItemsPerTrade = byte.MaxValue; // This is decided upon various factors, mainly stability of Steam servers when dealing with huge trade offers
internal const byte MaxTradesPerAccount = 5; // This is limit introduced by Valve
2016-01-22 10:13:02 +01:00
2015-10-31 05:27:30 +01:00
private readonly Bot Bot;
2020-11-14 22:37:00 +01:00
private readonly ConcurrentHashSet<ulong> HandledTradeOfferIDs = new();
private readonly SemaphoreSlim TradesSemaphore = new(1, 1);
2016-03-15 04:51:51 +01:00
2016-11-20 00:03:27 +01:00
private bool ParsingScheduled;
2015-10-25 06:16:50 +01:00
internal Trading(Bot bot) => Bot = bot ?? throw new ArgumentNullException(nameof(bot));
2015-10-25 06:16:50 +01:00
public void Dispose() => TradesSemaphore.Dispose();
2020-10-21 18:48:13 +02:00
[PublicAPI]
public static Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), List<uint>> GetInventorySets(IReadOnlyCollection<Steam.Asset> inventory) {
if ((inventory == null) || (inventory.Count == 0)) {
throw new ArgumentNullException(nameof(inventory));
}
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> sets = GetInventoryState(inventory);
return sets.ToDictionary(set => set.Key, set => set.Value.Values.OrderBy(amount => amount).ToList());
}
2019-01-17 16:48:38 +01:00
[PublicAPI]
2019-02-02 22:54:23 +01:00
public static bool IsFairExchange(IReadOnlyCollection<Steam.Asset> itemsToGive, IReadOnlyCollection<Steam.Asset> itemsToReceive) {
2020-11-14 22:37:00 +01:00
if ((itemsToGive == null) || (itemsToGive.Count == 0)) {
throw new ArgumentNullException(nameof(itemsToGive));
2019-01-17 16:48:38 +01:00
}
2020-11-14 22:37:00 +01:00
if ((itemsToReceive == null) || (itemsToReceive.Count == 0)) {
throw new ArgumentNullException(nameof(itemsToReceive));
}
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), uint> itemsToGiveAmounts = new();
2019-01-17 16:48:38 +01:00
foreach (Steam.Asset item in itemsToGive) {
2019-02-02 22:54:23 +01:00
(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);
itemsToGiveAmounts[key] = itemsToGiveAmounts.TryGetValue(key, out uint amount) ? amount + item.Amount : item.Amount;
2019-01-17 16:48:38 +01:00
}
2020-11-14 22:37:00 +01:00
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), uint> itemsToReceiveAmounts = new();
2019-01-17 16:48:38 +01:00
foreach (Steam.Asset item in itemsToReceive) {
2019-02-02 22:54:23 +01:00
(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);
itemsToReceiveAmounts[key] = itemsToReceiveAmounts.TryGetValue(key, out uint amount) ? amount + item.Amount : item.Amount;
2019-01-17 16:48:38 +01:00
}
2019-02-02 22:54:23 +01:00
// Ensure that amount of items to give is at least amount of items to receive (per all fairness factors)
foreach (((uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) key, uint amountToGive) in itemsToGiveAmounts) {
if (!itemsToReceiveAmounts.TryGetValue(key, out uint amountToReceive) || (amountToGive > amountToReceive)) {
2019-01-17 16:48:38 +01:00
return false;
}
}
return true;
}
2019-01-14 21:50:23 +01:00
[PublicAPI]
2020-11-14 22:37:00 +01:00
public static bool IsTradeNeutralOrBetter(HashSet<Steam.Asset> inventory, IReadOnlyCollection<Steam.Asset> itemsToGive, IReadOnlyCollection<Steam.Asset> itemsToReceive) {
if ((inventory == null) || (inventory.Count == 0)) {
throw new ArgumentNullException(nameof(inventory));
}
if ((itemsToGive == null) || (itemsToGive.Count == 0)) {
throw new ArgumentNullException(nameof(itemsToGive));
}
if ((itemsToReceive == null) || (itemsToReceive.Count == 0)) {
throw new ArgumentNullException(nameof(itemsToReceive));
2019-01-14 21:50:23 +01:00
}
// Input of this function is items we're expected to give/receive and our inventory (limited to realAppIDs of itemsToGive/itemsToReceive)
// The objective is to determine whether the new state is beneficial (or at least neutral) towards us
// There are a lot of factors involved here - different realAppIDs, different item types, possibility of user overpaying and more
// All of those cases should be verified by our unit tests to ensure that the logic here matches all possible cases, especially those that were incorrectly handled previously
// Firstly we get initial sets state of our inventory
2019-02-02 22:54:23 +01:00
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), List<uint>> initialSets = GetInventorySets(inventory);
2019-01-14 21:50:23 +01:00
// Once we have initial state, we remove items that we're supposed to give from our inventory
// This loop is a bit more complex due to the fact that we might have a mix of the same item splitted into different amounts
foreach (Steam.Asset itemToGive in itemsToGive) {
uint amountToGive = itemToGive.Amount;
2020-11-14 22:37:00 +01:00
HashSet<Steam.Asset> itemsToRemove = new();
2019-01-14 21:50:23 +01:00
// Keep in mind that ClassID is unique only within appID scope - we can do it like this because we're not dealing with non-Steam items here (otherwise we'd need to check appID too)
foreach (Steam.Asset item in inventory.Where(item => item.ClassID == itemToGive.ClassID)) {
if (amountToGive >= item.Amount) {
itemsToRemove.Add(item);
amountToGive -= item.Amount;
} else {
item.Amount -= amountToGive;
amountToGive = 0;
}
if (amountToGive == 0) {
break;
}
}
if (amountToGive > 0) {
2020-11-14 22:37:00 +01:00
throw new InvalidOperationException(nameof(amountToGive));
2019-01-14 21:50:23 +01:00
}
if (itemsToRemove.Count > 0) {
inventory.ExceptWith(itemsToRemove);
}
}
// Now we can add items that we're supposed to receive, this one doesn't require advanced amounts logic since we can just add items regardless
foreach (Steam.Asset itemToReceive in itemsToReceive) {
inventory.Add(itemToReceive);
}
// Now we can get final sets state of our inventory after the exchange
2019-02-02 22:54:23 +01:00
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), List<uint>> finalSets = GetInventorySets(inventory);
2019-01-14 21:50:23 +01:00
// Once we have both states, we can check overall fairness
2019-02-02 22:54:23 +01:00
foreach (((uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) set, List<uint> beforeAmounts) in initialSets) {
if (!finalSets.TryGetValue(set, out List<uint>? afterAmounts)) {
2019-01-26 18:17:07 +01:00
// If we have no info about this set, then it has to be a bad one
return false;
2019-01-26 18:14:07 +01:00
}
2019-01-14 21:50:23 +01:00
// If amount of unique items in the set decreases, this is always a bad trade (e.g. 1 1 -> 0 2)
if (afterAmounts.Count < beforeAmounts.Count) {
return false;
}
// If amount of unique items in the set increases, this is always a good trade (e.g. 0 2 -> 1 1)
if (afterAmounts.Count > beforeAmounts.Count) {
continue;
}
// At this point we're sure that amount of unique items stays the same, so we can evaluate actual sets
// We make use of the fact that our amounts are already sorted in ascending order, so we can just take the first value instead of calculating ourselves
uint beforeSets = beforeAmounts[0];
uint afterSets = afterAmounts[0];
// If amount of our sets for this game decreases, this is always a bad trade (e.g. 2 2 2 -> 3 2 1)
if (afterSets < beforeSets) {
return false;
}
// If amount of our sets for this game increases, this is always a good trade (e.g. 3 2 1 -> 2 2 2)
if (afterSets > beforeSets) {
continue;
}
// At this point we're sure that both number of unique items in the set stays the same, as well as number of our actual sets
// We need to ensure set progress here and keep in mind overpaying, so we'll calculate neutrality as a difference in amounts at appropriate indexes
// Neutrality can't reach value below 0 at any single point of calculation, as that would imply a loss of progress even if we'd end up with a positive value by the end
int neutrality = 0;
for (byte i = 0; i < afterAmounts.Count; i++) {
neutrality += (int) (afterAmounts[i] - beforeAmounts[i]);
if (neutrality < 0) {
return false;
}
}
}
// If we didn't find any reason above to reject this trade, it's at least neutral+ for us - it increases our progress towards badge completion
return true;
}
2019-02-02 22:54:23 +01:00
internal static (Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> FullState, Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> TradableState) GetDividedInventoryState(IReadOnlyCollection<Steam.Asset> inventory) {
if ((inventory == null) || (inventory.Count == 0)) {
throw new ArgumentNullException(nameof(inventory));
}
2020-11-14 22:37:00 +01:00
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> fullState = new();
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> tradableState = new();
foreach (Steam.Asset item in inventory) {
2019-02-02 22:54:23 +01:00
(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);
if (fullState.TryGetValue(key, out Dictionary<ulong, uint>? fullSet)) {
2019-02-16 17:34:17 +01:00
fullSet[item.ClassID] = fullSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
} else {
fullState[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
if (!item.Tradable) {
continue;
}
if (tradableState.TryGetValue(key, out Dictionary<ulong, uint>? tradableSet)) {
2019-02-16 17:34:17 +01:00
tradableSet[item.ClassID] = tradableSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
} else {
tradableState[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
}
return (fullState, tradableState);
}
internal static Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> GetTradableInventoryState(IReadOnlyCollection<Steam.Asset> inventory) {
if ((inventory == null) || (inventory.Count == 0)) {
throw new ArgumentNullException(nameof(inventory));
}
2020-11-14 22:37:00 +01:00
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> tradableState = new();
foreach (Steam.Asset item in inventory.Where(item => item.Tradable)) {
(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);
if (tradableState.TryGetValue(key, out Dictionary<ulong, uint>? tradableSet)) {
tradableSet[item.ClassID] = tradableSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
} else {
tradableState[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
}
return tradableState;
}
2020-11-14 22:37:00 +01:00
internal static HashSet<Steam.Asset> GetTradableItemsFromInventory(IReadOnlyCollection<Steam.Asset> inventory, IDictionary<ulong, uint> classIDs) {
if ((inventory == null) || (inventory.Count == 0)) {
throw new ArgumentNullException(nameof(inventory));
}
if ((classIDs == null) || (classIDs.Count == 0)) {
throw new ArgumentNullException(nameof(classIDs));
Implement ETradingPreferences.MatchActively This will probably need a lot more tests, tweaking and bugfixing, but basic logic is: - MatchActively added to TradingPreferences with value of 16 - User must also use SteamTradeMatcher, can't use MatchEverything - User must have statistics enabled and be eligible for being listed (no requirement of having 100 items minimum) Once all requirements are passed, statistics module will communicate with the listing and fetch match everything bots: - The matching will start in 1h since ASF start and will repeat every day (right now it starts in 1 minute to aid debugging). - Each matching is composed of up to 10 rounds maximum. - In each round ASF will fetch our inventory and inventory of listed bots in order to find MatchableTypes items to be matched. If match is found, offer is being sent and confirmed automatically. - Each set (composition of item type + appID it's from) can be matched in a single round only once, this is to minimize "items no longer available" as much as possible and also avoid a need to wait for each bot to react before sending all trades. - Round ends when we try to match a total of 20 bots, or we hit no items to match in consecutive 10 tries with 10 different bots. - If last round resulted in at least a single trade being sent, next round starts within 5 minutes since last one, otherwise matching ends and repeats the next day. We'll see how it works in practice, expect a lot of follow-up commits, unless I won't have anything to fix or improve.
2018-11-29 18:35:58 +01:00
}
2020-11-14 22:37:00 +01:00
HashSet<Steam.Asset> result = new();
Implement ETradingPreferences.MatchActively This will probably need a lot more tests, tweaking and bugfixing, but basic logic is: - MatchActively added to TradingPreferences with value of 16 - User must also use SteamTradeMatcher, can't use MatchEverything - User must have statistics enabled and be eligible for being listed (no requirement of having 100 items minimum) Once all requirements are passed, statistics module will communicate with the listing and fetch match everything bots: - The matching will start in 1h since ASF start and will repeat every day (right now it starts in 1 minute to aid debugging). - Each matching is composed of up to 10 rounds maximum. - In each round ASF will fetch our inventory and inventory of listed bots in order to find MatchableTypes items to be matched. If match is found, offer is being sent and confirmed automatically. - Each set (composition of item type + appID it's from) can be matched in a single round only once, this is to minimize "items no longer available" as much as possible and also avoid a need to wait for each bot to react before sending all trades. - Round ends when we try to match a total of 20 bots, or we hit no items to match in consecutive 10 tries with 10 different bots. - If last round resulted in at least a single trade being sent, next round starts within 5 minutes since last one, otherwise matching ends and repeats the next day. We'll see how it works in practice, expect a lot of follow-up commits, unless I won't have anything to fix or improve.
2018-11-29 18:35:58 +01:00
foreach (Steam.Asset item in inventory.Where(item => item.Tradable)) {
Implement ETradingPreferences.MatchActively This will probably need a lot more tests, tweaking and bugfixing, but basic logic is: - MatchActively added to TradingPreferences with value of 16 - User must also use SteamTradeMatcher, can't use MatchEverything - User must have statistics enabled and be eligible for being listed (no requirement of having 100 items minimum) Once all requirements are passed, statistics module will communicate with the listing and fetch match everything bots: - The matching will start in 1h since ASF start and will repeat every day (right now it starts in 1 minute to aid debugging). - Each matching is composed of up to 10 rounds maximum. - In each round ASF will fetch our inventory and inventory of listed bots in order to find MatchableTypes items to be matched. If match is found, offer is being sent and confirmed automatically. - Each set (composition of item type + appID it's from) can be matched in a single round only once, this is to minimize "items no longer available" as much as possible and also avoid a need to wait for each bot to react before sending all trades. - Round ends when we try to match a total of 20 bots, or we hit no items to match in consecutive 10 tries with 10 different bots. - If last round resulted in at least a single trade being sent, next round starts within 5 minutes since last one, otherwise matching ends and repeats the next day. We'll see how it works in practice, expect a lot of follow-up commits, unless I won't have anything to fix or improve.
2018-11-29 18:35:58 +01:00
if (!classIDs.TryGetValue(item.ClassID, out uint amount)) {
continue;
}
if (amount < item.Amount) {
item.Amount = amount;
}
result.Add(item);
if (amount == item.Amount) {
classIDs.Remove(item.ClassID);
} else {
classIDs[item.ClassID] = amount - item.Amount;
}
}
return result;
}
2019-02-02 22:54:23 +01:00
internal static bool IsEmptyForMatching(IReadOnlyDictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> fullState, IReadOnlyDictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> tradableState) {
2020-11-14 22:37:00 +01:00
if (fullState == null) {
throw new ArgumentNullException(nameof(fullState));
}
2020-11-14 22:37:00 +01:00
if (tradableState == null) {
throw new ArgumentNullException(nameof(tradableState));
}
foreach (((uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) set, IReadOnlyDictionary<ulong, uint> state) in tradableState) {
2020-08-23 20:45:24 +02:00
if (!fullState.TryGetValue(set, out Dictionary<ulong, uint>? fullSet) || (fullSet.Count == 0)) {
2020-11-14 22:37:00 +01:00
throw new InvalidOperationException(nameof(fullSet));
2018-12-09 21:26:22 +01:00
}
if (!IsEmptyForMatching(fullSet, state)) {
2018-12-09 21:26:22 +01:00
return false;
}
}
// We didn't find any matchable combinations, so this inventory is empty
return true;
}
internal static bool IsEmptyForMatching(IReadOnlyDictionary<ulong, uint> fullSet, IReadOnlyDictionary<ulong, uint> tradableSet) {
2020-11-14 22:37:00 +01:00
if (fullSet == null) {
throw new ArgumentNullException(nameof(fullSet));
}
if (tradableSet == null) {
throw new ArgumentNullException(nameof(tradableSet));
2018-12-09 21:26:22 +01:00
}
foreach ((ulong classID, uint amount) in tradableSet) {
switch (amount) {
2018-12-09 21:26:22 +01:00
case 0:
// No tradable items, this should never happen, dictionary should not have this key to begin with
2020-11-14 22:37:00 +01:00
throw new InvalidOperationException(nameof(amount));
2018-12-09 21:26:22 +01:00
case 1:
// Single tradable item, can be matchable or not depending on the rest of the inventory
if (!fullSet.TryGetValue(classID, out uint fullAmount) || (fullAmount == 0) || (fullAmount < amount)) {
2020-11-14 22:37:00 +01:00
throw new InvalidOperationException(nameof(fullAmount));
2018-12-09 21:26:22 +01:00
}
if (fullAmount > 1) {
// If we have a single tradable item but more than 1 in total, this is matchable
return false;
2018-12-09 21:26:22 +01:00
}
// A single exclusive tradable item is not matchable, continue
continue;
default:
// Any other combination of tradable items is always matchable
return false;
}
}
// We didn't find any matchable combinations, so this inventory is empty
return true;
}
2018-12-30 22:14:45 +01:00
internal void OnDisconnected() => HandledTradeOfferIDs.Clear();
internal async Task OnNewTrade() {
// We aim to have a maximum of 2 tasks, one already working, and one waiting in the queue
2016-11-20 00:03:27 +01:00
// This way we can call this function as many times as needed e.g. because of Steam events
2016-03-24 14:18:07 +01:00
lock (TradesSemaphore) {
2016-11-20 00:03:27 +01:00
if (ParsingScheduled) {
2016-04-26 18:01:19 +02:00
return;
2016-03-24 14:18:07 +01:00
}
2016-11-20 00:03:27 +01:00
ParsingScheduled = true;
2016-03-10 01:20:17 +01:00
}
2015-11-01 02:04:44 +01:00
2016-03-24 14:18:07 +01:00
await TradesSemaphore.WaitAsync().ConfigureAwait(false);
2015-11-01 02:04:44 +01:00
2016-11-20 00:03:27 +01:00
try {
2019-03-28 16:00:01 +01:00
bool lootableTypesReceived;
2018-12-08 01:45:13 +01:00
using (await Bot.Actions.GetTradingLock().ConfigureAwait(false)) {
2019-04-10 22:03:18 +02:00
lock (TradesSemaphore) {
ParsingScheduled = false;
}
2019-03-28 16:00:01 +01:00
lootableTypesReceived = await ParseActiveTrades().ConfigureAwait(false);
}
2020-01-01 12:45:32 +01:00
if (lootableTypesReceived && Bot.BotConfig.SendOnFarmingFinished && (Bot.BotConfig.LootableTypes.Count > 0)) {
await Bot.Actions.SendInventory(filterFunction: item => Bot.BotConfig.LootableTypes.Contains(item.Type)).ConfigureAwait(false);
2018-12-08 01:45:13 +01:00
}
2016-11-20 00:03:27 +01:00
} finally {
TradesSemaphore.Release();
}
2015-10-25 06:16:50 +01:00
}
2019-08-26 00:30:00 +02:00
private static Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> GetInventoryState(IReadOnlyCollection<Steam.Asset> inventory) {
if ((inventory == null) || (inventory.Count == 0)) {
throw new ArgumentNullException(nameof(inventory));
2019-08-26 00:30:00 +02:00
}
2020-11-14 22:37:00 +01:00
Dictionary<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity), Dictionary<ulong, uint>> state = new();
2019-08-26 00:30:00 +02:00
foreach (Steam.Asset item in inventory) {
(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);
if (state.TryGetValue(key, out Dictionary<ulong, uint>? set)) {
2019-08-26 00:30:00 +02:00
set[item.ClassID] = set.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
} else {
state[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
}
return state;
}
2019-03-28 16:00:01 +01:00
private async Task<bool> ParseActiveTrades() {
HashSet<Steam.TradeOffer>? tradeOffers = await Bot.ArchiWebHandler.GetActiveTradeOffers().ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
2016-05-13 06:32:42 +02:00
if ((tradeOffers == null) || (tradeOffers.Count == 0)) {
2019-03-28 16:00:01 +01:00
return false;
2015-11-01 02:04:44 +01:00
}
2015-10-25 06:16:50 +01:00
2018-12-30 22:14:45 +01:00
if (HandledTradeOfferIDs.Count > 0) {
HandledTradeOfferIDs.IntersectWith(tradeOffers.Select(tradeOffer => tradeOffer.TradeOfferID));
2018-12-10 21:21:08 +01:00
}
IEnumerable<Task<(ParseTradeResult? TradeResult, bool RequiresMobileConfirmation)>> tasks = tradeOffers.Where(tradeOffer => !HandledTradeOfferIDs.Contains(tradeOffer.TradeOfferID)).Select(ParseTrade);
IList<(ParseTradeResult? TradeResult, bool RequiresMobileConfirmation)> results = await Utilities.InParallel(tasks).ConfigureAwait(false);
if (Bot.HasMobileAuthenticator) {
2020-11-14 22:37:00 +01:00
HashSet<ulong> mobileTradeOfferIDs = results.Where(result => (result.TradeResult?.Result == ParseTradeResult.EResult.Accepted) && result.RequiresMobileConfirmation).Select(result => result.TradeResult!.TradeOfferID).ToHashSet();
2018-12-15 00:27:15 +01:00
2018-10-13 00:17:45 +02:00
if (mobileTradeOfferIDs.Count > 0) {
2020-07-10 00:28:46 +02:00
(bool twoFactorSuccess, _) = await Bot.Actions.HandleTwoFactorAuthenticationConfirmations(true, MobileAuthenticator.Confirmation.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false);
2019-01-23 17:58:37 +01:00
if (!twoFactorSuccess) {
2018-12-30 22:14:45 +01:00
HandledTradeOfferIDs.ExceptWith(mobileTradeOfferIDs);
2019-03-28 16:00:01 +01:00
return false;
2018-10-13 01:26:09 +02:00
}
}
2016-06-19 07:37:31 +02:00
}
2016-08-15 21:47:31 +02:00
HashSet<ParseTradeResult> validTradeResults = results.Where(result => result.TradeResult != null).Select(result => result.TradeResult!).ToHashSet();
if (validTradeResults.Count > 0) {
await PluginsCore.OnBotTradeOfferResults(Bot, validTradeResults).ConfigureAwait(false);
}
2019-03-28 16:00:01 +01:00
2020-11-14 22:37:00 +01:00
return results.Any(result => (result.TradeResult?.Result == ParseTradeResult.EResult.Accepted) && (!result.RequiresMobileConfirmation || Bot.HasMobileAuthenticator) && (result.TradeResult.ReceivedItemTypes?.Any(receivedItemType => Bot.BotConfig.LootableTypes.Contains(receivedItemType)) == true));
2015-10-25 06:16:50 +01:00
}
private async Task<(ParseTradeResult? TradeResult, bool RequiresMobileConfirmation)> ParseTrade(Steam.TradeOffer tradeOffer) {
2016-05-30 01:57:06 +02:00
if (tradeOffer == null) {
throw new ArgumentNullException(nameof(tradeOffer));
2016-05-30 01:57:06 +02:00
}
2019-12-29 19:21:31 +01:00
if (tradeOffer.State != ETradeOfferState.Active) {
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, tradeOffer.State));
2018-12-15 00:27:15 +01:00
2018-10-13 00:17:45 +02:00
return (null, false);
2015-10-25 06:16:50 +01:00
}
2018-12-30 22:14:45 +01:00
if (!HandledTradeOfferIDs.Add(tradeOffer.TradeOfferID)) {
// We've already seen this trade, this should not happen
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.IgnoringTrade, tradeOffer.TradeOfferID));
2018-12-15 00:27:15 +01:00
2018-12-31 02:11:10 +01:00
return (new ParseTradeResult(tradeOffer.TradeOfferID, ParseTradeResult.EResult.Ignored, tradeOffer.ItemsToReceive), false);
2018-12-10 22:11:15 +01:00
}
2019-04-10 22:26:49 +02:00
ParseTradeResult.EResult result = await ShouldAcceptTrade(tradeOffer).ConfigureAwait(false);
bool tradeRequiresMobileConfirmation = false;
2018-12-15 00:27:15 +01:00
2019-04-10 22:26:49 +02:00
switch (result) {
case ParseTradeResult.EResult.Ignored:
case ParseTradeResult.EResult.Rejected:
2019-04-24 11:27:09 +02:00
bool accept = await PluginsCore.OnBotTradeOffer(Bot, tradeOffer).ConfigureAwait(false);
if (accept) {
2019-04-10 22:26:49 +02:00
result = ParseTradeResult.EResult.Accepted;
}
break;
}
2019-04-10 22:26:49 +02:00
switch (result) {
2018-10-13 00:17:45 +02:00
case ParseTradeResult.EResult.Accepted:
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.AcceptingTrade, tradeOffer.TradeOfferID));
2018-04-21 21:58:18 +02:00
2018-10-13 00:17:45 +02:00
(bool success, bool requiresMobileConfirmation) = await Bot.ArchiWebHandler.AcceptTradeOffer(tradeOffer.TradeOfferID).ConfigureAwait(false);
2018-12-30 22:14:45 +01:00
if (!success) {
2019-04-10 22:26:49 +02:00
result = ParseTradeResult.EResult.TryAgain;
2018-12-30 22:14:45 +01:00
goto case ParseTradeResult.EResult.TryAgain;
}
if (tradeOffer.ItemsToReceive.Sum(item => item.Amount) > tradeOffer.ItemsToGive.Sum(item => item.Amount)) {
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericTrace(string.Format(CultureInfo.CurrentCulture, Strings.BotAcceptedDonationTrade, tradeOffer.TradeOfferID));
}
2019-04-10 22:26:49 +02:00
tradeRequiresMobileConfirmation = requiresMobileConfirmation;
break;
2018-12-30 22:14:45 +01:00
case ParseTradeResult.EResult.Blacklisted:
2018-12-30 23:21:44 +01:00
case ParseTradeResult.EResult.Rejected when Bot.BotConfig.BotBehaviour.HasFlag(BotConfig.EBotBehaviour.RejectInvalidTrades):
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.RejectingTrade, tradeOffer.TradeOfferID));
2018-12-30 22:14:45 +01:00
if (!await Bot.ArchiWebHandler.DeclineTradeOffer(tradeOffer.TradeOfferID).ConfigureAwait(false)) {
2019-04-10 22:26:49 +02:00
result = ParseTradeResult.EResult.TryAgain;
2018-12-30 22:14:45 +01:00
goto case ParseTradeResult.EResult.TryAgain;
}
2018-12-15 00:27:15 +01:00
2019-04-10 22:26:49 +02:00
break;
2018-12-30 22:14:45 +01:00
case ParseTradeResult.EResult.Ignored:
2018-12-30 23:21:44 +01:00
case ParseTradeResult.EResult.Rejected:
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.IgnoringTrade, tradeOffer.TradeOfferID));
2018-12-15 00:27:15 +01:00
2019-04-10 22:26:49 +02:00
break;
2018-12-30 22:14:45 +01:00
case ParseTradeResult.EResult.TryAgain:
HandledTradeOfferIDs.Remove(tradeOffer.TradeOfferID);
goto case ParseTradeResult.EResult.Ignored;
2017-06-19 08:23:01 +02:00
default:
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(result), result));
2018-12-15 00:27:15 +01:00
2018-10-13 00:17:45 +02:00
return (null, false);
2016-06-19 13:59:56 +02:00
}
2019-04-10 22:26:49 +02:00
return (new ParseTradeResult(tradeOffer.TradeOfferID, result, tradeOffer.ItemsToReceive), tradeRequiresMobileConfirmation);
2015-10-25 06:16:50 +01:00
}
2016-04-01 20:18:21 +02:00
2019-04-10 22:26:49 +02:00
private async Task<ParseTradeResult.EResult> ShouldAcceptTrade(Steam.TradeOffer tradeOffer) {
2020-11-14 22:37:00 +01:00
if (tradeOffer == null) {
throw new ArgumentNullException(nameof(tradeOffer));
}
2018-12-15 00:27:15 +01:00
2020-11-14 22:37:00 +01:00
if (ASF.GlobalConfig == null) {
throw new InvalidOperationException(nameof(ASF.GlobalConfig));
}
if (Bot.Bots == null) {
throw new InvalidOperationException(nameof(Bot.Bots));
2016-04-01 20:18:21 +02:00
}
2017-03-28 21:27:01 +02:00
if (tradeOffer.OtherSteamID64 != 0) {
// Always accept trades from SteamMasterID
2020-11-14 22:37:00 +01:00
if (Bot.HasAccess(tradeOffer.OtherSteamID64, BotConfig.EAccess.Master)) {
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Accepted, nameof(tradeOffer.OtherSteamID64) + " " + tradeOffer.OtherSteamID64 + ": " + BotConfig.EAccess.Master));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.Accepted;
2017-03-28 21:27:01 +02:00
}
// Always deny trades from blacklisted steamIDs
2017-03-28 21:27:01 +02:00
if (Bot.IsBlacklistedFromTrades(tradeOffer.OtherSteamID64)) {
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Blacklisted, nameof(tradeOffer.OtherSteamID64) + " " + tradeOffer.OtherSteamID64));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.Blacklisted;
2017-03-28 21:27:01 +02:00
}
2016-10-27 22:01:38 +02:00
}
2016-10-21 21:33:55 +02:00
// Check if it's donation trade
2018-04-21 21:52:04 +02:00
switch (tradeOffer.ItemsToGive.Count) {
case 0 when tradeOffer.ItemsToReceive.Count == 0:
2018-12-30 22:14:45 +01:00
// If it's steam issue, try again later
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.TryAgain, nameof(tradeOffer.ItemsToReceive.Count) + " = 0"));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.TryAgain;
2018-04-21 21:52:04 +02:00
case 0:
// Otherwise react accordingly, depending on our preference
bool acceptDonations = Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.AcceptDonations);
bool acceptBotTrades = !Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.DontAcceptBotTrades);
2020-11-14 22:37:00 +01:00
switch (acceptDonations) {
case true when acceptBotTrades:
// If we accept donations and bot trades, accept it right away
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Accepted, nameof(acceptDonations) + " = " + true + " && " + nameof(acceptBotTrades) + " = " + true));
2020-08-11 11:34:32 +02:00
2020-11-14 22:37:00 +01:00
return ParseTradeResult.EResult.Accepted;
2020-11-14 22:37:00 +01:00
case false when !acceptBotTrades:
// If we don't accept donations, neither bot trades, deny it right away
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Rejected, nameof(acceptDonations) + " = " + false + " && " + nameof(acceptBotTrades) + " = " + false));
2020-08-11 11:34:32 +02:00
2020-11-14 22:37:00 +01:00
return ParseTradeResult.EResult.Rejected;
2018-04-21 21:52:04 +02:00
}
2016-10-21 21:33:55 +02:00
2018-04-21 21:52:04 +02:00
// Otherwise we either accept donations but not bot trades, or we accept bot trades but not donations
2018-09-23 02:17:17 +02:00
bool isBotTrade = (tradeOffer.OtherSteamID64 != 0) && Bot.Bots.Values.Any(bot => bot.SteamID == tradeOffer.OtherSteamID64);
2018-12-15 00:27:15 +01:00
2020-08-11 11:34:32 +02:00
ParseTradeResult.EResult result = (acceptDonations && !isBotTrade) || (acceptBotTrades && isBotTrade) ? ParseTradeResult.EResult.Accepted : ParseTradeResult.EResult.Rejected;
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, result, nameof(acceptDonations) + " = " + acceptDonations + " && " + nameof(acceptBotTrades) + " = " + acceptBotTrades + " && " + nameof(isBotTrade) + " = " + isBotTrade));
2020-08-11 11:34:32 +02:00
return result;
2016-04-01 20:18:21 +02:00
}
// If we don't have SteamTradeMatcher enabled, this is the end for us
if (!Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.SteamTradeMatcher)) {
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Rejected, nameof(BotConfig.ETradingPreferences.SteamTradeMatcher) + " = " + false));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.Rejected;
}
2018-12-30 23:21:44 +01:00
// Decline trade if we're giving more count-wise, this is a very naive pre-check, it'll be strengthened in more detailed fair types exchange next
2016-05-06 23:31:00 +02:00
if (tradeOffer.ItemsToGive.Count > tradeOffer.ItemsToReceive.Count) {
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Rejected, nameof(tradeOffer.ItemsToGive.Count) + ": " + tradeOffer.ItemsToGive.Count + " > " + tradeOffer.ItemsToReceive.Count));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.Rejected;
}
2018-04-23 22:54:27 +02:00
// Decline trade if we're requested to handle any not-accepted item type or if it's not fair games/types exchange
2019-02-02 22:54:23 +01:00
if (!tradeOffer.IsValidSteamItemsRequest(Bot.BotConfig.MatchableTypes) || !IsFairExchange(tradeOffer.ItemsToGive, tradeOffer.ItemsToReceive)) {
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Rejected, nameof(tradeOffer.IsValidSteamItemsRequest) + " || " + nameof(IsFairExchange)));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.Rejected;
}
2016-04-01 20:18:21 +02:00
// At this point we're sure that STM trade is valid
2016-06-27 01:45:41 +02:00
// Fetch trade hold duration
byte? holdDuration = await Bot.GetTradeHoldDuration(tradeOffer.OtherSteamID64, tradeOffer.TradeOfferID).ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
2020-11-14 22:37:00 +01:00
switch (holdDuration) {
case null:
// If we can't get trade hold duration, try again later
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.TryAgain, nameof(holdDuration)));
2020-08-11 11:34:32 +02:00
2020-11-14 22:37:00 +01:00
return ParseTradeResult.EResult.TryAgain;
2020-11-14 22:37:00 +01:00
// If user has a trade hold, we add extra logic
2016-06-27 01:45:41 +02:00
// If trade hold duration exceeds our max, or user asks for cards with short lifespan, reject the trade
2020-11-14 22:37:00 +01:00
case > 0 when (holdDuration.Value > ASF.GlobalConfig.MaxTradeHoldDuration) || tradeOffer.ItemsToGive.Any(item => ((item.Type == Steam.Asset.EType.FoilTradingCard) || (item.Type == Steam.Asset.EType.TradingCard)) && CardsFarmer.SalesBlacklist.Contains(item.RealAppID)):
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Rejected, nameof(holdDuration) + " > 0: " + holdDuration.Value));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.Rejected;
}
2016-10-21 21:33:55 +02:00
// If we're matching everything, this is enough for us
if (Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.MatchEverything)) {
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.Accepted, BotConfig.ETradingPreferences.MatchEverything));
2020-08-11 11:34:32 +02:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.Accepted;
2016-10-21 21:33:55 +02:00
}
// Get sets we're interested in
2020-11-14 22:37:00 +01:00
HashSet<(uint RealAppID, Steam.Asset.EType Type, Steam.Asset.ERarity Rarity)> wantedSets = new();
foreach (Steam.Asset item in tradeOffer.ItemsToGive) {
2019-02-02 22:54:23 +01:00
wantedSets.Add((item.RealAppID, item.Type, item.Rarity));
}
// Now check if it's worth for us to do the trade
HashSet<Steam.Asset> inventory;
2018-12-15 00:27:15 +01:00
try {
2021-02-20 23:38:48 +01:00
inventory = await Bot.ArchiWebHandler.GetInventoryAsync().Where(item => wantedSets.Contains((item.RealAppID, item.Type, item.Rarity))).ToHashSetAsync().ConfigureAwait(false);
2020-08-11 11:34:32 +02:00
} catch (HttpRequestException e) {
// If we can't check our inventory when not using MatchEverything, this is a temporary failure, try again later
2020-08-11 11:34:32 +02:00
Bot.ArchiLogger.LogGenericWarningException(e);
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.TryAgain, nameof(inventory)));
2020-08-11 11:34:32 +02:00
return ParseTradeResult.EResult.TryAgain;
} catch (Exception e) {
// If we can't check our inventory when not using MatchEverything, this is a temporary failure, try again later
Bot.ArchiLogger.LogGenericException(e);
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.TryAgain, nameof(inventory)));
return ParseTradeResult.EResult.TryAgain;
}
if (inventory.Count == 0) {
2018-12-30 22:14:45 +01:00
// If we can't check our inventory when not using MatchEverything, this is a temporary failure, try again later
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(inventory)));
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, ParseTradeResult.EResult.TryAgain, nameof(inventory)));
2018-12-15 00:27:15 +01:00
2019-04-10 22:26:49 +02:00
return ParseTradeResult.EResult.TryAgain;
}
bool accept = IsTradeNeutralOrBetter(inventory, tradeOffer.ItemsToGive.Select(item => item.CreateShallowCopy()).ToHashSet(), tradeOffer.ItemsToReceive.Select(item => item.CreateShallowCopy()).ToHashSet());
2018-12-30 23:21:44 +01:00
// We're now sure whether the trade is neutral+ for us or not
2020-08-11 11:34:32 +02:00
ParseTradeResult.EResult acceptResult = accept ? ParseTradeResult.EResult.Accepted : ParseTradeResult.EResult.Rejected;
2020-11-14 22:37:00 +01:00
Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.BotTradeOfferResult, tradeOffer.TradeOfferID, acceptResult, nameof(IsTradeNeutralOrBetter)));
2020-08-11 11:34:32 +02:00
return acceptResult;
}
public sealed class ParseTradeResult {
[PublicAPI]
2020-11-14 22:37:00 +01:00
public EResult Result { get; }
[PublicAPI]
2020-11-14 22:37:00 +01:00
public ulong TradeOfferID { get; }
2019-04-10 22:26:49 +02:00
internal readonly ImmutableHashSet<Steam.Asset.EType>? ReceivedItemTypes;
2018-12-30 22:14:45 +01:00
internal ParseTradeResult(ulong tradeOfferID, EResult result, IReadOnlyCollection<Steam.Asset>? itemsToReceive = null) {
2020-11-14 22:37:00 +01:00
if (tradeOfferID == 0) {
throw new ArgumentOutOfRangeException(nameof(tradeOfferID));
}
if ((result == EResult.Unknown) || !Enum.IsDefined(typeof(EResult), result)) {
throw new InvalidEnumArgumentException(nameof(result), (int) result, typeof(EResult));
}
2018-10-13 00:17:45 +02:00
TradeOfferID = tradeOfferID;
Result = result;
2018-10-13 00:29:58 +02:00
2020-11-14 22:37:00 +01:00
if (itemsToReceive?.Count > 0) {
2019-04-10 22:39:28 +02:00
ReceivedItemTypes = itemsToReceive.Select(item => item.Type).ToImmutableHashSet();
2018-10-13 00:29:58 +02:00
}
}
public enum EResult : byte {
Unknown,
2018-10-13 00:17:45 +02:00
Accepted,
2018-12-30 22:14:45 +01:00
Blacklisted,
Ignored,
2018-12-30 23:21:44 +01:00
Rejected,
2018-12-30 22:14:45 +01:00
TryAgain
}
}
2015-10-25 06:16:50 +01:00
}
2018-08-01 23:11:15 +02:00
}