Skip to content

Commit

Permalink
Merge pull request #17 from Syriiin/beatmap-osu-extension
Browse files Browse the repository at this point in the history
Use .osu extension for beatmaps
  • Loading branch information
Syriiin authored May 16, 2024
2 parents fcf34d5 + a790dab commit b703244
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 121 deletions.
3 changes: 1 addition & 2 deletions Difficalcy.Catch/Controllers/CatchCalculatorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Difficalcy.Catch.Controllers
{
public class CatchCalculatorController : CalculatorController<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation, CatchCalculatorService>
public class CatchCalculatorController(CatchCalculatorService calculatorService) : CalculatorController<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation, CatchCalculatorService>(calculatorService)
{
public CatchCalculatorController(CatchCalculatorService calculatorService) : base(calculatorService) { }
}
}
4 changes: 2 additions & 2 deletions Difficalcy.Catch/Services/CalculatorWorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<Beatmap>(reader).Decode(reader);
Expand Down
4 changes: 1 addition & 3 deletions Difficalcy.Catch/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions Difficalcy.Mania/Controllers/ManiaCalculatorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Difficalcy.Mania.Controllers
{
public class ManiaCalculatorController : CalculatorController<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation, ManiaCalculatorService>
public class ManiaCalculatorController(ManiaCalculatorService calculatorService) : CalculatorController<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation, ManiaCalculatorService>(calculatorService)
{
public ManiaCalculatorController(ManiaCalculatorService calculatorService) : base(calculatorService) { }
}
}
4 changes: 2 additions & 2 deletions Difficalcy.Mania/Services/CalculatorWorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<Beatmap>(reader).Decode(reader);
Expand Down
9 changes: 2 additions & 7 deletions Difficalcy.Mania/Services/ManiaCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace Difficalcy.Mania.Services
{
public class ManiaCalculatorService : CalculatorService<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation>
public class ManiaCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : CalculatorService<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation>(cache)
{
private readonly IBeatmapProvider _beatmapProvider;
private readonly IBeatmapProvider _beatmapProvider = beatmapProvider;
private ManiaRuleset ManiaRuleset { get; } = new ManiaRuleset();

public override CalculatorInfo Info
Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions Difficalcy.Mania/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions Difficalcy.Osu/Controllers/OsuCalculatorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Difficalcy.Osu.Controllers
{
public class OsuCalculatorController : CalculatorController<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation, OsuCalculatorService>
public class OsuCalculatorController(OsuCalculatorService calculatorService) : CalculatorController<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation, OsuCalculatorService>(calculatorService)
{
public OsuCalculatorController(OsuCalculatorService calculatorService) : base(calculatorService) { }
}
}
4 changes: 2 additions & 2 deletions Difficalcy.Osu/Services/CalculatorWorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<Beatmap>(reader).Decode(reader);
Expand Down
4 changes: 1 addition & 3 deletions Difficalcy.Osu/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions Difficalcy.Taiko/Controllers/TaikoCalculatorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Difficalcy.Taiko.Controllers
{
public class TaikoCalculatorController : CalculatorController<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation, TaikoCalculatorService>
public class TaikoCalculatorController(TaikoCalculatorService calculatorService) : CalculatorController<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation, TaikoCalculatorService>(calculatorService)
{
public TaikoCalculatorController(TaikoCalculatorService calculatorService) : base(calculatorService) { }
}
}
4 changes: 2 additions & 2 deletions Difficalcy.Taiko/Services/CalculatorWorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<Beatmap>(reader).Decode(reader);
Expand Down
9 changes: 2 additions & 7 deletions Difficalcy.Taiko/Services/TaikoCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace Difficalcy.Taiko.Services
{
public class TaikoCalculatorService : CalculatorService<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation>
public class TaikoCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : CalculatorService<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation>(cache)
{
private readonly IBeatmapProvider _beatmapProvider;
private readonly IBeatmapProvider _beatmapProvider = beatmapProvider;
private TaikoRuleset TaikoRuleset { get; } = new TaikoRuleset();

public override CalculatorInfo Info
Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions Difficalcy.Taiko/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
12 changes: 4 additions & 8 deletions Difficalcy.Tests/DummyCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ public class DummyCalculatorServiceTest : CalculatorServiceTest<DummyScore, Dumm
[InlineData(15, 1500, "test 1", 150)]
[InlineData(10, 1000, "test 2", 100)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new DummyScore { BeatmapId = beatmapId, Mods = mods });
=> TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new DummyScore { BeatmapId = beatmapId, Mods = mods });
}

/// <summary>
/// A dummy calculator service implementation that calculates difficulty as (beatmap id + mods) / 10 and performance as difficulty * 100
/// </summary>
public class DummyCalculatorService : CalculatorService<DummyScore, DummyDifficulty, DummyPerformance, DummyCalculation>
public class DummyCalculatorService(ICache cache) : CalculatorService<DummyScore, DummyDifficulty, DummyPerformance, DummyCalculation>(cache)
{
public DummyCalculatorService(ICache cache) : base(cache)
{
}

public override CalculatorInfo Info =>
new CalculatorInfo
new()
{
RulesetName = "Dummy",
CalculatorName = "Dummy calculator",
Expand All @@ -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 }
Expand Down
9 changes: 2 additions & 7 deletions Difficalcy/Controllers/CalculatorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ namespace Difficalcy.Controllers
[ApiController]
[Route("/api")]
[Produces("application/json")]
public abstract class CalculatorController<TScore, TDifficulty, TPerformance, TCalculation, TCalculatorService> : ControllerBase
public abstract class CalculatorController<TScore, TDifficulty, TPerformance, TCalculation, TCalculatorService>(TCalculatorService calculatorService) : ControllerBase
where TScore : Score
where TDifficulty : Difficulty
where TPerformance : Performance
where TCalculation : Calculation<TDifficulty, TPerformance>
where TCalculatorService : CalculatorService<TScore, TDifficulty, TPerformance, TCalculation>
{
protected readonly TCalculatorService calculatorService;

public CalculatorController(TCalculatorService calculatorService)
{
this.calculatorService = calculatorService;
}
protected readonly TCalculatorService calculatorService = calculatorService;

/// <summary>
/// Returns a set of information describing the calculator.
Expand Down
9 changes: 2 additions & 7 deletions Difficalcy/DifficalcyStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Difficalcy
{
abstract public class DifficalcyStartup
abstract public class DifficalcyStartup(IConfiguration configuration)
{
public abstract string OpenApiTitle { get; }

Expand All @@ -20,12 +20,7 @@ abstract public class DifficalcyStartup
/// </summary>
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)
Expand Down
2 changes: 1 addition & 1 deletion Difficalcy/Services/InMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 5 additions & 17 deletions Difficalcy/Services/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> GetAsync(string key)
{
var redisValue = await _redisDatabase.StringGetAsync(key);
var redisValue = await redisDatabase.StringGetAsync(key);

if (redisValue.IsNull)
return null;
Expand All @@ -24,18 +17,13 @@ public async Task<string> 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()
{
Expand Down
17 changes: 5 additions & 12 deletions Difficalcy/Services/TestBeatmapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> EnsureBeatmap(string beatmapId)
{
var resourceName = $"{_resourceAssemblyName}.Resources.{beatmapId}";
var resourceName = $"{resourceAssemblyName}.Resources.{beatmapId}";
var info = ResourceAssembly.GetManifestResourceInfo(resourceName);
return Task.FromResult(info != null);
}
Expand All @@ -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;
}

Expand All @@ -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"));
}
}
}
Expand Down
Loading

0 comments on commit b703244

Please sign in to comment.