def test_full_game(basic_game_setup_with_3_players):
    room, p1, p2, p3 = basic_game_setup_with_3_players()
    room: GameEventProcessor
    p1: GameTestClient
    p2: GameTestClient
    p3: GameTestClient

    assert len(room.session.players) == 3
    p1viewjson = p1.refresh().json()
    p1view = GameStatePlayerView(**p1viewjson)
    p2view = GameStatePlayerView(**p2.refresh().json())
    p3view = GameStatePlayerView(**p3.refresh().json())

    view_client = {
        p1view: p1,
        p2view: p2,
        p3view: p3,
    }

    client_view = {
        p1: p1view,
        p2: p2view,
        p3: p3view,
    }

    tzar = [
        p for p in [p1view, p2view, p3view]
        if p.player.current_role == CardsAgainstHumanityRoles.TZAR
    ][0]
    tzar_client = view_client[tzar]
    player_clients: List[GameTestClient] = [p1, p2, p3]
    player_clients.remove(tzar_client)
    for pc in player_clients:
        pc: GameTestClient
        view: GameStatePlayerView = client_view[pc]
        pc.submit(view.player.cards_in_hand[0:view.currently_active_card.pick])

    tzar_view_after_submission = GameStatePlayerView(
        **tzar_client.refresh().json())

    assert tzar_view_after_submission.state == GameStates.TZAR_CHOOSING_WINNER

    winner_name = list(tzar_view_after_submission.player_submissions.keys())[0]
    tzar_client.select_winner(
        SelectWinningSubmission(submission=Submission(
            **tzar_view_after_submission.player_submissions[winner_name].dict(
            ))))

    result = tzar_client.advance()
    assert result.status_code == 200, "Advancing game should happened"

    view_after_winner_select = GameStatePlayerView(
        **tzar_client.refresh().json())
    assert view_after_winner_select.state == GameStates.PLAYERS_SUBMITTING_CARDS
    assert (
        view_after_winner_select.player.current_role !=
        CardsAgainstHumanityRoles.TZAR), "Same player cannot be a tzar again."
Beispiel #2
0
async def advance_from_voting(
        room_name: str,
        game_mapper: GameEventMapper = Depends(get_game_mapper),
        token: str = Depends(JWTBearer()),
        conman: ConnectionManager = Depends(get_websocket_connection_manager),
):
    user = decodeJWT(token)
    if user is None:
        raise HTTPException(404, "User not found with this token.")
    room = game_mapper.get_game(room_name)
    if room is None:
        raise HTTPException(404, "Room does not exist.")
    if user.user_id not in room.session.player_lookup.keys():
        raise HTTPException(404, "User not in the game room.")
    if room.session.state == GameStates.PLAYERS_INSPECTING_RESULT:
        room.on_new_event(AdvanceRoundAfterVoting(), user.user_id)
    else:
        raise HTTPException(400, "You can't do that in this game state.")

    await broadcast_event(
        room_name,
        conman,
        f"{user.user_id} has started a new round!",
    )
    return GameStatePlayerView.from_game_state(room.session, user.user_id)
Beispiel #3
0
async def select_winner(
        room_name: str,
        winner: SelectWinningSubmission,
        game_mapper: GameEventMapper = Depends(get_game_mapper),
        token: str = Depends(JWTBearer()),
        conman: ConnectionManager = Depends(get_websocket_connection_manager),
):
    user = decodeJWT(token)
    if user is None:
        raise HTTPException(404, "User not found with this token.")
    room = game_mapper.get_game(room_name)
    if room is None:
        raise HTTPException(404, "Room does not exist.")

    try:
        room.on_new_event(winner, user.user_id)
    except GameHasEnded as e:
        pass  # It's fine. Just proceed.
    # TODO: event broadcast shall be triggered somehow inside from the SM
    await broadcast_event(
        room_name,
        conman,
        f"{user.user_id} voted for the winner",
    )

    return GameStatePlayerView.from_game_state(room.session, user.user_id)
    def _game_and_clients():
        prefill_cards_to_database()
        mapper = get_clean_game_mapper
        room_name = "test_room"
        client1 = GameTestClient(valid_user_token("user1"),
                                 test_client,
                                 room_name=room_name)
        client2 = GameTestClient(valid_user_token("user2"),
                                 test_client,
                                 room_name=room_name)
        client3 = GameTestClient(valid_user_token("user3"),
                                 test_client,
                                 room_name=room_name)

        new_game_response = client1.new_game()

        assert new_game_response.status_code == status.HTTP_200_OK
        assert client1.player_join().status_code == status.HTTP_200_OK
        assert client2.player_join().status_code == status.HTTP_200_OK
        assert client3.player_join().status_code == status.HTTP_200_OK
        assert mapper.get_game(room_name) is not None

        start_response = client1.start_game()
        assert start_response.status_code == status.HTTP_200_OK, start_response.content
        assert (GameStatePlayerView(**start_response.json()).state ==
                GameStates.PLAYERS_SUBMITTING_CARDS)
        return mapper.get_game(room_name), client1, client2, client3
Beispiel #5
0
def refresh(
        room_name: str,
        game_mapper: GameEventMapper = Depends(get_game_mapper),
        token: str = Depends(JWTBearer()),
):
    user = decodeJWT(token)
    if user is None:
        raise HTTPException(404, "User not found with this token.")
    room = game_mapper.get_game(room_name)
    if room is None:
        raise HTTPException(404, "Room does not exist.")
    if user.user_id not in room.session.player_lookup.keys():
        raise HTTPException(404, "User not in the game room.")

    return GameStatePlayerView.from_game_state(room.session, user.user_id)
Beispiel #6
0
async def join_game(
        room_name: str,
        game_mapper: GameEventMapper = Depends(get_game_mapper),
        token: str = Depends(JWTBearer()),
        conman: ConnectionManager = Depends(get_websocket_connection_manager),
):
    user = decodeJWT(token)
    if user is None:
        raise HTTPException(404, "User not found with this token.")
    room = game_mapper.get_game(room_name)
    if room is None:
        raise HTTPException(404, "Room does not exist.")

    room.session.player_join(user.user_id)

    await broadcast_event(room_name, conman,
                          f"{user.user_id} has joined the game!")

    return GameStatePlayerView.from_game_state(room.session, user.user_id)
Beispiel #7
0
async def submit_cards(
        room_name: str,
        cards: List[WhiteCard],
        game_mapper: GameEventMapper = Depends(get_game_mapper),
        token: str = Depends(JWTBearer()),
        conman: ConnectionManager = Depends(get_websocket_connection_manager),
):
    user = decodeJWT(token)
    if user is None:
        raise HTTPException(404, "User not found with this token.")
    room = game_mapper.get_game(room_name)
    if room is None:
        raise HTTPException(404, "Room does not exist.")

    room.on_new_event(
        PlayerSubmitCards(submitting_user=user.user_id, cards=cards),
        user.user_id,
    )

    await broadcast_event(room_name, conman,
                          f"{user.user_id} has submitted cards!")

    return GameStatePlayerView.from_game_state(room.session, user.user_id)