diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index 2c81b6e74..5e39bedcb 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -7,12 +7,13 @@ from django.apps import apps from django.conf import settings +from siarnaq.api.teams.models import Team + if TYPE_CHECKING: from typing import Iterable from siarnaq.api.compete.models import Match, MatchParticipant from siarnaq.api.episodes.models import Tournament, TournamentRound - from siarnaq.api.teams.models import Team _headers = { @@ -24,8 +25,7 @@ "User-Agent": "", } -AUTH_TYPE = "v1" -URL_BASE = "https://api.challonge.com/v2/" +URL_BASE = "https://api.challonge.com/v2.1/" def create_tournament(tournament: Tournament, *, is_private: bool): @@ -84,16 +84,14 @@ def bulk_add_teams(tournament: Tournament, teams: Iterable[Team], *, is_private: { "name": team.name, "seed": idx + 1, - "misc": json.dumps( - {"team_id": team.id, "submission_id": team.active_submission} - ), + "misc": json.dumps({"team_id": team.id}), } for (idx, team) in enumerate(teams) ] payload = { "data": { - "type": "Participant", + "type": "Participants", "attributes": { "participants": participants_for_challonge, }, @@ -138,7 +136,7 @@ def get_tournament_data(tournament: Tournament, *, is_private: bool): # in case of bracket reset matches = [item for item in data["included"] if item["type"] == "match"] match_last = max( - matches, key=lambda match: match["attributes"]["suggestedPlayOrder"] + matches, key=lambda match: match["attributes"]["suggested_play_order"] ) # Give it its own round match_last["attributes"]["round"] += 1 @@ -156,7 +154,7 @@ def _get_round_indexes(tournament: Tournament, *, is_private: bool): round_indexes = list() matches = [item for item in tournament_data["included"] if item["type"] == "match"] - matches.sort(key=lambda i: i["attributes"]["suggestedPlayOrder"]) + matches.sort(key=lambda i: i["attributes"]["suggested_play_order"]) for match in matches: round_index = match["attributes"]["round"] @@ -271,6 +269,14 @@ def get_match_and_participant_objects_for_round(round: TournamentRound): # and map them with IDs for easy lookup challonge_participants = dict() + # Hold a map of all teams' latest submission for the tournament, + # for quick simple lookups later + team_submissions = dict( + Team.objects.with_active_submission() + .values_list("pk", "active_submission") + .all() + ) + for item in tournament_data_private["included"]: match item: case { @@ -322,15 +328,12 @@ def get_match_and_participant_objects_for_round(round: TournamentRound): ) match_objects.append(match_object) - # Note that Challonge 1-indexes its player indexes - # while our internal data model (in Siarnaq) 0-indexes. - for (siarnaq_player_index, challonge_player_index) in enumerate( - ["player1", "player2"] - ): + challonge_points = challonge_match["attributes"]["points_by_participant"] + for (player_index, challonge_points_participant) in enumerate(challonge_points): # This looks ugly but it's how to parse through the Challonge-related data. - challonge_participant_id_private = challonge_match["relationships"][ - challonge_player_index - ]["data"]["id"] + challonge_participant_id_private = str( + challonge_points_participant["participant_id"] + ) challonge_participant_id_public = challonge_team_ids_private_to_public[ challonge_participant_id_private ] @@ -340,13 +343,13 @@ def get_match_and_participant_objects_for_round(round: TournamentRound): ] ) team_id = misc_key["team_id"] - submission_id = misc_key["submission_id"] + submission_id = team_submissions[team_id] match_participant_object = apps.get_model("compete", "MatchParticipant")( team_id=team_id, submission_id=submission_id, match=match_object, - player_index=siarnaq_player_index, + player_index=player_index, external_id_private=challonge_participant_id_private, external_id_public=challonge_participant_id_public, ) @@ -393,7 +396,7 @@ def update_match(match: Match, *, is_private: bool): if is_private else participant.external_id_public, "score_set": str(participant.score), - "advancing": True if participant.score == high_score else False, + "advancing": participant.score == high_score, } for participant in participants ]