refactor(gacha-point): route TryExchangeAsync through IInventoryTransaction

Change signature from (Viewer, packId, cardId) to (IInventoryTransaction, packId, cardId).
Drop RewardGrantService from GachaPointService ctor. PackController.ExchangeGachaPoint opens
tx with GachaPointBalances/Received extra includes, passes tx, commits on success.
Update GachaPointServiceTests to use inv.BeginAsync + tx pattern.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-31 16:55:08 -04:00
parent b6bf9b7495
commit c37c04c1b7
4 changed files with 60 additions and 60 deletions

View File

@@ -202,26 +202,18 @@ public class PackController : SVSimController
{ {
if (!TryGetViewerId(out long viewerId)) return Unauthorized(); if (!TryGetViewerId(out long viewerId)) return Unauthorized();
// Load the viewer with the collections the service mutates (balances, received marker, // Open inventory tx with extra includes for GachaPointBalances + GachaPointReceived
// cards, cosmetics). AsSplitQuery per project_ef_split_query memory. // (needed by TryExchangeAsync to validate balance and already-received guard).
var viewer = await _db.Viewers await using var tx = await _inv.BeginAsync(viewerId, configure: cfg => cfg
.Include(v => v.GachaPointBalances) .WithInclude(v => v.GachaPointBalances)
.Include(v => v.GachaPointReceived) .WithInclude(v => v.GachaPointReceived));
.Include(v => v.Cards)
.Include(v => v.Sleeves)
.Include(v => v.Emblems)
.Include(v => v.Degrees)
.Include(v => v.LeaderSkins)
.Include(v => v.MyPageBackgrounds)
.AsSplitQuery()
.FirstAsync(v => v.Id == viewerId);
// Use odds_gacha_id (the seasonal pack id) — that's where the balance / received marker // Use odds_gacha_id (the seasonal pack id) — that's where the balance / received marker
// live. Mirrors the GetGachaPointRewards fix. // live. Mirrors the GetGachaPointRewards fix.
var outcome = await _gachaPoint.TryExchangeAsync(viewer, request.OddsGachaId, request.CardId); var outcome = await _gachaPoint.TryExchangeAsync(tx, request.OddsGachaId, request.CardId);
if (!outcome.Success) return BadRequest(new { error = outcome.Error }); if (!outcome.Success) return BadRequest(new { error = outcome.Error });
await _db.SaveChangesAsync(); await tx.CommitAsync();
return new ExchangeGachaPointResponse return new ExchangeGachaPointResponse
{ {

View File

@@ -5,6 +5,7 @@ using SVSim.Database.Enums;
using SVSim.Database.Models; using SVSim.Database.Models;
using SVSim.Database.Repositories.PackDrawTables; using SVSim.Database.Repositories.PackDrawTables;
using SVSim.Database.Services; using SVSim.Database.Services;
using SVSim.Database.Services.Inventory;
using SVSim.EmulatedEntrypoint.Models.Dtos; using SVSim.EmulatedEntrypoint.Models.Dtos;
namespace SVSim.EmulatedEntrypoint.Services; namespace SVSim.EmulatedEntrypoint.Services;
@@ -13,13 +14,11 @@ public sealed class GachaPointService : IGachaPointService
{ {
private readonly SVSimDbContext _db; private readonly SVSimDbContext _db;
private readonly IPackDrawTableRepository _drawTables; private readonly IPackDrawTableRepository _drawTables;
private readonly RewardGrantService _grants;
public GachaPointService(SVSimDbContext db, IPackDrawTableRepository drawTables, RewardGrantService grants) public GachaPointService(SVSimDbContext db, IPackDrawTableRepository drawTables)
{ {
_db = db; _db = db;
_drawTables = drawTables; _drawTables = drawTables;
_grants = grants;
} }
public async Task<IReadOnlyList<GachaPointRewardDto>> GetRewardsAsync(int packId, long viewerId) public async Task<IReadOnlyList<GachaPointRewardDto>> GetRewardsAsync(int packId, long viewerId)
@@ -176,8 +175,9 @@ public sealed class GachaPointService : IGachaPointService
} }
} }
public async Task<ExchangeOutcome> TryExchangeAsync(Viewer viewer, int packId, long cardId) public async Task<ExchangeOutcome> TryExchangeAsync(IInventoryTransaction tx, int packId, long cardId)
{ {
var viewer = tx.Viewer;
var pack = await _db.Packs.FirstOrDefaultAsync(p => p.Id == packId); var pack = await _db.Packs.FirstOrDefaultAsync(p => p.Id == packId);
if (pack?.GachaPointConfig is null) if (pack?.GachaPointConfig is null)
return ExchangeOutcome.Fail("pack_not_exchangeable"); return ExchangeOutcome.Fail("pack_not_exchangeable");
@@ -206,23 +206,13 @@ public sealed class GachaPointService : IGachaPointService
PackId = packId, CardId = cardId, ReceivedAt = DateTime.UtcNow, PackId = packId, CardId = cardId, ReceivedAt = DateTime.UtcNow,
}); });
// Grant the card itself through RewardGrantService — its CardCosmeticReward cascade // Grant the card via the inventory tx — its CardCosmeticReward cascade covers the
// covers the Emblem (standard legendary) or Skin+Emblem (leader) the catalog // Emblem (standard legendary) or Skin+Emblem (leader). Convert at the wire boundary
// advertised. The catalog's reward_list is a wire-shape *display* (what the player // so ExchangeOutcome still carries RewardListEntry for the controller response.
// sees on /pack/get_gacha_point_rewards) — the actual grant uses the canonical var granted = await tx.GrantAsync(UserGoodsType.Card, cardId, 1);
// primitive per feedback_reward_grant_service. For leader-card exchanges the catalog var rewardList = granted
// also advertises a synthetic Sleeve(=card_id) entry, but that's not in .Select(g => new RewardListEntry { RewardType = g.RewardType, RewardId = g.RewardId, RewardNum = g.RewardNum })
// CardCosmeticRewards; if a capture ever shows leader exchanges granting a sleeve .ToList();
// row, add that here. Today no leader exchange has been captured.
var granted = await _grants.ApplyAsync(viewer, UserGoodsType.Card, cardId, 1);
var rewardList = new List<RewardListEntry>();
foreach (var g in granted)
{
rewardList.Add(new RewardListEntry
{
RewardType = g.RewardType, RewardId = g.RewardId, RewardNum = g.RewardNum,
});
}
return ExchangeOutcome.Ok(rewardList); return ExchangeOutcome.Ok(rewardList);
} }

