From 27e37b79e0c5503821d3bffa15c7a0db87515b41 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 13 Feb 2018 13:23:33 +0100 Subject: [PATCH] Improve !owns output for multiple bots --- ArchiSteamFarm/Bot.cs | 49 ++++++++++--------- .../Localization/Strings.Designer.cs | 6 +-- ArchiSteamFarm/Localization/Strings.resx | 6 +-- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 04b23c0cc..09636688d 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -3507,18 +3507,18 @@ namespace ArchiSteamFarm { return responses.Count > 0 ? string.Join("", responses) : null; } - private async Task<(string Response, bool OwnsEverything)> ResponseOwns(ulong steamID, string query) { + private async Task<(string Response, HashSet OwnedGameIDs)> ResponseOwns(ulong steamID, string query) { if ((steamID == 0) || string.IsNullOrEmpty(query)) { ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(query)); - return (null, false); + return (null, null); } if (!IsOperator(steamID)) { - return (null, false); + return (null, null); } if (!IsConnectedAndLoggedOn) { - return (FormatBotResponse(Strings.BotNotConnected), false); + return (FormatBotResponse(Strings.BotNotConnected), null); } await LimitGiftsRequestsAsync().ConfigureAwait(false); @@ -3531,15 +3531,15 @@ namespace ArchiSteamFarm { } if ((ownedGames == null) || (ownedGames.Count == 0)) { - return (FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(ownedGames))), false); + return (FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(ownedGames))), null); } StringBuilder response = new StringBuilder(); - - bool ownsEverything = true; + HashSet ownedGameIDs = new HashSet(); if (query.Equals("*")) { foreach (KeyValuePair ownedGame in ownedGames) { + ownedGameIDs.Add(ownedGame.Key); response.Append(FormatBotResponse(string.Format(Strings.BotOwnedAlreadyWithName, ownedGame.Key, ownedGame.Value))); } } else { @@ -3549,14 +3549,15 @@ namespace ArchiSteamFarm { // Check if this is gameID if (uint.TryParse(game, out uint gameID) && (gameID != 0)) { if (OwnedPackageIDs.ContainsKey(gameID)) { + ownedGameIDs.Add(gameID); response.Append(FormatBotResponse(string.Format(Strings.BotOwnedAlready, gameID))); continue; } if (ownedGames.TryGetValue(gameID, out string ownedName)) { + ownedGameIDs.Add(gameID); response.Append(FormatBotResponse(string.Format(Strings.BotOwnedAlreadyWithName, gameID, ownedName))); } else { - ownsEverything = false; response.Append(FormatBotResponse(string.Format(Strings.BotNotOwnedYet, gameID))); } @@ -3564,19 +3565,14 @@ namespace ArchiSteamFarm { } // This is a string, so check our entire library - bool ownsAnything = false; foreach (KeyValuePair ownedGame in ownedGames.Where(ownedGame => ownedGame.Value.IndexOf(game, StringComparison.OrdinalIgnoreCase) >= 0)) { - ownsAnything = true; + ownedGameIDs.Add(ownedGame.Key); response.Append(FormatBotResponse(string.Format(Strings.BotOwnedAlreadyWithName, ownedGame.Key, ownedGame.Value))); } - - if (!ownsAnything) { - ownsEverything = false; - } } } - return (response.Length > 0 ? response.ToString() : FormatBotResponse(string.Format(Strings.BotNotOwnedYet, query)), ownsEverything); + return (response.Length > 0 ? response.ToString() : FormatBotResponse(string.Format(Strings.BotNotOwnedYet, query)), ownedGameIDs); } private static async Task ResponseOwns(ulong steamID, string botNames, string query) { @@ -3590,13 +3586,13 @@ namespace ArchiSteamFarm { return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null; } - IEnumerable> tasks = bots.Select(bot => bot.ResponseOwns(steamID, query)); - ICollection<(string Response, bool OwnsEverything)> results; + IEnumerable OwnedGameIDs)>> tasks = bots.Select(bot => bot.ResponseOwns(steamID, query)); + ICollection<(string Response, HashSet OwnedGameIDs)> results; switch (Program.GlobalConfig.OptimizationMode) { case GlobalConfig.EOptimizationMode.MinMemoryUsage: - results = new List<(string Response, bool OwnsEverything)>(bots.Count); - foreach (Task<(string Response, bool OwnsEverything)> task in tasks) { + results = new List<(string Response, HashSet OwnedGameIDs)>(bots.Count); + foreach (Task<(string Response, HashSet OwnedGameIDs)> task in tasks) { results.Add(await task.ConfigureAwait(false)); } @@ -3606,13 +3602,22 @@ namespace ArchiSteamFarm { break; } - List<(string Response, bool OwnsEverything)> validResults = new List<(string Response, bool OwnsEverything)>(results.Where(result => !string.IsNullOrEmpty(result.Response))); + List<(string Response, HashSet OwnedGameIDs)> validResults = new List<(string Response, HashSet OwnedGameIDs)>(results.Where(result => !string.IsNullOrEmpty(result.Response))); if (validResults.Count == 0) { return null; } - string extraResponse = string.Format(Strings.BotOwnsOverview, validResults.Count(result => result.OwnsEverything), validResults.Count); - return string.Join("", validResults.Select(result => result.Response)) + FormatStaticResponse(extraResponse); + Dictionary ownedGameCounts = new Dictionary(); + foreach (uint gameID in validResults.Where(validResult => (validResult.OwnedGameIDs != null) && (validResult.OwnedGameIDs.Count > 0)).SelectMany(validResult => validResult.OwnedGameIDs)) { + if (ownedGameCounts.TryGetValue(gameID, out ushort count)) { + ownedGameCounts[gameID] = ++count; + } else { + ownedGameCounts[gameID] = 1; + } + } + + IEnumerable extraResponses = ownedGameCounts.Select(kv => FormatStaticResponse(string.Format(Strings.BotOwnsOverviewPerGame, kv.Value, validResults.Count, kv.Key))); + return string.Join("", validResults.Select(result => result.Response).Concat(extraResponses)); } private string ResponsePassword(ulong steamID) { diff --git a/ArchiSteamFarm/Localization/Strings.Designer.cs b/ArchiSteamFarm/Localization/Strings.Designer.cs index 8231f1a06..25b1ffb79 100644 --- a/ArchiSteamFarm/Localization/Strings.Designer.cs +++ b/ArchiSteamFarm/Localization/Strings.Designer.cs @@ -484,11 +484,11 @@ namespace ArchiSteamFarm.Localization { } /// - /// Wyszukuje zlokalizowany ciąg podobny do ciągu There are {0}/{1} bots that already own all of the games being checked.. + /// Wyszukuje zlokalizowany ciąg podobny do ciągu {0}/{1} bots already own game {2}.. /// - internal static string BotOwnsOverview { + internal static string BotOwnsOverviewPerGame { get { - return ResourceManager.GetString("BotOwnsOverview", resourceCulture); + return ResourceManager.GetString("BotOwnsOverviewPerGame", resourceCulture); } } diff --git a/ArchiSteamFarm/Localization/Strings.resx b/ArchiSteamFarm/Localization/Strings.resx index f53eb1b09..84c6208ed 100644 --- a/ArchiSteamFarm/Localization/Strings.resx +++ b/ArchiSteamFarm/Localization/Strings.resx @@ -654,9 +654,9 @@ StackTrace: Done clearing Steam discovery queue #{0}. {0} will be replaced by queue number - - There are {0}/{1} bots that already own all of the games being checked. - {0} will be replaced by number of bots that already own games being checked, {1} will be replaced by total number of bots that were checked during the process + + {0}/{1} bots already own game {2}. + {0} will be replaced by number of bots that already own particular game being checked, {1} will be replaced by total number of bots that were checked during the process, {2} will be replaced by game's ID (number) Refreshing packages data...