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}")
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)
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)
async def leave_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.") # This kinda deserves a test: if room.session.state != GameStates.FINISHED: room.session.player_leaves(user.user_id) if len(room.session.players) == 0: game_mapper.end_game(room_name) await broadcast_event(room_name, conman, f"{user.user_id} has left the game!") return LeaveResponse()
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)
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)
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)
from fast_cards.internal.cards_against_humanity_rules.game_event_processor import ( GameEventMapper, ) from fast_cards.users.crud import get_user_by_username from fast_cards.websocket.connection_manager import ConnectionManager def get_db(): db = SessionLocal() try: yield db finally: db.close() manager = ConnectionManager() game_mapper = GameEventMapper() def get_game_mapper() -> Iterator[GameEventMapper]: yield game_mapper def get_websocket_connection_manager() -> Iterator[ConnectionManager]: yield manager async def get_token_header(x_token: str = Header(...), db=Depends(get_db)): token = decodeJWT(x_token) if token is None: raise HTTPException(status_code=400, detail="X-Token header invalid") user = get_user_by_username(db, token.user_id)