Files
ArchiSteamFarm/ArchiSteamFarm/SteamSaleEvent.cs

114 lines
3.9 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
// |
2020-02-01 23:33:35 +01:00
// Copyright 2015-2020 Ł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.
using System;
2019-02-02 22:54:23 +01:00
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using AngleSharp.Dom;
using ArchiSteamFarm.Localization;
2019-01-10 23:44:32 +01:00
using JetBrains.Annotations;
namespace ArchiSteamFarm {
internal sealed class SteamSaleEvent : IAsyncDisposable {
private const byte MaxSingleQueuesDaily = 3; // This is only a failsafe for infinite queue clearing
2017-06-15 02:57:06 +02:00
private readonly Bot Bot;
2017-12-21 19:38:45 +01:00
private readonly Timer SaleEventTimer;
2019-01-10 23:44:32 +01:00
internal SteamSaleEvent([NotNull] Bot bot) {
2017-06-03 18:59:22 +02:00
Bot = bot ?? throw new ArgumentNullException(nameof(bot));
2017-12-21 19:38:45 +01:00
SaleEventTimer = new Timer(
async e => await ExploreDiscoveryQueue().ConfigureAwait(false),
null,
2019-03-03 16:22:25 +01:00
TimeSpan.FromHours(1.1) + TimeSpan.FromSeconds(ASF.LoadBalancingDelay * Bot.Bots.Count), // Delay
2018-12-03 00:59:01 +01:00
TimeSpan.FromHours(8.1) // Period
);
}
public async ValueTask DisposeAsync() => await SaleEventTimer.DisposeAsync().ConfigureAwait(false);
private async Task ExploreDiscoveryQueue() {
2017-06-03 18:59:22 +02:00
if (!Bot.IsConnectedAndLoggedOn) {
return;
}
2017-06-26 01:37:14 +02:00
Bot.ArchiLogger.LogGenericTrace(Strings.Starting);
2017-06-15 02:59:03 +02:00
for (byte i = 0; (i < MaxSingleQueuesDaily) && (await IsDiscoveryQueueAvailable().ConfigureAwait(false)).GetValueOrDefault(); i++) {
2019-02-02 22:54:23 +01:00
ImmutableHashSet<uint> queue = await Bot.ArchiWebHandler.GenerateNewDiscoveryQueue().ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
if ((queue == null) || (queue.Count == 0)) {
2017-06-26 01:37:14 +02:00
Bot.ArchiLogger.LogGenericTrace(string.Format(Strings.ErrorIsEmpty, nameof(queue)));
2018-12-15 00:27:15 +01:00
break;
}
Bot.ArchiLogger.LogGenericInfo(string.Format(Strings.ClearingDiscoveryQueue, i));
2017-01-02 19:57:06 +01:00
// We could in theory do this in parallel, but who knows what would happen...
foreach (uint queuedAppID in queue) {
if (await Bot.ArchiWebHandler.ClearFromDiscoveryQueue(queuedAppID).ConfigureAwait(false)) {
continue;
}
Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed);
2018-12-15 00:27:15 +01:00
return;
}
Bot.ArchiLogger.LogGenericInfo(string.Format(Strings.DoneClearingDiscoveryQueue, i));
}
2017-06-26 01:37:14 +02:00
Bot.ArchiLogger.LogGenericTrace(Strings.Done);
}
2017-06-03 18:59:22 +02:00
private async Task<bool?> IsDiscoveryQueueAvailable() {
2020-04-18 14:08:37 +02:00
using IDocument htmlDocument = await Bot.ArchiWebHandler.GetDiscoveryQueuePage().ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
if (htmlDocument == null) {
return null;
}
IElement htmlNode = htmlDocument.SelectSingleNode("//div[@class='subtext']");
2018-12-15 00:27:15 +01:00
if (htmlNode == null) {
2017-06-03 18:59:22 +02:00
// Valid, no cards for exploring the queue available
return false;
}
string text = htmlNode.TextContent;
2018-12-15 00:27:15 +01:00
if (string.IsNullOrEmpty(text)) {
Bot.ArchiLogger.LogNullError(nameof(text));
2018-12-15 00:27:15 +01:00
return null;
}
2017-06-26 01:37:14 +02:00
Bot.ArchiLogger.LogGenericTrace(text);
// It'd make more sense to check against "Come back tomorrow", but it might not cover out-of-the-event queue
2018-03-09 15:43:25 +01:00
return text.StartsWith("You can get ", StringComparison.Ordinal);
}
}
2018-09-08 00:46:40 +02:00
}