getWorstAvg method

Future<String> getWorstAvg(
  1. List<TimeTraining> timesList,
  2. int numAvg
)

Método para calcular la peor media de X tiempos en una sesión.

Este método calcula la peor media (más alta) DE UN bloques de numAvg tiempos, ordenados por fecha de más reciente a más antigua. Para cada bloque, se eliminan el mejor y el peor tiempo, y se calcula la media con los tiempos restantes (regla WCA).

Parámetros:

  • timesList: Lista de tiempos TimeTraining registrados en la sesión.
  • numAvg: Número de tiempos consecutivos a considerar por bloque.

Retorna:

  • String: La peor media en formato "mm:ss.ss".
    • "--:--.--" si no hay suficientes tiempos para calcularla.

Implementation

Future<String> getWorstAvg(List<TimeTraining> timesList, int numAvg) async {
  if (timesList.length < numAvg) {
    // SI NO HAY SUFICIENTES TIEMPOS PARA HACER LA MEDIA, DEVUELVE EL VALOR POR
    // DEFECTO
    return "--:--.--";
  }

  // MEJOR MEDIA (VALOR MAS ALTO)
  double worstAvgTimeInSeconds = -double.infinity;

  // ORDENAMOS LA LISTA POR FECHA MAS RECIENTE
  timesList.sort((a, b) {
    DateTime dateA = DateTime.parse(a.registrationDate);
    DateTime dateB = DateTime.parse(b.registrationDate);
    // ORDENAMOS DE MAS RECIENTE A MAS ANTIGUO
    return dateB.compareTo(dateA);
  });

  // RECORREMOS LA LISTA TOMBANDO BLOQUES DE numAvg TIEMPOS
  for (int i = 0; i <= timesList.length - numAvg; i++) {
    // TOMAMOS UN BLOQUE DE numAvg TIEMPOS
    List<TimeTraining> block = timesList.sublist(i, i + numAvg);

    // ORDENAMOS EL BLOQUE POR TIEMPO (DE MENOR A MAYOR)
    block.sort((a, b) => a.timeInSeconds.compareTo(b.timeInSeconds));

    // ELIMINAMOS EL MEJOR Y EL PEOR TIEMPO
    List<TimeTraining> trimmedBlock = block.sublist(1, block.length - 1);

    // CALCULAMOS LA MEDIA DEL BLOQUE QUEDANDO CON LOS TIEMPOS RESTANTES
    double sum = 0.0;
    for (var time in trimmedBlock) {
      sum += time.timeInSeconds;
    }
    double avg = sum / trimmedBlock.length;

    // ACTUALIZAMOS SI ESTA MEDIA ES LA PEOR (LA MAS ALTA)
    if (avg > worstAvgTimeInSeconds) {
      worstAvgTimeInSeconds = avg;
    }
  }

  // CONVERTIMOS EL TIEMPO EN SEGUNDOS A FORMATO "mm:ss.ss"
  String formatTime(double timeInSeconds) {
    int minutes = timeInSeconds ~/ 60;
    double seconds = timeInSeconds % 60;
    // AÑADE UN CARACTER '0' A LA IZQUIERDA SI TIENE MENOS DE 5 CARACTERES
    // (si es 9.45 -> 09.45, pero si es 11.45 se queda igual)
    return "${minutes}:${seconds.toStringAsFixed(2).padLeft(5, '0')}";
  }
  return formatTime(worstAvgTimeInSeconds);
}