Esempio n. 1
0
    def test_gamehole_unique_constraint(self):
        game = make_game()
        game.start()
        game.save()

        player = game.players.all()[0]
        coursehole = game.course.coursehole_set.all()[0]

        gh1 = GameHole(
            player=player,
            game=game,
            coursehole=coursehole,
            throws=3,
            ob_throws=0,
        )
        gh1.save()

        # Create identical gamehole as gh1
        gh2 = GameHole(
            player=player,
            game=game,
            coursehole=coursehole,
            throws=3,
            ob_throws=0,
        )

        # Should not be able to save gh2, violates
        # unique constraint.
        self.assertRaises(IntegrityError, gh2.save)
Esempio n. 2
0
    def update(self, req, pk):
        game = Game.objects.get(pk=pk)

        if req.content_type and req.data:
            for req_gh in req.data:
                # Create or find gamehole object
                try:
                    gh = GameHole.objects.get(
                        player__id=req_gh["player_id"],
                        game__id=game.id,
                        coursehole__id=req_gh["coursehole_id"])
                except GameHole.DoesNotExist:
                    gh = GameHole()
                    gh.player_id = req_gh["player_id"]
                    gh.game_id = game.id
                    gh.coursehole_id = req_gh["coursehole_id"]

                # Set properties and save gamehole
                gh.throws = req_gh["throws"]
                gh.ob_throws = req_gh["ob_throws"]
                gh.save()

            return rc.ALL_OK

        else:
            return rc.BAD_REQUEST
Esempio n. 3
0
    def test_gamehole_on_wrong_course(self):
        game = make_game()
        game.start()
        game.save()

        player = game.players.all()[0]
        coursehole = game.course.coursehole_set.all()[0]

        gh1 = GameHole(
            player=player,
            game=game,
            coursehole=coursehole,
            throws=3,
            ob_throws=0,
        )
        gh1.save()

        # Create a new course
        course2 = Course.objects.create(
            arena=game.course.arena,
            name="test - course 2",
        )

        # Create a new coursehole
        coursehole2 = CourseHole.objects.create(
            course=course2,
            hole=coursehole.hole,
            order=1,
            name="test - coursehole 2",
        )

        gh2 = GameHole(
            player=player,
            game=game,
            coursehole=coursehole2,
            throws=3,
            ob_throws=0,
        )

        # We shouldn't be able to save gh2,
        # as its coursehole is not the same as the games
        self.assertRaises(ValidationError, gh2.save)
Esempio n. 4
0
def play(req, pk):
    game = get_object_or_404(Game, id=pk)

    if req.method == "POST":
        if "game-state-change" in req.POST:
            if not "wanted-state" in req.POST:
                return HttpResponseBadRequest(
                    "Required 'wanted-state' argument")

            wanted_state = req.POST["wanted-state"]
            if not hasattr(game, wanted_state):
                return HttpResponseBadRequest("Unknown wanted state")

            # Run method to change state for game
            state_changer = getattr(game, wanted_state)

            state_changer()
            game.save()

        elif "score" in req.POST:
            # Map between pgh tuple and throws dict
            scores = _parse_scores(req.POST.items())

            # Iterate through pgh tuples and score dict
            for pgh, throws in scores.items():
                # Realize objects from pgh
                player_id, game_id, coursehole_id = pgh[0], pgh[1], pgh[2]

                # Create gamehole object
                try:
                    gh = GameHole.objects.get(
                        player__id=player_id,
                        game__id=game_id,
                        coursehole__id=coursehole_id)
                except GameHole.DoesNotExist:
                    gh = GameHole()
                    gh.player_id = player_id
                    gh.game_id = game_id
                    gh.coursehole_id = coursehole_id

                # Set throws if present
                if "throws" in throws:
                    gh.throws = throws["throws"]
                else:
                    gh.throws = 0

                # Set ob throws in present
                if "ob_throws" in throws:
                    gh.ob_throws = throws["ob_throws"]
                else:
                    gh.ob_throws = 0

                gh.save()

        return HttpResponseRedirect(reverse("golfstats-games-games-play",
            args=[game.id]))

    data = {
        "game": game,
    }
    return render(req, "games/game_play.html", data)