Exemple #1
0
    def get_scorecard(self, request):
        """Returns the scorecard for a game."""
        game = get_by_urlsafe(request.urlsafe_game_key, Game)
        if not game:
            raise endpoints.NotFoundException('Game not found!')

        scorecard = Scorecard.query(Scorecard.game == game.key).get()
        if not scorecard:
          raise endpoints.NotFoundException('Scorecard not found!')

        return scorecard.to_form()
Exemple #2
0
    def score_turn(self, request):
        """
        Calculates the score for the Turn.
        Required Params: url safe key for the Turn and the
        scoring category.
        """

        # Get the turn
        turn = get_by_urlsafe(request.urlsafe_turn_key, Turn)
        if not turn:
            raise endpoints.NotFoundException('Turn not found!')

        if turn.is_complete:
            message = ('Turn {} is already scored!').format(
                request.urlsafe_turn_key)
            raise endpoints.ConflictException(message)

        # Get the game
        game = turn.game.get()

        # Get the score card for this game.
        scorecard = Scorecard.query(Scorecard.game == game.key).get()

        # Check that the category_type is one of the expected types.
        category_type = request.category_type
        print 'category_type:', category_type
        print scorecard.category_scores.keys()
        if str(category_type) not in scorecard.category_scores.keys():
            message = ('Category {} not found!').format(category_type)
            raise endpoints.ConflictException(message)


        # Check if there is already a score entered for the selected category.
        current_score = scorecard.category_scores[str(category_type)]

        """The YAHTZEE category is the only category which can be scored more than once.
           So check the category type and whether or not the category had already been scored.
        """
        if category_type is not 'YAHZTEE' and current_score > -1:
            message = ('{} category already contains a score.  Please select a different score category.').format(
                str(category_type))
            raise endpoints.ConflictException(message)

        # Calculate the score for this turn based on the category selected.
        score = scorecard.calculate_score_for_category(
            turn.dice, category_type)

        # Update the game history.
        entry = (str(category_type), score)
        # Add the entry to the game history.
        game.history[turn.number].append(entry)

        # Update the scorecard with the calculated score.
        scorecard.category_scores[str(category_type)] = score

        # Turn is now complete
        game.has_incomplete_turn = False
        turn.is_complete = True
        turn.put()

        # Save the updated scorecard values.
        scorecard.put()

        # Check to see if the game is over.
        game_over = scorecard.check_full()
        print 'game_over = ', game_over

        # If the game is now over, calculate the final score.
        if game_over:

            final_score = scorecard.calculate_final_score()
            print 'final score:', final_score

            # End the game
            game.game_over(final_score)

        # Save the changes made to game
        game.put()


        return scorecard.to_form()