diff --git a/SVSim.Database/Repositories/PackDrawTable/IPackDrawTableRepository.cs b/SVSim.Database/Repositories/PackDrawTable/IPackDrawTableRepository.cs new file mode 100644 index 0000000..7728e94 --- /dev/null +++ b/SVSim.Database/Repositories/PackDrawTable/IPackDrawTableRepository.cs @@ -0,0 +1,7 @@ +namespace SVSim.Database.Repositories.PackDrawTables; + +public interface IPackDrawTableRepository +{ + /// Returns the draw table for , or null if not seeded. + Task GetAsync(int packId); +} diff --git a/SVSim.Database/Repositories/PackDrawTable/PackDrawTable.cs b/SVSim.Database/Repositories/PackDrawTable/PackDrawTable.cs new file mode 100644 index 0000000..893bd4c --- /dev/null +++ b/SVSim.Database/Repositories/PackDrawTable/PackDrawTable.cs @@ -0,0 +1,14 @@ +using SVSim.Database.Models; + +namespace SVSim.Database.Repositories.PackDrawTables; + +/// +/// All draw data for a single pack: per-pack config + slot rates + per-card weights. +/// Loaded as one unit by . +/// +public sealed class PackDrawTable +{ + public required PackDrawConfigEntry Config { get; init; } + public required IReadOnlyList SlotRates { get; init; } + public required IReadOnlyList CardWeights { get; init; } +} diff --git a/SVSim.Database/Repositories/PackDrawTable/PackDrawTableRepository.cs b/SVSim.Database/Repositories/PackDrawTable/PackDrawTableRepository.cs new file mode 100644 index 0000000..aee7abd --- /dev/null +++ b/SVSim.Database/Repositories/PackDrawTable/PackDrawTableRepository.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; + +namespace SVSim.Database.Repositories.PackDrawTables; + +public class PackDrawTableRepository : IPackDrawTableRepository +{ + private readonly SVSimDbContext _db; + public PackDrawTableRepository(SVSimDbContext db) { _db = db; } + + public async Task GetAsync(int packId) + { + var config = await _db.PackDrawConfigs.FirstOrDefaultAsync(c => c.Id == packId); + if (config is null) return null; + + var slotRates = await _db.PackDrawSlotRates + .Where(s => s.PackId == packId) + .ToListAsync(); + + var cardWeights = await _db.PackDrawCardWeights + .Where(w => w.PackId == packId) + .ToListAsync(); + + return new PackDrawTable + { + Config = config, + SlotRates = slotRates, + CardWeights = cardWeights, + }; + } +} diff --git a/SVSim.EmulatedEntrypoint/Program.cs b/SVSim.EmulatedEntrypoint/Program.cs index 524916c..193ee56 100644 --- a/SVSim.EmulatedEntrypoint/Program.cs +++ b/SVSim.EmulatedEntrypoint/Program.cs @@ -74,6 +74,7 @@ public class Program builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); + builder.Services.AddScoped(); builder.Services.AddTransient(); // Scoped (not Singleton) to avoid the singleton-depends-on-scoped-DbContext lifecycle // pitfall. Cost: one indexed single-row query per section per request — trivial. No diff --git a/SVSim.UnitTests/Repositories/PackDrawTableRepositoryTests.cs b/SVSim.UnitTests/Repositories/PackDrawTableRepositoryTests.cs new file mode 100644 index 0000000..7129004 --- /dev/null +++ b/SVSim.UnitTests/Repositories/PackDrawTableRepositoryTests.cs @@ -0,0 +1,43 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using SVSim.Bootstrap.Importers; +using SVSim.Database; +using SVSim.Database.Repositories.PackDrawTables; +using SVSim.UnitTests.Infrastructure; + +namespace SVSim.UnitTests.Repositories; + +public class PackDrawTableRepositoryTests +{ + private static string SeedDir => Path.Combine(AppContext.BaseDirectory, "Data", "seeds"); + + [Test] + public async Task GetAsync_returns_null_when_pack_unseeded() + { + using var factory = new SVSimTestFactory(); + using var scope = factory.Services.CreateScope(); + var repo = scope.ServiceProvider.GetRequiredService(); + + var table = await repo.GetAsync(123456); + + Assert.That(table, Is.Null); + } + + [Test] + public async Task GetAsync_returns_config_slot_rates_and_card_weights_for_seeded_pack() + { + using var factory = new SVSimTestFactory(); + using var scope = factory.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var repo = scope.ServiceProvider.GetRequiredService(); + + await new PackDrawTableImporter().ImportAsync(db, SeedDir); + var table = await repo.GetAsync(10000); + + Assert.That(table, Is.Not.Null); + Assert.That(table!.Config.AnimationRatePct, Is.EqualTo(8.0)); + Assert.That(table.SlotRates.Count, Is.EqualTo(7)); + Assert.That(table.CardWeights.Count, Is.EqualTo(3)); + Assert.That(table.CardWeights.All(w => w.PackId == 10000), Is.True); + } +}