def save_turn_one_game_state(game, deck, player_one_hand):
        """Save the state of the game after player one has made a move.

        Args:
          game: current game the player is playing in.
          deck: the deck state after the player has drawn cards for the card_id
            exchange phase.
          hand: the final hand the player has after the desired cards have been
            replaced.
        """

        # Save player one's final hand

        hand = Poker.serialize_hand(player_one_hand)
        final_hand = Hand(
            player=game.player_one,
            game=game.key,
            hand=hand,
            state=str(HandState.ENDING)
        )
        final_hand.put()

        game.active_player = game.player_two
        game.deck = deck.serialize()
        game.put()
        taskqueue.add(
            url='/tasks/send_move_email',
            params={
                'game_key': game.key.urlsafe(),
                'user_key': game.active_player.urlsafe()
            },
            transactional=True
        )
    def new_game(player_one, player_two, game_id):
        """Creates and returns a new game.

        Args:
          player_one: A key representing player one.
          player_two: A key representing player two.
          game_id: A string representing a game_id for generating a Game.key.

        Returns:
          A game detailing the players, the active player, and the deck.
        """
        game_key = ndb.Key(Game, game_id)

        game = Game(
            key=game_key,
            player_one=player_one,
            player_two=player_two,
            active_player=player_one,
            game_over=False
        )
        deck = Deck()
        deck.shuffle()

        # Deal out each player's starting hand

        player_one_hand = Poker.serialize_hand(deck.draw(5))
        hand = Hand(
            player=player_one,
            game=game.key,
            hand=player_one_hand,
            state=str(HandState.STARTING)
        )
        hand.put()

        player_two_hand = Poker.serialize_hand(deck.draw(5))
        hand = Hand(
            player=player_two,
            game=game.key,
            hand=player_two_hand,
            state=str(HandState.STARTING)
        )
        hand.put()

        game.deck = deck.serialize()
        game.put()

        # Send email to active player signaling the start of the game

        taskqueue.add(
            url='/tasks/send_move_email',
            params={
                'game_key': game.key.urlsafe(),
                'user_key': game.active_player.urlsafe()
            },
            transactional=True
        )
        return game
    def save_turn_two_game_state(game, deck, player_two_hand, player_one_hand):
        """Save the state of the game after player two has made a move.

        This should signal the end of the game.

        Args:
          game: current game the player is playing in.
          deck: the deck state after the player has drawn cards for the card_id
            exchange phase.
          player_two_hand: the final hand player two has after the desired
            cards have been replaced.
          player_one_hand: player one's final hand.
        """

        # Save player two's final hand

        hand = Poker.serialize_hand(player_two_hand)
        final_hand = Hand(
            player=game.player_two,
            game=game.key,
            hand=hand,
            state=str(HandState.ENDING)
        )
        final_hand.put()

        # Check game outcome and send email to players with results.

        game_outcome = Poker.game_outcome(player_one_hand, player_two_hand)
        game.game_over = True
        game.active_player = None

        if game_outcome == 0:
            game.winner = None
        elif game_outcome == 1:
            game.winner = game.player_one
        else:
            game.winner = game.player_two
        game.deck = deck.serialize()
        game.put()
        taskqueue.add(
            url='/tasks/send_game_result_email',
            params={
                'game_key': game.key.urlsafe()
            },
            transactional=True
        )