feat(inventory): GrantAsync handles cosmetic branches

Sleeve/Emblem/Skin/Degree/MyPageBG grants are idempotent on the viewer's
owned-collection but always emit a wire entry at the top level (preserves
"+1 sleeve" purchase popup). Unknown ids throw InventoryCatalogException.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-31 15:51:46 -04:00
parent 3bc38b407b
commit a1cf1d7519
2 changed files with 112 additions and 0 deletions

View File

@@ -74,6 +74,31 @@ internal sealed class InventoryTransaction : IInventoryTransaction
_ops.Add(new GrantOp(type, detailId, num, spot, false));
return Single(type, detailId, spot);
case UserGoodsType.Sleeve:
AddCosmeticIfMissing(Viewer.Sleeves, detailId, _db.Sleeves);
_ops.Add(new GrantOp(type, detailId, num, 1, false));
return Single(type, detailId, 1);
case UserGoodsType.Emblem:
AddCosmeticIfMissing(Viewer.Emblems, detailId, _db.Emblems);
_ops.Add(new GrantOp(type, detailId, num, 1, false));
return Single(type, detailId, 1);
case UserGoodsType.Skin:
AddCosmeticIfMissing(Viewer.LeaderSkins, detailId, _db.LeaderSkins);
_ops.Add(new GrantOp(type, detailId, num, 1, false));
return Single(type, detailId, 1);
case UserGoodsType.Degree:
AddCosmeticIfMissing(Viewer.Degrees, detailId, _db.Degrees);
_ops.Add(new GrantOp(type, detailId, num, 1, false));
return Single(type, detailId, 1);
case UserGoodsType.MyPageBG:
AddCosmeticIfMissing(Viewer.MyPageBackgrounds, detailId, _db.MyPageBackgrounds);
_ops.Add(new GrantOp(type, detailId, num, 1, false));
return Single(type, detailId, 1);
default:
throw new NotImplementedException(
$"UserGoodsType {type} grant lands in a subsequent task");
@@ -99,6 +124,24 @@ internal sealed class InventoryTransaction : IInventoryTransaction
throw new InvalidOperationException("Inventory transaction already committed");
}
private static bool AddCosmeticIfMissing<T>(List<T> collection, long detailId, Microsoft.EntityFrameworkCore.DbSet<T> catalog) where T : class
{
if (collection.Any(e => GetId(e) == detailId)) return false;
var entity = catalog.Find(checked((int)detailId))
?? throw new InventoryCatalogException(
$"Cosmetic id {detailId} not in catalog for type {typeof(T).Name}");
collection.Add(entity);
return true;
}
private static long GetId<T>(T e)
{
var prop = typeof(T).GetProperty("Id")
?? throw new InvalidOperationException($"Type {typeof(T).Name} missing Id property");
var val = prop.GetValue(e);
return val switch { long l => l, int i => i, _ => 0 };
}
public async ValueTask DisposeAsync()
{
if (!_committed)

View File

@@ -0,0 +1,69 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SVSim.Database;
using SVSim.Database.Enums;
using SVSim.Database.Models;
using SVSim.Database.Services.Inventory;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Services.Inventory;
public class InventoryGrantCosmeticTests
{
[Test]
public async Task Sleeve_added_when_missing()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var scope = factory.Services.CreateScope();
var ctx = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
const int sleeveId = 2_000_000_001;
ctx.Sleeves.Add(new SleeveEntry { Id = sleeveId });
await ctx.SaveChangesAsync();
var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId);
var granted = await tx.GrantAsync(UserGoodsType.Sleeve, sleeveId, 1);
Assert.That(granted, Has.Count.EqualTo(1));
Assert.That(granted[0].RewardType, Is.EqualTo((int)UserGoodsType.Sleeve));
Assert.That(granted[0].RewardId, Is.EqualTo(sleeveId));
Assert.That(granted[0].RewardNum, Is.EqualTo(1));
Assert.That(tx.Viewer.Sleeves.Any(s => s.Id == sleeveId), Is.True);
}
[Test]
public async Task Sleeve_idempotent_when_already_owned_but_still_emits_entry()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var scope = factory.Services.CreateScope();
var ctx = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
const int sleeveId = 2_000_000_002;
var sleeve = new SleeveEntry { Id = sleeveId };
ctx.Sleeves.Add(sleeve);
var v = await ctx.Viewers.Include(x => x.Sleeves).FirstAsync(x => x.Id == viewerId);
v.Sleeves.Add(sleeve);
await ctx.SaveChangesAsync();
var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId);
var granted = await tx.GrantAsync(UserGoodsType.Sleeve, sleeveId, 1);
Assert.That(granted, Has.Count.EqualTo(1), "top-level cosmetic grant emits even if owned");
Assert.That(tx.Viewer.Sleeves.Count(s => s.Id == sleeveId), Is.EqualTo(1), "no duplicate row");
}
[Test]
public async Task Unknown_cosmetic_id_throws_catalog_exception()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var scope = factory.Services.CreateScope();
var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId);
Assert.ThrowsAsync<InventoryCatalogException>(
async () => { await tx.GrantAsync(UserGoodsType.Sleeve, 999_999, 1); });
}
}