Пример #1
0
class SolitaireAPI(remote.Service):

    @endpoints.method(request_message=USER_REQUEST,
                      response_message=StringMessage,
                      path='user',
                      name='create_user',
                      http_method='POST')
    def create_user(self, request):
        """Create a new user"""

        # Check that user already exists
        if User.query(User.user_name == request.user_name).get():
            raise endpoints.ConflictException(
                "A user with that name already exists")
        # Create a new user
        user = User(user_name=request.user_name, email=request.email)
        user.put()

        return StringMessage(
            message="User {} created!".format(request.user_name))

    @endpoints.method(request_message=NEW_GAME_REQUEST,
                      response_message=GameForm,
                      path='game',
                      name='new_game',
                      http_method='POST')
    def new_game(self, request):
        """Create a new game"""
        user = User.query(User.user_name == request.user_name).get()
        if not user:
            raise endpoints.NotFoundException(
                "A user with that name does not exist")

        game = SolitaireGame(None, None, None, None, False)
        game.new_game()

        # Convert card stacks to JSON
        game_json = to_json(game)
        try:
            game_db = Game.new_game(user=user.key,
                                    piles=game_json['piles'],
                                    foundations=game_json['foundations'],
                                    deck=game_json['deck'],
                                    open_deck=game_json['open_deck'])
        except Exception, e:
            logging.error(str(e))

        # Save game history
        GameHistory.new_history(game=game_db.key,
                                sequence=game_db.moves,
                                game_over=game_db.game_over,
                                piles=game_db.piles,
                                foundations=game_db.foundations,
                                deck=game_db.deck,
                                open_deck=game_db.open_deck)

        return game_db.to_form('New game created!')
Пример #2
0
    def make_move(self, request):
        """Make a move"""
        action = request.action
        origin = request.origin
        destination = request.destination
        card_position = request.card_position

        # If action is MOVE, the source and destination
        # field cannot be empty
        if action == Action.MOVE:
            if not origin or not destination:
                raise endpoints.BadRequestException(
                    'Souce and Destination must not be empty for MOVE action')

            if not card_position:
                card_position = -1

        # If action is SHOW, the source field cannot be empty
        if action == Action.SHOW:
            if not origin:
                raise endpoints.BadRequestException(
                    'Souce must not be empty for SHOW action')

        # Load the game from DB
        game_db = get_by_urlsafe(request.urlsafe_game_key, Game)
        if not game_db:
            raise endpoints.NotFoundException("The Game does not exist")

        # If game is over, copy to form and return
        if game_db.game_over:
            return game_db.to_form('Game already over')

        # Create Python game object
        game = to_python(piles=game_db.piles,
                         foundations=game_db.foundations,
                         deck=game_db.deck,
                         open_deck=game_db.open_deck,
                         game_over=game_db.game_over)

        # Make the move

        # To track whether any cards are moved or upturned

        changed = False
        if action == Action.DEAL:
            game.deal()
            changed = True
            game_db.moves += 1
            game.print_game()

        if action == Action.MOVE:
            changed = game.move(origin=str(origin),
                                destination=str(destination),
                                card_position=card_position)
            if changed:
                game_db.moves += 1
            else:
                game.print_game()
                raise endpoints.BadRequestException("Illigal move. Try again.")

            game.print_game()

        if action == Action.SHOW:
            changed = game.show_top(str(origin))

            if changed:
                game_db.moves += 1
            else:
                game.print_game()
                raise endpoints.BadRequestException('Could not show the card.')

            game.print_game()

        # Convert the game to JSON format
        game_json = to_json(game)

        # If changed, update the fields and save in DB
        if changed:
            game_db.piles = game_json['piles']
            game_db.foundations = game_json['foundations']
            game_db.deck = game_json['deck']
            game_db.open_deck = game_json['open_deck']
            game_db.game_over = game_json['game_over']

            game_db.put()

            GameHistory.new_history(game=game_db.key,
                                    sequence=game_db.moves,
                                    game_over=game_db.game_over,
                                    piles=game_db.piles,
                                    foundations=game_db.foundations,
                                    deck=game_db.deck,
                                    open_deck=game_db.open_deck)

        if game_db.game_over:
            game_db.save_game()

        return game_db.to_form('Made a move')