Example #1
0
def generate_tournament(
    teams,
    teams_per_match=2,
    first_round=None,
    tournament=None,
    planned_start=None,
    planned_finish=None,
    team_defaults=None,
):
    num_teams = len(teams)
    first_round_match_count = num_teams // teams_per_match

    print('Creating tournament...')
    if tournament is None:
        tournament = get_tournament(
            'Tournament {}'.format(Tournament.objects.count() + 1))
    else:
        tournament = get_tournament(tournament)
    print('Created tournament')

    print('Entering teams...')
    entries = []
    team_objects = []
    if isinstance(teams, dict):
        for team, players in teams.items():
            team_obj = get_team(team, defaults=team_defaults)
            team_objects.append(team_obj)

            user_objects = [get_user(p) for p in players]

            entry = TeamEntry(team=team_obj, tournament=tournament)
            entry.save()
            entry.players = user_objects
            entry.save()
            entries.append(entry)

    else:
        for team in teams:
            team_obj = get_team(team, defaults=team_defaults)
            team_objects.append(team_obj)

            entry = TeamEntry(team=team_obj, tournament=tournament)
            entry.save()
            entries.append(entry)
    print('Entered teams')

    print('Adding matches...')
    previous_round = [get_match(tournament, name='Final')]

    current_round_id = 2

    while len(previous_round) < first_round_match_count:
        current_round = []
        for match in previous_round:
            current_round.extend([
                get_match(tournament,
                          name='{}:{}'.format(match.name, i),
                          winner_next=match) for i in range(2)
            ])
        print('- added {} matches to tier {}'.format(len(current_round),
                                                     current_round_id))
        current_round_id += 1
        previous_round = current_round

    print('Setting up matches...')
    if not first_round:
        first_round = previous_round

    for i, match in enumerate(first_round):
        match.teams = team_objects[i * teams_per_match:(i + 1) *
                                   teams_per_match]
        match.save()
    print('First round complete')

    print('Done!')

    return tournament
Example #2
0
def generate_tournament(
    teams,
    teams_per_match=2,
    first_round=None,
    tournament=None,
    planned_start=None,
    planned_finish=None,
    team_defaults=None,
):
    num_teams = len(teams)
    first_round_match_count = num_teams // teams_per_match

    print('Creating tournament...')
    if tournament is None:
        tournament = get_tournament(
            'Tournament {}'.format(Tournament.objects.count() + 1)
        )
    else:
        tournament = get_tournament(tournament)
    print('Created tournament')

    print('Entering teams...')
    entries = []
    team_objects = []
    if isinstance(teams, dict):
        for team, players in teams.items():
            team_obj = get_team(team, defaults=team_defaults)
            team_objects.append(team_obj)

            user_objects = [get_user(p) for p in players]

            entry = TeamEntry(team=team_obj, tournament=tournament)
            entry.save()
            entry.players = user_objects
            entry.save()
            entries.append(entry)

    else:
        for team in teams:
            team_obj = get_team(team, defaults=team_defaults)
            team_objects.append(team_obj)

            entry = TeamEntry(team=team_obj, tournament=tournament)
            entry.save()
            entries.append(entry)
    print('Entered teams')

    print('Adding matches...')
    previous_round = [get_match(tournament, name='Final')]

    current_round_id = 2

    while len(previous_round) < first_round_match_count:
        current_round = []
        for match in previous_round:
            current_round.extend([
                get_match(
                    tournament,
                    name='{}:{}'.format(match.name, i),
                    winner_next=match
                )
                for i in range(2)
            ])
        print('- added {} matches to tier {}'.format(
            len(current_round), current_round_id))
        current_round_id += 1
        previous_round = current_round

    print('Setting up matches...')
    if not first_round:
        first_round = previous_round

    for i, match in enumerate(first_round):
        match.teams = team_objects[
            i * teams_per_match: (i + 1) * teams_per_match
        ]
        match.save()
    print('First round complete')

    print('Done!')

    return tournament
Example #3
0
def add_entry(team, players, tournament):
    entry = TeamEntry(team=team, tournament=tournament)
    entry.save()
    entry.players = players
    entry.save()