155 lines
6.2 KiB
C#
155 lines
6.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SVSim.Database;
|
|
using SVSim.Database.Enums;
|
|
using SVSim.Database.Models;
|
|
using SVSim.EmulatedEntrypoint.Models.Dtos;
|
|
|
|
namespace SVSim.EmulatedEntrypoint.Services;
|
|
|
|
public sealed class GachaPointService : IGachaPointService
|
|
{
|
|
private readonly SVSimDbContext _db;
|
|
private readonly ICardPoolProvider _pools;
|
|
|
|
public GachaPointService(SVSimDbContext db, ICardPoolProvider pools)
|
|
{
|
|
_db = db;
|
|
_pools = pools;
|
|
}
|
|
|
|
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);
|
|
|
|
// EF Core 8 has no ToHashSetAsync on IQueryable — materialize via ToListAsync then hash.
|
|
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)
|
|
// Neutral cards have Class=null; client wire-encodes them as class_id="0".
|
|
.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 (IsLeaderCard(skin))
|
|
{
|
|
// 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 (no Sleeve cosmetic row
|
|
// is required — synthesizing from card.Id is robust to missing rows).
|
|
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,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
else
|
|
{
|
|
standard.Add(new GachaPointRewardDto
|
|
{
|
|
ClassId = classId, CardId = card.Id, IsReceived = isReceived,
|
|
RewardList =
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Leader cards are identified purely by the data shape: a (non-foil legendary) card with
|
|
/// a <see cref="CosmeticType.Skin"/> cosmetic-reward row is a leader card. There is no
|
|
/// is_leader flag, no card-id pattern, no other signal — the presence of the Skin row is
|
|
/// the entire heuristic. Callers must have already filtered to Rarity.Legendary &&
|
|
/// !IsFoil before invoking this.
|
|
/// </summary>
|
|
private static bool IsLeaderCard(CardCosmeticReward? skin) => skin is not null;
|
|
|
|
public void Accrue(Viewer viewer, PackConfigEntry pack, PackChildGachaEntry child, int packNumber)
|
|
{
|
|
if (pack.GachaPointConfig is null) return;
|
|
if (packNumber <= 0) return;
|
|
|
|
// Per-child override wins when set (>0); fall back to the pack's default.
|
|
int perPack = child.OverrideIncreaseGachaPoint > 0
|
|
? child.OverrideIncreaseGachaPoint
|
|
: pack.GachaPointConfig.IncreaseGachaPoint;
|
|
if (perPack <= 0) return;
|
|
|
|
int delta = perPack * packNumber;
|
|
|
|
var existing = viewer.GachaPointBalances.FirstOrDefault(b => b.PackId == pack.Id);
|
|
if (existing is null)
|
|
{
|
|
viewer.GachaPointBalances.Add(new ViewerGachaPointBalance
|
|
{
|
|
PackId = pack.Id, Points = delta,
|
|
});
|
|
}
|
|
else
|
|
{
|
|
existing.Points += delta;
|
|
}
|
|
}
|
|
|
|
public Task<ExchangeOutcome> TryExchangeAsync(Viewer viewer, int packId, long cardId)
|
|
=> throw new NotImplementedException();
|
|
}
|