def rankings_table(self): if not self.rankings2: return None from helpers.rankings_helper import RankingsHelper precisions = [] for item in RankingsHelper.get_sort_order_info(self): precisions.append(item['precision']) rankings_table = [] has_record = False has_qual_avg = False for rank in self.rankings2: row = [rank['rank'], rank['team_key'][3:]] # for i, item in enumerate(rank['sort_orders']): for i, precision in enumerate(precisions): # row.append('%.*f' % (precisions[i], round(item, precisions[i]))) row.append( '%.*f' % (precision, round(rank['sort_orders'][i], precision))) if rank['record']: row.append('{}-{}-{}'.format(rank['record']['wins'], rank['record']['losses'], rank['record']['ties'])) has_record = True row.append(rank['dq']) row.append(rank['matches_played']) if rank['qual_average'] is None and self.year != 2017: row.append('%.*f' % (2, round( rank['sort_orders'][0] / rank['matches_played'] if rank['matches_played'] > 0 else 0, 2))) else: has_qual_avg = True rankings_table.append(row) title_row = ['Rank', 'Team'] sort_order_info = RankingsHelper.get_sort_order_info(self) for item in sort_order_info: title_row.append(item['name']) if has_record: title_row += ['Record (W-L-T)'] title_row += ['DQ', 'Played'] if not has_qual_avg and self.year != 2017: title_row.append('{}/Match*'.format(sort_order_info[0]['name'])) rankings_table = [title_row] + rankings_table return rankings_table
def _process_request(self, request, event_key): rankings = JSONRankingsParser.parse(request.body) event_details = EventDetails(id=event_key, rankings=rankings) if event_details.year >= 2017: # TODO: Temporary fix. Should directly parse request into rankings2 event_details.rankings2 = RankingsHelper.convert_rankings( event_details) EventDetailsManipulator.createOrUpdate(event_details) self.response.out.write( json.dumps({'Success': "Rankings successfully updated"}))
def _process_request(self, request, event_key): rankings = JSONRankingsParser.parse(request.body) event_details = EventDetails( id=event_key, rankings=rankings ) if event_details.year >= 2017: # TODO: Temporary fix. Should directly parse request into rankings2 event_details.rankings2 = RankingsHelper.convert_rankings(event_details) EventDetailsManipulator.createOrUpdate(event_details) self.response.out.write(json.dumps({'Success': "Rankings successfully updated"}))
def get(self, year): self._require_admin() event_keys = Event.query(Event.year==int(year)).fetch(keys_only=True) event_details = ndb.get_multi([ndb.Key(EventDetails, key.id()) for key in event_keys]) updated = [] for event_detail in event_details: if event_detail: logging.info(event_detail.key.id()) event_detail.rankings2 = RankingsHelper.convert_rankings(event_detail) updated.append(event_detail) EventDetailsManipulator.createOrUpdate(updated) self.response.out.write("DONE")
def parse(self, response): rankings = [] for team in response['Rankings']: count = 1 order_name = 'sortOrder{}'.format(count) sort_orders = [] while order_name in team: sort_orders.append(team[order_name]) count += 1 order_name = 'sortOrder{}'.format(count) rankings.append(RankingsHelper.build_ranking( self.year, team['rank'], 'frc{}'.format(team['teamNumber']), team['wins'], team['losses'], team['ties'], team['qualAverage'], team['matchesPlayed'], team['dq'], sort_orders)) return rankings
def renderable_rankings(self): from helpers.rankings_helper import RankingsHelper has_extra_stats = False if self.rankings2: for rank in self.rankings2: rank['extra_stats'] = [] if self.year in {2017, 2018, 2019}: rank['extra_stats'] = [ int( round(rank['sort_orders'][0] * rank['matches_played'])), ] has_extra_stats = True elif rank['qual_average'] is None: rank['extra_stats'] = [ rank['sort_orders'][0] / rank['matches_played'] if rank['matches_played'] > 0 else 0, ] has_extra_stats = True sort_order_info = RankingsHelper.get_sort_order_info(self) extra_stats_info = [] if has_extra_stats: if self.year in {2017, 2018, 2019}: extra_stats_info = [{ 'name': 'Total Ranking Points', 'precision': 0, }] else: extra_stats_info = [{ 'name': '{}/Match'.format(sort_order_info[0]['name']), 'precision': 2, }] return { 'rankings': self.rankings2, 'sort_order_info': sort_order_info, 'extra_stats_info': extra_stats_info, }
def renderable_rankings(self): from helpers.rankings_helper import RankingsHelper has_extra_stats = False if self.rankings2: for rank in self.rankings2: rank['extra_stats'] = [] if self.year in {2017, 2018}: rank['extra_stats'] = [ int(round(rank['sort_orders'][0] * rank['matches_played'])), ] has_extra_stats = True elif rank['qual_average'] is None: rank['extra_stats'] = [ rank['sort_orders'][0] / rank['matches_played'] if rank['matches_played'] > 0 else 0, ] has_extra_stats = True sort_order_info = RankingsHelper.get_sort_order_info(self) extra_stats_info = [] if has_extra_stats: if self.year in {2017, 2018}: extra_stats_info = [{ 'name': 'Total Ranking Points', 'precision': 0, }] else: extra_stats_info = [{ 'name': '{}/Match'.format(sort_order_info[0]['name']), 'precision': 2, }] return { 'rankings': self.rankings2, 'sort_order_info': sort_order_info, 'extra_stats_info': extra_stats_info, }
def _build_qual_info(cls, team_key, event_details, matches, year): if not matches['qm']: status = 'not_started' else: status = 'completed' for match in matches['qm']: if not match.has_been_played: status = 'playing' break if event_details and event_details.rankings2: rankings = event_details.rankings2 qual_info = None for ranking in rankings: if ranking['team_key'] == team_key: qual_info = { 'status': status, 'ranking': ranking, } break if qual_info: qual_info['num_teams'] = len(rankings) qual_info['sort_order_info'] = RankingsHelper.get_sort_order_info(event_details) return qual_info else: # Use matches as fallback all_teams = set() wins = 0 losses = 0 ties = 0 qual_score_sum = 0 matches_played = 0 for match in matches['qm']: for color in ['red', 'blue']: for team in match.alliances[color]['teams']: all_teams.add(team) if team == team_key and match.has_been_played and \ team_key not in match.alliances[color]['surrogates']: matches_played += 1 if match.winning_alliance == color: wins += 1 elif match.winning_alliance == '': ties += 1 else: losses += 1 qual_score_sum += match.alliances[color]['score'] qual_average = float(qual_score_sum) / matches_played if matches_played else 0 if team_key in all_teams: return { 'status': status, 'ranking': { 'rank': None, 'matches_played': matches_played, 'dq': None, 'record': { 'wins': wins, 'losses': losses, 'ties': ties, } if year != 2015 else None, 'qual_average': qual_average if year == 2015 else None, 'sort_orders': None, 'team_key': team_key, }, 'num_teams': len(all_teams), 'sort_order_info': None } else: return None
def _build_qual_info(cls, team_key, event_details, matches, year): if not matches['qm']: status = 'not_started' else: status = 'completed' for match in matches['qm']: if not match.has_been_played: status = 'playing' break if event_details and event_details.rankings2: rankings = event_details.rankings2 qual_info = None for ranking in rankings: if ranking['team_key'] == team_key: qual_info = { 'status': status, 'ranking': ranking, } break if qual_info: qual_info['num_teams'] = len(rankings) qual_info[ 'sort_order_info'] = RankingsHelper.get_sort_order_info( event_details) return qual_info else: # Use matches as fallback all_teams = set() wins = 0 losses = 0 ties = 0 qual_score_sum = 0 matches_played = 0 for match in matches['qm']: for color in ['red', 'blue']: for team in match.alliances[color]['teams']: all_teams.add(team) if team == team_key and match.has_been_played and \ team_key not in match.alliances[color]['surrogates']: matches_played += 1 if match.winning_alliance == color: wins += 1 elif match.winning_alliance == '': ties += 1 else: losses += 1 qual_score_sum += match.alliances[color]['score'] qual_average = float( qual_score_sum) / matches_played if matches_played else 0 if team_key in all_teams: return { 'status': status, 'ranking': { 'rank': None, 'matches_played': matches_played, 'dq': None, 'record': { 'wins': wins, 'losses': losses, 'ties': ties, } if year != 2015 else None, 'qual_average': qual_average if year == 2015 else None, 'sort_orders': None, 'team_key': team_key, }, 'num_teams': len(all_teams), 'sort_order_info': None } else: return None
def renderable_rankings(self): from helpers.rankings_helper import RankingsHelper return { 'rankings': self.rankings2, 'sort_order_info': RankingsHelper.get_sort_order_info(self), }