From f5b6899387543327db217db78d159f737688856a Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 19 Feb 2017 12:43:45 +0100 Subject: [PATCH] Code review --- ArchiSteamFarm/Bot.cs | 85 ++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 3bb049b69..c26ea87e8 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -208,10 +208,7 @@ namespace ArchiSteamFarm { CallbackManager.Subscribe(OnSharedLibraryLockStatus); ArchiWebHandler = new ArchiWebHandler(this); - CardsFarmer = new CardsFarmer(this); - CardsFarmer.SetInitialState(BotConfig.Paused); - //SteamSaleEvent = new SteamSaleEvent(this); Trading = new Trading(this); @@ -219,14 +216,7 @@ namespace ArchiSteamFarm { Statistics = new Statistics(this); } - if ((BotConfig.SendTradePeriod > 0) && (BotConfig.SteamMasterID != 0)) { - SendItemsTimer = new Timer( - async e => await ResponseLoot(BotConfig.SteamMasterID).ConfigureAwait(false), - null, - TimeSpan.FromHours(BotConfig.SendTradePeriod) + TimeSpan.FromMinutes(Bots.Count), // Delay - TimeSpan.FromHours(BotConfig.SendTradePeriod) // Period - ); - } + InitModules(); HeartBeatTimer = new Timer( async e => await HeartBeat().ConfigureAwait(false), @@ -235,7 +225,7 @@ namespace ArchiSteamFarm { TimeSpan.FromMinutes(1) // Period ); - Initialize().Forget(); + InitStart().Forget(); } public void Dispose() { @@ -430,28 +420,8 @@ namespace ArchiSteamFarm { Stop(false); BotConfig = args.BotConfig; - CardsFarmer.SetInitialState(BotConfig.Paused); - - if ((BotConfig.SendTradePeriod > 0) && (BotConfig.SteamMasterID != 0)) { - TimeSpan delay = TimeSpan.FromHours(BotConfig.SendTradePeriod) + TimeSpan.FromMinutes(Bots.Count); - TimeSpan period = TimeSpan.FromHours(BotConfig.SendTradePeriod); - - if (SendItemsTimer == null) { - SendItemsTimer = new Timer( - async e => await ResponseLoot(BotConfig.SteamMasterID).ConfigureAwait(false), - null, - delay, // Delay - period // Period - ); - } else { - SendItemsTimer.Change(delay, period); - } - } else if (SendItemsTimer != null) { - SendItemsTimer.Dispose(); - SendItemsTimer = null; - } - - await Initialize().ConfigureAwait(false); + InitModules(); + await InitStart().ConfigureAwait(false); } finally { InitializationSemaphore.Release(); } @@ -883,16 +853,6 @@ namespace ArchiSteamFarm { ); } - private async Task Initialize() { - if (!BotConfig.Enabled) { - ArchiLogger.LogGenericInfo(Strings.BotInstanceNotStartingBecauseDisabled); - return; - } - - // Start - await Start().ConfigureAwait(false); - } - private async Task InitializeFamilySharing() { HashSet steamIDs = await ArchiWebHandler.GetFamilySharingSteamIDs().ConfigureAwait(false); if ((steamIDs == null) || (steamIDs.Count == 0)) { @@ -902,7 +862,7 @@ namespace ArchiSteamFarm { SteamFamilySharingIDs.ReplaceIfNeededWith(steamIDs); } - private bool InitializeLoginAndPassword(bool requiresPassword) { + private bool InitLoginAndPassword(bool requiresPassword) { if (string.IsNullOrEmpty(BotConfig.SteamLogin)) { BotConfig.SteamLogin = Program.GetUserInput(ASF.EUserInputType.Login, BotName); if (string.IsNullOrEmpty(BotConfig.SteamLogin)) { @@ -918,6 +878,29 @@ namespace ArchiSteamFarm { return !string.IsNullOrEmpty(BotConfig.SteamPassword); } + private void InitModules() { + CardsFarmer.SetInitialState(BotConfig.Paused); + + if ((BotConfig.SendTradePeriod > 0) && (BotConfig.SteamMasterID != 0)) { + TimeSpan delay = TimeSpan.FromHours(BotConfig.SendTradePeriod) + TimeSpan.FromMinutes(Bots.Count); + TimeSpan period = TimeSpan.FromHours(BotConfig.SendTradePeriod); + + if (SendItemsTimer == null) { + SendItemsTimer = new Timer( + async e => await ResponseLoot(BotConfig.SteamMasterID).ConfigureAwait(false), + null, + delay, // Delay + period // Period + ); + } else { + SendItemsTimer.Change(delay, period); + } + } else if (SendItemsTimer != null) { + SendItemsTimer.Dispose(); + SendItemsTimer = null; + } + } + private void InitPermanentConnectionFailure() { if (!KeepRunning) { return; @@ -928,6 +911,16 @@ namespace ArchiSteamFarm { new Bot(BotName).Forget(); } + private async Task InitStart() { + if (!BotConfig.Enabled) { + ArchiLogger.LogGenericInfo(Strings.BotInstanceNotStartingBecauseDisabled); + return; + } + + // Start + await Start().ConfigureAwait(false); + } + private bool IsMaster(ulong steamID) { if (steamID != 0) { return (steamID == BotConfig.SteamMasterID) || IsOwner(steamID); @@ -1075,7 +1068,7 @@ namespace ArchiSteamFarm { } } - if (!InitializeLoginAndPassword(false)) { + if (!InitLoginAndPassword(false)) { Stop(); return; }