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
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}")
def test_player_select_winner_behaviour(database_connection, prefill_official_cards_to_database): """ room_name: str, round_count: int, db: Session, preferences: GamePreferences, :return: """ prefill_official_cards_to_database() sm = GameFactory.new_session( room_name="test room", db=database_connection, preferences=GamePreferences.god_is_dead_mode(), ) assert isinstance(sm, GodIsDeadModeStateMachine) players = ["P1", "P2", "P3"] for p in players: sm.player_join(p) assert len(sm.players) == 3 assert sm.tzar() is None sm.start_game() with pytest.raises(GameHasEnded): for i in range(15): to_pick = sm.currently_active_card.pick submissions = {} for p in players: cards_to_submit = sm.player_lookup[p].cards_in_hand[0:to_pick] submissions[p] = cards_to_submit sm.player_submit_card( PlayerSubmitCards(submitting_user=p, cards=cards_to_submit)) for p in players: sub = Submission( black_card=sm.currently_active_card, white_cards=submissions[list(submissions.keys())[0]], ) sws = SelectWinningSubmission(submission=sub) sm.select_winner( sender_name=p, winner=sws, ) sm.advance_after_voting() assert sm.player_lookup[players[0]].points == i + 1 assert sm.player_lookup[players[0]].points == 10 assert sm.player_lookup[players[1]].points == 0 assert sm.player_lookup[players[2]].points == 0
def test_player_advance_after_winner_raises_exception_when_submitted_invalidly( database_connection, prefill_official_cards_to_database): """ room_name: str, round_count: int, db: Session, preferences: GamePreferences, :return: """ prefill_official_cards_to_database() sm = GameFactory.new_session( room_name="test room", db=database_connection, preferences=GamePreferences.god_is_dead_mode(), ) assert isinstance(sm, GodIsDeadModeStateMachine) players = ["P1", "P2", "P3"] for p in players: sm.player_join(p) assert len(sm.players) == 3 assert sm.tzar() is None sm.start_game() to_pick = sm.currently_active_card.pick submissions = {} for p in players: cards_to_submit = sm.player_lookup[p].cards_in_hand[0:to_pick] submissions[p] = cards_to_submit sm.player_submit_card( PlayerSubmitCards(submitting_user=p, cards=cards_to_submit)) for p in players: sub = Submission( black_card=sm.currently_active_card, white_cards=submissions[list(submissions.keys())[0]], ) sws = SelectWinningSubmission(submission=sub) sm.select_winner( sender_name=p, winner=sws, ) # Only the last advance is valid if p != players[-1]: with pytest.raises(InvalidPlayerAction): print(f"{p} votes") sm.advance_after_voting() else: sm.advance_after_voting()
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
def test_game_creation_with_gidmode(valid_user_token, test_client, get_clean_game_mapper, prefill_cards_to_database): prefill_cards_to_database() mapper = get_clean_game_mapper room_name = "test_room" client1 = GameTestClient(valid_user_token("user1"), test_client, room_name) client2 = GameTestClient(valid_user_token("user2"), test_client, room_name) new_game_response = client1.new_game( {"preferences": GamePreferences.god_is_dead_mode().dict()}) 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 room = mapper.get_game(room_name) assert room is not None room.session.mode = GameModes.GOD_IS_DEAD
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"