View File

@@ -1,4 +1,5 @@
using SVSim.Database.Models; using SVSim.Database.Models;
using SVSim.Database.Services.Inventory;
using SVSim.EmulatedEntrypoint.Models.Dtos; using SVSim.EmulatedEntrypoint.Models.Dtos;
namespace SVSim.EmulatedEntrypoint.Services; namespace SVSim.EmulatedEntrypoint.Services;
@@ -23,11 +24,14 @@ public interface IGachaPointService
void Accrue(Viewer viewer, PackConfigEntry pack, PackChildGachaEntry child, int packNumber); void Accrue(Viewer viewer, PackConfigEntry pack, PackChildGachaEntry child, int packNumber);
/// <summary> /// <summary>
/// Validate + execute an exchange. Returns the grant outcome on success (reward_list /// Validate + execute an exchange using the provided inventory transaction (which must
/// entries the controller will return in <see cref="Dtos.Responses.Pack.ExchangeGachaPointResponse"/>), /// have <c>GachaPointBalances</c> and <c>GachaPointReceived</c> loaded on <c>tx.Viewer</c>
/// or a failure result describing why. Mutates the in-memory graph; caller saves. /// via <see cref="IInventoryService.BeginAsync"/> extra includes). Grants the card via
/// the tx. Returns the grant outcome on success (reward_list entries already converted to
/// <see cref="RewardListEntry"/>), or a failure result describing why. Caller commits
/// the tx on success.
/// </summary> /// </summary>
Task<ExchangeOutcome> TryExchangeAsync(Viewer viewer, int packId, long cardId); Task<ExchangeOutcome> TryExchangeAsync(IInventoryTransaction tx, int packId, long cardId);
} }
public sealed record ExchangeOutcome(bool Success, string? Error, IReadOnlyList<RewardListEntry> RewardList) public sealed record ExchangeOutcome(bool Success, string? Error, IReadOnlyList<RewardListEntry> RewardList)

View File

