feat(inventory): CommitAsync + currency-collision rule
Last post-state per currency wins; non-currency grants collapse to final count per (type, id). Deltas are verbatim queued, no cascade. SaveChanges + DB tx commit happen atomically inside Commit; failure leaves rollback to DisposeAsync. CS0649 warning on _committed is now resolved. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -241,8 +241,88 @@ internal sealed class InventoryTransaction : IInventoryTransaction
|
||||
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();
|
||||
public async Task<InventoryCommitResult> CommitAsync(CancellationToken ct = default)
|
||||
{
|
||||
ThrowIfCommitted();
|
||||
|
||||
await _db.SaveChangesAsync(ct);
|
||||
await _dbTx.CommitAsync(ct);
|
||||
_committed = true;
|
||||
|
||||
var rewardList = BuildRewardList();
|
||||
var deltas = BuildDeltas();
|
||||
return new InventoryCommitResult(rewardList, deltas);
|
||||
}
|
||||
|
||||
private IReadOnlyList<GrantedReward> BuildRewardList()
|
||||
{
|
||||
// Pass 1 — for each currency type, find the last op (spend OR grant) that touched it
|
||||
// and emit a single entry with its post-state. Skip the sentinel item-debit currency.
|
||||
var lastCurrencyPost = new Dictionary<UserGoodsType, int>();
|
||||
var orderedTouches = new List<UserGoodsType>(); // preserve first-touch order for stable output
|
||||
|
||||
foreach (var op in _ops)
|
||||
{
|
||||
switch (op)
|
||||
{
|
||||
case SpendOp s when (int)s.Currency >= 0:
|
||||
var goodsForSpend = SpendCurrencyToGoodsType(s.Currency);
|
||||
if (!lastCurrencyPost.ContainsKey(goodsForSpend)) orderedTouches.Add(goodsForSpend);
|
||||
lastCurrencyPost[goodsForSpend] = checked((int)s.PostState);
|
||||
break;
|
||||
|
||||
case GrantOp g when IsCurrency(g.Type):
|
||||
if (!lastCurrencyPost.ContainsKey(g.Type)) orderedTouches.Add(g.Type);
|
||||
lastCurrencyPost[g.Type] = g.PostStateOrCount;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var output = new List<GrantedReward>();
|
||||
foreach (var type in orderedTouches)
|
||||
{
|
||||
output.Add(new GrantedReward((int)type, 0, lastCurrencyPost[type]));
|
||||
}
|
||||
|
||||
// Pass 2 — non-currency grants: one entry per (type, id) using LAST post-state for items
|
||||
// and cards (collapses multi-add to final count) and 1 for cosmetics.
|
||||
var nonCurrencyKey = new Dictionary<(UserGoodsType, long), int>();
|
||||
var nonCurrencyOrder = new List<(UserGoodsType, long)>();
|
||||
|
||||
foreach (var op in _ops.OfType<GrantOp>())
|
||||
{
|
||||
if (IsCurrency(op.Type)) continue;
|
||||
var key = (op.Type, op.DetailId);
|
||||
if (!nonCurrencyKey.ContainsKey(key)) nonCurrencyOrder.Add(key);
|
||||
nonCurrencyKey[key] = op.PostStateOrCount;
|
||||
}
|
||||
foreach (var (type, id) in nonCurrencyOrder)
|
||||
{
|
||||
output.Add(new GrantedReward((int)type, id, nonCurrencyKey[(type, id)]));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
private IReadOnlyList<GrantedReward> BuildDeltas()
|
||||
=> _ops.OfType<GrantOp>()
|
||||
.Where(o => !o.IsCascade)
|
||||
.Select(o => new GrantedReward((int)o.Type, o.DetailId, o.Num))
|
||||
.ToList();
|
||||
|
||||
private static bool IsCurrency(UserGoodsType t) =>
|
||||
t is UserGoodsType.Crystal
|
||||
or UserGoodsType.Rupy
|
||||
or UserGoodsType.RedEther
|
||||
or UserGoodsType.SpotCardPoint;
|
||||
|
||||
private static UserGoodsType SpendCurrencyToGoodsType(SpendCurrency c) => c switch
|
||||
{
|
||||
SpendCurrency.Crystal => UserGoodsType.Crystal,
|
||||
SpendCurrency.Rupee => UserGoodsType.Rupy,
|
||||
SpendCurrency.RedEther => UserGoodsType.RedEther,
|
||||
SpendCurrency.SpotPoint => UserGoodsType.SpotCardPoint,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(c)),
|
||||
};
|
||||
|
||||
private static IReadOnlyList<GrantedReward> Single(UserGoodsType type, long id, int num)
|
||||
=> new[] { new GrantedReward((int)type, id, num) };
|
||||
|
||||
Reference in New Issue
Block a user