getPbTimeBySession method

Future<String> getPbTimeBySession(
  1. List<TimeTraining> timesList,
  2. bool isDnf
)

Método para obtener el mejor tiempo registrado en una sesión.

Este método recorre la lista de tiempos de la sesión y devuelve el mejor tiempo en formato "minutos:segundos.decimal". Se valida que el mejor tiempo no contenga una penalización DNF.

Parámetros:

  • timesList: Lista de tiempos TimeTraining para los cuales se calcula el mejor tiempo.
  • isDnf: Booleano para saber si el tiempo actual al que se le esta aplicando este método ha sido establecido como DNF.

Retorna:

  • String: El mejor tiempo en formato "minutos:segundos.decimal" que no contenga un DNF.
  • "0:00.00": Si no hay tiempos antes o si los anteriores tiempos tienen una penalizacion de DNF entonces retornará el tiempo en formato "0:00.00".

Implementation

Future<String> getPbTimeBySession(
    List<TimeTraining> timesList, bool isDnf) async {
  var pbTime = 0.0; // INICIALIZAR EL MEJOR TIEMPO
  var worstTime = 0.0; // INICIALIZAR EL PEOR TIEMPO
  int minutes = 0;
  // ATRIBUTO PARA SABER SSI TODOS LOS TIEMPOS SON DNF
  bool isDnfAll = false;
  // CONTADOR PARA SABER CUANDOS TIEMPOS SON DNF
  int cont = 0;

  for (var tim in timesList) {
    // SI EL TIEMPO TIENE UNA PENALIZACION DE 'DNF' SUBE EL CONTADOR
    if (tim.penalty == "DNF") cont++;
  } // SE RECORRE LA LSITA

  // CUENTA EL ULTIMO TIEMPO SI LE PONE UN DNF
  if (isDnf == true) cont++;

  // SI EL CONTADOR Y EL NUMERO TOTAL DE TIEMPOS ES EL MISMO SIGNIFICA QUE
  // TODOS LOS TIEMPOS TIENEN DNF
  if (cont == timesList.length) isDnfAll = true;

  // SI HA PUESTO DNF Y TODA LA LISTA TIENE DNF
  if (isDnfAll == true) return "0:00.00";

  if (isDnf) {
    // SI EL TIEMPO ACTUAL SE LE AÑADE UN DNF, ENTONCES SE RECORRE TODOS LOS TIEMPOS
    // MENOS EL ACTUAL, PARA SABER QUE TIEMPO ES EL PEOR Y SIN DNF
    for (int index = 0; index <= (timesList.length - 2); index++) {
      if (timesList[index].timeInSeconds > worstTime &&
          timesList[index].penalty != "DNF") {
        worstTime = timesList[index].timeInSeconds;
      } // SI EL TIEMPO ES MENOR AL PEOR TIEMPO SE ESTABLECE
    }

    pbTime = worstTime; // EL MEJOR TIEMPO ES EL PEOR (para darle un valor)

    for (int index = 0; index <= (timesList.length - 2); index++) {
      if (timesList[index].timeInSeconds < pbTime &&
          timesList[index].penalty != "DNF") {
        pbTime = timesList[index].timeInSeconds;
      } // SI EL TIEMPO ES MAYOR AL MEJOR TIEMPO SE ESTABLECE
    }

  } else {
    for (var times in timesList) {
      if (times.timeInSeconds > worstTime && times.penalty != "DNF") {
        worstTime = times.timeInSeconds;
      } // SI EL TIEMPO ES MENOR AL PEOR TIEMPO SE ESTABLECE
    } // SE RECORRE TODOS LOS TIEMPOS BUSCANDO EL PEOR TIEMPO DE LA SESION

    pbTime = worstTime; // EL MEJOR TIEMPO ES EL PEOR (para darle un valor)

    for (var times in timesList) {
      // SI EL TIEMPO NO TIENE DNF Y ES MENOR QUE EL MEJOR TIEMPO, SE ACTUALIZA EL PB
      if (times.timeInSeconds < pbTime &&
          times.penalty != "DNF") {
        pbTime = times.timeInSeconds;
      } // SI EL TIEMPO ES MAYOR AL MEJOR TIEMPO SE ESTABLECE
    } // SE RECORRE TODOS LOS TIEMPOS BUSCANDO EL MEJOR TIEMPO DE LA SESION
  }

  while (pbTime >= 60) {
    minutes++;
    pbTime -= 60; // RESTANIS 60 SEGUNDO CUANDO PASE UN MINUTO
  } // RECORREMOS EL TIEMPO MIENTRAS TENGA MINUTOS

  if (pbTime < 10) {
    return "$minutes:0${pbTime.toStringAsFixed(2)}";
  } else {
    return "$minutes:${pbTime.toStringAsFixed(2)}";
  } // DEVUELVE ELMEJOR TIEMPO FORMATEADO
}