2026-09-04 14:03:41 +03:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using RudderSdk.Core.Models.BattlePass;
|
|
|
|
|
|
|
|
|
|
namespace RudderSdk.Core;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A scenario battle-pass node. Battle-pass operations are bound to this
|
|
|
|
|
/// node's scenario/node/run ids; <see cref="LevelUpAsync"/>, <see cref="EndAsync"/>
|
|
|
|
|
/// and a successful <see cref="PurchasePremiumAsync"/> post callbacks.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class BattlePassEffect
|
|
|
|
|
{
|
|
|
|
|
private readonly EffectHandle _handle;
|
|
|
|
|
private readonly BattlePassService _battlePass;
|
|
|
|
|
|
|
|
|
|
internal BattlePassEffect(EffectHandle handle, BattlePassService battlePass)
|
|
|
|
|
{
|
|
|
|
|
_handle = handle;
|
|
|
|
|
_battlePass = battlePass;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Run id.</summary>
|
|
|
|
|
public string RunId => _handle.RunId;
|
|
|
|
|
|
|
|
|
|
/// <summary>Scenario id.</summary>
|
2026-09-06 22:25:32 +03:00
|
|
|
public string ScenarioSlug => _handle.ScenarioSlug;
|
2026-09-04 14:03:41 +03:00
|
|
|
|
|
|
|
|
/// <summary>Node id.</summary>
|
|
|
|
|
public string NodeId => _handle.NodeId;
|
|
|
|
|
|
|
|
|
|
/// <summary>Node data payload.</summary>
|
|
|
|
|
public JObject Data => _handle.Data;
|
|
|
|
|
|
|
|
|
|
/// <summary>Reads a typed value from the node data.</summary>
|
|
|
|
|
public T Get<T>(string key, T defaultValue = default!) => _handle.Get(key, defaultValue);
|
|
|
|
|
|
|
|
|
|
/// <summary>Reads current progress (xp, level, premium ownership, claimed tiers) for this node.</summary>
|
|
|
|
|
public Task<GetBattlePassProgressResponse> GetProgressAsync(CancellationToken cancellationToken = default)
|
2026-09-06 22:25:32 +03:00
|
|
|
=> _battlePass.GetProgressAsync(_handle.ScenarioSlug, _handle.NodeId, cancellationToken);
|
2026-09-04 14:03:41 +03:00
|
|
|
|
|
|
|
|
/// <summary>Credits xp from a configured source.</summary>
|
|
|
|
|
public Task<AddBattlePassXpResponse> AddXpAsync(string source, long amount, CancellationToken cancellationToken = default)
|
|
|
|
|
=> _battlePass.AddXpAsync(new AddBattlePassXpRequest
|
|
|
|
|
{
|
2026-09-06 22:25:32 +03:00
|
|
|
ScenarioSlug = _handle.ScenarioSlug,
|
2026-09-04 14:03:41 +03:00
|
|
|
NodeId = _handle.NodeId,
|
|
|
|
|
RunId = _handle.RunId,
|
|
|
|
|
Source = source,
|
|
|
|
|
Amount = amount
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Claims a tier reward at a reached level.
|
|
|
|
|
/// <paramref name="track"/> is <see cref="BattlePassService.TrackFree"/> or <see cref="BattlePassService.TrackPremium"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Task<ClaimBattlePassRewardResponse> ClaimRewardAsync(int level, string track, CancellationToken cancellationToken = default)
|
|
|
|
|
=> _battlePass.ClaimRewardAsync(new ClaimBattlePassRewardRequest
|
|
|
|
|
{
|
2026-09-06 22:25:32 +03:00
|
|
|
ScenarioSlug = _handle.ScenarioSlug,
|
2026-09-04 14:03:41 +03:00
|
|
|
NodeId = _handle.NodeId,
|
|
|
|
|
RunId = _handle.RunId,
|
|
|
|
|
Level = level,
|
|
|
|
|
Track = track
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <summary>Purchases the premium track, then posts <c>onPremiumPurchase</c> on success.</summary>
|
|
|
|
|
public async Task<PurchaseBattlePassPremiumResponse> PurchasePremiumAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var response = await _battlePass.PurchasePremiumAsync(new PurchaseBattlePassPremiumRequest
|
|
|
|
|
{
|
2026-09-06 22:25:32 +03:00
|
|
|
ScenarioSlug = _handle.ScenarioSlug,
|
2026-09-04 14:03:41 +03:00
|
|
|
NodeId = _handle.NodeId,
|
|
|
|
|
RunId = _handle.RunId,
|
|
|
|
|
IdempotencyKey = Guid.NewGuid().ToString()
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (response != null && response.Success == true)
|
|
|
|
|
await _handle.CompleteAsync("onPremiumPurchase", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return response!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Posts the <c>onLevelUp</c> callback.</summary>
|
|
|
|
|
public Task LevelUpAsync(CancellationToken cancellationToken = default) => _handle.CompleteAsync("onLevelUp", cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <summary>Posts the <c>onLevelUp</c> callback (fire-and-forget).</summary>
|
|
|
|
|
public void LevelUp() => _handle.Complete("onLevelUp");
|
|
|
|
|
|
|
|
|
|
/// <summary>Posts the <c>onComplete</c> callback.</summary>
|
|
|
|
|
public Task EndAsync(CancellationToken cancellationToken = default) => _handle.CompleteAsync("onComplete", cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <summary>Posts the <c>onComplete</c> callback (fire-and-forget).</summary>
|
|
|
|
|
public void End() => _handle.Complete("onComplete");
|
|
|
|
|
}
|