def most_awards_per_state(award_id: Type.int, num_per_state: Type.int): # https://github.com/the-blue-alliance/the-blue-alliance/blob/master/consts/award_type.py#L6 r, cache_hit = cache_frame(inspect.currentframe()) if r is not None: return r, cache_hit events = [call(tba.events, year=y) for y in range(1992, 2019)] events = helpers.flatten_lists(events) events = helpers.filter_official_events(events) events = list(filter(lambda e: e["country"] in ["USA", "Canada"], events)) key_to_state = {e["key"]: e["state_prov"] for e in events} event_awards = batch_call(events, tba.event_awards, lambda e: [], lambda e: {"event": e["key"]}) win_counts = defaultdict(lambda: defaultdict(lambda: 0)) for awards_list in event_awards: for award in awards_list: if award["award_type"] == award_id: for recipient in award["recipient_list"]: win_counts[key_to_state[award["event_key"]]][ recipient["team_key"]] += 1 top5_per_state = {} for state, leaderboard in win_counts.items(): leaderboard = sorted(leaderboard.items(), key=lambda t: -t[1]) top5_per_state[state] = leaderboard[:num_per_state] return top5_per_state
def get_teams_in_state(state, year=2019): i = 0 ny_teams = [] while True: page = call(tba.teams, page=i, year=year) if len(page) == 0: break for team in page: if team["state_prov"] == state: ny_teams.append(team["team_number"]) i += 1 return ny_teams
def team_info(team_number: Type.int): r, cache_hit = cache_frame(inspect.currentframe()) if r is not None: return r, cache_hit return call(tba.team, team=team_number)
def head_to_head(team_1: Type.int, team_2: Type.int): r, cache_hit = cache_frame(inspect.currentframe()) if r is not None: return r, cache_hit origin_key = f"frc{max(team_1, team_2)}" target_key = f"frc{min(team_1, team_2)}" rookie_year = call(tba.team, team=origin_key)["rookie_year"] won_together = 0 lost_together = 0 tied_together = 0 won_against = 0 lost_against = 0 tied_against = 0 for year in range(rookie_year, 2019): matches = call(tba.team_matches, team=origin_key, year=year, simple=True) for match in matches: event = call(tba.event, event=match["event_key"]) if event["event_type_string"] == "Offseason": continue blue = match["alliances"]["blue"]["team_keys"] red = match["alliances"]["red"]["team_keys"] if match["event_key"][:4] == "2015": if ( match["alliances"]["blue"]["score"] > match["alliances"]["red"]["score"] ): winner = "blue" elif ( match["alliances"]["blue"]["score"] < match["alliances"]["red"]["score"] ): winner = "red" else: winner = "" else: winner = match["winning_alliance"] if origin_key in blue: if target_key in blue: if winner == "blue": won_together += 1 elif winner == "red": lost_together += 1 else: tied_together += 1 elif target_key in red: if winner == "blue": won_against += 1 elif winner == "red": lost_against += 1 else: tied_against += 1 else: if target_key in blue: if winner == "blue": lost_against += 1 elif winner == "red": won_against += 1 else: tied_against += 1 elif target_key in red: if winner == "blue": lost_together += 1 elif winner == "red": won_together += 1 else: tied_together += 1 together_pct = round( won_together * 100 / max(won_together + tied_together + lost_together, 1), 1 ) against_pct = round( won_against * 100 / max(won_against + tied_against + lost_against, 1), 1 ) s = ( f"{origin_key} with {target_key} is " f"{won_together}-{lost_together}-{tied_together} ({together_pct}%)" ) s += ( "<br>" + f"{origin_key} against {target_key} is " f"{won_against}-{lost_against}-{tied_against} ({against_pct}%)" ) return s
def score_team_event(team_number, event_key, refresh=False, wk=7): event_blob = call(tba.event, event=event_key, refresh=refresh) team_key = f"frc{team_number}" if event_blob["week"] is not None: # not champs award_pt_map = { chairmans: 60, engineering_inspiration: 45, rookie_all_star: 25, rookie_inspiration: 15, autonomous: 20, creativity: 20, innovation_in_control: 20, industrial_design: 20, engineering_excellence: 20, quality: 20, deans_list: 5, woodie_flowers: 10, entrepreneurship: 5, gracious_professionalism: 5, imagery: 5, highest_rookie_seed: 5, judges: 5, safety: 5, spirit: 5, } else: award_pt_map = { chairmans: 110, chairmans_finalist: 90, engineering_inspiration: 60, rookie_all_star: 35, rookie_inspiration: 20, autonomous: 30, creativity: 30, innovation_in_control: 30, industrial_design: 30, engineering_excellence: 30, quality: 30, deans_list: 15, woodie_flowers: 30, entrepreneurship: 10, gracious_professionalism: 10, imagery: 10, highest_rookie_seed: 10, judges: 10, safety: 10, spirit: 10, } award_pts = 0 event_awards = call(tba.event_awards, event=event_key, refresh=refresh) for award in event_awards: for recipient in award["recipient_list"]: if recipient["team_key"] == team_key: award_pts += award_pt_map.get(award["award_type"], 0) try: district_pts = call(tba.event_district_points, event=event_key, refresh=refresh) except TypeError: district_pts = {"qual_points": 0, "alliance_points": 0, "elim_points": 0} else: if district_pts is None or not district_pts["points"]: district_pts = {"qual_points": 0, "alliance_points": 0, "elim_points": 0} else: try: district_pts = district_pts["points"][team_key] except KeyError: district_pts = { "qual_points": 0, "alliance_points": 0, "elim_points": 0, } qual_pts = district_pts["qual_points"] picking_pts = district_pts["alliance_points"] if event_blob["event_type_string"] in [ "District Championship", "District Championship Division", ]: qual_pts /= 3 picking_pts /= 3 # elim_pts = district_pts['elim_points'] # force elims points elim_pts = 0 matches = call(tba.event_matches, event=event_key, refresh=refresh) for match in matches: if match["comp_level"] == "qm": continue for color in ["blue", "red"]: if team_key in match["alliances"][color]["team_keys"]: if ( match["winning_alliance"] is not None and match["winning_alliance"] == color ): elim_pts += 5 to_return = [ qual_pts, picking_pts, elim_pts, award_pts, qual_pts + picking_pts + elim_pts + award_pts, ] if event_key in child_events.keys(): parent = child_events[event_key] parent_scores = score_team_event(team_number, parent, refresh=refresh, wk=wk) for i, score in enumerate(parent_scores, start=0): to_return[i] += score return { "qual": to_return[0], "alliance": to_return[1], "elim": to_return[2], "award": to_return[3], "total": to_return[4], }