Exemplo n.º 1
0
async def get_all_players(session: Session = Depends(
    dal.get_session)) -> List[player_schemas.Details]:
    """ Retreive multiple players records from DB """

    db_players = player_ops.query_players_by_(session).all()

    return db_players
Exemplo n.º 2
0
async def get_player(
    *, id: int, session: Session = Depends(dal.get_session)
) -> player_schemas.Details:
    """ Retrieve a player record from DB """
    conditions = and_(User.id == id, )

    db_player = player_ops.query_players_by_(session, conditions).first()

    if db_player is None:
        raise HTTPException(status.HTTP_404_NOT_FOUND,
                            detail="Player not found")

    return db_player
Exemplo n.º 3
0
def invite_player(
        *,
        player_id: int,
        db_player: User = Depends(auth_ops.get_current_user),
        session: Session = Depends(dal.get_session),
) -> player_schemas.Details:
    """ Invite player to join the team """

    db_team = db_player.team

    # Team doesn't exist
    if db_team is None:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail="You are not part of a team",
        )

    # The team is full
    if len(db_team.players) >= constants.MAX_TEAM_MEMBERS:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail="The team is full",
        )

    conditions = and_(User.id == player_id, )

    db_new_player = player_ops.query_players_by_(session, conditions).first()

    # Wrong player id
    if db_new_player is None:
        raise HTTPException(
            status.HTTP_404_NOT_FOUND,
            detail="Player not found",
        )

    # New player has already been invited
    if db_team in db_new_player.team_invites:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail=f"This player has already been invited",
        )

    # New player is part of another team
    if db_new_player.team is not None:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail="Player is already part of a team",
        )

    player_ops.invite_player(session, db_new_player, db_team)
Exemplo n.º 4
0
def remove_invitation(
        *,
        player_id: int,
        db_player: User = Depends(auth_ops.get_current_user),
        session: Session = Depends(dal.get_session),
) -> player_schemas.Details:
    """ Delete player invitation to join the team """

    db_team = db_player.team

    conditions = and_(User.id == player_id, )

    db_new_player = player_ops.query_players_by_(session, conditions).first()

    # Player not found
    if db_new_player is None:
        raise HTTPException(
            status.HTTP_404_NOT_FOUND,
            detail="Player not found",
        )

    # Team doesn't exist
    if db_team is None:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail="You are not part of a team",
        )

    # The team hasn't invited the player
    if db_new_player not in db_team.player_invites:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail=f"This player has not been invited",
        )

    player_ops.remove_invitation(session, db_new_player, db_team)