96 lines
4.2 KiB
C#
96 lines
4.2 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using RudderSdk.Core.Models.BattlePass;
|
|
|
|
namespace RudderSdk.Core;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public sealed class BattlePassSession
|
|
{
|
|
private readonly BattlePassService _battlePass;
|
|
|
|
internal BattlePassSession(ScenarioNodeContext context, BattlePassService battlePass)
|
|
{
|
|
Context = context;
|
|
_battlePass = battlePass;
|
|
}
|
|
|
|
/// <summary>Underlying node context.</summary>
|
|
public ScenarioNodeContext Context { get; }
|
|
|
|
/// <summary>Node id.</summary>
|
|
public string Id => Context.NodeId;
|
|
|
|
/// <summary>Reads a typed value from the node data.</summary>
|
|
public T Get<T>(string key, T defaultValue = default!) => Context.Get(key, defaultValue);
|
|
|
|
/// <summary>Reads current progress (xp, level, premium ownership, claimed tiers) for this node.</summary>
|
|
public Task<GetBattlePassProgressResponse> GetProgressAsync(CancellationToken cancellationToken = default)
|
|
=> _battlePass.GetProgressAsync(Context.ScenarioId, Context.NodeId, cancellationToken);
|
|
|
|
/// <summary>Credits xp from a configured source.</summary>
|
|
public Task<AddBattlePassXpResponse> 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);
|
|
|
|
/// <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
|
|
{
|
|
ScenarioId = Context.ScenarioId,
|
|
NodeId = Context.NodeId,
|
|
RunId = Context.RunId,
|
|
Level = level,
|
|
Track = track
|
|
}, cancellationToken);
|
|
|
|
/// <summary>Purchases the premium track, then crosses onPremiumPurchase on success.</summary>
|
|
public async Task<PurchaseBattlePassPremiumResponse> 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)
|
|
await Context.CompleteAsync("onPremiumPurchase", cancellationToken).ConfigureAwait(false);
|
|
|
|
return response!;
|
|
}
|
|
|
|
/// <summary>Advances the run through the onLevelUp handle.</summary>
|
|
public Task LevelUpAsync(CancellationToken cancellationToken = default) => Context.CompleteAsync("onLevelUp", cancellationToken);
|
|
|
|
/// <summary>Advances the run through the onLevelUp handle (fire-and-forget).</summary>
|
|
public void LevelUp() => Context.Complete("onLevelUp");
|
|
|
|
/// <summary>Advances the run through the onMaxLevel handle.</summary>
|
|
public Task MaxLevelAsync(CancellationToken cancellationToken = default) => Context.CompleteAsync("onMaxLevel", cancellationToken);
|
|
|
|
/// <summary>Advances the run through the onMaxLevel handle (fire-and-forget).</summary>
|
|
public void MaxLevel() => Context.Complete("onMaxLevel");
|
|
|
|
/// <summary>Advances the run through the onComplete handle.</summary>
|
|
public Task CompleteAsync(CancellationToken cancellationToken = default) => Context.CompleteAsync("onComplete", cancellationToken);
|
|
|
|
/// <summary>Advances the run through the onComplete handle (fire-and-forget).</summary>
|
|
public void Complete() => Context.Complete("onComplete");
|
|
}
|