Пример #1
0
    def join_game(self, request):
        """One of the players, creates the game and gets the game-id and gives that ID
        to the other player in order to play between each other"""
        player = Player.query(Player.name == request.player_name).get()
        print player
        if not player:
            raise endpoints.NotFoundException(
                'A Player with that name does not exist!, '
                'we need a second player in order to join the game')
        try:
            game = gameutils.get_by_urlsafe(request.urlsafe_key, Game)
            game.player2 = player.key
            game.put()
        except ValueError:
            raise endpoints.BadRequestException(
                'please verify the information '
                'of the second player')

        # Use a task queue to update the average attempts remaining.
        # This operation is not needed to complete the creation of a new game
        # so it is performed out of sequence.

        return game.to_form(
            'Second Player Joined the Game, we are ready to start the game!',
            player.name)
Пример #2
0
    def assign_ship_on_board(self, request):
        """One of the players, tries to assing one boat to his board game"""

        player = Player.query(Player.name == request.player_name).get()
        """we validate that the player is in the Data Base"""
        if not player:
            raise endpoints.NotFoundException('player not found')

        game = gameutils.get_by_urlsafe(request.urlsafe_key, Game)
        """we validate that the game where we want to create the board exists"""
        if not game:
            raise endpoints.NotFoundException(
                'Game not found in the DB, please start a new game')

        board = Board.query(Board.key == player.board).get()
        """we validate that the board where we want to create the board exists"""
        if not board:
            raise endpoints.NotFoundException('board not found')
        """we validate that the board of the player is active, the player can't create
            multiple boards for the same Game"""
        if not player.board and not player.board_active:
            raise endpoints.ConflictException(
                'This player has already an empty board have already a board')

        if player.board != board.key:
            raise endpoints.ConflictException(
                'the board for this player is not the proper')

        if gameutils.valid_positions(request.start_x_position,
                                     request.start_y_position,
                                     SHIPS_IDS[request.ship_id],
                                     request.orientation):
            raise endpoints.BadRequestException(
                'Please verify the position that you choose for the boat')
        """Here we check if the boat sent
        in the request is already active in the board"""
        if gameutils.check_if_boat_is_active(request.ship_id, board):
            raise endpoints.BadRequestException(
                'Please the selected boat that you sent '
                'in the request is already active in the board')

        if gameutils.place_boat_in_board(board, request.start_x_position,
                                         request.start_y_position,
                                         request.ship_id, request.orientation):
            raise endpoints.BadRequestException(
                'The place for the boat is not available, '
                'Please verify the position that you choose for the boat')

        try:
            gameutils.log_board_on_console(board)
            board.put()
        except ValueError:
            raise endpoints.BadRequestException(
                'please verify the information ')

        return StringMessage(
            message='Boat Assigned!'.format(request.player_name))
Пример #3
0
    def make_move(self, request):
        """One of the players, tries to hit the opponent boat"""

        player = Player.query(Player.name == request.player_name).get()
        """we validate that the player is in the Data Base"""
        if not player:
            raise endpoints.NotFoundException('player not found')

        game = gameutils.get_by_urlsafe(request.urlsafe_key, Game)
        """we validate that the game where we want to create the board exists"""
        if not game:
            raise endpoints.NotFoundException(
                'Game not found in the DB, please start a new game')

        board = Board.query(Board.key == player.board).get()
        """we validate that the board where we want to create the board exists"""
        if not board:
            raise endpoints.NotFoundException('board not found')
        """we validate that the board of the player is active, the player can't create
            multiple boards for the same Game"""
        if not player.board and not player.board_active:
            raise endpoints.ConflictException(
                'This player has already an empty board have already a board')

        if player.board != board.key:
            raise endpoints.ConflictException(
                'the board for this player is not the proper')

        if not gameutils.valid_target_pointed(request.x_position,
                                              request.y_position):
            raise endpoints.ConflictException(
                'the targeted position is not ok')

        try:
            result = gameutils.search_in_board(board, request.x_position,
                                               request.y_position)

            if result == "error":
                raise endpoints.ConflictException(
                    'there is a problem with the BOARD')
            else:
                score = Score.query(Score.player_name == player.name,
                                    Score.board == board, Score.game == game)
                board.add_target(request.x_position, request.y_position)
                game.add_move_to_history(request.x_position,
                                         request.y_position, player.name)
                message = score.target_hitted(player, request.x_position,
                                              request.y_position, board, game)
                if score.check_if_win():
                    message = "You sunk the last Boat, you win!!!"
                    board.deactivate()
                return StringMessage(message=message)

        except ValueError:
            raise endpoints.BadRequestException(
                'please verify the information ')
Пример #4
0
    def create_empty_board(self, request):
        """One of the players, creates the game and gets the game-id and gives that ID
        to the other player in order to play between each other"""
        player = Player.query(Player.name == request.player_name).get()
        game = gameutils.get_by_urlsafe(request.urlsafe_key, Game)
        """ HERE WE START THE PROPER VALIDATIONS FOR THIS ENDPOINT"""
        """we validate that the player is in the Data Base"""
        if not player:
            raise endpoints.NotFoundException(
                'A Player with that name does not exist!, '
                'we need a second player in order to join the game')
        """we validate that the game where we want to create the board exists"""
        if not game:
            raise endpoints.NotFoundException(
                'Game not found in the DB, please start a new game')
        """we validate that the game where we want to create the board is not Over"""
        if not game.game_over == False:
            raise endpoints.ConflictException('Game is over')
        """we validate that the board of the player is active, the player can't create
                    multiple boards for the same Game"""
        if player.board and player.board_active:
            raise endpoints.ConflictException(
                'This player has already an empty board have already a board')

        try:
            board = Board.new_board(player, Board.create_empty_board(), game)
            player.board_active = True
            player.board = board.key
            player.put()
            board.put()
        except ValueError:
            raise endpoints.BadRequestException(
                'please verify the information '
                'of the board')

        # Use a task queue to update the average attempts remaining.
        # This operation is not needed to complete the creation of a new game
        # so it is performed out of sequence.

        return board.to_form("Board created", player.name,
                             board.aircraft_carrier, board.battleship,
                             board.submarine, board.destroyer,
                             board.patrol_boat)
Пример #5
0
    def get_board(self, request):
        """One of the players, creates the game and gets the game-id and gives that ID
        to the other player in order to play between each other"""
        try:
            player = Player.query(Player.name == request.player_name).get()
            game = gameutils.get_by_urlsafe(request.urlsafe_key, Game)
            board = Board.query(Board.player == player.key
                                and Board.game == game.key).get()

            if not board:
                raise endpoints.NotFoundException(
                    'The Players Board for the selected game is not found')
            gameutils.log_board_on_console(board)
        except ValueError:
            raise endpoints.BadRequestException(
                'please verify the information '
                'of the second player')

        # Use a task queue to update the average attempts remaining.
        # This operation is not needed to complete the creation of a new game
        # so it is performed out of sequence.

        return StringMessage(message='Board Found and printed in the console'.
                             format(request.player_name))