def handle(self, *args, **options): challenge_id = options['challenge_id'] try: challenge = Challenge.objects.get(id=challenge_id) except Challenge.DoesNotExist: raise CommandError('Challenge "%s" does not exist.' % challenge_id) team_pcs = challenge.teams.all() try: dummy_team = TeamParticipatesChallenge.objects.get( id=options['test_team_pc_id']).team except TeamParticipatesChallenge.DoesNotExist: raise CommandError('Dummy team "%s" does not exist.' % options['test_team_pc_id']) group_size = options['group_size'] if group_size < 1: raise CommandError('group_size should be more than 0') submitters = [] for team_pc in team_pcs: if team_pc.has_submitted(): if team_pc.team != dummy_team: submitters.append(team_pc.team) shuffle(submitters) n = submitters.__len__() submitters += [dummy_team for i in range((group_size - n % group_size) % group_size)] n = submitters.__len__() groups = [[] for i in range(n // group_size)] for i in range(n): groups[i % (groups.__len__())].append(submitters[i]) for i in range(groups.__len__()): competition = Competition( tag=options['tag'], challenge=challenge, type='league', name='گروه %d لیگ اولیه انتخابی' % (i+1) ) competition.save() for map in Map.objects.filter(name__in=options['map_name']): competition.maps.add(map) competition.save() competition.create_new_league(groups[i], 1) competition.save() self.stdout.write(self.style.SUCCESS('Successfully created seeding league.'))
def test_create_new_league(self): challenge = Challenge.objects.all()[0] for team in Team.objects.all(): participation = TeamParticipatesChallenge() participation.team = team participation.challenge = challenge participation.save() competition = Competition(challenge=challenge, type='league') competition.save() maps = list(Map.objects.all()) for map in maps: competition.maps.add(map) competition.create_new_league(teams=Team.objects.all(), rounds_num=2) # expected result # 1 -> 3 # 2 -> 4 # 1 -> 4 # 3 -> 2 # 1 -> 2 # 4 -> 3 # 3 -> 1 # 4 -> 2 # 4 -> 1 # 2 -> 3 # 2 -> 1 # 3 -> 4 matches = list(Match.objects.all()) self.assertEqual(matches[0].part1.object_id, 1) self.assertEqual(matches[0].part2.object_id, 3) self.assertEqual(matches[1].part1.object_id, 3) self.assertEqual(matches[1].part2.object_id, 2) self.assertEqual(matches[2].part1.object_id, 1) self.assertEqual(matches[2].part2.object_id, 2) self.assertEqual(matches[3].part2.object_id, 1) self.assertEqual(matches[3].part1.object_id, 3) self.assertEqual(matches[4].part2.object_id, 3) self.assertEqual(matches[4].part1.object_id, 2) self.assertEqual(matches[5].part2.object_id, 1) self.assertEqual(matches[5].part1.object_id, 2)
def create_new_league(self, request, queryset): teams = list(queryset) shuffle(teams) if len(teams) < 1: from django.contrib import messages messages.error(request, _('no selected teams!')) return first_challenge = teams[0].challenge for team in teams: if team.challenge != first_challenge: from django.contrib import messages messages.error(request, _('Only teams from one challenge!')) return new_competition = Competition(challenge=first_challenge, name=str(len(first_challenge.competitions.all()) + 1), type='league') new_competition.save() new_competition.create_new_league( [team.team for team in teams], 1 ) new_competition.save()