Ejemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        match = MatchModel.last_match(request.spark)
        if match:
            return redirect(reverse('league:preview'))

        team1, _= PlayerModel.objects.get_or_create(name="Team 1")
        team2, _ = PlayerModel.objects.get_or_create(name="Team 2")

        MatchModel.create_match(request.spark, team1, team2)
        return redirect(reverse('league:preview'))
Ejemplo n.º 2
0
    def test_goal(self):
        spark = SparkDeviceModel.objects.create(spark_id="123456")

        player1 = PlayerModel.objects.create(name="aaa'")
        player2 = PlayerModel.objects.create(name="bbb'")

        match = MatchModel.create_match(spark, player1, player2)
        self.assertTrue(match)
        self.assertTrue(spark)

        response = self.client.get('/matches/current/')

        self.assertEqual(response.json()['white_count'], 0)
        self.assertEqual(response.json()['black_count'], 0)
        self.assertEqual(match.white_count, 0)
        self.assertEqual(match.black_count, 0)

        response = self.client.post('/sparks/{0}/goal/'.format(spark.spark_id), data={
            "data": "3,5",
        })

        response = self.client.get('/matches/current/')

        self.assertEqual(response.json()['white_count'], 3)
        self.assertEqual(response.json()['black_count'], 5)
        self.assertEqual(match.white_count, 3)
        self.assertEqual(match.black_count, 5)
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        form = StartMatchForm(self.request.POST)
        if form.is_valid():
            player1_white, _ = PlayerModel.objects.get_or_create(name=form.cleaned_data['player1_white'])
            player2_white = form.cleaned_data['player2_white']

            player1_black, _ = PlayerModel.objects.get_or_create(name=form.cleaned_data['player1_black'])
            player2_black = form.cleaned_data['player2_black']

            if player2_black:
                player2_black, _ = PlayerModel.objects.get_or_create(name=player2_black)
            else:
                player2_black = None

            if player2_white:
                player2_white, _ = PlayerModel.objects.get_or_create(name=player2_white)
            else:
                player2_white = None

            try:
                match = MatchModel.create_match(request.spark, player1_white, player1_black, player2_white,
                                                player2_black)
                return redirect(reverse('league:preview'))

            except MatchModel.WrongPlayers:
                error = "You need minimum two players, one for each team."
            except MatchModel.MatchAlreadyInProgress:
                error = "Match is already in progress"

        return self.render_to_response({"form": form})
Ejemplo n.º 4
0
    def test_match_finish(self):
        spark = SparkDeviceModel.objects.create(spark_id="123456")

        player1 = PlayerModel.objects.create(name="aaa'")
        player2 = PlayerModel.objects.create(name="bbb'")

        match = MatchModel.create_match(spark, player1, player2)
        self.assertTrue(match)
        self.assertTrue(spark)

        response = self.client.post('/sparks/{0}/goal/'.format(spark.spark_id), data={
            "data": "1,5",
        })

        self.assertEqual(response.status_code, 200)
        self.assertEqual(match.white_count, 1)
        self.assertEqual(match.black_count, 5)
        self.assertEqual(match.finished, False)

        response = self.client.post('/sparks/{0}/goal/'.format(spark.spark_id), data={
            "data": "1,6",
        })

        match.refresh_from_db()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(match.white_count, 1)
        self.assertEqual(match.black_count, 6)
        self.assertEqual(match.finished, True)
Ejemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        old_match = MatchModel.objects.filter(device=request.spark, finished=True).last()
        old_match.cancel()

        match = MatchModel.last_match(request.spark)
        if match:
            return redirect(reverse('league:preview'))

        white_players = old_match.white_side_players.all()
        black_players = old_match.black_side_players.all()

        wp1 = white_players[0]
        bp1 = black_players[0]

        wp2 = white_players[1] if len(white_players) > 1 else None
        bp2 = black_players[1] if len(black_players) > 1 else None

        MatchModel.create_match(request.spark, bp1, wp1, bp2, wp2)
        return redirect(reverse('league:preview'))