Files
ArchiSteamFarm/ArchiSteamFarm/Trading.cs

100 lines
3.3 KiB
C#
Raw Normal View History

2015-10-28 19:21:27 +01:00
/*
_ _ _ ____ _ _____
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2016-01-16 04:21:36 +01:00
Copyright 2015-2016 Łukasz "JustArchi" Domeradzki
2015-10-28 19:21:27 +01:00
Contact: JustArchi@JustArchi.net
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;
2015-10-25 06:16:50 +01:00
using System.Threading;
using System.Threading.Tasks;
namespace ArchiSteamFarm {
internal sealed class Trading {
internal const byte MaxItemsPerTrade = 150; // This is due to limit on POST size in WebBrowser
internal const byte MaxTradesPerAccount = 5; // This is limit introduced by Valve
2016-01-22 10:13:02 +01:00
2016-01-26 23:05:53 +01:00
private static readonly SemaphoreSlim InventorySemaphore = new SemaphoreSlim(1);
2015-10-31 05:27:30 +01:00
private readonly Bot Bot;
private readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1);
2015-10-25 06:16:50 +01:00
private volatile byte ParsingTasks = 0;
2016-01-26 23:05:53 +01:00
internal static async Task LimitInventoryRequestsAsync() {
await InventorySemaphore.WaitAsync().ConfigureAwait(false);
var releaseLater = Task.Run(async () => {
await Utilities.SleepAsync(3000).ConfigureAwait(false); // We must add some delay to not get caught by Steam rate limiter
InventorySemaphore.Release();
});
2016-01-26 23:05:53 +01:00
}
2015-10-25 06:16:50 +01:00
internal Trading(Bot bot) {
Bot = bot;
}
2015-11-01 02:04:44 +01:00
internal async void CheckTrades() {
2015-10-25 06:16:50 +01:00
if (ParsingTasks < 2) {
ParsingTasks++;
2015-11-01 02:04:44 +01:00
await Semaphore.WaitAsync().ConfigureAwait(false);
await ParseActiveTrades().ConfigureAwait(false);
Semaphore.Release();
ParsingTasks--;
2015-10-25 06:16:50 +01:00
}
}
private async Task ParseActiveTrades() {
List<SteamTradeOffer> tradeOffers = Bot.ArchiWebHandler.GetTradeOffers();
2015-11-01 02:04:44 +01:00
if (tradeOffers == null) {
return;
}
2015-10-25 06:16:50 +01:00
2015-11-01 02:04:44 +01:00
List<Task> tasks = new List<Task>();
foreach (SteamTradeOffer tradeOffer in tradeOffers) {
if (tradeOffer.trade_offer_state == SteamTradeOffer.ETradeOfferState.Active) {
tasks.Add(Task.Run(async () => await ParseTrade(tradeOffer).ConfigureAwait(false)));
2015-11-01 02:04:44 +01:00
}
2015-10-25 06:16:50 +01:00
}
2015-11-01 02:04:44 +01:00
await Task.WhenAll(tasks).ConfigureAwait(false);
2015-12-17 22:04:13 +01:00
await Bot.AcceptAllConfirmations().ConfigureAwait(false);
2015-10-25 06:16:50 +01:00
}
private async Task ParseTrade(SteamTradeOffer tradeOffer) {
if (tradeOffer == null) {
return;
}
ulong tradeID;
if (!ulong.TryParse(tradeOffer.tradeofferid, out tradeID)) {
return;
}
2016-03-06 02:20:41 +01:00
if (tradeOffer.items_to_give.Count == 0 || tradeOffer.OtherSteamID64 == Bot.BotConfig.SteamMasterID) {
2016-01-24 18:08:27 +01:00
Logging.LogGenericInfo("Accepting trade: " + tradeID, Bot.BotName);
await Bot.ArchiWebHandler.AcceptTradeOffer(tradeID).ConfigureAwait(false);
2015-10-25 06:16:50 +01:00
} else {
2016-01-24 18:08:27 +01:00
Logging.LogGenericInfo("Ignoring trade: " + tradeID, Bot.BotName);
2015-10-25 06:16:50 +01:00
}
}
}
}