From 93191f9066694e3d9637036f24ed96f28237d226 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 27 Jun 2016 21:09:58 +0200 Subject: [PATCH] Add logic for MaxGamesPlayedConcurrently This is a limit introduced by Steam Network, limit for Steam Client is 30, but we don't have to respect that one --- ArchiSteamFarm/Bot.cs | 4 ++++ ArchiSteamFarm/BotConfig.cs | 10 ++++++++++ ArchiSteamFarm/CardsFarmer.cs | 10 ++++++++++ ArchiSteamFarm/ConcurrentHashSet.cs | 10 +++++++++- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 45c413a57..55e583eb9 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -1180,6 +1180,10 @@ namespace ArchiSteamFarm { await CardsFarmer.SwitchToManualMode(false).ConfigureAwait(false); } else { + if (gameIDs.Count > CardsFarmer.MaxGamesPlayedConcurrently) { + gameIDs = new HashSet(gameIDs.Take(CardsFarmer.MaxGamesPlayedConcurrently)); + } + if (!CardsFarmer.ManualMode) { await CardsFarmer.SwitchToManualMode(true).ConfigureAwait(false); } diff --git a/ArchiSteamFarm/BotConfig.cs b/ArchiSteamFarm/BotConfig.cs index 417eee9cb..4f7ed7705 100644 --- a/ArchiSteamFarm/BotConfig.cs +++ b/ArchiSteamFarm/BotConfig.cs @@ -27,6 +27,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Linq; namespace ArchiSteamFarm { [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] @@ -124,6 +125,15 @@ namespace ArchiSteamFarm { return null; } + // User might not know what he's doing + // Ensure that he can't screw core ASF variables + if (botConfig.GamesPlayedWhileIdle.Count <= CardsFarmer.MaxGamesPlayedConcurrently) { + return botConfig; + } + + Logging.LogGenericWarning("Playing more than " + CardsFarmer.MaxGamesPlayedConcurrently + " games concurrently is not possible, only first " + CardsFarmer.MaxGamesPlayedConcurrently + " entries from GamesPlayedWhileIdle will be used"); + botConfig.GamesPlayedWhileIdle = new HashSet(botConfig.GamesPlayedWhileIdle.Take(CardsFarmer.MaxGamesPlayedConcurrently)); + return botConfig; } diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index 6f0bb9814..6cb1fbc14 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -35,6 +35,8 @@ using Newtonsoft.Json; namespace ArchiSteamFarm { internal sealed class CardsFarmer { + internal const byte MaxGamesPlayedConcurrently = 32; // This is limit introduced by Steam Network + [JsonProperty] internal readonly ConcurrentDictionary GamesToFarm = new ConcurrentDictionary(); @@ -420,6 +422,10 @@ namespace ArchiSteamFarm { if (game.Value > maxHour) { maxHour = game.Value; } + + if (CurrentGamesFarming.Count >= MaxGamesPlayedConcurrently) { + break; + } } if (maxHour >= 2) { @@ -506,6 +512,10 @@ namespace ArchiSteamFarm { return true; } + if (appIDs.Count > MaxGamesPlayedConcurrently) { + appIDs = new ConcurrentHashSet(appIDs.Take(MaxGamesPlayedConcurrently)); + } + Bot.ArchiHandler.PlayGames(appIDs); bool success = true; diff --git a/ArchiSteamFarm/ConcurrentHashSet.cs b/ArchiSteamFarm/ConcurrentHashSet.cs index fc3d54c91..c41acb2a0 100644 --- a/ArchiSteamFarm/ConcurrentHashSet.cs +++ b/ArchiSteamFarm/ConcurrentHashSet.cs @@ -30,12 +30,20 @@ using System.Threading; namespace ArchiSteamFarm { internal sealed class ConcurrentHashSet : ICollection, IDisposable { - private readonly HashSet HashSet = new HashSet(); + private readonly HashSet HashSet; private readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim(); public bool IsReadOnly => false; public IEnumerator GetEnumerator() => new ConcurrentEnumerator(HashSet, Lock); + internal ConcurrentHashSet() { + HashSet = new HashSet(); + } + + internal ConcurrentHashSet(IEnumerable collection) { + HashSet = new HashSet(collection); + } + public int Count { get { Lock.EnterReadLock();