feat(pack): gacha-point catalog read (legendaries + leader cards)
This commit is contained in:
121
SVSim.EmulatedEntrypoint/Services/GachaPointService.cs
Normal file
121
SVSim.EmulatedEntrypoint/Services/GachaPointService.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
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;
|
||||
|
||||
public sealed class GachaPointService : IGachaPointService
|
||||
{
|
||||
private readonly SVSimDbContext _db;
|
||||
private readonly ICardPoolProvider _pools;
|
||||
private readonly RewardGrantService _grants;
|
||||
|
||||
public GachaPointService(SVSimDbContext db, ICardPoolProvider pools, RewardGrantService grants)
|
||||
{
|
||||
_db = db;
|
||||
_pools = pools;
|
||||
_grants = grants;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<GachaPointRewardDto>> GetRewardsAsync(int packId, long viewerId)
|
||||
{
|
||||
var pack = await _db.Packs.FirstOrDefaultAsync(p => p.Id == packId);
|
||||
if (pack?.GachaPointConfig is null) return Array.Empty<GachaPointRewardDto>();
|
||||
|
||||
var pool = _pools.GetPool(pack);
|
||||
|
||||
var receivedCardIds = (await _db.Viewers
|
||||
.Where(v => v.Id == viewerId)
|
||||
.SelectMany(v => v.GachaPointReceived)
|
||||
.Where(r => r.PackId == packId)
|
||||
.Select(r => r.CardId)
|
||||
.ToListAsync()).ToHashSet();
|
||||
|
||||
var legendaryCardIds = pool
|
||||
.Where(c => c.Rarity == Rarity.Legendary && !c.IsFoil)
|
||||
.Select(c => c.Id)
|
||||
.ToHashSet();
|
||||
|
||||
// Pull both cosmetic types in one trip. Group by card_id for O(1) lookup below.
|
||||
var cosmeticsByCard = await _db.CardCosmeticRewards
|
||||
.Where(r => legendaryCardIds.Contains(r.CardId)
|
||||
&& (r.Type == CosmeticType.Emblem || r.Type == CosmeticType.Skin))
|
||||
.ToListAsync();
|
||||
var cosmeticLookup = cosmeticsByCard
|
||||
.GroupBy(r => r.CardId)
|
||||
.ToDictionary(g => g.Key, g => g.ToList());
|
||||
|
||||
var standard = new List<GachaPointRewardDto>();
|
||||
var leader = new List<GachaPointRewardDto>();
|
||||
|
||||
foreach (var card in pool
|
||||
.Where(c => c.Rarity == Rarity.Legendary && !c.IsFoil)
|
||||
.OrderBy(c => c.Class?.Id ?? 0).ThenBy(c => c.Id))
|
||||
{
|
||||
if (!cosmeticLookup.TryGetValue(card.Id, out var cosmetics)) continue;
|
||||
var emblem = cosmetics.FirstOrDefault(c => c.Type == CosmeticType.Emblem);
|
||||
var skin = cosmetics.FirstOrDefault(c => c.Type == CosmeticType.Skin);
|
||||
if (emblem is null) continue; // every gacha-point entry has an emblem
|
||||
|
||||
var classId = (card.Class?.Id ?? 0).ToString();
|
||||
var isReceived = receivedCardIds.Contains(card.Id);
|
||||
|
||||
if (skin is null)
|
||||
{
|
||||
standard.Add(new GachaPointRewardDto
|
||||
{
|
||||
ClassId = classId, CardId = card.Id, IsReceived = isReceived,
|
||||
RewardList =
|
||||
{
|
||||
new GachaPointRewardDetailEntry
|
||||
{
|
||||
RewardType = (int)UserGoodsType.Emblem,
|
||||
RewardDetailId = emblem.CosmeticId,
|
||||
RewardNumber = 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// Leader card — 3 entries in capture order: Sleeve/Card-cosmetic (type 6),
|
||||
// Skin (type 10), Emblem (type 7). The reward_type=6 entry's detail id is the
|
||||
// card_id itself, mirroring the prod capture exactly.
|
||||
leader.Add(new GachaPointRewardDto
|
||||
{
|
||||
ClassId = classId, CardId = card.Id, IsReceived = isReceived,
|
||||
RewardList =
|
||||
{
|
||||
new GachaPointRewardDetailEntry
|
||||
{
|
||||
RewardType = (int)UserGoodsType.Sleeve, RewardDetailId = card.Id, RewardNumber = 1,
|
||||
},
|
||||
new GachaPointRewardDetailEntry
|
||||
{
|
||||
RewardType = (int)UserGoodsType.Skin,
|
||||
RewardDetailId = skin.CosmeticId, RewardNumber = 1,
|
||||
},
|
||||
new GachaPointRewardDetailEntry
|
||||
{
|
||||
RewardType = (int)UserGoodsType.Emblem,
|
||||
RewardDetailId = emblem.CosmeticId, RewardNumber = 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Standard first, then leader — matches the prod capture order for pack 10008.
|
||||
standard.AddRange(leader);
|
||||
return standard;
|
||||
}
|
||||
|
||||
public void Accrue(Viewer viewer, PackConfigEntry pack, PackChildGachaEntry child, int packNumber)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public Task<ExchangeOutcome> TryExchangeAsync(Viewer viewer, int packId, long cardId)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
37
SVSim.EmulatedEntrypoint/Services/IGachaPointService.cs
Normal file
37
SVSim.EmulatedEntrypoint/Services/IGachaPointService.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using SVSim.Database.Models;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Services;
|
||||
|
||||
public interface IGachaPointService
|
||||
{
|
||||
/// <summary>
|
||||
/// Build the gacha-point exchange catalog for one pack, with per-viewer is_received
|
||||
/// resolved. Returns an empty list if the pack has no gacha-point config or no eligible
|
||||
/// cards in its pool — callers should treat the empty result as a valid response, not
|
||||
/// an error. Order: standard legendaries first (class_id ASC, card_id ASC), then leader
|
||||
/// cards (class_id ASC, card_id ASC).
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<GachaPointRewardDto>> GetRewardsAsync(int packId, long viewerId);
|
||||
|
||||
/// <summary>
|
||||
/// Increment the viewer's balance for <paramref name="pack"/> by
|
||||
/// <c>child.OverrideIncreaseGachaPoint ?? pack.GachaPointConfig.IncreaseGachaPoint</c>
|
||||
/// times <paramref name="packNumber"/>. No-op when the pack lacks a GachaPointConfig.
|
||||
/// Caller is responsible for SaveChangesAsync.
|
||||
/// </summary>
|
||||
void Accrue(Viewer viewer, PackConfigEntry pack, PackChildGachaEntry child, int packNumber);
|
||||
|
||||
/// <summary>
|
||||
/// Validate + execute an exchange. Returns the grant outcome on success (reward_list
|
||||
/// entries the controller will return in <see cref="Dtos.Responses.Pack.ExchangeGachaPointResponse"/>),
|
||||
/// or a failure result describing why. Mutates the in-memory graph; caller saves.
|
||||
/// </summary>
|
||||
Task<ExchangeOutcome> TryExchangeAsync(Viewer viewer, int packId, long cardId);
|
||||
}
|
||||
|
||||
public sealed record ExchangeOutcome(bool Success, string? Error, IReadOnlyList<RewardListEntry> RewardList)
|
||||
{
|
||||
public static ExchangeOutcome Fail(string error) => new(false, error, Array.Empty<RewardListEntry>());
|
||||
public static ExchangeOutcome Ok(IReadOnlyList<RewardListEntry> rewards) => new(true, null, rewards);
|
||||
}
|
||||
Reference in New Issue
Block a user