예제 #1
0
def test_round_score_meld_bad_hand(app, patch_ws_messenger):  # pylint: disable=unused-argument
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/score_hand_meld' page is requested (POST)
    THEN check that the response is successful
    """
    game_id, round_id, team_ids, player_ids = test_utils.setup_complete_game(4)

    # FIXME: This is pinochle-specific and should be moved to the play_pinochle test.
    play_pinochle.start(round_id=round_id)
    player_id = choice(player_ids)
    # Choose another player so that the cards don't match exactly.
    temp_player_id = choice([x for x in player_ids if x != player_id])
    hand_id = test_utils.query_player_hand_id(player_id=temp_player_id)
    temp_cards = [item.card for item in utils.query_hand_list(hand_id)]
    print(f"round_id={round_id}, player_id={player_id}")
    print(f"player_cards={temp_cards}")
    print(f"temp_cards= {','.join(temp_cards)}")
    cards_str = ",".join(temp_cards)
    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.get(
            f"/api/round/{round_id}/score_meld?player_id={player_id}&cards={cards_str}"
        )
        assert response.status == "409 CONFLICT"
예제 #2
0
def test_round_score_meld_hand_no_trump(app, patch_ws_messenger):  # pylint: disable=unused-argument
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/score_hand_meld' page is requested (POST)
    THEN check that the response is successful
    """
    game_id, round_id, team_ids, player_ids = test_utils.setup_complete_game(4)

    # FIXME: This is pinochle-specific and should be moved to the play_pinochle test.
    play_pinochle.start(round_id=round_id)
    player_id = choice(player_ids)
    hand_id = test_utils.query_player_hand_id(player_id=player_id)
    temp_cards = [item.card for item in utils.query_hand_list(hand_id)]
    print(f"round_id={round_id}, player_id={player_id}")
    print(f"player_cards={temp_cards}")
    print(f"temp_cards= {','.join(temp_cards)}")
    cards_str = ",".join(temp_cards)
    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.get(
            f"/api/round/{round_id}/score_meld?player_id={player_id}&cards={cards_str}"
        )
        assert response.status == "200 OK"
        response_str = response.get_data(as_text=True)
        response_data = json.loads(response_str)
        score = response_data.get("score")
        assert "score" in response_str
        assert isinstance(score, int)
        print(f"score={score}")

    # Verify database agrees.
    updated_player = utils.query_player(player_id=player_id)
    print(f"updated_player={updated_player}")
    assert updated_player.meld_score == score
