Ejemplo n.º 1
0
 def round_with_results(self):
     from results.controllers.ResultsController import ResultsController
     controller = ResultsController()
     if controller.results_entered_for_round(self.round):
         return self.round
     else:
         return self.round - 1
Ejemplo n.º 2
0
class ResultControllerTestCase(TestCase):

    def setUp(self):
        self.resultController = ResultsController()

        self.result = Result()
        self.debate = MagicMock()
        self.debateController = MagicMock()
        self.team = MagicMock

        self.debateController.debate_for_round.return_value = self.debate
        self.debate.result.return_value = self.result

        self.resultController.debate_controller = self.debateController

    def testRaisesExceptionForRoundNotDrawn(self):
        tournament = Tournament.instance()
        tournament.round = 1
        tournament.save()
        with self.assertRaises(TournamentStateException):
            self.resultController.results_entered_for_round(10)

    def testReturnsFalseForRoundWithoutAnyResults(self):
        generate_objects.setup_IV_R1()
        self.assertFalse(self.resultController.results_entered_for_round(1), "No results entered but returned true")

    def testReturnsTrueForRoundWithAllResults(self):
        generate_objects.setup_IV_R1()

        for debate in Debate.objects.filter(round=1):
            generate_objects.valid_result_given_debate(debate)

        self.assertTrue(self.resultController.results_entered_for_round(1), "All results entered but returned false")

    def testReturnsFalseForPartiallyEnteredRound(self):
        generate_objects.setup_IV_R1()
        generate_objects.valid_result_given_debate(Debate.objects.first())

        self.assertFalse(self.resultController.results_entered_for_round(1), "Only some results entered but returned true")

    def testGetResultForRound(self):
        return_value = self.resultController.result_for_team(self.team, 1)
        self.assertEqual(self.debate.result, return_value)
Ejemplo n.º 3
0
    def get(self, request):
        template = loader.get_template('draw/index.html')

        team_count = Team.objects.all().count()
        results_controller = ResultsController()
        tournament = Tournament.instance()

        context = RequestContext(request, {
            'teams_count' : Team.objects.all().count(),
            'teams_ok' : (team_count % 4 == 0) and (team_count >= 4),
            'rooms' : team_count / 4,
            'venues_ok' : Venue.objects.all().count() >= team_count/4,
            'tournament' : tournament,
            'this_round' : tournament.round,
            'next_round' : tournament.round + 1,
            'results_entered' : results_controller.results_entered_for_round(tournament.round)
        })
        return HttpResponse(template.render(context))
Ejemplo n.º 4
0
class DrawController:

    resultsController = None
    pointsController = None

    def __init__(self):
        self.resultsController = ResultsController()
        self.pointsController = PointsController()

    def draw_next_round(self):
        tournament = Tournament.instance()
        next_round = tournament.round + 1
        pools = self.create_pools(Team.objects.all(), tournament.round)
        pools = self.remove_empty(pools)
        pools = self.shuffle_pools(pools)
        pools = self.balance_pools(pools)
        debates = self.draw_from_pools(next_round, pools)
        tournament.round = next_round
        tournament.save()
        mapper = VenueMapper()
        mapper.map_venues(next_round)
        return debates

    def create_pools(self, teams, max_round):
        if not self.resultsController.results_entered_for_round(Tournament.instance().round):
            raise TournamentStateException("All results for current round must be entered to draw")
        pools = self.create_blank_pools(max_round)

        for team in teams:
            points = self.pointsController.total_points_for_team(team, 1)
            pools[points].append(team)

        return pools

    @staticmethod
    def create_blank_pools(max_round):
        pools = {}
        if max_round == 0:
            return pools

        for points in range(0, 4 + ((max_round - 1) * 3)):
            pools.update({points: []})
        return pools

    @staticmethod
    def remove_empty(pools):
        keys = pools.keys()
        for key in keys:
            pool = pools[key]
            if len(pool) < 1:
                pools.pop(key)
        return pools

    @staticmethod
    def next_viable_pool(current, pools):
        for i in range(current + 1, len(pools.values())):
            next_pool = pools.values()[i]
            if len(next_pool) != 0:
                return next_pool
        raise ValueError("No viable pool")

    @staticmethod
    def shuffle_pools(pools):
        for pool in pools.values():
            random.shuffle(pool)
        return pools

    def balance_pools(self, pools):
        flattened_pools = [item for sublist in pools.values() for item in sublist]
        if len(flattened_pools) % 4 != 0:
            raise ValueError("Number of teams must be divisible by 4")

        made_swap = True
        while made_swap:
            made_swap = False
            for i in range(0, len(pools.values())):
                pool = pools.values()[i]
                if (len(pool) % 4 != 0) and pool != pools.values()[-1]:
                    teams_needed = 4 - (len(pool) % 4)
                    for t in range(0, teams_needed):
                        source_pool = self.next_viable_pool(i, pools)
                        pool.append(source_pool.pop(0))
                        made_swap = True

        return pools

    def draw_from_pools(self, round, pools):
        debates = []
        for pool in pools.values():
            if len(pool) % 4 != 0:
                raise ValueError("Number of teams must be divisible by four")

            num_debates = len(pool) / 4

            for i in range(0, num_debates):
                debate = Debate(round=round)
                debate.OG = pool[(i * 4)]
                debate.OO = pool[(i * 4) + 1]
                debate.CG = pool[(i * 4) + 2]
                debate.CO = pool[(i * 4) + 3]
                debate.full_clean()
                debate.save()
                debates.append(debate)
        return debates