feat(pack): gacha-point exchange (debit + grant)
Implements GachaPointService.TryExchangeAsync: validates pack exchangeability, balance >= threshold, card in catalog, not already received; debits balance, marks received, grants the card through RewardGrantService (cascade handles cosmetics). Re-adds the RewardGrantService injection that was removed in the Task 3 fix-up (matches the "inject when you call" convention). Card grant produces the wire-shape reward_list directly via the cosmetic cascade — the catalog's reward_list remains the display-only shape for /pack/get_gacha_point_rewards. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Enums;
|
||||
using SVSim.Database.Models;
|
||||
using SVSim.Database.Services;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Services;
|
||||
@@ -10,11 +11,13 @@ public sealed class GachaPointService : IGachaPointService
|
||||
{
|
||||
private readonly SVSimDbContext _db;
|
||||
private readonly ICardPoolProvider _pools;
|
||||
private readonly RewardGrantService _grants;
|
||||
|
||||
public GachaPointService(SVSimDbContext db, ICardPoolProvider pools)
|
||||
public GachaPointService(SVSimDbContext db, ICardPoolProvider pools, RewardGrantService grants)
|
||||
{
|
||||
_db = db;
|
||||
_pools = pools;
|
||||
_grants = grants;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<GachaPointRewardDto>> GetRewardsAsync(int packId, long viewerId)
|
||||
@@ -149,6 +152,55 @@ public sealed class GachaPointService : IGachaPointService
|
||||
}
|
||||
}
|
||||
|
||||
public Task<ExchangeOutcome> TryExchangeAsync(Viewer viewer, int packId, long cardId)
|
||||
=> throw new NotImplementedException();
|
||||
public async Task<ExchangeOutcome> TryExchangeAsync(Viewer viewer, int packId, long cardId)
|
||||
{
|
||||
var pack = await _db.Packs.FirstOrDefaultAsync(p => p.Id == packId);
|
||||
if (pack?.GachaPointConfig is null)
|
||||
return ExchangeOutcome.Fail("pack_not_exchangeable");
|
||||
|
||||
int threshold = pack.GachaPointConfig.ExchangeablePoint;
|
||||
var balance = viewer.GachaPointBalances.FirstOrDefault(b => b.PackId == packId);
|
||||
int currentPoints = balance?.Points ?? 0;
|
||||
if (currentPoints < threshold)
|
||||
return ExchangeOutcome.Fail("insufficient_gacha_points");
|
||||
|
||||
// Validate the card is in the catalog by re-running GetRewardsAsync. This re-uses the
|
||||
// same eligibility rules (in-pool + Legendary + has Emblem cosmetic) without
|
||||
// duplicating them — and naturally excludes ticket-only packs whose pool we already
|
||||
// hide from /pack/info.
|
||||
var catalog = await GetRewardsAsync(packId, viewer.Id);
|
||||
var entry = catalog.FirstOrDefault(e => e.CardId == cardId);
|
||||
if (entry is null)
|
||||
return ExchangeOutcome.Fail("card_not_exchangeable");
|
||||
|
||||
if (viewer.GachaPointReceived.Any(r => r.PackId == packId && r.CardId == cardId))
|
||||
return ExchangeOutcome.Fail("already_received");
|
||||
|
||||
// Debit balance + mark received.
|
||||
balance!.Points -= threshold;
|
||||
viewer.GachaPointReceived.Add(new ViewerGachaPointReceived
|
||||
{
|
||||
PackId = packId, CardId = cardId, ReceivedAt = DateTime.UtcNow,
|
||||
});
|
||||
|
||||
// Grant the card itself through RewardGrantService — its CardCosmeticReward cascade
|
||||
// covers the Emblem (standard legendary) or Skin+Emblem (leader) the catalog
|
||||
// advertised. The catalog's reward_list is a wire-shape *display* (what the player
|
||||
// sees on /pack/get_gacha_point_rewards) — the actual grant uses the canonical
|
||||
// primitive per feedback_reward_grant_service. For leader-card exchanges the catalog
|
||||
// also advertises a synthetic Sleeve(=card_id) entry, but that's not in
|
||||
// CardCosmeticRewards; if a capture ever shows leader exchanges granting a sleeve
|
||||
// row, add that here. Today no leader exchange has been captured.
|
||||
var granted = await _grants.ApplyAsync(viewer, UserGoodsType.Card, cardId, 1);
|
||||
var rewardList = new List<RewardListEntry>();
|
||||
foreach (var g in granted)
|
||||
{
|
||||
rewardList.Add(new RewardListEntry
|
||||
{
|
||||
RewardType = g.RewardType, RewardId = g.RewardId, RewardNum = g.RewardNum,
|
||||
});
|
||||
}
|
||||
|
||||
return ExchangeOutcome.Ok(rewardList);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user