def get_friendly_time(self): """Returns a user-friendly representation of this solve's overall time, including penalties. """ if not self.time: return '' # TODO: handle blind DNFs, which show "DNF(time here)" if self.is_dnf: return 'DNF' total_time = self.get_total_time() if self.UserEventResults.is_fmc: converted_value = int(total_time) / 100 if converted_value == int(converted_value): converted_value = int(converted_value) return converted_value if self.UserEventResults.is_mbld: return build_mbld_results(total_time) converted_to_friendly = convert_centiseconds_to_friendly_time( total_time) if self.is_plus_two: converted_to_friendly = converted_to_friendly + '+' return converted_to_friendly
def friendly_time(value): """ Jinja custom filter to convert a time in cs to a user-friendly time. """ if value is None: return '' try: converted_value = int(value) except ValueError: return value return convert_centiseconds_to_friendly_time(converted_value)
def build_mbld_results(coded_value): """ Builds and returns a user-friendly representation of MBLD results from the coded integer representation. """ coded_result = str(coded_value) if coded_result == 'DNF': return 'DNF' num_successful, num_attempted, seconds = __parse_mbld_coded_value( coded_result) time = convert_centiseconds_to_friendly_time(seconds * 100) time_no_fractions = time[:len(time) - 3] return '{}/{} {}'.format(num_successful, num_attempted, time_no_fractions)
def __format_for_friendly(self, value): """ Utility method to return a friendly representation of the value passed in. Depends on whether or not this UserEventResults is for an FMC event or not. """ if not value: return '' if value == 'DNF': return value if self.is_fmc: converted_value = int(value) / 100 if converted_value == int(converted_value): converted_value = int(converted_value) return converted_value if self.is_mbld: return build_mbld_results(value) return convert_centiseconds_to_friendly_time(value)
def __build_times_string(results, event_format, is_fmc, is_blind, is_mbld): """ Builds a list of individual times, with best and worst times in parentheses if appropriate for the given event format. """ # Extract the solves out of the UserEventResults, since we'll be using them often solves = results.solves # Bo1 is special, just return the friendly representation of the one time if event_format == EventFormat.Bo1: first_solve = solves[0] if first_solve.is_dnf: return DNF else: if is_mbld: return build_mbld_results(first_solve.get_total_time()) else: return convert_centiseconds_to_friendly_time( first_solve.get_total_time()) # Build a list which contains the user-friendly representation of the total solve time for each solve if not is_fmc: if is_mbld: friendly_times = [ build_mbld_results(solve.get_total_time()) for solve in solves ] else: friendly_times = [ convert_centiseconds_to_friendly_time(solve.get_total_time()) for solve in solves ] # FMC is special, the 'time' is actually the number of moves, not number of centiseconds, so instead # interpret the time value here as "centimoves" and convert to integer number of moves represented as a string else: friendly_times = [ str(int(solve.get_total_time() / 100)) for solve in solves ] # Iterate over the solves themselves, and if any of those solves have +2 penalty, add a '+' marker to the # corresponding user-friendly representation of that time for i, solve in enumerate(solves): if solve.is_plus_two: friendly_times[i] = PLUS_TWO_SOLVE_TEMPLATE.format( solve_time=friendly_times[i]) # Initialize some variables to hold the current best and worst times, and the index at which they # reside, so we mark those with parentheses curr_best = MAX curr_worst = -1 best_index = -1 worst_index = -1 # Store the indices of any DNFs, so we can denote replace the user-friendly times with "DNF" dnf_indicies = list() have_found_dnf = False # Iterate over each list, and check if the solve is the current best or worst time, remembering the # index if it is. Only the first DNF counts as the worst time. Remember indices of each DNF for i, solve in enumerate(solves): if (not solve.is_dnf) and (solve.get_total_time() < curr_best): best_index = i curr_best = solve.get_total_time() if (not have_found_dnf) and (solve.get_total_time() > curr_worst): worst_index = i curr_worst = solve.get_total_time() if solve.is_dnf: if not have_found_dnf: worst_index = i have_found_dnf = True dnf_indicies.append(i) # For all DNFs in the solve, replace the corresponding user-friendly time with "DNF", # or wrap the time with "DNF(<time>)" for blind events for i in dnf_indicies: if is_blind: friendly_times[i] = BLIND_EVENT_DNF_SOLVE_TEMPLATE.format( solve_time=friendly_times[i]) else: friendly_times[i] = DNF # Best-of-3 events can be "complete" without all solves being finished. # If we have fewer than 3 times for a Bo3 event, keep adding 'DNS' (did not start) until # we have 3 time entries if event_format == EventFormat.Bo3: while len(friendly_times) < 3: friendly_times.append(DNS) # For Bo3 and Mo3 event formats, we don't enclose best and worst times in parentheses. # Return the list of times if a list is requested, otherwise join each time with a comma and # return the final result. if event_format in [EventFormat.Bo3, EventFormat.Mo3]: return SOLVE_TIMES_SEPARATOR.join(friendly_times) # For other event formats, we enclose the best and worst times in parentheses. friendly_times[best_index] = SOLVE_PARENS_TEMPLATE.format( solve_time=friendly_times[best_index]) friendly_times[worst_index] = SOLVE_PARENS_TEMPLATE.format( solve_time=friendly_times[worst_index]) # Return the list of times if a list is requested, otherwise join each time with a comma and # return the final result. return SOLVE_TIMES_SEPARATOR.join(friendly_times)