Exemplo n.º 1
0
def create_new_game(
        room_name: str,
        preferences: Optional[GamePreferences],
        game_mapper: GameEventMapper = Depends(get_game_mapper),
        db: Session = Depends(get_db),
):
    if game_mapper.get_game(room_name) is not None:
        raise HTTPException(403, "Room already exist.")
    if preferences is None:
        preferences = GamePreferences.default()
    logger.info(f"Creating new room with preferences: {preferences}")
    try:
        new_game = game_mapper.new_game(room_name, db,
                                        preferences).session.dict()
        return new_game
    except ValueError:
        logger.error(f"User tried to start game, but not enough decks.")
        raise HTTPException(
            status_code=409,
            detail=f"Please select more decks, "
            f"the selected decks alone "
            f"do not contain enough cards for a proper game",
        )
    except Exception as e:
        raise HTTPException(status_code=500,
                            detail=f"Something went wrong : {e}")
Exemplo n.º 2
0
def test_game_session_creation_and_user_assignment(
    database_connection, prefill_cards_to_database
):
    prefill_cards_to_database()
    db = database_connection
    sess = GameFactory.new_session("test", db, GamePreferences.default())
    assert len(sess.players) == 0
    assert len(sess.black_cards) == 45
    assert len(sess.white_cards) > 15

    sess.player_join("Joe")
    sess.player_join("David")

    assert len(sess.players) == 2

    player_names = [p.username for p in sess.players]
    assert "Joe" in player_names
    assert "David" in player_names

    for p in sess.players:
        assert len(p.cards_in_hand) == CARDS_IN_PLAYER_HAND

    sess.player_leaves("David")
    assert len(sess.players) == 1
    player_names = [p.username for p in sess.players]
    assert "Joe" in player_names
Exemplo n.º 3
0
def default_game_with_3_players(database_connection, prefill_cards_to_database):
    prefill_cards_to_database()
    db = database_connection
    sess = GameFactory.new_session("test", db, GamePreferences.default())
    ### STARTING GAME PHASE ######
    with pytest.raises(InvalidPlayerAction):
        sess.start_game()
    assert sess.state == GameStates.STARTING

    sess.player_join("Joe")
    sess.player_join("David")
    sess.player_join("Peter")
    yield sess
Exemplo n.º 4
0
def test_game_starts_only_with_enough_players(
    database_connection, prefill_cards_to_database
):
    prefill_cards_to_database()
    db = database_connection
    sess: GameStateMachine = GameFactory.new_session(
        "test", db, GamePreferences.default()
    )
    ### STARTING GAME PHASE ######
    with pytest.raises(InvalidPlayerAction):
        sess.start_game()
    assert sess.state == GameStates.STARTING

    sess.player_join("Joe")
    sess.player_join("David")
    sess.player_join("Peter")

    sess.start_game()
    assert sess.state == GameStates.PLAYERS_SUBMITTING_CARDS

    assert sess.currently_active_card.pick is not None

    ### TZAR ELECTION ###
    tzars = [
        p for p in sess.players if p.current_role == CardsAgainstHumanityRoles.TZAR
    ]
    assert len(tzars) == 1
    first_tzar = tzars[0]

    everyone_else: List[CardsAgainstHumanityPlayer] = [
        p for p in sess.players if p.current_role == CardsAgainstHumanityRoles.PLAYER
    ]

    ### ASSERTING PLAYER CARD ASSIGNMENT ###
    active_card_picks = sess.currently_active_card.pick
    for player in everyone_else:
        assert len(player.cards_in_hand) == CARDS_IN_PLAYER_HAND
        sess.player_submit_card(
            PlayerSubmitCards(
                submitting_user=player.username,
                cards=player.cards_in_hand[0:active_card_picks],
            )
        )
        assert len(player.cards_in_hand) == CARDS_IN_PLAYER_HAND - active_card_picks

    ### TZAR IS CHOSING THE WINNER ###
    assert sess.state == GameStates.TZAR_CHOOSING_WINNER
    the_winner_player: CardsAgainstHumanityPlayer = everyone_else[0]
    winning_submission = the_winner_player.submissions[-1]

    sess.select_winner(
        sender_name=sess.tzar.username,
        winner=SelectWinningSubmission(submission=winning_submission),
    )

    ### CHECK AWARDED POINTS ###
    for p in everyone_else:
        if p != the_winner_player:
            assert p.points == 0
        else:
            assert p.points == 1

    sess.advance_after_voting()
    new_tzars = [
        p for p in sess.players if p.current_role == CardsAgainstHumanityRoles.TZAR
    ]
    assert len(new_tzars) == 1, "must be only 1 tzar!"
    assert (
        new_tzars[0] != first_tzar
    ), "The same player cannot be tzar two times in a row"