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:
@@ -4,6 +4,7 @@ using NUnit.Framework;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Enums;
|
||||
using SVSim.Database.Models;
|
||||
using SVSim.Database.Services.Inventory;
|
||||
using SVSim.EmulatedEntrypoint.Services;
|
||||
using SVSim.UnitTests.Infrastructure;
|
||||
|
||||
@@ -380,14 +381,17 @@ public class GachaPointServiceTests
|
||||
|
||||
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
|
||||
|
||||
var viewer = await db.Viewers
|
||||
.Include(v => v.GachaPointBalances)
|
||||
.FirstAsync(v => v.Id == viewerId);
|
||||
var viewer = await db.Viewers.Include(v => v.GachaPointBalances).FirstAsync(v => v.Id == viewerId);
|
||||
viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 399 });
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
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.Error, Is.EqualTo("insufficient_gacha_points"));
|
||||
@@ -403,14 +407,17 @@ public class GachaPointServiceTests
|
||||
|
||||
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
|
||||
|
||||
var viewer = await db.Viewers
|
||||
.Include(v => v.GachaPointBalances)
|
||||
.FirstAsync(v => v.Id == viewerId);
|
||||
var viewer = await db.Viewers.Include(v => v.GachaPointBalances).FirstAsync(v => v.Id == viewerId);
|
||||
viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 400 });
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
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.Error, Is.EqualTo("card_not_exchangeable"));
|
||||
@@ -438,7 +445,12 @@ public class GachaPointServiceTests
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
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.Error, Is.EqualTo("already_received"));
|
||||
@@ -454,29 +466,31 @@ public class GachaPointServiceTests
|
||||
|
||||
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
|
||||
|
||||
var viewer = await db.Viewers
|
||||
var preViewer = await db.Viewers
|
||||
.Include(v => v.GachaPointBalances)
|
||||
.Include(v => v.GachaPointReceived)
|
||||
.Include(v => v.Cards)
|
||||
.Include(v => v.Emblems)
|
||||
.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();
|
||||
|
||||
var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>();
|
||||
var outcome = await svc.TryExchangeAsync(viewer, 10008, 108041010);
|
||||
await db.SaveChangesAsync();
|
||||
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.True);
|
||||
|
||||
// Balance debited.
|
||||
Assert.That(viewer.GachaPointBalances.Single().Points, Is.EqualTo(100));
|
||||
await tx.CommitAsync();
|
||||
|
||||
// Balance debited (check via tx.Viewer which is tracked).
|
||||
Assert.That(tx.Viewer.GachaPointBalances.Single().Points, Is.EqualTo(100));
|
||||
|
||||
// Marker written.
|
||||
Assert.That(viewer.GachaPointReceived
|
||||
Assert.That(tx.Viewer.GachaPointReceived
|
||||
.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.Any(r => r.RewardType == (int)UserGoodsType.Card && r.RewardId == 108041010),
|
||||
Is.True, "card grant missing");
|
||||
|
||||
Reference in New Issue
Block a user