From 52f3a86255d7fbad83f20747850cb6907be83013 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 8 Jun 2016 23:26:37 +0200 Subject: [PATCH] EXPERIMENTAL: Closes #238 Needs further tests --- ArchiSteamFarm/ArchiHandler.cs | 4 +- ArchiSteamFarm/Bot.cs | 17 +++----- ArchiSteamFarm/CardsFarmer.cs | 77 ++++++++++++++++++++++------------ 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index c0dca2511..6b92fc4ee 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -85,9 +85,7 @@ namespace ArchiSteamFarm { JobID = jobID; if (msg.count_new_items > 0) { - Notifications = new HashSet { - ENotification.Items - }; + Notifications = new HashSet { ENotification.Items }; } } } diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 10b56a169..aa78f643a 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -1962,26 +1962,19 @@ namespace ArchiSteamFarm { return; } - bool checkTrades = false; - bool markInventory = false; foreach (ArchiHandler.NotificationsCallback.ENotification notification in callback.Notifications) { switch (notification) { case ArchiHandler.NotificationsCallback.ENotification.Items: - markInventory = true; + CardsFarmer.OnNewItemsNotification(); + if (BotConfig.DismissInventoryNotifications) { + await ArchiWebHandler.MarkInventory().ConfigureAwait(false); + } break; case ArchiHandler.NotificationsCallback.ENotification.Trading: - checkTrades = true; + await Trading.CheckTrades().ConfigureAwait(false); break; } } - - if (checkTrades) { - Trading.CheckTrades().Forget(); - } - - if (markInventory && BotConfig.DismissInventoryNotifications) { - await ArchiWebHandler.MarkInventory().ConfigureAwait(false); - } } private void OnOfflineMessage(ArchiHandler.OfflineMessageCallback callback) { diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index d4b7d571e..7c5cfe6ac 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -44,7 +44,7 @@ namespace ArchiSteamFarm { internal bool ManualMode { get; private set; } - private bool NowFarming; + private bool KeepFarming, NowFarming; internal CardsFarmer(Bot bot) { if (bot == null) { @@ -107,7 +107,7 @@ namespace ArchiSteamFarm { return; } - NowFarming = true; + KeepFarming = NowFarming = true; FarmingSemaphore.Release(); // From this point we allow other calls to shut us down do { @@ -170,6 +170,7 @@ namespace ArchiSteamFarm { } Logging.LogGenericInfo("Sending signal to stop farming", Bot.BotName); + KeepFarming = false; FarmResetEvent.Set(); Logging.LogGenericInfo("Waiting for reaction...", Bot.BotName); @@ -181,7 +182,6 @@ namespace ArchiSteamFarm { Logging.LogGenericWarning("Timed out!", Bot.BotName); } - FarmResetEvent.Reset(); Logging.LogGenericInfo("Farming stopped!", Bot.BotName); Bot.OnFarmingStopped(); FarmingSemaphore.Release(); @@ -192,6 +192,14 @@ namespace ArchiSteamFarm { await StartFarming().ConfigureAwait(false); } + internal void OnNewItemsNotification() { + if (!NowFarming) { + return; + } + + FarmResetEvent.Set(); + } + private static HashSet GetGamesToFarmSolo(ConcurrentDictionary gamesToFarm) { if (gamesToFarm == null) { Logging.LogNullError(nameof(gamesToFarm)); @@ -217,23 +225,28 @@ namespace ArchiSteamFarm { return false; } - byte maxPages = 1; - HtmlNodeCollection htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//a[@class='pagelink']"); - if ((htmlNodeCollection != null) && (htmlNodeCollection.Count > 0)) { - HtmlNode htmlNode = htmlNodeCollection[htmlNodeCollection.Count - 1]; - string lastPage = htmlNode.InnerText; - if (!string.IsNullOrEmpty(lastPage)) { - if (!byte.TryParse(lastPage, out maxPages)) { - maxPages = 1; // Should never happen - } - } + HtmlNode htmlNode = htmlDocument.DocumentNode.SelectSingleNode("(//a[@class='pagelink'])[last()]"); + if (htmlNode == null) { + Logging.LogNullError(nameof(htmlNode), Bot.BotName); + return false; + } + + string lastPage = htmlNode.InnerText; + if (string.IsNullOrEmpty(lastPage)) { + Logging.LogNullError(nameof(lastPage), Bot.BotName); + return false; + } + + byte maxPages; + if (byte.TryParse(lastPage, out maxPages) || (maxPages == 0)) { + Logging.LogNullError(nameof(maxPages), Bot.BotName); + return false; } GamesToFarm.Clear(); - CheckPage(htmlDocument); - if (maxPages <= 1) { + if (maxPages == 1) { return GamesToFarm.Count > 0; } @@ -423,22 +436,28 @@ namespace ArchiSteamFarm { } Bot.ArchiHandler.PlayGames(appID); + DateTime endFarmingDate = DateTime.Now.AddHours(Program.GlobalConfig.MaxFarmingTime); bool success = true; - bool? keepFarming = await ShouldFarm(appID).ConfigureAwait(false); - for (ushort farmingTime = 0; (farmingTime <= 60 * Program.GlobalConfig.MaxFarmingTime) && keepFarming.GetValueOrDefault(true); farmingTime += Program.GlobalConfig.FarmingDelay) { + + while (keepFarming.GetValueOrDefault(true) && (DateTime.Now < endFarmingDate)) { + Logging.LogGenericInfo("Still farming: " + appID, Bot.BotName); + + DateTime startFarmingPeriod = DateTime.Now; if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) { - success = false; - break; + FarmResetEvent.Reset(); + success = !KeepFarming; } // Don't forget to update our GamesToFarm hours - float timePlayed = Program.GlobalConfig.FarmingDelay / 60.0F; - GamesToFarm[appID] += timePlayed; + GamesToFarm[appID] += (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours; + + if (!success) { + break; + } keepFarming = await ShouldFarm(appID).ConfigureAwait(false); - Logging.LogGenericInfo("Still farming: " + appID, Bot.BotName); } Logging.LogGenericInfo("Stopped farming: " + appID, Bot.BotName); @@ -459,19 +478,25 @@ namespace ArchiSteamFarm { bool success = true; while (maxHour < 2) { + Logging.LogGenericInfo("Still farming: " + string.Join(", ", appIDs), Bot.BotName); + + DateTime startFarmingPeriod = DateTime.Now; if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) { - success = false; - break; + FarmResetEvent.Reset(); + success = !KeepFarming; } // Don't forget to update our GamesToFarm hours - float timePlayed = Program.GlobalConfig.FarmingDelay / 60.0F; + float timePlayed = (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours; foreach (uint appID in appIDs) { GamesToFarm[appID] += timePlayed; } + if (!success) { + break; + } + maxHour += timePlayed; - Logging.LogGenericInfo("Still farming: " + string.Join(", ", appIDs), Bot.BotName); } Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", appIDs), Bot.BotName);