refactor(builddeck): route currency spend through CurrencySpendService

Inject ICurrencySpendService and replace the inline crystal/rupee debit
block in BuildDeckController.Buy with TrySpendAsync calls, so freeplay
mode gets the no-deduct path automatically. All 18 BuildDeckController
tests pass unchanged.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-29 14:12:43 -04:00
parent 163299504a
commit 2e021c8b9e

View File

@@ -22,15 +22,18 @@ public class BuildDeckController : SVSimController
private readonly IBuildDeckRepository _repo; private readonly IBuildDeckRepository _repo;
private readonly SVSimDbContext _db; private readonly SVSimDbContext _db;
private readonly RewardGrantService _rewards; private readonly RewardGrantService _rewards;
private readonly ICurrencySpendService _spend;
public BuildDeckController( public BuildDeckController(
IBuildDeckRepository repo, IBuildDeckRepository repo,
SVSimDbContext db, SVSimDbContext db,
RewardGrantService rewards) RewardGrantService rewards,
ICurrencySpendService spend)
{ {
_repo = repo; _repo = repo;
_db = db; _db = db;
_rewards = rewards; _rewards = rewards;
_spend = spend;
} }
/// <summary> /// <summary>
@@ -200,19 +203,15 @@ public class BuildDeckController : SVSimController
// Debit + post-state currency entry // Debit + post-state currency entry
if (request.SalesType == 1) if (request.SalesType == 1)
{ {
ulong cost = (ulong)priceCrystal!.Value; var r = await _spend.TrySpendAsync(viewer, SpendCurrency.Crystal, priceCrystal!.Value);
if (viewer.Currency.Crystals < cost) if (!r.Success) return BadRequest(new { error = "insufficient_crystals" });
return BadRequest(new { error = "insufficient_crystals" }); rewardList.Add(new RewardListEntry { RewardType = 2, RewardId = 0, RewardNum = (int)r.PostStateTotal });
viewer.Currency.Crystals -= cost;
rewardList.Add(new RewardListEntry { RewardType = 2, RewardId = 0, RewardNum = (int)viewer.Currency.Crystals });
} }
else if (request.SalesType == 2) else if (request.SalesType == 2)
{ {
ulong cost = (ulong)priceRupy!.Value; var r = await _spend.TrySpendAsync(viewer, SpendCurrency.Rupee, priceRupy!.Value);
if (viewer.Currency.Rupees < cost) if (!r.Success) return BadRequest(new { error = "insufficient_rupees" });
return BadRequest(new { error = "insufficient_rupees" }); rewardList.Add(new RewardListEntry { RewardType = 9, RewardId = 0, RewardNum = (int)r.PostStateTotal });
viewer.Currency.Rupees -= cost;
rewardList.Add(new RewardListEntry { RewardType = 9, RewardId = 0, RewardNum = (int)viewer.Currency.Rupees });
} }
// sales_type == 0 (free): no debit, no currency entry // sales_type == 0 (free): no debit, no currency entry