From 2a02e5a258bffca5bb03c2b1198a4c9057bf1f27 Mon Sep 17 00:00:00 2001 From: Noah Stolk <31079637+NoahStolk@users.noreply.github.com> Date: Fri, 29 Mar 2024 19:46:35 +0100 Subject: [PATCH] Fix or suppress some warnings --- .../Program.cs | 6 +++--- .../Mods/AssetType.cs | 2 ++ .../Spawnsets/SpawnsetSorting.cs | 2 ++ .../Spawnsets/GetSpawnsetByHashCustomLeaderboard.cs | 2 +- src/DevilDaggersInfo.Web.Client/Constants.cs | 4 ++-- .../CustomLeaderboardCriteriaTypeExtensions.cs | 12 ++++++------ .../StateObjects/Admin/Mods/AddModState.cs | 2 +- .../PlayerCustomLeaderboardStatisticsRepository.cs | 4 ++-- .../GlobalCustomLeaderboardEntryData.cs | 2 +- .../Utils/IntegerArrayCompressor.cs | 2 +- .../DiscordUserIdFetchBackgroundService.cs | 4 ++-- .../LeaderboardHistoryBackgroundService.cs | 6 +++--- .../PlayerNameFetchBackgroundService.cs | 8 ++++---- .../Middleware/ExceptionMiddleware.cs | 2 +- 14 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/DevilDaggersInfo.DevUtil.FetchLeaderboard/Program.cs b/src/DevilDaggersInfo.DevUtil.FetchLeaderboard/Program.cs index d0e463286..4a80ffb82 100644 --- a/src/DevilDaggersInfo.DevUtil.FetchLeaderboard/Program.cs +++ b/src/DevilDaggersInfo.DevUtil.FetchLeaderboard/Program.cs @@ -11,7 +11,7 @@ for (int i = 0; i < totalPages; i++) { stopwatch.Restart(); - entries.AddRange(await Fetch(i * 100 + 1)); + entries.AddRange(await FetchAsync(i * 100 + 1)); Console.WriteLine($"Fetching page {i + 1}/{totalPages} took {stopwatch.ElapsedMilliseconds / 1000f} seconds."); // Write the file after every fetch in case it crashes and all progress is lost. @@ -35,7 +35,7 @@ static byte[] GetBytes(List entries) return ms.ToArray(); } -static async Task> Fetch(int rank) +static async Task> FetchAsync(int rank) { byte[] data = await ExecuteRequest(rank); @@ -43,7 +43,7 @@ static async Task> Fetch(int rank) int rankIterator = 0; int bytePos = 83; - List compressedEntries = new(); + List compressedEntries = []; while (rankIterator < entryCount) { short usernameLength = BitConverter.ToInt16(data, bytePos); diff --git a/src/DevilDaggersInfo.Web.ApiSpec.Main/Mods/AssetType.cs b/src/DevilDaggersInfo.Web.ApiSpec.Main/Mods/AssetType.cs index ce13c15fd..70e4b7630 100644 --- a/src/DevilDaggersInfo.Web.ApiSpec.Main/Mods/AssetType.cs +++ b/src/DevilDaggersInfo.Web.ApiSpec.Main/Mods/AssetType.cs @@ -1,6 +1,8 @@ namespace DevilDaggersInfo.Web.ApiSpec.Main.Mods; +#pragma warning disable CA1027 // Not a flag enum. public enum AssetType : byte +#pragma warning restore CA1027 { Mesh = 0x01, Texture = 0x02, diff --git a/src/DevilDaggersInfo.Web.ApiSpec.Main/Spawnsets/SpawnsetSorting.cs b/src/DevilDaggersInfo.Web.ApiSpec.Main/Spawnsets/SpawnsetSorting.cs index 241f0a08f..7f670bac1 100644 --- a/src/DevilDaggersInfo.Web.ApiSpec.Main/Spawnsets/SpawnsetSorting.cs +++ b/src/DevilDaggersInfo.Web.ApiSpec.Main/Spawnsets/SpawnsetSorting.cs @@ -1,6 +1,8 @@ namespace DevilDaggersInfo.Web.ApiSpec.Main.Spawnsets; +#pragma warning disable CA1027 // Not a flag enum. public enum SpawnsetSorting +#pragma warning restore CA1027 { Name = 0, AuthorName = 1, diff --git a/src/DevilDaggersInfo.Web.ApiSpec.Tools/Spawnsets/GetSpawnsetByHashCustomLeaderboard.cs b/src/DevilDaggersInfo.Web.ApiSpec.Tools/Spawnsets/GetSpawnsetByHashCustomLeaderboard.cs index 273b88667..94bba8dc0 100644 --- a/src/DevilDaggersInfo.Web.ApiSpec.Tools/Spawnsets/GetSpawnsetByHashCustomLeaderboard.cs +++ b/src/DevilDaggersInfo.Web.ApiSpec.Tools/Spawnsets/GetSpawnsetByHashCustomLeaderboard.cs @@ -4,5 +4,5 @@ public record GetSpawnsetByHashCustomLeaderboard { public required int CustomLeaderboardId { get; init; } - public required List CustomEntries { get; init; } = new(); + public required List CustomEntries { get; init; } = []; } diff --git a/src/DevilDaggersInfo.Web.Client/Constants.cs b/src/DevilDaggersInfo.Web.Client/Constants.cs index 9f5579d40..1abc21bd1 100644 --- a/src/DevilDaggersInfo.Web.Client/Constants.cs +++ b/src/DevilDaggersInfo.Web.Client/Constants.cs @@ -6,12 +6,12 @@ public static class Constants public const string DiscordUrl = "https://discord.gg/NF32j8S"; #pragma warning restore S1075 // URIs should not be hardcoded - public static DateTime TrackingCustomLeaderboardSubmitCounts { get; } = new(2020, 8, 25); - // TODO: Move to main and admin API projects? public const int PageSizeDefault = 25; public const int PageSizeMin = 15; public const int PageSizeMax = 35; + public static DateTime TrackingCustomLeaderboardSubmitCounts { get; } = new(2020, 8, 25, 0, 0, 0, DateTimeKind.Utc); + public static IEnumerable PageSizeOptions { get; } = Enumerable.Range(3, 5).Select(i => i * 5); } diff --git a/src/DevilDaggersInfo.Web.Client/Extensions/CustomLeaderboardCriteriaTypeExtensions.cs b/src/DevilDaggersInfo.Web.Client/Extensions/CustomLeaderboardCriteriaTypeExtensions.cs index 8a0dfb7e7..88a52ca7d 100644 --- a/src/DevilDaggersInfo.Web.Client/Extensions/CustomLeaderboardCriteriaTypeExtensions.cs +++ b/src/DevilDaggersInfo.Web.Client/Extensions/CustomLeaderboardCriteriaTypeExtensions.cs @@ -124,12 +124,12 @@ public static string GetColor(this CustomLeaderboardCriteriaType criteriaType) { return criteriaType switch { - CustomLeaderboardCriteriaType.GemsCollected => "#f00",// TODO: Use same color as graph - CustomLeaderboardCriteriaType.GemsDespawned => "#888",// TODO: Use same color as graph - CustomLeaderboardCriteriaType.GemsEaten => "#0f0",// TODO: Use same color as graph - CustomLeaderboardCriteriaType.EnemiesKilled => "#f80",// TODO: Use same color as graph - CustomLeaderboardCriteriaType.DaggersFired => "#f80",// TODO: Use same color as graph - CustomLeaderboardCriteriaType.DaggersHit => "#f80",// TODO: Use same color as graph + CustomLeaderboardCriteriaType.GemsCollected => "#f00", // TODO: Use same color as graph + CustomLeaderboardCriteriaType.GemsDespawned => "#888", // TODO: Use same color as graph + CustomLeaderboardCriteriaType.GemsEaten => "#0f0", // TODO: Use same color as graph + CustomLeaderboardCriteriaType.EnemiesKilled => "#f80", // TODO: Use same color as graph + CustomLeaderboardCriteriaType.DaggersFired => "#f80", // TODO: Use same color as graph + CustomLeaderboardCriteriaType.DaggersHit => "#f80", // TODO: Use same color as graph CustomLeaderboardCriteriaType.HomingStored => UpgradesV3_2.Level4.Color.HexCode, CustomLeaderboardCriteriaType.HomingEaten => EnemiesV3_2.Ghostpede.Color.HexCode, CustomLeaderboardCriteriaType.Skull1Kills => EnemiesV3_2.Skull1.Color.HexCode, diff --git a/src/DevilDaggersInfo.Web.Client/StateObjects/Admin/Mods/AddModState.cs b/src/DevilDaggersInfo.Web.Client/StateObjects/Admin/Mods/AddModState.cs index 3c542826e..29d7712d7 100644 --- a/src/DevilDaggersInfo.Web.Client/StateObjects/Admin/Mods/AddModState.cs +++ b/src/DevilDaggersInfo.Web.Client/StateObjects/Admin/Mods/AddModState.cs @@ -25,7 +25,7 @@ public class AddModState : IStateObject public List? PlayerIds { get; set; } [MaxLength(ModConstants.BinaryMaxFiles, ErrorMessage = ModConstants.BinaryMaxFilesErrorMessage)] - public List Binaries { get; set; } = new(); + public List Binaries { get; set; } = []; [MaxLength(ModConstants.ScreenshotMaxFiles, ErrorMessage = ModConstants.ScreenshotMaxFilesErrorMessage)] public Dictionary Screenshots { get; set; } = new(); diff --git a/src/DevilDaggersInfo.Web.Server.Domain.Main/Repositories/PlayerCustomLeaderboardStatisticsRepository.cs b/src/DevilDaggersInfo.Web.Server.Domain.Main/Repositories/PlayerCustomLeaderboardStatisticsRepository.cs index 0f9d2a098..538cdc1bb 100644 --- a/src/DevilDaggersInfo.Web.Server.Domain.Main/Repositories/PlayerCustomLeaderboardStatisticsRepository.cs +++ b/src/DevilDaggersInfo.Web.Server.Domain.Main/Repositories/PlayerCustomLeaderboardStatisticsRepository.cs @@ -46,7 +46,7 @@ public async Task> GetCustomLeaderboa .ToListAsync(); if (customEntries.Count == 0) - return new(); + return []; // ! Navigation property. Dictionary<(SpawnsetGameMode GameMode, CustomLeaderboardRankSorting RankSorting), int> totalCustomLeaderboards = await _dbContext.CustomLeaderboards @@ -57,7 +57,7 @@ public async Task> GetCustomLeaderboa .Select(g => new { g.Key, Count = g.Count() }) .ToDictionaryAsync(a => (a.Key.GameMode, a.Key.RankSorting), a => a.Count); - List stats = new(); + List stats = []; foreach (SpawnsetGameMode gameMode in Enum.GetValues()) { foreach (CustomLeaderboardRankSorting rankSorting in Enum.GetValues()) diff --git a/src/DevilDaggersInfo.Web.Server.Domain/Models/CustomLeaderboards/GlobalCustomLeaderboardEntryData.cs b/src/DevilDaggersInfo.Web.Server.Domain/Models/CustomLeaderboards/GlobalCustomLeaderboardEntryData.cs index c9b499bf8..8a66a4ff4 100644 --- a/src/DevilDaggersInfo.Web.Server.Domain/Models/CustomLeaderboards/GlobalCustomLeaderboardEntryData.cs +++ b/src/DevilDaggersInfo.Web.Server.Domain/Models/CustomLeaderboards/GlobalCustomLeaderboardEntryData.cs @@ -3,7 +3,7 @@ namespace DevilDaggersInfo.Web.Server.Domain.Models.CustomLeaderboards; // TODO: Immutable. public record GlobalCustomLeaderboardEntryData { - public List Rankings { get; } = new(); + public List Rankings { get; } = []; public int LeviathanCount { get; set; } // TODO: init diff --git a/src/DevilDaggersInfo.Web.Server.Domain/Utils/IntegerArrayCompressor.cs b/src/DevilDaggersInfo.Web.Server.Domain/Utils/IntegerArrayCompressor.cs index ea61f6cc1..63c4c6508 100644 --- a/src/DevilDaggersInfo.Web.Server.Domain/Utils/IntegerArrayCompressor.cs +++ b/src/DevilDaggersInfo.Web.Server.Domain/Utils/IntegerArrayCompressor.cs @@ -21,7 +21,7 @@ public static byte[] CompressData(ushort[] data) /// public static byte[] CompressData(int[] data) { - if (data.Length == 0 || data.All(i => i <= 0)) + if (data.Length == 0 || Array.TrueForAll(data, i => i <= 0)) return Array.Empty(); byte bitCount = GetBitCount(data.Max()); diff --git a/src/DevilDaggersInfo.Web.Server/HostedServices/DiscordUserIdFetchBackgroundService.cs b/src/DevilDaggersInfo.Web.Server/HostedServices/DiscordUserIdFetchBackgroundService.cs index ff31e2522..fe8bd625c 100644 --- a/src/DevilDaggersInfo.Web.Server/HostedServices/DiscordUserIdFetchBackgroundService.cs +++ b/src/DevilDaggersInfo.Web.Server/HostedServices/DiscordUserIdFetchBackgroundService.cs @@ -9,7 +9,7 @@ public class DiscordUserIdFetchBackgroundService : AbstractBackgroundService private readonly IServiceScopeFactory _serviceScopeFactory; private readonly ClubberClient _clubberClient; - public DiscordUserIdFetchBackgroundService(IServiceScopeFactory serviceScopeFactory, ClubberClient clubberClient, BackgroundServiceMonitor backgroundServiceMonitor, ILogger logger) + public DiscordUserIdFetchBackgroundService(IServiceScopeFactory serviceScopeFactory, ClubberClient clubberClient, BackgroundServiceMonitor backgroundServiceMonitor, ILogger logger) : base(backgroundServiceMonitor, logger) { _serviceScopeFactory = serviceScopeFactory; @@ -47,7 +47,7 @@ protected override async Task ExecuteTaskAsync(CancellationToken stoppingToken) List ids = users.ConvertAll(u => u.LeaderboardId); List players = dbContext.Players.Where(p => ids.Contains(p.Id) && p.DiscordUserId == null).ToList(); - List<(int PlayerId, ulong? OldId, ulong NewId)> logs = new(); + List<(int PlayerId, ulong? OldId, ulong NewId)> logs = []; foreach (PlayerEntity player in players) { DdUser? user = users.Where(u => u.LeaderboardId == player.Id).MinBy(u => u.DiscordId); diff --git a/src/DevilDaggersInfo.Web.Server/HostedServices/LeaderboardHistoryBackgroundService.cs b/src/DevilDaggersInfo.Web.Server/HostedServices/LeaderboardHistoryBackgroundService.cs index 1ed54dd38..fc4368a89 100644 --- a/src/DevilDaggersInfo.Web.Server/HostedServices/LeaderboardHistoryBackgroundService.cs +++ b/src/DevilDaggersInfo.Web.Server/HostedServices/LeaderboardHistoryBackgroundService.cs @@ -28,7 +28,7 @@ protected override async Task ExecuteTaskAsync(CancellationToken stoppingToken) return; IDdLeaderboardService.LeaderboardResponse? leaderboard = null; - List entries = new(); + List entries = []; const int leaderboardPageCount = 5; const int playerPerPage = 100; @@ -39,10 +39,10 @@ protected override async Task ExecuteTaskAsync(CancellationToken stoppingToken) { response = await _leaderboardClient.GetLeaderboard(playerPerPage * i + 1, 100); } - catch (DdLeaderboardException) + catch (DdLeaderboardException ex) { const int interval = 5; - Logger.LogWarning("Couldn't get DD leaderboard (page {Page} of {Total}). Waiting {Interval} seconds...", i, leaderboardPageCount, interval); + Logger.LogWarning(ex, "Couldn't get DD leaderboard (page {Page} of {Total}). Waiting {Interval} seconds...", i, leaderboardPageCount, interval); await Task.Delay(TimeSpan.FromSeconds(interval), stoppingToken); continue; // Continue without increasing i, so the request is retried. diff --git a/src/DevilDaggersInfo.Web.Server/HostedServices/PlayerNameFetchBackgroundService.cs b/src/DevilDaggersInfo.Web.Server/HostedServices/PlayerNameFetchBackgroundService.cs index abcc5f5ae..38572ec98 100644 --- a/src/DevilDaggersInfo.Web.Server/HostedServices/PlayerNameFetchBackgroundService.cs +++ b/src/DevilDaggersInfo.Web.Server/HostedServices/PlayerNameFetchBackgroundService.cs @@ -11,7 +11,7 @@ public class PlayerNameFetchBackgroundService : AbstractBackgroundService private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IDdLeaderboardService _leaderboardClient; - public PlayerNameFetchBackgroundService(IServiceScopeFactory serviceScopeFactory, IDdLeaderboardService leaderboardClient, BackgroundServiceMonitor backgroundServiceMonitor, ILogger logger) + public PlayerNameFetchBackgroundService(IServiceScopeFactory serviceScopeFactory, IDdLeaderboardService leaderboardClient, BackgroundServiceMonitor backgroundServiceMonitor, ILogger logger) : base(backgroundServiceMonitor, logger) { _serviceScopeFactory = serviceScopeFactory; @@ -39,10 +39,10 @@ protected override async Task ExecuteTaskAsync(CancellationToken stoppingToken) { entries = await _leaderboardClient.GetEntriesByIds(playerIds); } - catch (DdLeaderboardException) + catch (DdLeaderboardException ex) { const int interval = 5; - Logger.LogWarning("Couldn't get entries from DD leaderboards. Waiting {Interval} seconds...", interval); + Logger.LogWarning(ex, "Couldn't get entries from DD leaderboards. Waiting {Interval} seconds...", interval); await Task.Delay(TimeSpan.FromSeconds(interval), stoppingToken); } @@ -51,7 +51,7 @@ protected override async Task ExecuteTaskAsync(CancellationToken stoppingToken) if (entries == null) return; - List<(int PlayerId, string OldName, string NewName)> logs = new(); + List<(int PlayerId, string OldName, string NewName)> logs = []; foreach (IDdLeaderboardService.EntryResponse entry in entries) { PlayerEntity? player = await dbContext.Players.FirstOrDefaultAsync(p => p.Id == entry.Id, stoppingToken); diff --git a/src/DevilDaggersInfo.Web.Server/Middleware/ExceptionMiddleware.cs b/src/DevilDaggersInfo.Web.Server/Middleware/ExceptionMiddleware.cs index 28f3ed85d..d9e51b7e4 100644 --- a/src/DevilDaggersInfo.Web.Server/Middleware/ExceptionMiddleware.cs +++ b/src/DevilDaggersInfo.Web.Server/Middleware/ExceptionMiddleware.cs @@ -24,7 +24,7 @@ public async Task InvokeAsync(HttpContext context) { if (context.Response.HasStarted) { - _logger.LogWarning("The response has already started, the exception middleware will not be executed."); + _logger.LogWarning(ex, "The response has already started, the exception middleware will not be executed."); throw; }