当前位置: 首页>>代码示例>>Python>>正文


Python Move.allocate_ids方法代码示例

本文整理汇总了Python中models.Move.allocate_ids方法的典型用法代码示例。如果您正苦于以下问题:Python Move.allocate_ids方法的具体用法?Python Move.allocate_ids怎么用?Python Move.allocate_ids使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Move的用法示例。


在下文中一共展示了Move.allocate_ids方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: makeMove

# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import allocate_ids [as 别名]
    def makeMove(self, request):
        """
        authenticated player makes a move, implemented by creating a move,
         updating Game and Player
        """
        g_key = ndb.Key(urlsafe=request.websafeGameKey)
        game = g_key.get()

        # set up for a move
        m_id = Move.allocate_ids(size=1, parent=g_key)[0]
        m_key = ndb.Key(Move, m_id, parent=g_key)

        player = Player.query(Player.displayName == request.player_name).get()
        next_player_name = game.playerTwo if player.displayName == game.playerOne else game.playerOne
        next_player = Player.query(Player.displayName == next_player_name).get()
        print('next_player', next_player_name)
        if game.gameOver:
            raise endpoints.UnauthorizedException('This game is already over.')
        # check if game is signed up by two players and by the current player
        elif game.playerOne is None or game.playerTwo is None:
            raise endpoints.UnauthorizedException(
                'Need 2 signed-up players to start')
        elif not player:
            raise endpoints.NotFoundException('no player found')
        elif player.displayName not in [game.playerTwo, game.playerOne]:
            raise endpoints.UnauthorizedException('You did not sign up')
        # check if the player had played the last move
        elif player.displayName != game.nextPlayer is not None:
            raise endpoints.UnauthorizedException(
                "Player {}, it is your opponent {}'s turn" .format(
                    player.displayName, game.nextPlayer))
        elif game.board[request.positionTaken] != '':
            raise endpoints.UnauthorizedException('The game board position {} \
             is already taken' .format(request.positionTaken))
        else:
            data = {}
            data['key'] = m_key
            data['moveNumber'] = game.gameCurrentMove + 1
            data['playerName'] = player.displayName
            data['positionTaken'] = request.positionTaken
            Move(**data).put()
            move = m_key.get()

            # Update Game on the position affected by the move, and player
            game.gameCurrentMove += 1
            game.board[request.positionTaken] = request.player_name
            setattr(game, 'nextPlayer', next_player_name)
            if game._isWon:
                setattr(game, 'gameWinner', getattr(player,'displayName'))
            # if no win and game.gameCurrentMove >= 9, it is a tie;
            # declare the winner as 'tie'
            if game.gameCurrentMove > 9:
                setattr(game, 'gameWinner', 'tie')
            if game._isWon or game.gameCurrentMove > 9:
                setattr(game, 'gameOver', True)
                player.gamesInProgress.remove(g_key.urlsafe())
                player.gamesCompleted.append(g_key.urlsafe())
                next_player.gamesInProgress.remove(g_key.urlsafe())
                next_player.gamesCompleted.append(g_key.urlsafe())
            # if no winner at step before 9, send a move invite to the opponent
            game.put()
            player.put()
            next_player.put()

        return game._copyGameToForm
开发者ID:zhangtreefish,项目名称:Project-4-for-Full-Stack-Nanodegree-at-Udacity,代码行数:67,代码来源:api.py


注:本文中的models.Move.allocate_ids方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。