diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 7bf325977..9ee191cd5 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -229,7 +229,7 @@ namespace ArchiSteamFarm { CardsFarmer = new CardsFarmer(this); Trading = new Trading(this); - if (BotConfig.AcceptConfirmationsPeriod > 0 && AcceptConfirmationsTimer == null) { + if (BotConfig.AcceptConfirmationsPeriod > 0) { AcceptConfirmationsTimer = new Timer( async e => await AcceptConfirmations().ConfigureAwait(false), null, @@ -238,7 +238,7 @@ namespace ArchiSteamFarm { ); } - if (BotConfig.SendTradePeriod > 0 && SendItemsTimer == null) { + if (BotConfig.SendTradePeriod > 0) { SendItemsTimer = new Timer( async e => await ResponseSendTrade(BotConfig.SteamMasterID).ConfigureAwait(false), null, @@ -516,9 +516,13 @@ namespace ArchiSteamFarm { } if (CardsFarmer.CurrentGamesFarming.Count > 0) { - return "Bot " + BotName + " is currently farming appIDs: " + string.Join(", ", CardsFarmer.CurrentGamesFarming) + " and has a total of " + CardsFarmer.GamesToFarm.Count + " games left to farm."; + return "Bot " + BotName + " is farming appIDs: " + string.Join(", ", CardsFarmer.CurrentGamesFarming) + " and has a total of " + CardsFarmer.GamesToFarm.Count + " games left to farm."; + } else if (SteamClient.IsConnected) { + return "Bot " + BotName + " is not farming anything."; + } else if (KeepRunning) { + return "Bot " + BotName + " is not connected."; } else { - return "Bot " + BotName + " is currently not farming anything."; + return "Bot " + BotName + " is not running."; } } @@ -546,9 +550,7 @@ namespace ArchiSteamFarm { StringBuilder result = new StringBuilder(Environment.NewLine); - int totalBotsCount = Bots.Count; - int runningBotsCount = 0; - + byte runningBotsCount = 0; foreach (Bot bot in Bots.Values) { result.Append(bot.ResponseStatus(steamID) + Environment.NewLine); if (bot.KeepRunning) { @@ -556,7 +558,7 @@ namespace ArchiSteamFarm { } } - result.Append("There are " + totalBotsCount + " bots initialized and " + runningBotsCount + " of them are currently running."); + result.Append("There are " + runningBotsCount + "/" + Bots.Count + "bots running."); return result.ToString(); } diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index a17b026ba..126df549f 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -39,12 +39,10 @@ namespace ArchiSteamFarm { private readonly ManualResetEvent FarmResetEvent = new ManualResetEvent(false); private readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1); - private readonly Bot Bot; private readonly Timer Timer; - private bool ManualMode = false; - private bool NowFarming = false; + private bool ManualMode, NowFarming; internal CardsFarmer(Bot bot) { if (bot == null) { @@ -53,7 +51,7 @@ namespace ArchiSteamFarm { Bot = bot; - if (Program.GlobalConfig.IdleFarmingPeriod > 0 && Timer == null) { + if (Program.GlobalConfig.IdleFarmingPeriod > 0) { Timer = new Timer( async e => await CheckGamesForFarming().ConfigureAwait(false), null, @@ -63,29 +61,6 @@ namespace ArchiSteamFarm { } } - internal static HashSet GetGamesToFarmSolo(ConcurrentDictionary gamesToFarm) { - if (gamesToFarm == null) { - return null; - } - - HashSet result = new HashSet(); - foreach (KeyValuePair keyValue in gamesToFarm) { - if (keyValue.Value >= 2) { - result.Add(keyValue.Key); - } - } - - return result; - } - - internal static uint GetAnyGameToFarm(ConcurrentDictionary gamesToFarm) { - if (gamesToFarm == null) { - return 0; - } - - return gamesToFarm.Keys.FirstOrDefault(); - } - internal async Task SwitchToManualMode(bool manualMode) { if (ManualMode == manualMode) { return false; @@ -104,62 +79,18 @@ namespace ArchiSteamFarm { return true; } - internal bool FarmMultiple(ConcurrentDictionary appIDs) { - if (appIDs.Count == 0) { - return true; - } - - float maxHour = 0; - foreach (float hour in appIDs.Values) { - if (hour > maxHour) { - maxHour = hour; - } - } - - CurrentGamesFarming.Clear(); - CurrentGamesFarming.TrimExcess(); - foreach (uint appID in appIDs.Keys) { - CurrentGamesFarming.Add(appID); - } - - Logging.LogGenericInfo("Now farming: " + string.Join(", ", appIDs.Keys), Bot.BotName); - if (Farm(maxHour, appIDs.Keys)) { - CurrentGamesFarming.Clear(); - return true; - } else { - CurrentGamesFarming.Clear(); - return false; - } - } - - internal async Task FarmSolo(uint appID) { - if (appID == 0) { - return true; - } - - CurrentGamesFarming.Clear(); - CurrentGamesFarming.TrimExcess(); - CurrentGamesFarming.Add(appID); - - Logging.LogGenericInfo("Now farming: " + appID, Bot.BotName); - if (await Farm(appID).ConfigureAwait(false)) { - float hours; - GamesToFarm.TryRemove(appID, out hours); - return true; - } else { - CurrentGamesFarming.Clear(); - return false; - } - } - - internal async Task RestartFarming() { - await StopFarming().ConfigureAwait(false); - await StartFarming().ConfigureAwait(false); - } - internal async Task StartFarming() { + if (NowFarming) { + return; + } + await Semaphore.WaitAsync().ConfigureAwait(false); + if (NowFarming) { + Semaphore.Release(); + return; + } + if (ManualMode) { Semaphore.Release(); // We have nothing to do, don't forget to release semaphore return; @@ -234,6 +165,10 @@ namespace ArchiSteamFarm { } internal async Task StopFarming() { + if (!NowFarming) { + return; + } + await Semaphore.WaitAsync().ConfigureAwait(false); if (!NowFarming) { @@ -252,6 +187,34 @@ namespace ArchiSteamFarm { Semaphore.Release(); } + internal async Task RestartFarming() { + await StopFarming().ConfigureAwait(false); + await StartFarming().ConfigureAwait(false); + } + + private static HashSet GetGamesToFarmSolo(ConcurrentDictionary gamesToFarm) { + if (gamesToFarm == null) { + return null; + } + + HashSet result = new HashSet(); + foreach (KeyValuePair keyValue in gamesToFarm) { + if (keyValue.Value >= 2) { + result.Add(keyValue.Key); + } + } + + return result; + } + + private static uint GetAnyGameToFarm(ConcurrentDictionary gamesToFarm) { + if (gamesToFarm == null) { + return 0; + } + + return gamesToFarm.Keys.FirstOrDefault(); + } + private async Task IsAnythingToFarm() { if (NowFarming) { return true; @@ -418,6 +381,54 @@ namespace ArchiSteamFarm { return !htmlNode.InnerText.Contains("No card drops"); } + private bool FarmMultiple(ConcurrentDictionary appIDs) { + if (appIDs.Count == 0) { + return true; + } + + float maxHour = 0; + foreach (float hour in appIDs.Values) { + if (hour > maxHour) { + maxHour = hour; + } + } + + CurrentGamesFarming.Clear(); + CurrentGamesFarming.TrimExcess(); + foreach (uint appID in appIDs.Keys) { + CurrentGamesFarming.Add(appID); + } + + Logging.LogGenericInfo("Now farming: " + string.Join(", ", appIDs.Keys), Bot.BotName); + if (Farm(maxHour, appIDs.Keys)) { + CurrentGamesFarming.Clear(); + return true; + } else { + CurrentGamesFarming.Clear(); + return false; + } + } + + private async Task FarmSolo(uint appID) { + if (appID == 0) { + return true; + } + + CurrentGamesFarming.Clear(); + CurrentGamesFarming.TrimExcess(); + CurrentGamesFarming.Add(appID); + + Logging.LogGenericInfo("Now farming: " + appID, Bot.BotName); + if (await Farm(appID).ConfigureAwait(false)) { + float hours; + GamesToFarm.TryRemove(appID, out hours); + return true; + } else { + CurrentGamesFarming.Clear(); + return false; + } + } + private async Task Farm(uint appID) { Bot.ArchiHandler.PlayGames(appID); diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 21caaf18c..b44d1794e 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -149,7 +149,7 @@ namespace ArchiSteamFarm { Logging.LogGenericInfo("Local version: " + Version + " | Remote version: " + newVersion); if (Version.CompareTo(newVersion) >= 0) { // If local version is the same or newer than remote version - if (GlobalConfig.AutoUpdates && AutoUpdatesTimer == null) { + if (AutoUpdatesTimer == null && GlobalConfig.AutoUpdates) { Logging.LogGenericInfo("ASF will automatically check for new versions every 24 hours"); AutoUpdatesTimer = new Timer( async e => await CheckForUpdate().ConfigureAwait(false),