@@ -4,6 +4,7 @@ using NUnit.Framework;
using SVSim.Database; using SVSim.Database;
using SVSim.Database.Enums; using SVSim.Database.Enums;
using SVSim.Database.Models; using SVSim.Database.Models;
using SVSim.Database.Services.Inventory;
using SVSim.EmulatedEntrypoint.Services; using SVSim.EmulatedEntrypoint.Services;
using SVSim.UnitTests.Infrastructure; using SVSim.UnitTests.Infrastructure;
@@ -380,14 +381,17 @@ public class GachaPointServiceTests
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400); SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
var viewer = await db.Viewers var viewer = await db.Viewers.Include(v => v.GachaPointBalances).FirstAsync(v => v.Id == viewerId);
.Include(v => v.GachaPointBalances)
.FirstAsync(v => v.Id == viewerId);
viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 399 }); viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 399 });
await db.SaveChangesAsync(); await db.SaveChangesAsync();
var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>(); var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>();
var outcome = await svc.TryExchangeAsync(viewer, 10008, 108041010); var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId, configure: cfg => cfg
.WithInclude(v => v.GachaPointBalances)
.WithInclude(v => v.GachaPointReceived));
var outcome = await svc.TryExchangeAsync(tx, 10008, 108041010);
Assert.That(outcome.Success, Is.False); Assert.That(outcome.Success, Is.False);
Assert.That(outcome.Error, Is.EqualTo("insufficient_gacha_points")); Assert.That(outcome.Error, Is.EqualTo("insufficient_gacha_points"));
@@ -403,14 +407,17 @@ public class GachaPointServiceTests
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400); SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
var viewer = await db.Viewers var viewer = await db.Viewers.Include(v => v.GachaPointBalances).FirstAsync(v => v.Id == viewerId);
.Include(v => v.GachaPointBalances)
.FirstAsync(v => v.Id == viewerId);
viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 400 }); viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 400 });
await db.SaveChangesAsync(); await db.SaveChangesAsync();
var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>(); var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>();
var outcome = await svc.TryExchangeAsync(viewer, 10008, cardId: 999999999); // not in pool var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId, configure: cfg => cfg
.WithInclude(v => v.GachaPointBalances)
.WithInclude(v => v.GachaPointReceived));
var outcome = await svc.TryExchangeAsync(tx, 10008, cardId: 999999999); // not in pool
Assert.That(outcome.Success, Is.False); Assert.That(outcome.Success, Is.False);
Assert.That(outcome.Error, Is.EqualTo("card_not_exchangeable")); Assert.That(outcome.Error, Is.EqualTo("card_not_exchangeable"));
@@ -438,7 +445,12 @@ public class GachaPointServiceTests
await db.SaveChangesAsync(); await db.SaveChangesAsync();
var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>(); var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>();
var outcome = await svc.TryExchangeAsync(viewer, 10008, 108041010); var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId, configure: cfg => cfg
.WithInclude(v => v.GachaPointBalances)
.WithInclude(v => v.GachaPointReceived));
var outcome = await svc.TryExchangeAsync(tx, 10008, 108041010);
Assert.That(outcome.Success, Is.False); Assert.That(outcome.Success, Is.False);
Assert.That(outcome.Error, Is.EqualTo("already_received")); Assert.That(outcome.Error, Is.EqualTo("already_received"));
@@ -454,29 +466,31 @@ public class GachaPointServiceTests
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400); SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
var viewer = await db.Viewers var preViewer = await db.Viewers
.Include(v => v.GachaPointBalances) .Include(v => v.GachaPointBalances)
.Include(v => v.GachaPointReceived)
.Include(v => v.Cards)
.Include(v => v.Emblems)
.FirstAsync(v => v.Id == viewerId); .FirstAsync(v => v.Id == viewerId);
viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 500 }); preViewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 500 });
await db.SaveChangesAsync(); await db.SaveChangesAsync();
var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>(); var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>();
var outcome = await svc.TryExchangeAsync(viewer, 10008, 108041010); var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await db.SaveChangesAsync(); await using var tx = await inv.BeginAsync(viewerId, configure: cfg => cfg
.WithInclude(v => v.GachaPointBalances)
.WithInclude(v => v.GachaPointReceived));
var outcome = await svc.TryExchangeAsync(tx, 10008, 108041010);
Assert.That(outcome.Success, Is.True); Assert.That(outcome.Success, Is.True);
// Balance debited. await tx.CommitAsync();
Assert.That(viewer.GachaPointBalances.Single().Points, Is.EqualTo(100));
// Balance debited (check via tx.Viewer which is tracked).
Assert.That(tx.Viewer.GachaPointBalances.Single().Points, Is.EqualTo(100));
// Marker written. // Marker written.
Assert.That(viewer.GachaPointReceived Assert.That(tx.Viewer.GachaPointReceived
.Any(r => r.PackId == 10008 && r.CardId == 108041010), Is.True); .Any(r => r.PackId == 10008 && r.CardId == 108041010), Is.True);
// Reward list non-empty: at minimum the card grant and the gacha-point post-state entry. // Reward list non-empty: at minimum the card grant.
Assert.That(outcome.RewardList, Is.Not.Empty); Assert.That(outcome.RewardList, Is.Not.Empty);
Assert.That(outcome.RewardList.Any(r => r.RewardType == (int)UserGoodsType.Card && r.RewardId == 108041010), Assert.That(outcome.RewardList.Any(r => r.RewardType == (int)UserGoodsType.Card && r.RewardId == 108041010),
Is.True, "card grant missing"); Is.True, "card grant missing");