recordKeyRound method

Future<void> recordKeyRound(
  1. String courseId,
  2. String exerciseId,
  3. RoundResult result,
  4. DateTime at,
)

Grava uma rodada transposta (EX05): o tom (RoundResult.key, "Sol maior"), a porcentagem e a hora at. O mapa do exercício guarda, por tom, a melhor porcentagem e a última tentativa (Pike 25). Nada lê isso ainda; é a base do treino do dia. Rodada sem tom não grava nada; se não der para gravar, a rodada segue.

Implementation

Future<void> recordKeyRound(
  String courseId,
  String exerciseId,
  RoundResult result,
  DateTime at,
) async {
  final key = result.key;
  if (key == null) return;
  final id = keyProgressKeyFor(courseId, exerciseId);
  final stored = await _readKeyProgress(id);
  if (stored == null) return; // ilegível: não sobrescreve o que está lá.
  final previous = stored[key];
  final percent = result.percent;
  final best = previous == null || percent > previous.bestPercent
      ? percent
      : previous.bestPercent;
  try {
    await _writeKeyProgress(id, {
      ...stored,
      key: KeyRecord(bestPercent: best, lastAt: at.millisecondsSinceEpoch),
    });
  } on Object catch (e) {
    DiagLog.log('erro', 'curso: não consegui gravar o tom de "$id": $e');
  }
}