From d85d41c215bef66189dbebc65e965c832dd1604a Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 1 Aug 2016 18:17:51 +0200 Subject: [PATCH] General improvements --- ArchiSteamFarm/ArchiHandler.cs | 2 +- ArchiSteamFarm/Bot.cs | 34 +++++++------- ArchiSteamFarm/CardsFarmer.cs | 82 +++++++++++++--------------------- ArchiSteamFarm/WCF.cs | 17 ++++--- 4 files changed, 59 insertions(+), 76 deletions(-) diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index f79df9e88..4a0356e8c 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -223,7 +223,7 @@ namespace ArchiSteamFarm { PlayGames(new List { gameID }, gameName); } - internal void PlayGames(ICollection gameIDs, string gameName = null) { + internal void PlayGames(IEnumerable gameIDs, string gameName = null) { if (gameIDs == null) { Logging.LogNullError(nameof(gameIDs), Bot.BotName); return; diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index a3f2ab1e2..df457e7cb 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -79,6 +79,19 @@ namespace ArchiSteamFarm { private bool FirstTradeSent, InvalidPassword, SkipFirstShutdown; private string AuthCode, TwoFactorCode; + internal static string GetAPIStatus() { + var response = new { + Bots + }; + + try { + return JsonConvert.SerializeObject(response); + } catch (JsonException e) { + Logging.LogGenericException(e); + return null; + } + } + internal static void InitializeCMs(uint cellID, IServerListProvider serverListProvider) { if (serverListProvider == null) { Logging.LogNullError(nameof(serverListProvider)); @@ -835,25 +848,12 @@ namespace ArchiSteamFarm { } private static string ResponseAPI(ulong steamID) { - if (steamID == 0) { - Logging.LogNullError(nameof(steamID)); - return null; + if (steamID != 0) { + return !IsOwner(steamID) ? null : GetAPIStatus(); } - if (!IsOwner(steamID)) { - return null; - } - - var response = new { - Bots - }; - - try { - return JsonConvert.SerializeObject(response); - } catch (JsonException e) { - Logging.LogGenericException(e); - return null; - } + Logging.LogNullError(nameof(steamID)); + return null; } private static string ResponseExit(ulong steamID) { diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index 8bf5f70e3..6472ac647 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -81,7 +81,7 @@ namespace ArchiSteamFarm { internal readonly ConcurrentHashSet GamesToFarm = new ConcurrentHashSet(); [JsonProperty] - internal readonly ConcurrentHashSet CurrentGamesFarming = new ConcurrentHashSet(); + internal readonly ConcurrentHashSet CurrentGamesFarming = new ConcurrentHashSet(); private readonly ManualResetEventSlim FarmResetEvent = new ManualResetEventSlim(false); private readonly SemaphoreSlim FarmingSemaphore = new SemaphoreSlim(1); @@ -170,12 +170,12 @@ namespace ArchiSteamFarm { if (Bot.BotConfig.CardDropsRestricted) { // If we have restricted card drops, we use complex algorithm Logging.LogGenericInfo("Chosen farming algorithm: Complex", Bot.BotName); while (GamesToFarm.Count > 0) { - HashSet gamesToFarmSolo = GetGamesToFarmSolo(GamesToFarm); + HashSet gamesToFarmSolo = new HashSet(GamesToFarm.Where(game => game.HoursPlayed >= 2)); if (gamesToFarmSolo.Count > 0) { while (gamesToFarmSolo.Count > 0) { - uint appID = gamesToFarmSolo.First(); - if (await FarmSolo(appID).ConfigureAwait(false)) { - gamesToFarmSolo.Remove(appID); + Game game = gamesToFarmSolo.First(); + if (await FarmSolo(game).ConfigureAwait(false)) { + gamesToFarmSolo.Remove(game); } else { NowFarming = false; return; @@ -183,8 +183,7 @@ namespace ArchiSteamFarm { } } else { if (FarmMultiple()) { - Logging.LogGenericInfo("Done farming: " + string.Join(", ", GamesToFarm.Select(g => g.AppID)), - Bot.BotName); + Logging.LogGenericInfo("Done farming: " + string.Join(", ", GamesToFarm.Select(g => g.AppID)), Bot.BotName); } else { NowFarming = false; return; @@ -194,8 +193,7 @@ namespace ArchiSteamFarm { } else { // If we have unrestricted card drops, we use simple algorithm Logging.LogGenericInfo("Chosen farming algorithm: Simple", Bot.BotName); while (GamesToFarm.Count > 0) { - uint appID = GamesToFarm.First().AppID; - if (await FarmSolo(appID).ConfigureAwait(false)) { + if (await FarmSolo(GamesToFarm.First()).ConfigureAwait(false)) { continue; } @@ -267,20 +265,6 @@ namespace ArchiSteamFarm { } } - private static HashSet GetGamesToFarmSolo(IEnumerable gamesToFarm) { - if (gamesToFarm == null) { - Logging.LogNullError(nameof(gamesToFarm)); - return null; - } - - HashSet result = new HashSet(); - foreach (Game game in gamesToFarm.Where(g => g.HoursPlayed >= 2)) { - result.Add(game.AppID); - } - - return result; - } - private async Task IsAnythingToFarm() { Logging.LogGenericInfo("Checking badges...", Bot.BotName); @@ -564,7 +548,7 @@ namespace ArchiSteamFarm { float maxHour = 0; foreach (Game game in GamesToFarm) { - CurrentGamesFarming.Add(game.AppID); + CurrentGamesFarming.Add(game); if (game.HoursPlayed > maxHour) { maxHour = game.HoursPlayed; } @@ -586,49 +570,44 @@ namespace ArchiSteamFarm { return result; } - private async Task FarmSolo(uint appID) { - if (appID == 0) { - Logging.LogNullError(nameof(appID), Bot.BotName); + private async Task FarmSolo(Game game) { + if (game == null) { + Logging.LogNullError(nameof(game), Bot.BotName); return true; } - CurrentGamesFarming.Add(appID); + CurrentGamesFarming.Add(game); - Logging.LogGenericInfo("Now farming: " + appID, Bot.BotName); + Logging.LogGenericInfo("Now farming: " + game.AppID + " (" + game.GameName + ")", Bot.BotName); - bool result = await Farm(appID).ConfigureAwait(false); + bool result = await Farm(game).ConfigureAwait(false); CurrentGamesFarming.ClearAndTrim(); if (!result) { return false; } - Game game = GamesToFarm.FirstOrDefault(g => g.AppID == appID); - if (game == null) { - return false; - } - GamesToFarm.Remove(game); TimeSpan timeSpan = TimeSpan.FromHours(game.HoursPlayed); - Logging.LogGenericInfo("Done farming: " + appID + " after " + timeSpan.ToString(@"hh\:mm") + " hours of playtime!", Bot.BotName); + Logging.LogGenericInfo("Done farming: " + game.AppID + " (" + game.GameName + ") after " + timeSpan.ToString(@"hh\:mm") + " hours of playtime!", Bot.BotName); return true; } - private async Task Farm(uint appID) { - if (appID == 0) { - Logging.LogNullError(nameof(appID), Bot.BotName); + private async Task Farm(Game game) { + if (game == null) { + Logging.LogNullError(nameof(game), Bot.BotName); return false; } - Bot.ArchiHandler.PlayGame(appID, Bot.BotConfig.CustomGamePlayedWhileFarming); + Bot.ArchiHandler.PlayGame(game.AppID, Bot.BotConfig.CustomGamePlayedWhileFarming); DateTime endFarmingDate = DateTime.Now.AddHours(Program.GlobalConfig.MaxFarmingTime); bool success = true; - bool? keepFarming = await ShouldFarm(appID).ConfigureAwait(false); + bool? keepFarming = await ShouldFarm(game.AppID).ConfigureAwait(false); while (keepFarming.GetValueOrDefault(true) && (DateTime.Now < endFarmingDate)) { - Logging.LogGenericInfo("Still farming: " + appID, Bot.BotName); + Logging.LogGenericInfo("Still farming: " + game.AppID + " (" + game.GameName + ")", Bot.BotName); DateTime startFarmingPeriod = DateTime.Now; if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) { @@ -637,31 +616,30 @@ namespace ArchiSteamFarm { } // Don't forget to update our GamesToFarm hours - Game game = GamesToFarm.First(g => g.AppID == appID); game.HoursPlayed += (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours; if (!success) { break; } - keepFarming = await ShouldFarm(appID).ConfigureAwait(false); + keepFarming = await ShouldFarm(game.AppID).ConfigureAwait(false); } - Logging.LogGenericInfo("Stopped farming: " + appID, Bot.BotName); + Logging.LogGenericInfo("Stopped farming: " + game.AppID + " (" + game.GameName + ")", Bot.BotName); return success; } - private bool FarmHours(float maxHour, ConcurrentHashSet appIDs) { - if ((maxHour < 0) || (appIDs == null) || (appIDs.Count == 0)) { - Logging.LogNullError(nameof(maxHour) + " || " + nameof(appIDs) + " || " + nameof(appIDs.Count), Bot.BotName); + private bool FarmHours(float maxHour, ConcurrentHashSet games) { + if ((maxHour < 0) || (games == null) || (games.Count == 0)) { + Logging.LogNullError(nameof(maxHour) + " || " + nameof(games), Bot.BotName); return false; } - Bot.ArchiHandler.PlayGames(appIDs, Bot.BotConfig.CustomGamePlayedWhileFarming); + Bot.ArchiHandler.PlayGames(games.Select(game => game.AppID), Bot.BotConfig.CustomGamePlayedWhileFarming); bool success = true; while (maxHour < 2) { - Logging.LogGenericInfo("Still farming: " + string.Join(", ", appIDs), Bot.BotName); + Logging.LogGenericInfo("Still farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName); DateTime startFarmingPeriod = DateTime.Now; if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) { @@ -671,7 +649,7 @@ namespace ArchiSteamFarm { // Don't forget to update our GamesToFarm hours float timePlayed = (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours; - foreach (Game game in GamesToFarm.Where(game => appIDs.Contains(game.AppID))) { + foreach (Game game in games) { game.HoursPlayed += timePlayed; } @@ -682,7 +660,7 @@ namespace ArchiSteamFarm { maxHour += timePlayed; } - Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", appIDs), Bot.BotName); + Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName); return success; } } diff --git a/ArchiSteamFarm/WCF.cs b/ArchiSteamFarm/WCF.cs index 8da8ee4ad..5900e3500 100644 --- a/ArchiSteamFarm/WCF.cs +++ b/ArchiSteamFarm/WCF.cs @@ -31,6 +31,9 @@ using System.ServiceModel.Description; namespace ArchiSteamFarm { [ServiceContract] internal interface IWCF { + [OperationContract] + string GetStatus(); + [OperationContract] string HandleCommand(string input); } @@ -59,15 +62,15 @@ namespace ArchiSteamFarm { return null; } + if (Program.GlobalConfig.SteamOwnerID == 0) { + return "Refusing to handle request because SteamOwnerID is not set!"; + } + Bot bot = Bot.Bots.Values.FirstOrDefault(); if (bot == null) { return "ERROR: No bots are enabled!"; } - if (Program.GlobalConfig.SteamOwnerID == 0) { - return "Refusing to handle request because SteamOwnerID is not set!"; - } - string command = "!" + input; string output = bot.Response(Program.GlobalConfig.SteamOwnerID, command).Result; // TODO: This should be asynchronous @@ -75,6 +78,8 @@ namespace ArchiSteamFarm { return output; } + public string GetStatus() => Program.GlobalConfig.SteamOwnerID == 0 ? "{}" : Bot.GetAPIStatus(); + public void Dispose() { Client?.Close(); StopServer(); @@ -138,10 +143,10 @@ namespace ArchiSteamFarm { } } - internal sealed class Client : ClientBase, IWCF { + internal sealed class Client : ClientBase { internal Client(Binding binding, EndpointAddress address) : base(binding, address) { } - public string HandleCommand(string input) { + internal string HandleCommand(string input) { if (string.IsNullOrEmpty(input)) { Logging.LogNullError(nameof(input)); return null;