Exemple #1
0
	def get_game_history(self, request):
		"""Get all moves in game"""

		history = ndb.Key('Game', request.game).get().game_history
		q = Board.query(ancestor=ndb.Key('Game', request.game))

		return multiBoard(items=[self._printBoard(board) for board in q])
Exemple #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))
Exemple #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 ')
Exemple #4
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))