Beispiel #1
0
    def create_match(self, request):
        """ Creates a new match.

        This match allows players to create a new match. A match is an
        'instance' of a game. This allows multiple users to play the same
        game at the same time or different times.


        Returns:
            StringMessage -- a confirmation that the match has been created

        Raises:
            BadRequestException -- raised when game has no questions or when
                                   game is not in play mode or when play mode
                                   enabling is required, but was not requested
        """
        # Get game and player from datastore
        game = get_by_urlsafe(request.urlsafe_game_key, Game)
        player = get_player(request.user_name)

        # Check if game has questions. A game with no questions is not playable
        if game.questions == []:
            raise endpoints.BadRequestException(
                'The game has no questions yet and cannot be played.')

        # Check if game has been changed from editing to play mode
        if not game.play_mode:
            # Only game creators can put a game into play mode
            if player.key != game.creator:
                raise endpoints.BadRequestException(
                    'Only the game creator can put it into play mode.')
            else:
                # Check if creator has explicitly requested to start the game
                if not request.start_game:
                    raise endpoints.BadRequestException(
                        'Play mode enabling was not requested.')
                else:
                    # Put game into play mode and save the change
                    game.play_mode = True
                    game.put()

        # Create the match
        match = Match.create_match(player=player.key, game=game)

        # Return a confirmation of the match creation
        return match.to_form()