From acae0d8d0e3d5ec147d53d447c1298a4f47420b2 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 13 Oct 2017 09:05:08 +0200 Subject: [PATCH] Improve shutdown looting As discussed in http://steamcommunity.com/groups/ascfarm/discussions/1/1483232961032339275/ - it's possible that SendOnFarmingFinished + ShutdownOnFarmingFinished bot will shutdown before master receives the trade and accepts it through default trading preferences. This can be easily solved by caching SteamID and using it instead. While I'm at it, let's rewrite other parts that made use of SteamID as well. --- ArchiSteamFarm/Bot.cs | 39 +++++++++++++++++++---------------- ArchiSteamFarm/Statistics.cs | 4 ++-- ArchiSteamFarm/SteamTarget.cs | 4 ++-- ArchiSteamFarm/Trading.cs | 2 +- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 8cf476455..04033ce9c 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -67,9 +67,6 @@ namespace ArchiSteamFarm { internal bool IsConnectedAndLoggedOn => SteamID != 0; internal bool IsPlayingPossible => !PlayingBlocked && (LibraryLockedBySteamID == 0); - [JsonProperty] - internal ulong SteamID => SteamClient?.SteamID ?? 0; - private readonly ArchiHandler ArchiHandler; private readonly BotDatabase BotDatabase; private readonly string BotName; @@ -96,9 +93,14 @@ namespace ArchiSteamFarm { private bool IsAccountLocked => AccountFlags.HasFlag(EAccountFlags.Lockdown); private string SentryFile => BotPath + ".bin"; + [JsonProperty] + private ulong SteamID => SteamClient?.SteamID ?? 0; + [JsonProperty] internal BotConfig BotConfig { get; private set; } + internal ulong CachedSteamID { get; private set; } + [JsonProperty] internal bool KeepRunning { get; private set; } @@ -676,7 +678,7 @@ namespace ArchiSteamFarm { return false; } - if (await ArchiWebHandler.Init(SteamID, SteamClient.Universe, callback.Nonce, BotConfig.SteamParentalPIN).ConfigureAwait(false)) { + if (await ArchiWebHandler.Init(CachedSteamID, SteamClient.Universe, callback.Nonce, BotConfig.SteamParentalPIN).ConfigureAwait(false)) { return true; } @@ -707,7 +709,7 @@ namespace ArchiSteamFarm { return; } - SteamFriends.RequestFriendInfo(SteamID, EClientPersonaStateFlag.PlayerName | EClientPersonaStateFlag.Presence); + SteamFriends.RequestFriendInfo(CachedSteamID, EClientPersonaStateFlag.PlayerName | EClientPersonaStateFlag.Presence); } internal async Task Response(ulong steamID, string message, ulong chatID = 0) { @@ -1111,7 +1113,7 @@ namespace ArchiSteamFarm { return result; } - private ulong GetFirstSteamMasterID() => BotConfig.SteamUserPermissions.Where(kv => (kv.Key != 0) && (kv.Key != SteamID) && (kv.Value == BotConfig.EPermission.Master)).Select(kv => kv.Key).OrderBy(steamID => steamID).FirstOrDefault(); + private ulong GetFirstSteamMasterID() => BotConfig.SteamUserPermissions.Where(kv => (kv.Key != 0) && (kv.Key != CachedSteamID) && (kv.Value == BotConfig.EPermission.Master)).Select(kv => kv.Key).OrderBy(steamID => steamID).FirstOrDefault(); private BotConfig.EPermission GetSteamUserPermission(ulong steamID) { if (steamID != 0) { @@ -1848,6 +1850,7 @@ namespace ArchiSteamFarm { } AccountFlags = callback.AccountFlags; + CachedSteamID = callback.ClientSteamID; if (IsAccountLimited) { ArchiLogger.LogGenericWarning(Strings.BotAccountLimited); @@ -2031,7 +2034,7 @@ namespace ArchiSteamFarm { return; } - if (callback.FriendID == SteamID) { + if (callback.FriendID == CachedSteamID) { Statistics?.OnPersonaState(callback).Forget(); } else if ((callback.FriendID == LibraryLockedBySteamID) && (callback.GameID == 0)) { LibraryLockedBySteamID = 0; @@ -2073,13 +2076,13 @@ namespace ArchiSteamFarm { // Ignore no status updates if (LibraryLockedBySteamID == 0) { - if ((callback.LibraryLockedBySteamID == 0) || (callback.LibraryLockedBySteamID == SteamID)) { + if ((callback.LibraryLockedBySteamID == 0) || (callback.LibraryLockedBySteamID == CachedSteamID)) { return; } LibraryLockedBySteamID = callback.LibraryLockedBySteamID; } else { - if ((callback.LibraryLockedBySteamID != 0) && (callback.LibraryLockedBySteamID != SteamID)) { + if ((callback.LibraryLockedBySteamID != 0) && (callback.LibraryLockedBySteamID != CachedSteamID)) { return; } @@ -2965,7 +2968,7 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotLootingMasterNotDefined); } - if (targetSteamMasterID == SteamID) { + if (targetSteamMasterID == CachedSteamID) { return FormatBotResponse(Strings.BotLootingYourself); } @@ -3158,7 +3161,7 @@ namespace ArchiSteamFarm { Dictionary ownedGames; if (await ArchiWebHandler.HasValidApiKey().ConfigureAwait(false)) { - ownedGames = await ArchiWebHandler.GetOwnedGames(SteamID).ConfigureAwait(false); + ownedGames = await ArchiWebHandler.GetOwnedGames(CachedSteamID).ConfigureAwait(false); } else { ownedGames = await ArchiWebHandler.GetMyOwnedGames().ConfigureAwait(false); } @@ -4036,11 +4039,11 @@ namespace ArchiSteamFarm { return IsOwner(steamID) ? FormatBotResponse(string.Format(Strings.BotNotFound, botNameTo)) : null; } - if (targetBot.SteamID == 0) { + if (!targetBot.IsConnectedAndLoggedOn) { return FormatBotResponse(Strings.BotNotConnected); } - if (targetBot.SteamID == SteamID) { + if (targetBot.CachedSteamID == CachedSteamID) { return FormatBotResponse(Strings.BotLootingYourself); } @@ -4101,7 +4104,7 @@ namespace ArchiSteamFarm { string tradeToken = null; - if (SteamFriends.GetFriendRelationship(targetBot.SteamID) != EFriendRelationship.Friend) { + if (SteamFriends.GetFriendRelationship(targetBot.CachedSteamID) != EFriendRelationship.Friend) { tradeToken = await targetBot.ArchiWebHandler.GetTradeToken().ConfigureAwait(false); if (string.IsNullOrEmpty(tradeToken)) { return FormatBotResponse(Strings.BotLootingFailed); @@ -4112,14 +4115,14 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotLootingFailed); } - if (!await ArchiWebHandler.SendTradeOffer(inventory, targetBot.SteamID, tradeToken).ConfigureAwait(false)) { + if (!await ArchiWebHandler.SendTradeOffer(inventory, targetBot.CachedSteamID, tradeToken).ConfigureAwait(false)) { return FormatBotResponse(Strings.BotLootingFailed); } if (HasMobileAuthenticator) { // Give Steam network some time to generate confirmations await Task.Delay(3000).ConfigureAwait(false); - if (!await AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, targetBot.SteamID).ConfigureAwait(false)) { + if (!await AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, targetBot.CachedSteamID).ConfigureAwait(false)) { return FormatBotResponse(Strings.BotLootingFailed); } } @@ -4272,7 +4275,7 @@ namespace ArchiSteamFarm { return; } - ArchiLogger.LogGenericTrace(steamID + "/" + SteamID + ": " + message); + ArchiLogger.LogGenericTrace(steamID + "/" + CachedSteamID + ": " + message); for (int i = 0; i < message.Length; i += MaxSteamMessageLength - 2) { if (i > 0) { @@ -4294,7 +4297,7 @@ namespace ArchiSteamFarm { return; } - ArchiLogger.LogGenericTrace(steamID + "/" + SteamID + ": " + message); + ArchiLogger.LogGenericTrace(steamID + "/" + CachedSteamID + ": " + message); for (int i = 0; i < message.Length; i += MaxSteamMessageLength - 2) { if (i > 0) { diff --git a/ArchiSteamFarm/Statistics.cs b/ArchiSteamFarm/Statistics.cs index a5db86190..fcae91b49 100644 --- a/ArchiSteamFarm/Statistics.cs +++ b/ArchiSteamFarm/Statistics.cs @@ -71,7 +71,7 @@ namespace ArchiSteamFarm { const string request = URL + "/api/HeartBeat"; Dictionary data = new Dictionary(2) { - { "SteamID", Bot.SteamID.ToString() }, + { "SteamID", Bot.CachedSteamID.ToString() }, { "Guid", Program.GlobalDatabase.Guid.ToString("N") } }; @@ -138,7 +138,7 @@ namespace ArchiSteamFarm { const string request = URL + "/api/Announce"; Dictionary data = new Dictionary(8) { - { "SteamID", Bot.SteamID.ToString() }, + { "SteamID", Bot.CachedSteamID.ToString() }, { "Guid", Program.GlobalDatabase.Guid.ToString("N") }, { "Nickname", nickname }, { "AvatarHash", avatarHash }, diff --git a/ArchiSteamFarm/SteamTarget.cs b/ArchiSteamFarm/SteamTarget.cs index 273a2b1c3..ff6c061a3 100644 --- a/ArchiSteamFarm/SteamTarget.cs +++ b/ArchiSteamFarm/SteamTarget.cs @@ -64,7 +64,7 @@ namespace ArchiSteamFarm { Bot bot; if (string.IsNullOrEmpty(BotName)) { - bot = Bot.Bots.Values.FirstOrDefault(targetBot => targetBot.IsConnectedAndLoggedOn && (targetBot.SteamID != SteamID)); + bot = Bot.Bots.Values.FirstOrDefault(targetBot => targetBot.IsConnectedAndLoggedOn && (targetBot.CachedSteamID != SteamID)); if (bot == null) { return; } @@ -73,7 +73,7 @@ namespace ArchiSteamFarm { return; } - if (!bot.IsConnectedAndLoggedOn || (bot.SteamID == SteamID)) { + if (!bot.IsConnectedAndLoggedOn || (bot.CachedSteamID == SteamID)) { return; } } diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 4e103c9e5..cadf631d6 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -266,7 +266,7 @@ namespace ArchiSteamFarm { } // Otherwise we either accept donations but not bot trades, or we accept bot trades but not donations - bool isBotTrade = (tradeOffer.OtherSteamID64 != 0) && Bot.Bots.Values.Any(bot => bot.SteamID == tradeOffer.OtherSteamID64); + bool isBotTrade = (tradeOffer.OtherSteamID64 != 0) && Bot.Bots.Values.Any(bot => bot.CachedSteamID == tradeOffer.OtherSteamID64); return new ParseTradeResult(tradeOffer.TradeOfferID, (acceptDonations && !isBotTrade) || (acceptBotTrades && isBotTrade) ? ParseTradeResult.EResult.AcceptedWithoutItemLose : ParseTradeResult.EResult.RejectedPermanently); }