Exemple #1
0
async def join(
    game: GameBaseJoin,
    session: Session = Depends(get_session),
    user: UserBaseSession = Depends(JWTBearer())
) -> Message:
    """Join game request"""
    TokenValidator.check_token(session, user.id)
    # Check if user is in another game
    db_game = ControllerGame.get_by_username(session, user.username)
    if db_game is not None:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User is in another game.")
    # Fetch game object
    db_game = ControllerGame.get_by_id(session, game.id)
    # Check if given game id is valid
    if db_game is None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Invalid game id.")
    # Check if game has open place for joining
    if db_game.second_user_id is not None:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Game is full.")
    # Check if game has password -> Verify password
    if db_game.password is not None:
        if game.password is None or Security.verify_pwd(game.password, db_game.password) is False:
            raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid password.")
    ControllerGame.join(session, game.id, user.id)
    return Message(detail="Joining is successful.")
Exemple #2
0
async def create(
    game: GameBaseCreate,
    session: Session = Depends(get_session),
    user: UserBaseSession = Depends(JWTBearer())
) -> GameBaseCreateResponse:
    """Create game request"""
    TokenValidator.check_token(session, user.id)
    # Check if user is in another game
    db_game = ControllerGame.get_by_username(session, user.username)
    if db_game is not None:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User is in another game.")
    if game.name is None:
        game.name = f"{user.username}'s Game"
    if game.password is not None:
        game.password = Security.get_pwd_hash(game.password)
    db_game = ControllerGame.create(session, game, user.id)
    return GameBaseCreateResponse(id=db_game.id)