feat(bp): AddPointsAsync plumbing + level-cross auto-grant + weekly cap
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -208,6 +208,89 @@ public sealed class BattlePassService : IBattlePassService
|
||||
return new BattlePassBuyOutcome(1, achieved, postState);
|
||||
}
|
||||
|
||||
public async Task<BattlePassPointGrant> AddPointsAsync(
|
||||
long viewerId, BattlePassPointSource source, int amount, CancellationToken ct)
|
||||
{
|
||||
var now = _time.GetUtcNow();
|
||||
var season = await _bp.GetActiveSeasonAsync(now, ct);
|
||||
if (season is null)
|
||||
{
|
||||
return new BattlePassPointGrant(0, 0, 0, 0, 0, source,
|
||||
Array.Empty<SVSim.Database.Services.GrantedReward>());
|
||||
}
|
||||
|
||||
var viewer = await _db.Viewers
|
||||
.Include(v => v.Cards).ThenInclude(c => c.Card)
|
||||
.Include(v => v.Sleeves).Include(v => v.Emblems).Include(v => v.LeaderSkins)
|
||||
.Include(v => v.Degrees).Include(v => v.MyPageBackgrounds).Include(v => v.Items).ThenInclude(i => i.Item)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(v => v.Id == viewerId, ct)
|
||||
?? throw new InvalidOperationException($"viewer {viewerId} not found");
|
||||
|
||||
var progress = await _viewerBp.GetOrCreateProgressAsync(viewerId, season.Id, ct);
|
||||
|
||||
int beforePoint = progress.CurrentPoint;
|
||||
var curve = await _bp.GetLevelCurveAsync(ct);
|
||||
int beforeLevel = ComputeLevel(curve, beforePoint);
|
||||
|
||||
RolloverWeeklyIfNeeded(progress, now);
|
||||
int headroom = Math.Max(0, WeeklyLimitPointDefault - progress.WeeklyPoints);
|
||||
int capped = Math.Max(0, Math.Min(amount, headroom));
|
||||
|
||||
progress.CurrentPoint += capped;
|
||||
progress.WeeklyPoints += capped;
|
||||
|
||||
int afterLevel = ComputeLevel(curve, progress.CurrentPoint);
|
||||
|
||||
var newlyClaimed = new List<SVSim.Database.Services.GrantedReward>();
|
||||
if (afterLevel > beforeLevel)
|
||||
{
|
||||
var rewards = await _bp.GetSeasonRewardsAsync(season.Id, ct);
|
||||
var claims = await _viewerBp.GetClaimsAsync(viewerId, season.Id, ct);
|
||||
var claimSet = claims.Select(c => (c.Track, c.Level)).ToHashSet();
|
||||
|
||||
for (int level = beforeLevel + 1; level <= afterLevel; level++)
|
||||
{
|
||||
foreach (var r in rewards.Where(r => r.Level == level))
|
||||
{
|
||||
if (r.Track == BattlePassTrack.Premium && !progress.IsPremium) continue;
|
||||
if (claimSet.Contains((r.Track, r.Level))) continue;
|
||||
_viewerBp.AddClaim(viewerId, season.Id, r.Track, r.Level, now);
|
||||
var granted = await _rewards.ApplyAsync(
|
||||
viewer, (UserGoodsType)r.RewardType, r.RewardDetailId, r.RewardNumber, ct);
|
||||
newlyClaimed.AddRange(granted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync(ct);
|
||||
|
||||
return new BattlePassPointGrant(
|
||||
BeforePoint: beforePoint,
|
||||
BeforeLevel: beforeLevel,
|
||||
AfterPoint: progress.CurrentPoint,
|
||||
AfterLevel: afterLevel,
|
||||
PointAdd: capped,
|
||||
Source: source,
|
||||
NewlyClaimed: newlyClaimed);
|
||||
}
|
||||
|
||||
private static void RolloverWeeklyIfNeeded(ViewerBattlePassProgressEntry progress, DateTimeOffset now)
|
||||
{
|
||||
// Open question (see spec "Open assumptions"): true Cygames boundary likely ties to a fixed
|
||||
// weekday/timezone. v1 uses a per-viewer 7-day sliding window from first grant.
|
||||
if (progress.WeeklyPeriodStart is null)
|
||||
{
|
||||
progress.WeeklyPeriodStart = now;
|
||||
return;
|
||||
}
|
||||
if (now - progress.WeeklyPeriodStart.Value >= TimeSpan.FromDays(7))
|
||||
{
|
||||
progress.WeeklyPeriodStart = now;
|
||||
progress.WeeklyPoints = 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal static int ComputeLevel(IReadOnlyList<BattlePassLevelEntry> curve, int point)
|
||||
{
|
||||
if (curve.Count == 0) return 1;
|
||||
|
||||
@@ -5,6 +5,16 @@ namespace SVSim.EmulatedEntrypoint.Services;
|
||||
|
||||
public interface IBattlePassService
|
||||
{
|
||||
/// <summary>
|
||||
/// Plumbing for future point-source endpoints (mission/retire, battle finish handlers).
|
||||
/// Bumps gauge by min(amount, weekly headroom), auto-grants rewards on every level crossed
|
||||
/// (premium track only when IsPremium), writes claim rows + currency/collection mutations
|
||||
/// via RewardGrantService. Returns delta info for caller to embed in
|
||||
/// battle_pass_gauge_info on its response. No live caller in v1; tested directly.
|
||||
/// </summary>
|
||||
Task<BattlePassPointGrant> AddPointsAsync(
|
||||
long viewerId, BattlePassPointSource source, int amount, CancellationToken ct);
|
||||
|
||||
/// <summary>Global level curve as wire-string dictionary; null if no levels seeded.</summary>
|
||||
Task<IReadOnlyDictionary<string, BattlePassLevel>?> GetLevelCurveAsync(CancellationToken ct);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user