- IndexResponse.BattlePassLevelInfo widened to IReadOnlyDictionary<string,BattlePassLevel>?
so any IReadOnlyDictionary impl (FrozenDictionary, wrapper, etc.) serializes correctly
instead of silently null-ing via a failed as-cast
- LoadController.Index now takes CancellationToken ct and threads it to GetLevelCurveAsync
instead of CancellationToken.None
- BattlePassRepository.ResetLevelCurveCache changed from public to internal; added
InternalsVisibleTo("SVSim.UnitTests") to SVSim.Database.csproj (was absent)
61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SVSim.Database.Models;
|
|
|
|
namespace SVSim.Database.Repositories.BattlePass;
|
|
|
|
public sealed class BattlePassRepository : IBattlePassRepository
|
|
{
|
|
private readonly SVSimDbContext _db;
|
|
|
|
// Process-level cache for the immutable level curve. Bootstrap re-baseline = host restart = cache cleared.
|
|
private static IReadOnlyList<BattlePassLevelEntry>? _curveCache;
|
|
private static readonly SemaphoreSlim _curveCacheLock = new(1, 1);
|
|
|
|
public BattlePassRepository(SVSimDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public async Task<BattlePassSeasonEntry?> GetActiveSeasonAsync(DateTimeOffset when, CancellationToken ct)
|
|
{
|
|
return await _db.BattlePassSeasons
|
|
.AsNoTracking()
|
|
.Where(s => s.StartDate <= when && s.EndDate > when)
|
|
.OrderByDescending(s => s.StartDate)
|
|
.FirstOrDefaultAsync(ct);
|
|
}
|
|
|
|
public Task<BattlePassSeasonEntry?> GetSeasonAsync(int seasonId, CancellationToken ct) =>
|
|
_db.BattlePassSeasons.AsNoTracking().FirstOrDefaultAsync(s => s.Id == seasonId, ct);
|
|
|
|
public async Task<List<BattlePassRewardEntry>> GetSeasonRewardsAsync(int seasonId, CancellationToken ct) =>
|
|
await _db.BattlePassRewards.AsNoTracking()
|
|
.Where(r => r.SeasonId == seasonId)
|
|
.OrderBy(r => r.Track).ThenBy(r => r.Level)
|
|
.ToListAsync(ct);
|
|
|
|
public async Task<IReadOnlyList<BattlePassLevelEntry>> GetLevelCurveAsync(CancellationToken ct)
|
|
{
|
|
if (_curveCache is not null) return _curveCache;
|
|
await _curveCacheLock.WaitAsync(ct);
|
|
try
|
|
{
|
|
if (_curveCache is null)
|
|
{
|
|
_curveCache = await _db.BattlePassLevels.AsNoTracking()
|
|
.OrderBy(e => e.Level)
|
|
.ToListAsync(ct);
|
|
}
|
|
return _curveCache;
|
|
}
|
|
finally { _curveCacheLock.Release(); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Drops the process-level level-curve cache. Tests that seed BattlePassLevels after the
|
|
/// cache has already been populated (by an earlier test's HTTP call) must call this before
|
|
/// re-seeding so the next read fetches fresh rows.
|
|
/// </summary>
|
|
internal static void ResetLevelCurveCache() => _curveCache = null;
|
|
}
|