|
| 1 | +"""Challonge integration.""" |
| 2 | + |
| 3 | +import csv |
| 4 | +import json |
| 5 | +import logging |
| 6 | +from collections import defaultdict |
| 7 | + |
| 8 | +import iso8601 |
| 9 | + |
| 10 | +from aocref import get_metadata |
| 11 | + |
| 12 | + |
| 13 | +LOGGER = logging.getLogger(__name__) |
| 14 | +EVENT_DATA = json.loads(open(get_metadata('events/events.json'), 'r').read()) |
| 15 | +SERIES_DATA = list(csv.DictReader(filter(lambda row: row[0] not in ['#', '\n'], |
| 16 | + open(get_metadata('events/series.csv'), 'r')))) |
| 17 | +CHALLONGE_API_URL = 'https://api.challonge.com/v1/tournaments/{}.json' |
| 18 | +STATE_PENDING = 'pending' |
| 19 | +SCORE_SEPARATOR_1 = ',' |
| 20 | +SCORE_SEPARATOR_2 = '-' |
| 21 | + |
| 22 | + |
| 23 | +def _map_participants(participant_data): |
| 24 | + """Map participant IDs to names.""" |
| 25 | + participants = {} |
| 26 | + for participant_wrapper in participant_data: |
| 27 | + participant = participant_wrapper['participant'] |
| 28 | + |
| 29 | + # Record group stage name, if applicable |
| 30 | + if len(participant['group_player_ids']) == 1: |
| 31 | + pid = participant['group_player_ids'][0] |
| 32 | + participants[pid] = participant['name'] |
| 33 | + |
| 34 | + pid = participant['id'] |
| 35 | + participants[pid] = participant['name'] |
| 36 | + return participants |
| 37 | + |
| 38 | + |
| 39 | +def _compute_total_score(scores_csv): |
| 40 | + """Compute total score per particpant. |
| 41 | +
|
| 42 | + Some tournament admins report scores per match in a series, |
| 43 | + but not all. Therefore, we have to return the lowest common |
| 44 | + denominator - total score for the series (challonge "match"). |
| 45 | + """ |
| 46 | + total_score = [0, 0] |
| 47 | + for score in scores_csv.split(SCORE_SEPARATOR_1): |
| 48 | + parts = score.split(SCORE_SEPARATOR_2) |
| 49 | + if len(parts) == 2: |
| 50 | + total_score[0] += int(parts[0]) |
| 51 | + total_score[1] += int(parts[1]) |
| 52 | + return total_score |
| 53 | + |
| 54 | + |
| 55 | +def _format_participants(match, participants, total_score): |
| 56 | + """Format participants as list.""" |
| 57 | + return [{ |
| 58 | + 'id': match[id_field], |
| 59 | + 'name': participants.get(match[id_field]), |
| 60 | + 'score': total_score[i], |
| 61 | + 'winner': match['winner_id'] == match[id_field] |
| 62 | + } for i, id_field in enumerate(['player1_id', 'player2_id'])] |
| 63 | + |
| 64 | + |
| 65 | +def _get_manual_tournament(tournament_id): |
| 66 | + """Get manually-entered data for a tournament.""" |
| 67 | + rounds = defaultdict(list) |
| 68 | + for row in SERIES_DATA: |
| 69 | + if row['tournament_id'] == tournament_id: |
| 70 | + total_score = _compute_total_score(row['score']) |
| 71 | + rounds[row['round_id']].append({ |
| 72 | + 'id': '{}-{}-{}'.format(tournament_id, row['round_id'], row['series_id']), |
| 73 | + 'played': iso8601.parse_date(row['played']), |
| 74 | + 'participants': [{ |
| 75 | + 'id': None, |
| 76 | + 'name': name, |
| 77 | + 'score': total_score[i], |
| 78 | + 'winner': max(total_score) == total_score[i] |
| 79 | + } for i, name in enumerate([row['p1'], row['p2']])] |
| 80 | + }) |
| 81 | + |
| 82 | + name = None |
| 83 | + for event in EVENT_DATA: |
| 84 | + for tournament in event['tournaments']: |
| 85 | + if isinstance(tournament, list) and tournament[0] == tournament_id: |
| 86 | + name = tournament[1] |
| 87 | + |
| 88 | + if name: |
| 89 | + return { |
| 90 | + 'id': tournament_id, |
| 91 | + 'name': name, |
| 92 | + 'rounds': rounds |
| 93 | + } |
| 94 | + return None |
| 95 | + |
| 96 | + |
| 97 | +def get_tournament(session, tournament_id): |
| 98 | + """Get round and participant data for a tournament.""" |
| 99 | + |
| 100 | + manual = _get_manual_tournament(tournament_id) |
| 101 | + if manual: |
| 102 | + LOGGER.info("falling back to manual data for %s (no Challonge bracket)", tournament_id) |
| 103 | + return manual |
| 104 | + |
| 105 | + data = session.get(CHALLONGE_API_URL.format(tournament_id), params={ |
| 106 | + 'include_matches': 1, |
| 107 | + 'include_participants': 1 |
| 108 | + }).json() |
| 109 | + |
| 110 | + tournament = data.get('tournament') |
| 111 | + participants = _map_participants(tournament['participants']) |
| 112 | + rounds = defaultdict(list) |
| 113 | + |
| 114 | + for match_wrapper in tournament['matches']: |
| 115 | + match = match_wrapper.get('match') |
| 116 | + if match['state'] != STATE_PENDING: |
| 117 | + total_score = _compute_total_score(match['scores_csv']) |
| 118 | + round_key = '{};{}'.format(match['round'], match['group_id'] or 0) |
| 119 | + rounds[round_key].append({ |
| 120 | + 'id': match['id'], |
| 121 | + 'participants': _format_participants(match, participants, total_score), |
| 122 | + 'played': iso8601.parse_date(match['updated_at']) |
| 123 | + }) |
| 124 | + return { |
| 125 | + 'id': tournament_id, |
| 126 | + 'name': tournament['name'], |
| 127 | + 'rounds': rounds |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +def get_events(session): |
| 132 | + """Get event data.""" |
| 133 | + for event in EVENT_DATA: |
| 134 | + tournaments = [] |
| 135 | + for tournament_id in event['tournaments']: |
| 136 | + if isinstance(tournament_id, list): |
| 137 | + tournament_id = tournament_id[0] |
| 138 | + tournaments.append(get_tournament(session, tournament_id)) |
| 139 | + yield { |
| 140 | + 'id': event['id'], |
| 141 | + 'name': event['name'], |
| 142 | + 'tournaments': tournaments |
| 143 | + } |
0 commit comments