feat(packs): PackDrawTable aggregate + IPackDrawTableRepository

Aggregate (Config + SlotRates + CardWeights) and a single-pack getter
loaded as one unit per /pack/open. PackOpenService consumes the
aggregate; tests use the production seed (fixture overlay) to validate
shape.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-30 21:53:33 -04:00
parent f7407fe382
commit 3c36124fa7
5 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
namespace SVSim.Database.Repositories.PackDrawTables;
public interface IPackDrawTableRepository
{
/// <summary>Returns the draw table for <paramref name="packId"/>, or null if not seeded.</summary>
Task<PackDrawTable?> GetAsync(int packId);
}

View File

@@ -0,0 +1,14 @@
using SVSim.Database.Models;
namespace SVSim.Database.Repositories.PackDrawTables;
/// <summary>
/// All draw data for a single pack: per-pack config + slot rates + per-card weights.
/// Loaded as one unit by <see cref="IPackDrawTableRepository.GetAsync"/>.
/// </summary>
public sealed class PackDrawTable
{
public required PackDrawConfigEntry Config { get; init; }
public required IReadOnlyList<PackDrawSlotRateEntry> SlotRates { get; init; }
public required IReadOnlyList<PackDrawCardWeightEntry> CardWeights { get; init; }
}

View File

@@ -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<PackDrawTable?> 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,
};
}
}

View File

@@ -74,6 +74,7 @@ public class Program
builder.Services.AddTransient<IPuzzleCatalogRepository, PuzzleCatalogRepository>();
builder.Services.AddTransient<IDeckRepository, DeckRepository>();
builder.Services.AddTransient<IPackRepository, PackRepository>();
builder.Services.AddScoped<SVSim.Database.Repositories.PackDrawTables.IPackDrawTableRepository, SVSim.Database.Repositories.PackDrawTables.PackDrawTableRepository>();
builder.Services.AddTransient<IBuildDeckRepository, BuildDeckRepository>();
// 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

View File

@@ -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<IPackDrawTableRepository>();
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<SVSimDbContext>();
var repo = scope.ServiceProvider.GetRequiredService<IPackDrawTableRepository>();
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);
}
}