All files / shared speedrun.ts

100% Statements 46/46
100% Branches 48/48
100% Functions 20/20
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64      3x 8x   3x 6x   19x   33x   75x   3x 36x 6x 4x 36x 36x         3x 69x 34x 53x 53x   34x     3x 36x 16x 16x     3x 34x   3x 11x 33x 11x 52x 36x 36x                            
import { counted, words } from './naming'
import type { LevelPractice, Speedrun, Split } from './types'
 
const levelSplits = (run: Speedrun) =>
  run.splits.filter((split) => run.levels.some((level) => level.name === split.level))
 
export const levelsBeaten = (run: Speedrun) =>
  `${run.won ? run.levels.length : levelSplits(run).length} of ${run.levels.length}`
 
export const searchedState = (run: Speedrun) => run.search?.state ?? run.searching
 
export const levelOf = (run: Speedrun, state: string) => run.levels.find((level) => level.states.includes(state))?.name
 
const firstState = (run: Speedrun, name: string) => run.levels.find((level) => level.name === name)?.states[0] ?? name
 
const progressText = (practice: LevelPractice | undefined, first: string) => {
  if (!practice) return null
  if (practice.beaten) return 'beaten'
  const reached = practice.reached === first ? [] : [words(practice.reached)]
  const details = Object.entries(practice.details).map(([name, value]) => `${name} ${value}`)
  return [...(practice.loaded ? ['practising'] : []), ...reached, counted(practice.pieces, 'piece'), ...details].join(
    ' · ',
  )
}
 
const goalDistance = (run: Speedrun, name: string) => {
  const states = run.levels.find((level) => level.name === name)?.states ?? []
  const known = states.flatMap((candidate) => {
    const distance = run.closest?.[candidate]
    return distance === undefined ? [] : [distance]
  })
  return known.length ? Math.min(...known) : null
}
 
const savedSeconds = (split: Split | undefined, frameRate: number) => {
  if (!split?.first_seconds) return null
  const frames = Math.round((split.first_seconds - split.level_seconds) * frameRate)
  return frames > 0 ? frames / frameRate : null
}
 
const withGoal = (progress: string | null, distance: number | null) =>
  distance === null ? progress : [progress, `${counted(distance, 'frame')} before the goal`].filter(Boolean).join(' · ')
 
export const levelRows = (run: Speedrun, state: string | null) => {
  const current = state === null ? null : (levelOf(run, state) ?? state)
  const names = [...(run.opening ? [run.opening] : []), ...run.levels.map(({ name }) => name)]
  return names.map((name) => {
    const split = run.splits.find((candidate) => candidate.level === name)
    const practice = run.practice?.[name]
    return {
      level: name === run.opening ? words(name) : name,
      time: split?.level_seconds ?? null,
      saved: savedSeconds(split, run.frame_rate),
      practice: practice?.beaten && practice.loaded ? practice.seconds : null,
      progress: practice?.beaten
        ? progressText(practice, firstState(run, name))
        : withGoal(progressText(practice, firstState(run, name)), goalDistance(run, name)),
      beaten: practice?.beaten ?? false,
      split: run.won ? (split?.seconds ?? null) : null,
      current: name === current,
    }
  })
}