feat(inventory): GrantAsync handles currency branches
Crystal/Rupy/RedEther/SpotCardPoint grants mutate ViewerCurrency in place and emit post-state-total wire entries. Op log records the post-state for later currency-collision resolution in CommitAsync. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,12 @@ internal sealed class InventoryTransaction : IInventoryTransaction
|
||||
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,
|
||||
@@ -38,8 +44,41 @@ internal sealed class InventoryTransaction : IInventoryTransaction
|
||||
public Task<SpendResult> TryDebitAsync(UserGoodsType type, long detailId, int num, CancellationToken ct = default)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public Task<IReadOnlyList<GrantedReward>> GrantAsync(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);
|
||||
|
||||
default:
|
||||
throw new NotImplementedException(
|
||||
$"UserGoodsType {type} grant lands in a subsequent task");
|
||||
}
|
||||
}
|
||||
|
||||
public Task<int> BackfillCardCosmeticsAsync(CancellationToken ct = default)
|
||||
=> throw new NotImplementedException();
|
||||
@@ -51,6 +90,15 @@ internal sealed class InventoryTransaction : IInventoryTransaction
|
||||
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");
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (!_committed)
|
||||
|
||||
Reference in New Issue
Block a user