From 6a12a2661209acdb8d52447607e3265a699e273e Mon Sep 17 00:00:00 2001 From: JustArchi Date: Thu, 7 Jan 2016 22:29:55 +0100 Subject: [PATCH] WCF: Make it possible to launch also non-targetted commands --- ArchiSteamFarm/Bot.cs | 8 ++++++++ ArchiSteamFarm/Program.cs | 14 ++++++++------ ArchiSteamFarm/WCF.cs | 20 +++++++++++++------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 7f3f97765..2c00b630f 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -98,6 +98,14 @@ namespace ArchiSteamFarm { return true; } + internal static string GetAnyBotName() { + foreach (string botName in Bots.Keys) { + return botName; + } + + return null; + } + internal static int GetRunningBotsCount() { return Bots.Count; } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 9120072a8..01ac37b4a 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -151,7 +151,7 @@ namespace ArchiSteamFarm { } internal static void OnBotShutdown() { - if (Mode != EMode.Server && Bot.GetRunningBotsCount() == 0) { + if (Bot.GetRunningBotsCount() == 0) { Logging.LogGenericInfo("Main", "No bots are running, exiting"); ShutdownResetEvent.Set(); } @@ -190,10 +190,10 @@ namespace ArchiSteamFarm { continue; } - Logging.LogGenericNotice("WCF", "Command sent: " + arg); + Logging.LogGenericNotice("WCF", "Command sent: \"" + arg + "\""); // We intentionally execute this async block synchronously - Logging.LogGenericNotice("WCF", "Response received: " + WCF.SendCommand(arg)); + Logging.LogGenericNotice("WCF", "Response received: \"" + WCF.SendCommand(arg) + "\""); /* Task.Run(async () => { Logging.LogGenericNotice("WCF", "Response received: " + await WCF.SendCommand(arg).ConfigureAwait(false)); @@ -257,12 +257,14 @@ namespace ArchiSteamFarm { // Check if we got any bots running OnBotShutdown(); + // Wait for signal to shutdown ShutdownResetEvent.WaitOne(); - // We got a signal to shutdown - WCF.StopServer(); + // We got a signal to shutdown, consider giving user some time to read the message + Thread.Sleep(5000); - Thread.Sleep(5000); // We're shuting down, consider giving user some time to read the message + // This is over, cleanup only now + WCF.StopServer(); } } } diff --git a/ArchiSteamFarm/WCF.cs b/ArchiSteamFarm/WCF.cs index 125c2ec62..ea83a8ca9 100644 --- a/ArchiSteamFarm/WCF.cs +++ b/ArchiSteamFarm/WCF.cs @@ -63,21 +63,27 @@ namespace ArchiSteamFarm { } string[] args = input.Split(' '); - if (args.Length < 2) { - return "ERROR: Too few arguments, expected: (ExtraArgs)"; + + string botName; + + if (args.Length > 1) { // If we have args[1] provided, use given botName + botName = args[1]; + } else { // If not, just pick first one + botName = Bot.GetAnyBotName(); } - // Bot name is args[1] - string botName = args[1]; + if (string.IsNullOrEmpty(botName)) { + return "ERROR: Invalid botName: " + botName; + } Bot bot; if (!Bot.Bots.TryGetValue(botName, out bot)) { - return "ERROR: Couldn't find any bot named " + botName; + return "ERROR: Couldn't find any bot named: " + botName; } - string command = '!' + input; - Logging.LogGenericInfo("WCF", "Received command: \"" + command + "\""); + Logging.LogGenericInfo("WCF", "Received command: \"" + input + "\""); + string command = '!' + input; string output = bot.HandleMessage(command).Result; // TODO: This should be asynchronous Logging.LogGenericInfo("WCF", "Answered to command: \"" + input + "\" with: \"" + output + "\"");