From 862d72f964116b96505ad5c8f3fe8cd4102c8933 Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Sun, 14 Jan 2024 22:12:32 -0500 Subject: [PATCH 1/8] Use newer version of challonge api --- backend/siarnaq/bracket/challonge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index 2c81b6e74..dfb8f6f74 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -25,7 +25,7 @@ } 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): From df3755e12ca5b1e75b17100446c0ca4a6bf75dd2 Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Mon, 15 Jan 2024 14:09:40 -0500 Subject: [PATCH 2/8] Update fields for challonge v2.1 api --- backend/siarnaq/bracket/challonge.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index dfb8f6f74..9330f1ce4 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -138,7 +138,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 +156,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"] @@ -322,15 +322,13 @@ 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"] - ): + for player_index in range(2): # 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_match["attributes"]["points_by_participant"][player_index][ + "participant_id" + ] + ) challonge_participant_id_public = challonge_team_ids_private_to_public[ challonge_participant_id_private ] @@ -346,7 +344,7 @@ def get_match_and_participant_objects_for_round(round: TournamentRound): 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, ) From bced174e708435889fd94e20bdb6dfe7f2801600 Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Mon, 15 Jan 2024 14:32:29 -0500 Subject: [PATCH 3/8] Cleaner way to get participant points --- backend/siarnaq/bracket/challonge.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index 9330f1ce4..6397d2435 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -322,12 +322,11 @@ def get_match_and_participant_objects_for_round(round: TournamentRound): ) match_objects.append(match_object) - for player_index in range(2): + 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 = str( - challonge_match["attributes"]["points_by_participant"][player_index][ - "participant_id" - ] + challonge_points_participant["participant_id"] ) challonge_participant_id_public = challonge_team_ids_private_to_public[ challonge_participant_id_private From 5913da3e9dbc0c30a62a0732e3b8c764c8b4f575 Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Mon, 15 Jan 2024 14:33:29 -0500 Subject: [PATCH 4/8] Remove auth type version; unused --- backend/siarnaq/bracket/challonge.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index 6397d2435..ec9be6495 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -24,7 +24,6 @@ "User-Agent": "", } -AUTH_TYPE = "v1" URL_BASE = "https://api.challonge.com/v2.1/" From dffc1a946723e8af4b49176b3a2de35250bb2827 Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Mon, 15 Jan 2024 14:41:56 -0500 Subject: [PATCH 5/8] Clean up --- backend/siarnaq/bracket/challonge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index ec9be6495..a8f45cc33 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -389,7 +389,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 ] From 2a7d02ec89a97e0080b7bc3a17fa75b483bebd09 Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Sun, 26 May 2024 21:58:14 -0400 Subject: [PATCH 6/8] pluralize more things --- backend/siarnaq/bracket/challonge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index a8f45cc33..a3ca8d83c 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -92,7 +92,7 @@ def bulk_add_teams(tournament: Tournament, teams: Iterable[Team], *, is_private: payload = { "data": { - "type": "Participant", + "type": "Participants", "attributes": { "participants": participants_for_challonge, }, From d3a3b46774e1a8d2484621ef93a0eb4b5d27698c Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Mon, 27 May 2024 00:29:47 -0400 Subject: [PATCH 7/8] Get submission ID from database --- backend/siarnaq/bracket/challonge.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index a3ca8d83c..061af96c2 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 = { @@ -270,6 +271,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 { @@ -336,7 +345,7 @@ 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, From 07de343dec581750b7e1f4af3d752aca8443f611 Mon Sep 17 00:00:00 2001 From: Nathan Kim Date: Mon, 27 May 2024 00:30:04 -0400 Subject: [PATCH 8/8] Don't submission ID in challonge --- backend/siarnaq/bracket/challonge.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/siarnaq/bracket/challonge.py b/backend/siarnaq/bracket/challonge.py index 061af96c2..5e39bedcb 100644 --- a/backend/siarnaq/bracket/challonge.py +++ b/backend/siarnaq/bracket/challonge.py @@ -84,9 +84,7 @@ 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) ]