sightEndlessRound function

SightStep sightEndlessRound(
  1. int lap,
  2. Random rng, {
  3. SightStep? figure,
})

O passo de uma rodada do sem fim na volta lap: a figura, o andamento, o tamanho, a mistura e a margem de erro daquela volta.

A figura vem de sightEndlessDraw entre as dos níveis 21 a 100 (50% da fase 5, 25% da 4, 15% da 3, 10% da 2). Sua etapa (sightEndlessStage) depende de quantas voltas se passaram desde que o andamento dela chegou ao da peça; as outras figuras da mistura são só as que já chegaram. O passo devolvido vai para generateSight, que monta o exercício; a mesma semente refaz a mesma rodada. Com figure (o passo d de um ingrediente), a primeira figura não é sorteada: serve aos testes.

Implementation

SightStep sightEndlessRound(int lap, Random rng, {SightStep? figure}) {
  if (lap < 1) throw RangeError.range(lap, 1, null, 'lap');
  final pool = sightEndlessFigures();
  if (pool.isEmpty) {
    throw StateError('o sem fim precisa das figuras dos níveis 21 em diante');
  }
  final first = figure ?? sightEndlessDraw(pool, rng);
  final stage = sightEndlessStage(lap - sightEndlessCapLap(first));
  final figures = [first];
  final ready = [
    for (final d in pool)
      if (d != first && sightEndlessCapLap(d) <= lap) d,
  ];
  while (figures.length < stage.figures) {
    final left = [
      for (final d in ready)
        if (!figures.contains(d)) d,
    ];
    if (left.isEmpty) break;
    figures.add(sightEndlessDraw(left, rng));
  }

  // O andamento de uma mistura é o da figura mais lenta: as outras só entram
  // quando já estão no andamento da peça.
  final tempo = figures.map((d) => sightEndlessBpm(d, lap)).reduce(min);
  SightStep section(SightStep d) => d.withRecipe(
    d.recipe!.copyWith(
      errorsPerNote: stage.errorsPerNote,
      maxErrorsPerMeasure: stage.maxErrorsPerMeasure,
      endlessLap: lap,
    ),
    bpm: tempo,
  );

  if (stage.layout.isEmpty) return section(first);
  final sections = [
    for (final (i, bars) in stage.layout.indexed)
      SightEndlessSection(section(figures[i < figures.length ? i : 0]), bars),
  ];
  final head = sections.first.step;
  final hands = [for (final s in sections) s.step.hand];
  return head.withRecipe(
    head.recipe!.copyWith(
      hand: SightHandLimits(
        maxSpan: hands.map((h) => h.maxSpan).reduce(max),
        maxTones: hands.map((h) => h.maxTones).reduce(max),
        fastLeap: hands.map((h) => h.fastLeap).reduce(max),
      ),
      unlocked: {for (final s in sections) ...s.step.unlocked},
    ),
    bpm: tempo,
    sections: sections,
  );
}