From 4592a20c89343017d7e8aa8d8350b57b8b82c1c5 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 29 Jun 2019 15:59:38 +0200 Subject: [PATCH] General code review --- ArchiSteamFarm/ASF.cs | 11 +++----- ArchiSteamFarm/Bot.cs | 4 +++ .../ApiAuthenticationMiddleware.cs | 26 ++++++++++++------- ArchiSteamFarm/Json/Steam.cs | 26 +++++++++++++++++++ ArchiSteamFarm/Statistics.cs | 10 +++++++ 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/ArchiSteamFarm/ASF.cs b/ArchiSteamFarm/ASF.cs index 764454faa..533c05ee0 100644 --- a/ArchiSteamFarm/ASF.cs +++ b/ArchiSteamFarm/ASF.cs @@ -346,12 +346,7 @@ namespace ArchiSteamFarm { return false; } - switch (botName) { - case SharedInfo.ASF: - return false; - default: - return true; - } + return !botName.Equals(SharedInfo.ASF, StringComparison.OrdinalIgnoreCase); } private static async void OnChanged(object sender, FileSystemEventArgs e) { @@ -426,7 +421,7 @@ namespace ArchiSteamFarm { return; } - if (botName.Equals(SharedInfo.ASF)) { + if (botName.Equals(SharedInfo.ASF, StringComparison.OrdinalIgnoreCase)) { ArchiLogger.LogGenericInfo(Strings.GlobalConfigChanged); await RestartOrExit().ConfigureAwait(false); @@ -520,7 +515,7 @@ namespace ArchiSteamFarm { return; } - if (botName.Equals(SharedInfo.ASF)) { + if (botName.Equals(SharedInfo.ASF, StringComparison.OrdinalIgnoreCase)) { if (File.Exists(fullPath)) { return; } diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 0bec3bf3a..504591dbb 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -138,9 +138,11 @@ namespace ArchiSteamFarm { } } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = SharedInfo.UlongCompatibilityStringPrefix + nameof(SteamID))] [NotNull] private string SSteamID => SteamID.ToString(); +#pragma warning restore IDE0051 [JsonProperty] public EAccountFlags AccountFlags { get; private set; } @@ -168,8 +170,10 @@ namespace ArchiSteamFarm { private string AuthCode; +#pragma warning disable IDE0052 [JsonProperty] private string AvatarHash; +#pragma warning restore IDE0052 private Timer ConnectionFailureTimer; private string DeviceID; diff --git a/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs b/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs index 8d1eeb09f..2d184e815 100644 --- a/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs +++ b/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs @@ -39,20 +39,26 @@ namespace ArchiSteamFarm.IPC.Integration { private const byte MaxFailedAuthorizationAttempts = 5; private static readonly SemaphoreSlim AuthorizationSemaphore = new SemaphoreSlim(1, 1); - - [SuppressMessage("ReSharper", "UnusedMember.Local")] - private static readonly Timer ClearFailedAuthorizationsTimer = new Timer( - e => FailedAuthorizations.Clear(), - null, - TimeSpan.FromHours(FailedAuthorizationsCooldownInHours), // Delay - TimeSpan.FromHours(FailedAuthorizationsCooldownInHours) // Period - ); - private static readonly ConcurrentDictionary FailedAuthorizations = new ConcurrentDictionary(); + private static Timer ClearFailedAuthorizationsTimer; + private readonly RequestDelegate Next; - public ApiAuthenticationMiddleware([NotNull] RequestDelegate next) => Next = next ?? throw new ArgumentNullException(nameof(next)); + public ApiAuthenticationMiddleware([NotNull] RequestDelegate next) { + Next = next ?? throw new ArgumentNullException(nameof(next)); + + lock (FailedAuthorizations) { + if (ClearFailedAuthorizationsTimer == null) { + ClearFailedAuthorizationsTimer = new Timer( + e => FailedAuthorizations.Clear(), + null, + TimeSpan.FromHours(FailedAuthorizationsCooldownInHours), // Delay + TimeSpan.FromHours(FailedAuthorizationsCooldownInHours) // Period + ); + } + } + } [PublicAPI] public async Task InvokeAsync(HttpContext context) { diff --git a/ArchiSteamFarm/Json/Steam.cs b/ArchiSteamFarm/Json/Steam.cs index 605d2cd92..a7de8d9f2 100644 --- a/ArchiSteamFarm/Json/Steam.cs +++ b/ArchiSteamFarm/Json/Steam.cs @@ -70,6 +70,7 @@ namespace ArchiSteamFarm.Json { [PublicAPI] public EType Type { get; internal set; } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "amount", Required = Required.Always)] [NotNull] private string AmountText { @@ -91,6 +92,7 @@ namespace ArchiSteamFarm.Json { Amount = amount; } } +#pragma warning restore IDE0051 [JsonProperty(PropertyName = "assetid", Required = Required.DisallowNull)] [NotNull] @@ -114,6 +116,7 @@ namespace ArchiSteamFarm.Json { } } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "classid", Required = Required.DisallowNull)] [NotNull] private string ClassIDText { @@ -133,7 +136,9 @@ namespace ArchiSteamFarm.Json { ClassID = classID; } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "contextid", Required = Required.DisallowNull)] [NotNull] private string ContextIDText { @@ -155,13 +160,16 @@ namespace ArchiSteamFarm.Json { ContextID = contextID; } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)] [NotNull] private string IDText { get => AssetIDText; set => AssetIDText = value; } +#pragma warning restore IDE0051 // Constructed from trades being received or plugins public Asset(uint appID, ulong contextID, ulong classID, uint amount, bool marketable = true, uint realAppID = 0, EType type = EType.Unknown, ERarity rarity = ERarity.Unknown) { @@ -218,6 +226,7 @@ namespace ArchiSteamFarm.Json { internal ulong TradeOfferID { get; private set; } internal EType Type { get; private set; } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "html", Required = Required.DisallowNull)] private string HTML { set { @@ -287,6 +296,7 @@ namespace ArchiSteamFarm.Json { } } } +#pragma warning restore IDE0051 [JsonConstructor] private ConfirmationDetails() { } @@ -318,6 +328,7 @@ namespace ArchiSteamFarm.Json { [PublicAPI] public bool Success { get; private set; } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "success", Required = Required.Always)] private byte SuccessNumber { set { @@ -337,6 +348,7 @@ namespace ArchiSteamFarm.Json { } } } +#pragma warning restore IDE0051 [JsonConstructor] protected NumberResponse() { } @@ -418,6 +430,7 @@ namespace ArchiSteamFarm.Json { internal ulong LastAssetID { get; private set; } internal bool MoreItems { get; private set; } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "last_assetid", Required = Required.DisallowNull)] private string LastAssetIDText { set { @@ -436,11 +449,14 @@ namespace ArchiSteamFarm.Json { LastAssetID = lastAssetID; } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "more_items", Required = Required.DisallowNull)] private byte MoreItemsNumber { set => MoreItems = value > 0; } +#pragma warning restore IDE0051 [JsonConstructor] private InventoryResponse() { } @@ -456,6 +472,7 @@ namespace ArchiSteamFarm.Json { internal bool Tradable { get; private set; } internal Asset.EType Type { get; private set; } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "classid", Required = Required.Always)] private string ClassIDText { set { @@ -474,12 +491,16 @@ namespace ArchiSteamFarm.Json { ClassID = classID; } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "marketable", Required = Required.Always)] private byte MarketableNumber { set => Marketable = value > 0; } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "tags", Required = Required.DisallowNull)] private ImmutableHashSet Tags { set { @@ -492,11 +513,14 @@ namespace ArchiSteamFarm.Json { (Type, Rarity, RealAppID) = InterpretTags(value); } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "tradable", Required = Required.Always)] private byte TradableNumber { set => Tradable = value > 0; } +#pragma warning restore IDE0051 [JsonConstructor] private Description() { } @@ -703,6 +727,7 @@ namespace ArchiSteamFarm.Json { internal ulong TradeOfferID { get; private set; } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "tradeofferid", Required = Required.Always)] private string TradeOfferIDText { set { @@ -721,6 +746,7 @@ namespace ArchiSteamFarm.Json { TradeOfferID = tradeOfferID; } } +#pragma warning restore IDE0051 [JsonConstructor] private TradeOfferSendResponse() { } diff --git a/ArchiSteamFarm/Statistics.cs b/ArchiSteamFarm/Statistics.cs index 70e15cb5f..0a7b0cc59 100644 --- a/ArchiSteamFarm/Statistics.cs +++ b/ArchiSteamFarm/Statistics.cs @@ -644,6 +644,7 @@ namespace ArchiSteamFarm { internal bool MatchEverything { get; private set; } +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "matchable_backgrounds", Required = Required.Always)] private byte MatchableBackgroundsNumber { set { @@ -663,7 +664,9 @@ namespace ArchiSteamFarm { } } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "matchable_cards", Required = Required.Always)] private byte MatchableCardsNumber { set { @@ -683,7 +686,9 @@ namespace ArchiSteamFarm { } } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "matchable_emoticons", Required = Required.Always)] private byte MatchableEmoticonsNumber { set { @@ -703,7 +708,9 @@ namespace ArchiSteamFarm { } } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "matchable_foil_cards", Required = Required.Always)] private byte MatchableFoilCardsNumber { set { @@ -723,7 +730,9 @@ namespace ArchiSteamFarm { } } } +#pragma warning restore IDE0051 +#pragma warning disable IDE0051 [JsonProperty(PropertyName = "match_everything", Required = Required.Always)] private byte MatchEverythingNumber { set { @@ -743,6 +752,7 @@ namespace ArchiSteamFarm { } } } +#pragma warning restore IDE0051 [JsonConstructor] private ListedUser() { }