using System; using System.Threading; using System.Threading.Tasks; using RudderSdk.Core.Models.BattlePass; namespace RudderSdk.Core; /// /// Session of a scenario battle-pass node. Exposes the battle pass operations /// bound to this node's scenario/node/run ids, plus explicit boundary crossings /// the game drives from its UI; the server validates each crossing. /// public sealed class BattlePassSession { private readonly BattlePassService _battlePass; internal BattlePassSession(ScenarioNodeContext context, BattlePassService battlePass) { Context = context; _battlePass = battlePass; } /// Underlying node context. public ScenarioNodeContext Context { get; } /// Node id. public string Id => Context.NodeId; /// Reads a typed value from the node data. public T Get(string key, T defaultValue = default!) => Context.Get(key, defaultValue); /// Reads current progress (xp, level, premium ownership, claimed tiers) for this node. public Task GetProgressAsync(CancellationToken cancellationToken = default) => _battlePass.GetProgressAsync(Context.ScenarioId, Context.NodeId, cancellationToken); /// Credits xp from a configured source. public Task AddXpAsync(string source, long amount, CancellationToken cancellationToken = default) => _battlePass.AddXpAsync(new AddBattlePassXpRequest { ScenarioId = Context.ScenarioId, NodeId = Context.NodeId, RunId = Context.RunId, Source = source, Amount = amount }, cancellationToken); /// /// Claims a tier reward at a reached level. /// is or . /// public Task ClaimRewardAsync(int level, string track, CancellationToken cancellationToken = default) => _battlePass.ClaimRewardAsync(new ClaimBattlePassRewardRequest { ScenarioId = Context.ScenarioId, NodeId = Context.NodeId, RunId = Context.RunId, Level = level, Track = track }, cancellationToken); /// Purchases the premium track, then crosses onPremiumPurchase on success. public async Task PurchasePremiumAsync(CancellationToken cancellationToken = default) { var response = await _battlePass.PurchasePremiumAsync(new PurchaseBattlePassPremiumRequest { ScenarioId = Context.ScenarioId, NodeId = Context.NodeId, RunId = Context.RunId, IdempotencyKey = Guid.NewGuid().ToString() }, cancellationToken).ConfigureAwait(false); if (response != null && response.Success == true) await Context.CompleteAsync("onPremiumPurchase", cancellationToken).ConfigureAwait(false); return response!; } /// Advances the run through the onLevelUp handle. public Task LevelUpAsync(CancellationToken cancellationToken = default) => Context.CompleteAsync("onLevelUp", cancellationToken); /// Advances the run through the onLevelUp handle (fire-and-forget). public void LevelUp() => Context.Complete("onLevelUp"); /// Advances the run through the onMaxLevel handle. public Task MaxLevelAsync(CancellationToken cancellationToken = default) => Context.CompleteAsync("onMaxLevel", cancellationToken); /// Advances the run through the onMaxLevel handle (fire-and-forget). public void MaxLevel() => Context.Complete("onMaxLevel"); /// Advances the run through the onComplete handle. public Task CompleteAsync(CancellationToken cancellationToken = default) => Context.CompleteAsync("onComplete", cancellationToken); /// Advances the run through the onComplete handle (fire-and-forget). public void Complete() => Context.Complete("onComplete"); }