playEventsOf function

List<ChanceAnalyticsEvent> playEventsOf(
  1. Map<String, dynamic>? request,
  2. Map<String, dynamic>? body, {
  3. required String campaignId,
  4. required int playNumber,
  5. String? entry,
})

The chance events a proxied play answer's facts call for: Digia Game Played iff the answer is isNew (the play it made, never a replay of an earlier answer), plus Digia Chance Granted (source extraPlay) when it carries an extraChance. request is the skin's /play body (its chanceId); body is {isSuccess, data: {response}}. playNumber is this play's 1-based number in the window; entry is where the window opened. The caller gates live tests. Never throws.

Implementation

List<ChanceAnalyticsEvent> playEventsOf(
  Map<String, dynamic>? request,
  Map<String, dynamic>? body, {
  required String campaignId,
  required int playNumber,
  String? entry,
}) {
  try {
    final answer = playResponseOf(body);
    if (answer == null || answer['isNew'] != true) return const [];
    final grant = answer['grant'];
    final value = grant is Map ? grant['value'] : null;
    final amountMinor = value is Map ? value['amountMinor'] : null;
    final chanceId = _textOf(request?['chanceId']);
    final out = <ChanceAnalyticsEvent>[
      _event(
        _playedName,
        chanceId == null ? null : 'played:$chanceId',
        campaignId,
        _withoutNulls({
          'chance_id': chanceId,
          'outcome_key': _textOf(answer['outcomeKey']),
          'outcome_kind': _textOf(answer['kind']),
          'reward_id': _textOf(answer['rewardId']),
          'grant_id': grant is Map ? _textOf(grant['grantId']) : null,
          'value_minor': amountMinor is num ? amountMinor : null,
          'currency': value is Map ? _textOf(value['currency']) : null,
          'fallback': answer['fallback'] == true,
          'play_number': playNumber,
          'is_replay': playNumber > 1,
          'entry': entry,
        }),
      ),
    ];
    final extra = answer['extraChance'];
    final extraId = extra is Map ? _textOf(extra['chanceId']) : null;
    if (extraId != null) {
      out.add(_event(
        _grantedName,
        'granted:$extraId',
        campaignId,
        _withoutNulls({
          'chance_id': extraId,
          'source': 'extraPlay',
          'expires_at': extra is Map ? _textOf(extra['expiresAt']) : null,
        }),
      ));
    }
    return out;
  } catch (_) {
    return const [];
  }
}