feat(inventory): TryDebitAsync dispatches currencies + Item
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -82,7 +82,34 @@ internal sealed class InventoryTransaction : IInventoryTransaction
|
||||
}
|
||||
|
||||
public Task<SpendResult> TryDebitAsync(UserGoodsType type, long detailId, int num, CancellationToken ct = default)
|
||||
=> throw new NotImplementedException();
|
||||
{
|
||||
ThrowIfCommitted();
|
||||
return type switch
|
||||
{
|
||||
UserGoodsType.Crystal => TrySpendAsync(SpendCurrency.Crystal, num, ct),
|
||||
UserGoodsType.Rupy => TrySpendAsync(SpendCurrency.Rupee, num, ct),
|
||||
UserGoodsType.RedEther => TrySpendAsync(SpendCurrency.RedEther, num, ct),
|
||||
UserGoodsType.SpotCardPoint => TrySpendAsync(SpendCurrency.SpotPoint, num, ct),
|
||||
UserGoodsType.Item => Task.FromResult(DebitItem(detailId, num)),
|
||||
_ => throw new NotSupportedException($"Debit not supported for {type}"),
|
||||
};
|
||||
}
|
||||
|
||||
private SpendResult DebitItem(long detailId, int num)
|
||||
{
|
||||
var owned = Viewer.Items.FirstOrDefault(i => i.Item.Id == (int)detailId);
|
||||
if (owned is null)
|
||||
throw new InventoryCatalogException($"Item {detailId} not owned by viewer");
|
||||
if (owned.Count < num)
|
||||
return new SpendResult(SpendOutcome.Insufficient, owned.Count);
|
||||
owned.Count -= num;
|
||||
// Item debit logged as a synthetic SpendOp so CommitAsync can track it.
|
||||
// Sentinel currency (int)-1 is filtered out by CommitAsync's currency-collision loop.
|
||||
_ops.Add(new SpendOp((SpendCurrency)(-1) /* sentinel */, num, owned.Count));
|
||||
// IsCascade: true so this GrantOp is excluded from BuildDeltas output.
|
||||
_ops.Add(new GrantOp(UserGoodsType.Item, detailId, 0, owned.Count, IsCascade: true));
|
||||
return new SpendResult(SpendOutcome.Success, owned.Count);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<GrantedReward>> GrantAsync(UserGoodsType type, long detailId, int num, CancellationToken ct = default)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user