feat(pack): gacha-point exchange (debit + grant)
Implements GachaPointService.TryExchangeAsync: validates pack exchangeability, balance >= threshold, card in catalog, not already received; debits balance, marks received, grants the card through RewardGrantService (cascade handles cosmetics). Re-adds the RewardGrantService injection that was removed in the Task 3 fix-up (matches the "inject when you call" convention). Card grant produces the wire-shape reward_list directly via the cosmetic cascade — the catalog's reward_list remains the display-only shape for /pack/get_gacha_point_rewards. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -322,4 +322,140 @@ public class GachaPointServiceTests
|
||||
"second Accrue must update the existing row, not create a duplicate");
|
||||
Assert.That(viewer.GachaPointBalances.Single().Points, Is.EqualTo(8));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TryExchange_fails_when_balance_below_threshold()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
long viewerId = await factory.SeedViewerAsync();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
|
||||
|
||||
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);
|
||||
|
||||
Assert.That(outcome.Success, Is.False);
|
||||
Assert.That(outcome.Error, Is.EqualTo("insufficient_gacha_points"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TryExchange_fails_when_card_not_in_pack_catalog()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
long viewerId = await factory.SeedViewerAsync();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
|
||||
|
||||
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
|
||||
|
||||
Assert.That(outcome.Success, Is.False);
|
||||
Assert.That(outcome.Error, Is.EqualTo("card_not_exchangeable"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TryExchange_fails_when_card_already_received()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
long viewerId = await factory.SeedViewerAsync();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
|
||||
|
||||
var viewer = await db.Viewers
|
||||
.Include(v => v.GachaPointBalances)
|
||||
.Include(v => v.GachaPointReceived)
|
||||
.FirstAsync(v => v.Id == viewerId);
|
||||
viewer.GachaPointBalances.Add(new ViewerGachaPointBalance { PackId = 10008, Points = 400 });
|
||||
viewer.GachaPointReceived.Add(new ViewerGachaPointReceived
|
||||
{
|
||||
PackId = 10008, CardId = 108041010, ReceivedAt = DateTime.UtcNow,
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>();
|
||||
var outcome = await svc.TryExchangeAsync(viewer, 10008, 108041010);
|
||||
|
||||
Assert.That(outcome.Success, Is.False);
|
||||
Assert.That(outcome.Error, Is.EqualTo("already_received"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TryExchange_succeeds_debits_marks_received_and_returns_grants()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
long viewerId = await factory.SeedViewerAsync();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
SeedPackWithOneLegendary(db, packId: 10008, threshold: 400);
|
||||
|
||||
var viewer = 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 });
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
var svc = scope.ServiceProvider.GetRequiredService<IGachaPointService>();
|
||||
var outcome = await svc.TryExchangeAsync(viewer, 10008, 108041010);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
Assert.That(outcome.Success, Is.True);
|
||||
|
||||
// Balance debited.
|
||||
Assert.That(viewer.GachaPointBalances.Single().Points, Is.EqualTo(100));
|
||||
|
||||
// Marker written.
|
||||
Assert.That(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.
|
||||
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");
|
||||
}
|
||||
|
||||
private static void SeedPackWithOneLegendary(SVSimDbContext db, int packId, int threshold)
|
||||
{
|
||||
var cls = db.Classes.Find(0) ?? new ClassEntry { Id = 0, Name = "Neutral" };
|
||||
if (db.Classes.Find(0) is null) db.Classes.Add(cls);
|
||||
var set = new ShadowverseCardSetEntry { Id = packId, IsInRotation = true };
|
||||
db.CardSets.Add(set);
|
||||
set.Cards.Add(new ShadowverseCardEntry
|
||||
{
|
||||
Id = 108041010, Name = "leg", Rarity = Rarity.Legendary,
|
||||
Class = cls, IsFoil = false,
|
||||
});
|
||||
db.CardCosmeticRewards.Add(new CardCosmeticReward
|
||||
{
|
||||
CardId = 108041010, Type = CosmeticType.Emblem, CosmeticId = 1080410100,
|
||||
});
|
||||
db.Packs.Add(new PackConfigEntry
|
||||
{
|
||||
Id = packId, BasePackId = packId, PackCategory = PackCategory.LegendCardPack,
|
||||
CommenceDate = DateTime.UtcNow.AddDays(-1), CompleteDate = DateTime.UtcNow.AddDays(30),
|
||||
GachaPointConfig = new PackGachaPointConfig { ExchangeablePoint = threshold, IncreaseGachaPoint = 1 },
|
||||
});
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user