Skip to content

Commit b703244

Browse files
authored
Merge pull request #17 from Syriiin/beatmap-osu-extension
Use .osu extension for beatmaps
2 parents fcf34d5 + a790dab commit b703244

24 files changed

+99
-121
lines changed

Difficalcy.Catch/Controllers/CatchCalculatorController.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Difficalcy.Catch.Controllers
66
{
7-
public class CatchCalculatorController : CalculatorController<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation, CatchCalculatorService>
7+
public class CatchCalculatorController(CatchCalculatorService calculatorService) : CalculatorController<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation, CatchCalculatorService>(calculatorService)
88
{
9-
public CatchCalculatorController(CatchCalculatorService calculatorService) : base(calculatorService) { }
109
}
1110
}

Difficalcy.Catch/Services/CalculatorWorkingBeatmap.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap
1414
{
1515
private readonly Beatmap _beatmap;
1616

17-
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { }
17+
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { }
1818

1919
private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null)
2020
{
@@ -24,7 +24,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma
2424
_beatmap.BeatmapInfo.Ruleset = beatmap.BeatmapInfo.Ruleset.OnlineID == 0 ? (new OsuRuleset()).RulesetInfo : ruleset.RulesetInfo;
2525
}
2626

27-
private static Beatmap readFromStream(Stream stream)
27+
private static Beatmap ReadFromStream(Stream stream)
2828
{
2929
using var reader = new LineBufferedReader(stream);
3030
return Decoder.GetDecoder<Beatmap>(reader).Decode(reader);

Difficalcy.Catch/Startup.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
namespace Difficalcy.Catch
77
{
8-
public class Startup : DifficalcyStartup
8+
public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration)
99
{
10-
public Startup(IConfiguration configuration) : base(configuration) { }
11-
1210
public override string OpenApiTitle => "Difficalcy.Catch";
1311

1412
public override string OpenApiVersion => "v1";

Difficalcy.Mania/Controllers/ManiaCalculatorController.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Difficalcy.Mania.Controllers
66
{
7-
public class ManiaCalculatorController : CalculatorController<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation, ManiaCalculatorService>
7+
public class ManiaCalculatorController(ManiaCalculatorService calculatorService) : CalculatorController<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation, ManiaCalculatorService>(calculatorService)
88
{
9-
public ManiaCalculatorController(ManiaCalculatorService calculatorService) : base(calculatorService) { }
109
}
1110
}

Difficalcy.Mania/Services/CalculatorWorkingBeatmap.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap
1414
{
1515
private readonly Beatmap _beatmap;
1616

17-
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { }
17+
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { }
1818

1919
private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null)
2020
{
@@ -24,7 +24,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma
2424
_beatmap.BeatmapInfo.Ruleset = beatmap.BeatmapInfo.Ruleset.OnlineID == 0 ? (new OsuRuleset()).RulesetInfo : ruleset.RulesetInfo;
2525
}
2626

27-
private static Beatmap readFromStream(Stream stream)
27+
private static Beatmap ReadFromStream(Stream stream)
2828
{
2929
using var reader = new LineBufferedReader(stream);
3030
return Decoder.GetDecoder<Beatmap>(reader).Decode(reader);

Difficalcy.Mania/Services/ManiaCalculatorService.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
namespace Difficalcy.Mania.Services
1818
{
19-
public class ManiaCalculatorService : CalculatorService<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation>
19+
public class ManiaCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : CalculatorService<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation>(cache)
2020
{
21-
private readonly IBeatmapProvider _beatmapProvider;
21+
private readonly IBeatmapProvider _beatmapProvider = beatmapProvider;
2222
private ManiaRuleset ManiaRuleset { get; } = new ManiaRuleset();
2323

2424
public override CalculatorInfo Info
@@ -38,11 +38,6 @@ public override CalculatorInfo Info
3838
}
3939
}
4040

41-
public ManiaCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : base(cache)
42-
{
43-
_beatmapProvider = beatmapProvider;
44-
}
45-
4641
protected override async Task EnsureBeatmap(string beatmapId)
4742
{
4843
await _beatmapProvider.EnsureBeatmap(beatmapId);

Difficalcy.Mania/Startup.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
namespace Difficalcy.Mania
77
{
8-
public class Startup : DifficalcyStartup
8+
public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration)
99
{
10-
public Startup(IConfiguration configuration) : base(configuration) { }
11-
1210
public override string OpenApiTitle => "Difficalcy.Mania";
1311

1412
public override string OpenApiVersion => "v1";

Difficalcy.Osu/Controllers/OsuCalculatorController.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Difficalcy.Osu.Controllers
66
{
7-
public class OsuCalculatorController : CalculatorController<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation, OsuCalculatorService>
7+
public class OsuCalculatorController(OsuCalculatorService calculatorService) : CalculatorController<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation, OsuCalculatorService>(calculatorService)
88
{
9-
public OsuCalculatorController(OsuCalculatorService calculatorService) : base(calculatorService) { }
109
}
1110
}

Difficalcy.Osu/Services/CalculatorWorkingBeatmap.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap
1313
{
1414
private readonly Beatmap _beatmap;
1515

16-
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { }
16+
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { }
1717

1818
private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null)
1919
{
@@ -22,7 +22,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma
2222
_beatmap.BeatmapInfo.Ruleset = ruleset.RulesetInfo;
2323
}
2424

25-
private static Beatmap readFromStream(Stream stream)
25+
private static Beatmap ReadFromStream(Stream stream)
2626
{
2727
using var reader = new LineBufferedReader(stream);
2828
return Decoder.GetDecoder<Beatmap>(reader).Decode(reader);

Difficalcy.Osu/Startup.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
namespace Difficalcy.Osu
77
{
8-
public class Startup : DifficalcyStartup
8+
public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration)
99
{
10-
public Startup(IConfiguration configuration) : base(configuration) { }
11-
1210
public override string OpenApiTitle => "Difficalcy.Osu";
1311

1412
public override string OpenApiVersion => "v1";

Difficalcy.Taiko/Controllers/TaikoCalculatorController.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Difficalcy.Taiko.Controllers
66
{
7-
public class TaikoCalculatorController : CalculatorController<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation, TaikoCalculatorService>
7+
public class TaikoCalculatorController(TaikoCalculatorService calculatorService) : CalculatorController<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation, TaikoCalculatorService>(calculatorService)
88
{
9-
public TaikoCalculatorController(TaikoCalculatorService calculatorService) : base(calculatorService) { }
109
}
1110
}

Difficalcy.Taiko/Services/CalculatorWorkingBeatmap.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CalculatorWorkingBeatmap : WorkingBeatmap
1414
{
1515
private readonly Beatmap _beatmap;
1616

17-
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, readFromStream(beatmapStream), beatmapId) { }
17+
public CalculatorWorkingBeatmap(Ruleset ruleset, Stream beatmapStream, string beatmapId) : this(ruleset, ReadFromStream(beatmapStream), beatmapId) { }
1818

1919
private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatmapId) : base(beatmap.BeatmapInfo, null)
2020
{
@@ -24,7 +24,7 @@ private CalculatorWorkingBeatmap(Ruleset ruleset, Beatmap beatmap, string beatma
2424
_beatmap.BeatmapInfo.Ruleset = beatmap.BeatmapInfo.Ruleset.OnlineID == 0 ? (new OsuRuleset()).RulesetInfo : ruleset.RulesetInfo;
2525
}
2626

27-
private static Beatmap readFromStream(Stream stream)
27+
private static Beatmap ReadFromStream(Stream stream)
2828
{
2929
using var reader = new LineBufferedReader(stream);
3030
return Decoder.GetDecoder<Beatmap>(reader).Decode(reader);

Difficalcy.Taiko/Services/TaikoCalculatorService.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
namespace Difficalcy.Taiko.Services
1818
{
19-
public class TaikoCalculatorService : CalculatorService<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation>
19+
public class TaikoCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : CalculatorService<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation>(cache)
2020
{
21-
private readonly IBeatmapProvider _beatmapProvider;
21+
private readonly IBeatmapProvider _beatmapProvider = beatmapProvider;
2222
private TaikoRuleset TaikoRuleset { get; } = new TaikoRuleset();
2323

2424
public override CalculatorInfo Info
@@ -38,11 +38,6 @@ public override CalculatorInfo Info
3838
}
3939
}
4040

41-
public TaikoCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : base(cache)
42-
{
43-
_beatmapProvider = beatmapProvider;
44-
}
45-
4641
protected override async Task EnsureBeatmap(string beatmapId)
4742
{
4843
await _beatmapProvider.EnsureBeatmap(beatmapId);

Difficalcy.Taiko/Startup.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
namespace Difficalcy.Taiko
77
{
8-
public class Startup : DifficalcyStartup
8+
public class Startup(IConfiguration configuration) : DifficalcyStartup(configuration)
99
{
10-
public Startup(IConfiguration configuration) : base(configuration) { }
11-
1210
public override string OpenApiTitle => "Difficalcy.Taiko";
1311

1412
public override string OpenApiVersion => "v1";

Difficalcy.Tests/DummyCalculatorServiceTest.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@ public class DummyCalculatorServiceTest : CalculatorServiceTest<DummyScore, Dumm
1111
[InlineData(15, 1500, "test 1", 150)]
1212
[InlineData(10, 1000, "test 2", 100)]
1313
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
14-
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new DummyScore { BeatmapId = beatmapId, Mods = mods });
14+
=> TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new DummyScore { BeatmapId = beatmapId, Mods = mods });
1515
}
1616

1717
/// <summary>
1818
/// A dummy calculator service implementation that calculates difficulty as (beatmap id + mods) / 10 and performance as difficulty * 100
1919
/// </summary>
20-
public class DummyCalculatorService : CalculatorService<DummyScore, DummyDifficulty, DummyPerformance, DummyCalculation>
20+
public class DummyCalculatorService(ICache cache) : CalculatorService<DummyScore, DummyDifficulty, DummyPerformance, DummyCalculation>(cache)
2121
{
22-
public DummyCalculatorService(ICache cache) : base(cache)
23-
{
24-
}
25-
2622
public override CalculatorInfo Info =>
27-
new CalculatorInfo
23+
new()
2824
{
2925
RulesetName = "Dummy",
3026
CalculatorName = "Dummy calculator",
@@ -40,7 +36,7 @@ protected override (object, string) CalculateDifficultyAttributes(DummyScore sco
4036
}
4137

4238
protected override DummyCalculation CalculatePerformance(DummyScore score, object difficultyAttributes) =>
43-
new DummyCalculation()
39+
new()
4440
{
4541
Difficulty = new DummyDifficulty() { Total = (double)difficultyAttributes },
4642
Performance = new DummyPerformance() { Total = (double)difficultyAttributes * 100 }

Difficalcy/Controllers/CalculatorController.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@ namespace Difficalcy.Controllers
99
[ApiController]
1010
[Route("/api")]
1111
[Produces("application/json")]
12-
public abstract class CalculatorController<TScore, TDifficulty, TPerformance, TCalculation, TCalculatorService> : ControllerBase
12+
public abstract class CalculatorController<TScore, TDifficulty, TPerformance, TCalculation, TCalculatorService>(TCalculatorService calculatorService) : ControllerBase
1313
where TScore : Score
1414
where TDifficulty : Difficulty
1515
where TPerformance : Performance
1616
where TCalculation : Calculation<TDifficulty, TPerformance>
1717
where TCalculatorService : CalculatorService<TScore, TDifficulty, TPerformance, TCalculation>
1818
{
19-
protected readonly TCalculatorService calculatorService;
20-
21-
public CalculatorController(TCalculatorService calculatorService)
22-
{
23-
this.calculatorService = calculatorService;
24-
}
19+
protected readonly TCalculatorService calculatorService = calculatorService;
2520

2621
/// <summary>
2722
/// Returns a set of information describing the calculator.

Difficalcy/DifficalcyStartup.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Difficalcy
1111
{
12-
abstract public class DifficalcyStartup
12+
abstract public class DifficalcyStartup(IConfiguration configuration)
1313
{
1414
public abstract string OpenApiTitle { get; }
1515

@@ -20,12 +20,7 @@ abstract public class DifficalcyStartup
2020
/// </summary>
2121
protected abstract string TestBeatmapAssembly { get; }
2222

23-
public DifficalcyStartup(IConfiguration configuration)
24-
{
25-
Configuration = configuration;
26-
}
27-
28-
public IConfiguration Configuration { get; }
23+
public IConfiguration Configuration { get; } = configuration;
2924

3025
// This method gets called by the runtime. Use this method to add services to the container.
3126
public void ConfigureServices(IServiceCollection services)

Difficalcy/Services/InMemoryCache.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class InMemoryCacheDatabase : ICacheDatabase
1414

1515
public class InMemoryCache : ICache
1616
{
17-
private InMemoryCacheDatabase _database = new InMemoryCacheDatabase();
17+
private readonly InMemoryCacheDatabase _database = new();
1818

1919
public ICacheDatabase GetDatabase() => _database;
2020
}

Difficalcy/Services/RedisCache.cs

+5-17
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33

44
namespace Difficalcy.Services
55
{
6-
public class RedisCacheDatabase : ICacheDatabase
6+
public class RedisCacheDatabase(IDatabase redisDatabase) : ICacheDatabase
77
{
8-
private IDatabase _redisDatabase;
9-
10-
public RedisCacheDatabase(IDatabase redisDatabase)
11-
{
12-
_redisDatabase = redisDatabase;
13-
}
14-
158
public async Task<string> GetAsync(string key)
169
{
17-
var redisValue = await _redisDatabase.StringGetAsync(key);
10+
var redisValue = await redisDatabase.StringGetAsync(key);
1811

1912
if (redisValue.IsNull)
2013
return null;
@@ -24,18 +17,13 @@ public async Task<string> GetAsync(string key)
2417

2518
public void Set(string key, string value)
2619
{
27-
_redisDatabase.StringSet(key, value, flags: CommandFlags.FireAndForget);
20+
redisDatabase.StringSet(key, value, flags: CommandFlags.FireAndForget);
2821
}
2922
}
3023

31-
public class RedisCache : ICache
24+
public class RedisCache(IConnectionMultiplexer redis) : ICache
3225
{
33-
private IConnectionMultiplexer _redis;
34-
35-
public RedisCache(IConnectionMultiplexer redis)
36-
{
37-
_redis = redis;
38-
}
26+
private readonly IConnectionMultiplexer _redis = redis;
3927

4028
public ICacheDatabase GetDatabase()
4129
{

Difficalcy/Services/TestBeatmapProvider.cs

+5-12
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,11 @@
55

66
namespace Difficalcy.Services
77
{
8-
public class TestBeatmapProvider : IBeatmapProvider
8+
public class TestBeatmapProvider(string resourceAssemblyName) : IBeatmapProvider
99
{
10-
private string _resourceAssemblyName;
11-
12-
public TestBeatmapProvider(string resourceAssemblyName)
13-
{
14-
_resourceAssemblyName = resourceAssemblyName;
15-
}
16-
1710
public Task<bool> EnsureBeatmap(string beatmapId)
1811
{
19-
var resourceName = $"{_resourceAssemblyName}.Resources.{beatmapId}";
12+
var resourceName = $"{resourceAssemblyName}.Resources.{beatmapId}";
2013
var info = ResourceAssembly.GetManifestResourceInfo(resourceName);
2114
return Task.FromResult(info != null);
2215
}
@@ -25,10 +18,10 @@ public Stream GetBeatmapStream(string beatmapId)
2518
{
2619
var resourceNamespace = "Testing.Beatmaps";
2720
var resourceName = $"{resourceNamespace}.{beatmapId}.osu";
28-
var fullResourceName = $"{_resourceAssemblyName}.Resources.{resourceName}";
21+
var fullResourceName = $"{resourceAssemblyName}.Resources.{resourceName}";
2922
var stream = ResourceAssembly.GetManifestResourceStream(fullResourceName);
3023
if (stream == null)
31-
throw new Exception($@"Unable to find resource ""{fullResourceName}"" in assembly ""{_resourceAssemblyName}""");
24+
throw new Exception($@"Unable to find resource ""{fullResourceName}"" in assembly ""{resourceAssemblyName}""");
3225
return stream;
3326
}
3427

@@ -37,7 +30,7 @@ private Assembly ResourceAssembly
3730
get
3831
{
3932
string localPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
40-
return Assembly.LoadFrom(Path.Combine(localPath, $"{_resourceAssemblyName}.dll"));
33+
return Assembly.LoadFrom(Path.Combine(localPath, $"{resourceAssemblyName}.dll"));
4134
}
4235
}
4336
}

0 commit comments

Comments
 (0)