Files
SVSimServer/SVSim.Database/Services/Inventory/InventoryTransaction.cs
gamer147 a1cf1d7519 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>
2026-05-31 15:51:46 -04:00

155 lines
6.1 KiB
C#

using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using SVSim.Database.Enums;
using SVSim.Database.Models;
using SVSim.Database.Models.Config;
namespace SVSim.Database.Services.Inventory;
internal sealed class InventoryTransaction : IInventoryTransaction
{
private readonly SVSimDbContext _db;
private readonly IDbContextTransaction _dbTx;
private readonly ILogger _log;
private readonly FreeplayConfig _freeplay;
private bool _committed;
public Viewer Viewer { get; }
public bool IsFreeplay => _freeplay.Enabled;
private readonly List<InventoryOp> _ops = new();
internal abstract record InventoryOp;
internal sealed record SpendOp(SpendCurrency Currency, long Cost, long PostState) : InventoryOp;
internal sealed record GrantOp(UserGoodsType Type, long DetailId, int Num, int PostStateOrCount, bool IsCascade) : InventoryOp;
public InventoryTransaction(
SVSimDbContext db,
IDbContextTransaction dbTx,
Viewer viewer,
FreeplayConfig freeplay,
ILogger log)
{
_db = db;
_dbTx = dbTx;
Viewer = viewer;
_freeplay = freeplay;
_log = log;
}
// Implementations land in later tasks. Throw NotImplementedException to keep the build green.
public Task<SpendResult> TrySpendAsync(SpendCurrency currency, long cost, CancellationToken ct = default)
=> throw new NotImplementedException();
public Task<SpendResult> TryDebitAsync(UserGoodsType type, long detailId, int num, CancellationToken ct = default)
=> throw new NotImplementedException();
public async Task<IReadOnlyList<GrantedReward>> GrantAsync(UserGoodsType type, long detailId, int num, CancellationToken ct = default)
{
ThrowIfCommitted();
switch (type)
{
case UserGoodsType.Rupy:
Viewer.Currency.Rupees += (ulong)num;
var rupy = checked((int)Viewer.Currency.Rupees);
_ops.Add(new GrantOp(type, detailId, num, rupy, false));
return Single(type, detailId, rupy);
case UserGoodsType.Crystal:
Viewer.Currency.Crystals += (ulong)num;
var crystal = checked((int)Viewer.Currency.Crystals);
_ops.Add(new GrantOp(type, detailId, num, crystal, false));
return Single(type, detailId, crystal);
case UserGoodsType.RedEther:
Viewer.Currency.RedEther += (ulong)num;
var red = checked((int)Viewer.Currency.RedEther);
_ops.Add(new GrantOp(type, detailId, num, red, false));
return Single(type, detailId, red);
case UserGoodsType.SpotCardPoint:
Viewer.Currency.SpotPoints += (ulong)num;
var spot = checked((int)Viewer.Currency.SpotPoints);
_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");
}
}
public Task<int> BackfillCardCosmeticsAsync(CancellationToken ct = default)
=> throw new NotImplementedException();
public long EffectiveBalance(SpendCurrency currency) => throw new NotImplementedException();
public bool OwnsCard(long cardId) => throw new NotImplementedException();
public bool OwnsCosmetic(CosmeticType type, int id) => throw new NotImplementedException();
public Task<InventoryCommitResult> CommitAsync(CancellationToken ct = default)
=> throw new NotImplementedException();
private static IReadOnlyList<GrantedReward> Single(UserGoodsType type, long id, int num)
=> new[] { new GrantedReward((int)type, id, num) };
private void ThrowIfCommitted()
{
if (_committed)
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)
{
await _dbTx.RollbackAsync();
_db.ChangeTracker.Clear();
}
await _dbTx.DisposeAsync();
}
}