All files / web/src/lib/audio buildFeedbackClipIds.ts

100% Statements 32/32
100% Branches 7/7
100% Functions 1/1
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 9x 9x 9x 4x 4x 5x 5x 5x 3x 3x  
import { CELEBRATION_CLIPS, STREAK_CLIPS } from './clips/feedback'
import { numberToClipIds } from './numberToClipIds'
 
export interface FeedbackContext {
  streak?: number
}
 
/**
 * Generate feedback clip IDs for a practice answer.
 *
 * Parallel to `buildFeedbackText` but returns clip ID arrays.
 *
 * Correct answers get a random congratulatory clip, or a streak milestone
 * clip when the streak hits an exact milestone (3, 5, 7, 10).
 * Incorrect answers get "the answer is" followed by the number clips.
 */
export function buildFeedbackClipIds(
  isCorrect: boolean,
  correctAnswer: number,
  context?: FeedbackContext
): string[] {
  if (isCorrect) {
    // At exact streak milestones, play the milestone clip instead
    const streak = context?.streak
    if (streak !== undefined && STREAK_CLIPS[streak]) {
      return [STREAK_CLIPS[streak]]
    }
    const idx = Math.floor(Math.random() * CELEBRATION_CLIPS.length)
    return [CELEBRATION_CLIPS[idx]]
  }
  return ['feedback-the-answer-is', ...numberToClipIds(Math.abs(correctAnswer))]
}