Files
gamer147 1c386b5ed0 feat(packs): rewrite PackOpenService against per-pack draw table
Sampler is now driven by PackDrawTable: roll DrawTier per slot by
cumulative slot-rate weights, then pick a card within tier by per-card
weights renormalized within the tier. Rate-less Guaranteed-Leader-Card
rows draw uniform over (pool minus owned), falling back to the full
pool when all are owned. Bonus slot fires once at the end of a 10-pack
open when HasBonusSlot is set.

Slot 8 falls back to the general slot's per-card weights for the rolled
tier when slot-8 has only a rarity-level rate quoted (the common shape
on normal packs).

PackController.Open loads the draw table + viewer owned card ids and
passes them to the sampler; the category-based forced-Legendary slot-8
override is gone. ICardFoilLookup replaces ICardPoolProvider for the
foil-twin heuristic.

Drops the test-fixture pack-draw seed overlay so the production seed
flows through the importer tests; controller tests that fabricate their
own card sets now call factory.SeedPackDrawTableAsync(...) to install a
matching stub draw table.

WeightedPick helper handles the cumulative-band roll for both stages.
Five sampler tests + four WeightedPick tests + five importer/repo
tests; full suite is 653/653 green.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-30 22:26:45 -04:00

31 lines
1.0 KiB
C#

using SVSim.Database.Services;
namespace SVSim.EmulatedEntrypoint.Services;
/// <summary>
/// Generic cumulative-band weighted picker used by PackOpenService for tier-by-slot
/// and card-within-tier sampling. Renormalizes weights internally (sums &lt;1 absorb
/// into the last band; sums &gt;1 scale down).
/// </summary>
public static class WeightedPick
{
public static T Pick<T>(IRandom rng, IReadOnlyList<T> items, IReadOnlyList<double> weights)
{
if (items.Count == 0) throw new ArgumentException("WeightedPick: items is empty.");
if (items.Count != weights.Count) throw new ArgumentException("WeightedPick: items / weights length mismatch.");
double sum = 0;
for (int i = 0; i < weights.Count; i++) sum += weights[i];
if (sum <= 0) return items[rng.Next(items.Count)];
double r = rng.NextDouble() * sum;
double cum = 0;
for (int i = 0; i < items.Count - 1; i++)
{
cum += weights[i];
if (r < cum) return items[i];
}
return items[^1];
}
}