From a8278760f8bdfdbeaba4329d88a9b7d19314ac35 Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Thu, 16 May 2024 20:22:31 +1000 Subject: [PATCH] Fix a bunch of code style issues --- .../Controllers/CatchCalculatorController.cs | 3 +-- .../Services/CalculatorWorkingBeatmap.cs | 4 ++-- Difficalcy.Catch/Startup.cs | 4 +--- .../Controllers/ManiaCalculatorController.cs | 3 +-- .../Services/CalculatorWorkingBeatmap.cs | 4 ++-- .../Services/ManiaCalculatorService.cs | 9 ++------ Difficalcy.Mania/Startup.cs | 4 +--- .../Controllers/OsuCalculatorController.cs | 3 +-- .../Services/CalculatorWorkingBeatmap.cs | 4 ++-- Difficalcy.Osu/Startup.cs | 4 +--- .../Controllers/TaikoCalculatorController.cs | 3 +-- .../Services/CalculatorWorkingBeatmap.cs | 4 ++-- .../Services/TaikoCalculatorService.cs | 9 ++------ Difficalcy.Taiko/Startup.cs | 4 +--- .../DummyCalculatorServiceTest.cs | 12 ++++------ .../Controllers/CalculatorController.cs | 9 ++------ Difficalcy/DifficalcyStartup.cs | 9 ++------ Difficalcy/Services/InMemoryCache.cs | 2 +- Difficalcy/Services/RedisCache.cs | 22 +++++-------------- Difficalcy/Services/TestBeatmapProvider.cs | 17 +++++--------- Difficalcy/Services/WebBeatmapProvider.cs | 11 +++------- 21 files changed, 42 insertions(+), 102 deletions(-) diff --git a/Difficalcy.Catch/Controllers/CatchCalculatorController.cs b/Difficalcy.Catch/Controllers/CatchCalculatorController.cs index e4a2596..28c5509 100644 --- a/Difficalcy.Catch/Controllers/CatchCalculatorController.cs +++ b/Difficalcy.Catch/Controllers/CatchCalculatorController.cs @@ -4,8 +4,7 @@ namespace Difficalcy.Catch.Controllers { - public class CatchCalculatorController : CalculatorController + public class CatchCalculatorController(CatchCalculatorService calculatorService) : CalculatorController(calculatorService) { - public CatchCalculatorController(CatchCalculatorService calculatorService) : base(calculatorService) { } } } diff --git a/Difficalcy.Catch/Services/CalculatorWorkingBeatmap.cs b/Difficalcy.Catch/Services/CalculatorWorkingBeatmap.cs index 05987b2..8193f63 100644 --- a/Difficalcy.Catch/Services/CalculatorWorkingBeatmap.cs +++ b/Difficalcy.Catch/Services/CalculatorWorkingBeatmap.cs @@ -14,7 +14,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap { private readonly Beatmap _beatmap; - public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { } + public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { } private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null) { @@ -24,7 +24,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma _beatmap.BeatmapInfo.Ruleset = beatmap.BeatmapInfo.Ruleset.OnlineID == 0 ? (new OsuRuleset()).RulesetInfo : ruleset.RulesetInfo; } - private static Beatmap readFromStream(Stream stream) + private static Beatmap ReadFromStream(Stream stream) { using var reader = new LineBufferedReader(stream); return Decoder.GetDecoder(reader).Decode(reader); diff --git a/Difficalcy.Catch/Startup.cs b/Difficalcy.Catch/Startup.cs index 98425bf..c64bd8a 100644 --- a/Difficalcy.Catch/Startup.cs +++ b/Difficalcy.Catch/Startup.cs @@ -5,10 +5,8 @@ namespace Difficalcy.Catch { - public class Startup : DifficalcyStartup + public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration) { - public Startup(IConfiguration configuration) : base(configuration) { } - public override string OpenApiTitle => "Difficalcy.Catch"; public override string OpenApiVersion => "v1"; diff --git a/Difficalcy.Mania/Controllers/ManiaCalculatorController.cs b/Difficalcy.Mania/Controllers/ManiaCalculatorController.cs index 16aa1b6..6829cd8 100644 --- a/Difficalcy.Mania/Controllers/ManiaCalculatorController.cs +++ b/Difficalcy.Mania/Controllers/ManiaCalculatorController.cs @@ -4,8 +4,7 @@ namespace Difficalcy.Mania.Controllers { - public class ManiaCalculatorController : CalculatorController + public class ManiaCalculatorController(ManiaCalculatorService calculatorService) : CalculatorController(calculatorService) { - public ManiaCalculatorController(ManiaCalculatorService calculatorService) : base(calculatorService) { } } } diff --git a/Difficalcy.Mania/Services/CalculatorWorkingBeatmap.cs b/Difficalcy.Mania/Services/CalculatorWorkingBeatmap.cs index ad467e5..08895ba 100644 --- a/Difficalcy.Mania/Services/CalculatorWorkingBeatmap.cs +++ b/Difficalcy.Mania/Services/CalculatorWorkingBeatmap.cs @@ -14,7 +14,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap { private readonly Beatmap _beatmap; - public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { } + public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { } private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null) { @@ -24,7 +24,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma _beatmap.BeatmapInfo.Ruleset = beatmap.BeatmapInfo.Ruleset.OnlineID == 0 ? (new OsuRuleset()).RulesetInfo : ruleset.RulesetInfo; } - private static Beatmap readFromStream(Stream stream) + private static Beatmap ReadFromStream(Stream stream) { using var reader = new LineBufferedReader(stream); return Decoder.GetDecoder(reader).Decode(reader); diff --git a/Difficalcy.Mania/Services/ManiaCalculatorService.cs b/Difficalcy.Mania/Services/ManiaCalculatorService.cs index 935a0ab..e24b893 100644 --- a/Difficalcy.Mania/Services/ManiaCalculatorService.cs +++ b/Difficalcy.Mania/Services/ManiaCalculatorService.cs @@ -16,9 +16,9 @@ namespace Difficalcy.Mania.Services { - public class ManiaCalculatorService : CalculatorService + public class ManiaCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : CalculatorService(cache) { - private readonly IBeatmapProvider _beatmapProvider; + private readonly IBeatmapProvider _beatmapProvider = beatmapProvider; private ManiaRuleset ManiaRuleset { get; } = new ManiaRuleset(); public override CalculatorInfo Info @@ -38,11 +38,6 @@ public override CalculatorInfo Info } } - public ManiaCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : base(cache) - { - _beatmapProvider = beatmapProvider; - } - protected override async Task EnsureBeatmap(string beatmapId) { await _beatmapProvider.EnsureBeatmap(beatmapId); diff --git a/Difficalcy.Mania/Startup.cs b/Difficalcy.Mania/Startup.cs index da3078e..07cddaa 100644 --- a/Difficalcy.Mania/Startup.cs +++ b/Difficalcy.Mania/Startup.cs @@ -5,10 +5,8 @@ namespace Difficalcy.Mania { - public class Startup : DifficalcyStartup + public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration) { - public Startup(IConfiguration configuration) : base(configuration) { } - public override string OpenApiTitle => "Difficalcy.Mania"; public override string OpenApiVersion => "v1"; diff --git a/Difficalcy.Osu/Controllers/OsuCalculatorController.cs b/Difficalcy.Osu/Controllers/OsuCalculatorController.cs index 8fc6af7..038fcc3 100644 --- a/Difficalcy.Osu/Controllers/OsuCalculatorController.cs +++ b/Difficalcy.Osu/Controllers/OsuCalculatorController.cs @@ -4,8 +4,7 @@ namespace Difficalcy.Osu.Controllers { - public class OsuCalculatorController : CalculatorController + public class OsuCalculatorController(OsuCalculatorService calculatorService) : CalculatorController(calculatorService) { - public OsuCalculatorController(OsuCalculatorService calculatorService) : base(calculatorService) { } } } diff --git a/Difficalcy.Osu/Services/CalculatorWorkingBeatmap.cs b/Difficalcy.Osu/Services/CalculatorWorkingBeatmap.cs index 41e4c06..aa6ac39 100644 --- a/Difficalcy.Osu/Services/CalculatorWorkingBeatmap.cs +++ b/Difficalcy.Osu/Services/CalculatorWorkingBeatmap.cs @@ -13,7 +13,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap { private readonly Beatmap _beatmap; - public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { } + public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { } private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null) { @@ -22,7 +22,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma _beatmap.BeatmapInfo.Ruleset = ruleset.RulesetInfo; } - private static Beatmap readFromStream(Stream stream) + private static Beatmap ReadFromStream(Stream stream) { using var reader = new LineBufferedReader(stream); return Decoder.GetDecoder(reader).Decode(reader); diff --git a/Difficalcy.Osu/Startup.cs b/Difficalcy.Osu/Startup.cs index 6d67c0a..accb4ed 100644 --- a/Difficalcy.Osu/Startup.cs +++ b/Difficalcy.Osu/Startup.cs @@ -5,10 +5,8 @@ namespace Difficalcy.Osu { - public class Startup : DifficalcyStartup + public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration) { - public Startup(IConfiguration configuration) : base(configuration) { } - public override string OpenApiTitle => "Difficalcy.Osu"; public override string OpenApiVersion => "v1"; diff --git a/Difficalcy.Taiko/Controllers/TaikoCalculatorController.cs b/Difficalcy.Taiko/Controllers/TaikoCalculatorController.cs index de99031..d49cf84 100644 --- a/Difficalcy.Taiko/Controllers/TaikoCalculatorController.cs +++ b/Difficalcy.Taiko/Controllers/TaikoCalculatorController.cs @@ -4,8 +4,7 @@ namespace Difficalcy.Taiko.Controllers { - public class TaikoCalculatorController : CalculatorController + public class TaikoCalculatorController(TaikoCalculatorService calculatorService) : CalculatorController(calculatorService) { - public TaikoCalculatorController(TaikoCalculatorService calculatorService) : base(calculatorService) { } } } diff --git a/Difficalcy.Taiko/Services/CalculatorWorkingBeatmap.cs b/Difficalcy.Taiko/Services/CalculatorWorkingBeatmap.cs index f07e2fa..bb22d3c 100644 --- a/Difficalcy.Taiko/Services/CalculatorWorkingBeatmap.cs +++ b/Difficalcy.Taiko/Services/CalculatorWorkingBeatmap.cs @@ -14,7 +14,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap { private readonly Beatmap _beatmap; - public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { } + public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { } private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null) { @@ -24,7 +24,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma _beatmap.BeatmapInfo.Ruleset = beatmap.BeatmapInfo.Ruleset.OnlineID == 0 ? (new OsuRuleset()).RulesetInfo : ruleset.RulesetInfo; } - private static Beatmap readFromStream(Stream stream) + private static Beatmap ReadFromStream(Stream stream) { using var reader = new LineBufferedReader(stream); return Decoder.GetDecoder(reader).Decode(reader); diff --git a/Difficalcy.Taiko/Services/TaikoCalculatorService.cs b/Difficalcy.Taiko/Services/TaikoCalculatorService.cs index 425af53..7dd48ff 100644 --- a/Difficalcy.Taiko/Services/TaikoCalculatorService.cs +++ b/Difficalcy.Taiko/Services/TaikoCalculatorService.cs @@ -16,9 +16,9 @@ namespace Difficalcy.Taiko.Services { - public class TaikoCalculatorService : CalculatorService + public class TaikoCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : CalculatorService(cache) { - private readonly IBeatmapProvider _beatmapProvider; + private readonly IBeatmapProvider _beatmapProvider = beatmapProvider; private TaikoRuleset TaikoRuleset { get; } = new TaikoRuleset(); public override CalculatorInfo Info @@ -38,11 +38,6 @@ public override CalculatorInfo Info } } - public TaikoCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : base(cache) - { - _beatmapProvider = beatmapProvider; - } - protected override async Task EnsureBeatmap(string beatmapId) { await _beatmapProvider.EnsureBeatmap(beatmapId); diff --git a/Difficalcy.Taiko/Startup.cs b/Difficalcy.Taiko/Startup.cs index e255c8a..85f914c 100644 --- a/Difficalcy.Taiko/Startup.cs +++ b/Difficalcy.Taiko/Startup.cs @@ -5,10 +5,8 @@ namespace Difficalcy.Taiko { - public class Startup : DifficalcyStartup + public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration) { - public Startup(IConfiguration configuration) : base(configuration) { } - public override string OpenApiTitle => "Difficalcy.Taiko"; public override string OpenApiVersion => "v1"; diff --git a/Difficalcy.Tests/DummyCalculatorServiceTest.cs b/Difficalcy.Tests/DummyCalculatorServiceTest.cs index dc7fc8d..f18745f 100644 --- a/Difficalcy.Tests/DummyCalculatorServiceTest.cs +++ b/Difficalcy.Tests/DummyCalculatorServiceTest.cs @@ -11,20 +11,16 @@ public class DummyCalculatorServiceTest : CalculatorServiceTest base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new DummyScore { BeatmapId = beatmapId, Mods = mods }); + => TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new DummyScore { BeatmapId = beatmapId, Mods = mods }); } /// /// A dummy calculator service implementation that calculates difficulty as (beatmap id + mods) / 10 and performance as difficulty * 100 /// -public class DummyCalculatorService : CalculatorService +public class DummyCalculatorService(ICache cache) : CalculatorService(cache) { - public DummyCalculatorService(ICache cache) : base(cache) - { - } - public override CalculatorInfo Info => - new CalculatorInfo + new() { RulesetName = "Dummy", CalculatorName = "Dummy calculator", @@ -40,7 +36,7 @@ protected override (object, string) CalculateDifficultyAttributes(DummyScore sco } protected override DummyCalculation CalculatePerformance(DummyScore score, object difficultyAttributes) => - new DummyCalculation() + new() { Difficulty = new DummyDifficulty() { Total = (double)difficultyAttributes }, Performance = new DummyPerformance() { Total = (double)difficultyAttributes * 100 } diff --git a/Difficalcy/Controllers/CalculatorController.cs b/Difficalcy/Controllers/CalculatorController.cs index 2f1f001..049a744 100644 --- a/Difficalcy/Controllers/CalculatorController.cs +++ b/Difficalcy/Controllers/CalculatorController.cs @@ -9,19 +9,14 @@ namespace Difficalcy.Controllers [ApiController] [Route("/api")] [Produces("application/json")] - public abstract class CalculatorController : ControllerBase + public abstract class CalculatorController(TCalculatorService calculatorService) : ControllerBase where TScore : Score where TDifficulty : Difficulty where TPerformance : Performance where TCalculation : Calculation where TCalculatorService : CalculatorService { - protected readonly TCalculatorService calculatorService; - - public CalculatorController(TCalculatorService calculatorService) - { - this.calculatorService = calculatorService; - } + protected readonly TCalculatorService calculatorService = calculatorService; /// /// Returns a set of information describing the calculator. diff --git a/Difficalcy/DifficalcyStartup.cs b/Difficalcy/DifficalcyStartup.cs index fca33ce..56a12f5 100644 --- a/Difficalcy/DifficalcyStartup.cs +++ b/Difficalcy/DifficalcyStartup.cs @@ -9,7 +9,7 @@ namespace Difficalcy { - abstract public class DifficalcyStartup + abstract public class DifficalcyStartup(IConfiguration configuration) { public abstract string OpenApiTitle { get; } @@ -20,12 +20,7 @@ abstract public class DifficalcyStartup /// protected abstract string TestBeatmapAssembly { get; } - public DifficalcyStartup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } + public IConfiguration Configuration { get; } = configuration; // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) diff --git a/Difficalcy/Services/InMemoryCache.cs b/Difficalcy/Services/InMemoryCache.cs index 2051b4b..2fabb14 100644 --- a/Difficalcy/Services/InMemoryCache.cs +++ b/Difficalcy/Services/InMemoryCache.cs @@ -14,7 +14,7 @@ public class InMemoryCacheDatabase : ICacheDatabase public class InMemoryCache : ICache { - private InMemoryCacheDatabase _database = new InMemoryCacheDatabase(); + private readonly InMemoryCacheDatabase _database = new(); public ICacheDatabase GetDatabase() => _database; } diff --git a/Difficalcy/Services/RedisCache.cs b/Difficalcy/Services/RedisCache.cs index 90f1333..207608d 100644 --- a/Difficalcy/Services/RedisCache.cs +++ b/Difficalcy/Services/RedisCache.cs @@ -3,18 +3,11 @@ namespace Difficalcy.Services { - public class RedisCacheDatabase : ICacheDatabase + public class RedisCacheDatabase(IDatabase redisDatabase) : ICacheDatabase { - private IDatabase _redisDatabase; - - public RedisCacheDatabase(IDatabase redisDatabase) - { - _redisDatabase = redisDatabase; - } - public async Task GetAsync(string key) { - var redisValue = await _redisDatabase.StringGetAsync(key); + var redisValue = await redisDatabase.StringGetAsync(key); if (redisValue.IsNull) return null; @@ -24,18 +17,13 @@ public async Task GetAsync(string key) public void Set(string key, string value) { - _redisDatabase.StringSet(key, value, flags: CommandFlags.FireAndForget); + redisDatabase.StringSet(key, value, flags: CommandFlags.FireAndForget); } } - public class RedisCache : ICache + public class RedisCache(IConnectionMultiplexer redis) : ICache { - private IConnectionMultiplexer _redis; - - public RedisCache(IConnectionMultiplexer redis) - { - _redis = redis; - } + private readonly IConnectionMultiplexer _redis = redis; public ICacheDatabase GetDatabase() { diff --git a/Difficalcy/Services/TestBeatmapProvider.cs b/Difficalcy/Services/TestBeatmapProvider.cs index 4c88d11..fd28942 100644 --- a/Difficalcy/Services/TestBeatmapProvider.cs +++ b/Difficalcy/Services/TestBeatmapProvider.cs @@ -5,18 +5,11 @@ namespace Difficalcy.Services { - public class TestBeatmapProvider : IBeatmapProvider + public class TestBeatmapProvider(string resourceAssemblyName) : IBeatmapProvider { - private string _resourceAssemblyName; - - public TestBeatmapProvider(string resourceAssemblyName) - { - _resourceAssemblyName = resourceAssemblyName; - } - public Task EnsureBeatmap(string beatmapId) { - var resourceName = $"{_resourceAssemblyName}.Resources.{beatmapId}"; + var resourceName = $"{resourceAssemblyName}.Resources.{beatmapId}"; var info = ResourceAssembly.GetManifestResourceInfo(resourceName); return Task.FromResult(info != null); } @@ -25,10 +18,10 @@ public Stream GetBeatmapStream(string beatmapId) { var resourceNamespace = "Testing.Beatmaps"; var resourceName = $"{resourceNamespace}.{beatmapId}.osu"; - var fullResourceName = $"{_resourceAssemblyName}.Resources.{resourceName}"; + var fullResourceName = $"{resourceAssemblyName}.Resources.{resourceName}"; var stream = ResourceAssembly.GetManifestResourceStream(fullResourceName); if (stream == null) - throw new Exception($@"Unable to find resource ""{fullResourceName}"" in assembly ""{_resourceAssemblyName}"""); + throw new Exception($@"Unable to find resource ""{fullResourceName}"" in assembly ""{resourceAssemblyName}"""); return stream; } @@ -37,7 +30,7 @@ private Assembly ResourceAssembly get { string localPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!; - return Assembly.LoadFrom(Path.Combine(localPath, $"{_resourceAssemblyName}.dll")); + return Assembly.LoadFrom(Path.Combine(localPath, $"{resourceAssemblyName}.dll")); } } } diff --git a/Difficalcy/Services/WebBeatmapProvider.cs b/Difficalcy/Services/WebBeatmapProvider.cs index 7e7914a..b0d3b5e 100644 --- a/Difficalcy/Services/WebBeatmapProvider.cs +++ b/Difficalcy/Services/WebBeatmapProvider.cs @@ -5,15 +5,10 @@ namespace Difficalcy.Services { - public class WebBeatmapProvider : IBeatmapProvider + public class WebBeatmapProvider(IConfiguration configuration) : IBeatmapProvider { - private string _beatmapDirectory; - private readonly HttpClient _httpClient = new HttpClient(); - - public WebBeatmapProvider(IConfiguration configuration) - { - _beatmapDirectory = configuration["BEATMAP_DIRECTORY"]; - } + private readonly string _beatmapDirectory = configuration["BEATMAP_DIRECTORY"]; + private readonly HttpClient _httpClient = new(); public async Task EnsureBeatmap(string beatmapId) {