예제 #3
0
def test_deal_to_players_w_kitty(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/kitty' page is requested (GET)
    THEN check that the response contains the expected information
    """
    game_id, round_id, team_ids, player_ids = test_utils.setup_complete_game(4)

    # Retrieve the round's kitty hand
    round_info = utils.query_round(round_id=round_id)
    kitty_hand = str(round_info.hand_id)

    # Deal the cards
    play_pinochle.deal_pinochle(player_ids=player_ids, kitty_len=4, kitty_id=kitty_hand)

    # Inspect the results.
    kitty_cards = utils.query_hand_list(kitty_hand)
    assert len(kitty_cards) == 4
    for __, p_id in enumerate(player_ids):
        temp_player = utils.query_player(p_id)
        cards = utils.query_hand_list(temp_player.hand_id)
        assert len(cards) == 11
예제 #4
0
def _play_single_trick(
    round_id: str, player_ids: List[str], player_hand_ids: Dict[str, str]
) -> Tuple[Dict[str, str], List[str], List[str]]:
    """
    Play a single trick. Each player submits a card to the trick deck. Determination of the winner is performed by the caller.

    :param round_id: Round ID of the round being played.
    :type round_id: str
    :param player_ids: List of Player IDs playing the round.
    :type player_ids: List[str]
    :param player_hand_ids: Correlation of player IDs and their hand IDs.
    :type player_hand_ids: Dict[str, str]
    :return: Data needed to continue processing: player_card_dict, ordered_player_id_list, card_list
    :rtype: Tuple[Dict[str, str], List[str], List[str]]
    """
    # Request the ordered list of players for this round.
    ordered_player_id_list = play_pinochle.reorder_players(
        str(round_id),
        str(utils.query_trick_for_round_id(round_id).trick_starter))

    # Create a temporary list of 'cards' to keep track of played cards ourselves.
    player_list_len = len(player_ids)
    card_list = ["blank" for x in range(player_list_len)]
    # Same with the card <-> Player ID association
    player_card_dict = {}

    # Throw cards in a random player order. A new, temporary list of player_ids is
    # needed because shuffle does so in-place.
    t_player_ids = player_ids
    shuffle(t_player_ids)
    for p_id in t_player_ids:
        # Capture the *index* of the player in the ordered list so that it can be
        # sent as the UI does.
        t_player_idx = ordered_player_id_list.index(p_id)
        assert 0 <= t_player_idx < player_list_len
        # Choose a card from what remains in the player's hand.
        card = choice(utils.query_hand_list(player_hand_ids[p_id])).card
        play_pinochle.play_trick_card(round_id, p_id, card)
        # Track the trick information locally.
        card_list[t_player_idx] = card
        player_card_dict[p_id] = card

    return (
        player_card_dict,
        ordered_player_id_list,
        card_list,
    )
예제 #5
0
def test_play_trick_card_normal(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/kitty' page is requested (GET)
    THEN check that the response contains the expected information
    """
    game_id, round_id, team_ids, player_ids = test_utils.setup_complete_game(4)

    # Choose a random bid winner.
    winner_player_id = choice(player_ids)
    round_.update(round_id, {"bid_winner": winner_player_id})

    # Set trump for this round.
    round_trump = choice(SUITS).lower().rstrip("s")
    play_pinochle.set_trump(round_id, winner_player_id, round_trump)
    print(f"Trump for this round is: {round_trump}")

    # Create a trick for this round.
    t_trick, _ = trick.create(round_id)
    t_trick_id = str(t_trick["trick_id"])
    trick.update(
        t_trick_id,
        {"trick_starter": winner_player_id},
    )

    # Gather the hand ids for each player and team.
    player_names = {p_id: utils.query_player(p_id).name for p_id in player_ids}
    player_hand_ids = {
        p_id: test_utils.query_player_hand_id(p_id)
        for p_id in player_ids
    }
    team_hand_ids = {
        t_id: test_utils.query_team_hand_id(round_id, t_id)
        for t_id in team_ids
    }

    # Deal cards
    play_pinochle.deal_pinochle(player_ids)

    for p_h_id in player_hand_ids.values():
        assert len(utils.query_hand_list(p_h_id)) == 12
    for t_h_id in team_hand_ids.values():
        assert len(utils.query_hand_list(t_h_id)) == 0

    print("")
    print(f"First trick lead is: {player_names[winner_player_id]}")

    # "Play" tricks until players are out of cards.
    while len(utils.query_hand_list(player_hand_ids[winner_player_id])) > 0:
        t_trick_hand_id = str(utils.query_trick_for_round_id(round_id).hand_id)
        assert t_trick_hand_id
        # Play a single trick.
        (
            t_trick_player_card_dict,
            ordered_player_id_list,
            t_trick_card_list,
        ) = _play_single_trick(round_id, player_ids, player_hand_ids)

        # Determine trick winner via a different algorithm than test target
        t_trick_winner_id = _determine_trick_winner(
            t_trick_player_card_dict,
            round_trump,
            ordered_player_id_list,
            t_trick_card_list,
        )

        assert t_trick_winner_id
        assert t_trick_card_list == [
            x.card for x in utils.query_hand_list(t_trick_hand_id)
        ]

        # Retrieve updated data about the trick from the database.
        trick_winner = str(utils.query_trick(t_trick_id).trick_winner)
        assert trick_winner == t_trick_winner_id
        print(f"{player_names[trick_winner]} won this trick.")

        # Create new trick and start over.
        t_trick, _ = trick.create(round_id)
        t_trick_id = t_trick["trick_id"]
        trick.update(t_trick_id, {"trick_starter": trick_winner})
        winner_player_id = trick_winner
        print("")
        print(f"Next trick lead is: {player_names[winner_player_id]}")

    for t_h_id in team_hand_ids.values():
        assert len(utils.query_hand_list(t_h_id)) >= 0