コード例 #1
0
ファイル: servers.py プロジェクト: Natsku123/party-filler
def update_server(
        *,
        db: Session = Depends(deps.get_db),
        id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of server"),
        server: ServerUpdate,
        current_user: Player = Depends(deps.get_current_user),
) -> Any:
    db_server = crud.server.get(db=db, id=id)

    if db_server not in current_user.servers and not is_superuser(
            current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    if not db_server:
        raise HTTPException(status_code=404, detail="Server not found")

    if server.discord_id != db_server.discord_id:
        db_servers = crud.server.get_multi(
            db, filters={"discord_id": server.discord_id})

        if len(db_servers) > 0:
            raise HTTPException(status_code=400,
                                detail="Server already exists")

    db_server = crud.server.update(db=db, db_obj=db_server, obj_in=server)
    return db_server
コード例 #2
0
ファイル: players.py プロジェクト: Natsku123/party-filler
def get_is_superuser(
    *, current_user: Player = Depends(deps.get_current_user)) -> Any:
    if not current_user:
        raise HTTPException(status_code=401, detail="Not authorized")

    data = {"isSuperuser": is_superuser(current_user)}
    return schemas.IsSuperUser(**data)
コード例 #3
0
ファイル: parties.py プロジェクト: Natsku123/party-filler
def update_party(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of party"),
    party: PartyUpdate,
    current_user: Player = Depends(deps.get_current_user),
) -> Any:
    db_party = crud.party.get(db=db, id=id)

    if not db_party:
        raise HTTPException(status_code=404, detail="Party not found")

    if db_party.leader_id != current_user.id and not is_superuser(current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    leader = crud.player.get(db, party.leader_id)

    if not leader:
        raise HTTPException(status_code=404, detail="Leader not found")

    if party.game_id:
        game = crud.game.get(db, party.game_id)

        if not game:
            raise HTTPException(status_code=404, detail="Game not found")

    db_party = crud.party.update(db=db, db_obj=db_party, obj_in=party)
    return db_party
コード例 #4
0
ファイル: players.py プロジェクト: Natsku123/party-filler
def update_player(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of player"),
    current_user: Player = Depends(deps.get_current_user)
) -> Any:
    db_player = crud.player.get(db=db, id=id)

    if not db_player:
        raise HTTPException(status_code=404, detail="Player not found")

    if db_player.id != current_user.id and not is_superuser(current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    resp = main.oauth.discord.get("users/@me")
    profile = resp.json()

    if "code" not in profile:
        # Update player info
        player = PlayerUpdate(
            name=profile.get("username"),
            discriminator=profile.get("discriminator"),
            icon=profile.get("avatar"),
        )

        db_player = crud.player.update(db, db_obj=db_player, obj_in=player)

        # Get servers that Player uses
        guilds = main.oauth.discord.get("users/@me/guilds")

        # Create new servers if doesn't already exist or update existing
        for guild in guilds.json():
            server = crud.server.get_by_discord_id(db,
                                                   discord_id=guild.get("id"))

            if server is None:
                server_obj = ServerCreate(
                    name=guild.get("name"),
                    icon=guild.get("icon"),
                    discordId=guild.get("id"),
                )

                server = crud.server.create(db, obj_in=server_obj)
                db_player.servers.append(server)

            elif server.name != guild.get(
                    "name", server.name) or server.icon != guild.get(
                        "icon", server.icon):
                server_obj = ServerUpdate(
                    name=guild.get("name"),
                    icon=guild.get("icon"),
                    discordId=guild.get("id"),
                )

                server = crud.server.update(db,
                                            db_obj=server,
                                            obj_in=server_obj)

        db.commit()
        db.refresh(db_player)
コード例 #5
0
ファイル: members.py プロジェクト: Natsku123/party-filler
def create_member(
        *,
        db: Session = Depends(deps.get_db),
        member: MemberCreate,
        current_user: Player = Depends(deps.get_current_user),
        notify: bool = Query(False),
) -> Any:
    if not current_user or (current_user.id != member.player_id
                            and not is_superuser(current_user)):
        raise HTTPException(status_code=401, detail="Not authorized")

    member = crud.member.create(db, obj_in=member)

    # Send webhook to bot if notification wanted
    if notify and member.party.channel:
        webhook_data = {
            "member": member,
            "channel": member.party.channel,
            "event": {
                "name": "on_member_join",
                "timestamp": datetime_to_string(datetime.datetime.now()),
            },
        }
        webhook = schemas.MemberJoinWebhook.parse_obj(webhook_data)

        send_webhook.delay("http://bot:9080/webhook", webhook.json())

    if member.party.channel and len(
            member.party.members) == member.party.max_players:
        webhook_data = {
            "party": member.party,
            "event": {
                "name": "on_party_full"
            }
        }
        webhook = schemas.PartyFullWebhook.parse_obj(webhook_data)

        send_webhook.delay("http://bot:9080/webhook", webhook.json())

        crud.party.lock(db, crud.party.get(db, member.party_id))

    if (member.party.channel
            and len(member.party.members) == member.party.min_players
            and member.party.min_players != member.party.max_players):
        webhook_data = {
            "party": member.party,
            "event": {
                "name": "on_party_ready"
            }
        }
        webhook = schemas.PartyReadyWebhook.parse_obj(webhook_data)

        send_webhook.delay("http://bot:9080/webhook", webhook.json())

        crud.party.lock(db, crud.party.get(db, member.party_id))

    return member
コード例 #6
0
ファイル: servers.py プロジェクト: Natsku123/party-filler
def delete_server(
        *,
        db: Session = Depends(deps.get_db),
        id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of server"),
        current_user: Player = Depends(deps.get_current_user),
) -> Any:
    server = crud.server.get(db=db, id=id)

    if not server:
        raise HTTPException(status_code=404, detail="Server not found")

    if server not in current_user.servers and not is_superuser(current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    crud.server.remove(db=db, id=id)

    return server
コード例 #7
0
ファイル: parties.py プロジェクト: Natsku123/party-filler
def delete_party(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of party"),
    current_user: Player = Depends(deps.get_current_user),
) -> Any:
    party = crud.party.get(db=db, id=id)

    if not party:
        raise HTTPException(status_code=404, detail="Party not found")

    if party.leader_id != current_user.id and not is_superuser(current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    crud.party.remove(db=db, id=id)

    return party
コード例 #8
0
def update_game(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of game"),
    game: GameUpdate,
    current_user: Player = Depends(deps.get_current_user)
) -> Any:
    db_game = crud.game.get(db=db, id=id)

    if not db_game:
        raise HTTPException(status_code=404, detail="Game not found")

    if not current_user and not is_superuser(current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    db_game = crud.game.update(db=db, db_obj=db_game, obj_in=game)
    return db_game
コード例 #9
0
ファイル: players.py プロジェクト: Natsku123/party-filler
def update_player(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of player"),
    player: PlayerUpdate,
    current_user: Player = Depends(deps.get_current_user)
) -> Any:
    db_player = crud.player.get(db=db, id=id)

    if not db_player:
        raise HTTPException(status_code=404, detail="Player not found")

    if db_player.id != current_user.id and not is_superuser(current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    db_player = crud.player.update(db=db, db_obj=db_player, obj_in=player)
    return db_player
コード例 #10
0
def delete_role(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of role"),
    current_user: Player = Depends(deps.get_current_user)
) -> Any:
    role = crud.role.get(db=db, id=id)

    if not role:
        raise HTTPException(status_code=404, detail="Role not found")

    if (role.party and role.party.leader_id != current_user.id
            and not is_superuser(current_user)):
        raise HTTPException(status_code=401, detail="Not authorized")

    crud.role.remove(db=db, id=id)

    return role
コード例 #11
0
def update_role(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of role"),
    role: RoleUpdate,
    current_user: Player = Depends(deps.get_current_user)
) -> Any:
    db_role = crud.role.get(db=db, id=id)

    if not db_role:
        raise HTTPException(status_code=404, detail="Role not found")

    if (db_role.party and db_role.party.leader_id != current_user.id
            and not is_superuser(current_user)):
        raise HTTPException(status_code=401, detail="Not authorized")

    db_role = crud.role.update(db=db, db_obj=db_role, obj_in=role)
    return db_role
コード例 #12
0
def delete_game(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of game"),
    current_user: Player = Depends(deps.get_current_user),
    force: bool = Query(False, description="Force deletion")
) -> Any:
    game = crud.game.get(db=db, id=id)

    if not game:
        raise HTTPException(status_code=404, detail="Game not found")

    if not current_user and not is_superuser(current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    # if not force and len(game.parties) > 0:
    #     raise HTTPException(status_code=400, detail="Warning! Game is used by some parties, deletion will delete the parties also! Use force to continue.")

    crud.game.remove(db=db, id=id)

    return game
コード例 #13
0
ファイル: channels.py プロジェクト: Natsku123/party-filler
def delete_channel(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of channel"),
    current_user: Player = Depends(deps.get_current_user)
) -> Any:
    channel = crud.channel.get(db=db, id=id)

    if not channel:
        raise HTTPException(status_code=404, detail="Channel not found")

    db_server = crud.server.get(db=db, id=channel.server_id)

    if not db_server:
        raise HTTPException(status_code=404, detail="Server not found")

    if db_server not in current_user.servers and not is_superuser(
            current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    crud.channel.remove(db=db, id=id)

    return channel
コード例 #14
0
ファイル: channels.py プロジェクト: Natsku123/party-filler
def update_channel(
    *,
    db: Session = Depends(deps.get_db),
    id: int = Path(..., le=INTEGER_SIZE, gt=0, description="ID of channel"),
    channel: ChannelUpdate,
    current_user: Player = Depends(deps.get_current_user)
) -> Any:
    db_channel = crud.channel.get(db=db, id=id)

    if not db_channel:
        raise HTTPException(status_code=404, detail="Channel not found")

    if channel.discord_id != db_channel.discord_id:
        db_channels = crud.channel.get_multi(
            db, filters={"discord_id": channel.discord_id})

        if len(db_channels) > 0:
            raise HTTPException(
                status_code=400,
                detail="Channel with this Discord ID already exists")

    db_server = crud.server.get(db=db, id=db_channel.server_id)

    if not db_server:
        raise HTTPException(status_code=404, detail="Server not found")

    if db_server not in current_user.servers and not is_superuser(
            current_user):
        raise HTTPException(status_code=401, detail="Not authorized")

    if channel.server_id is None or channel.name is None:
        if settings.BOT_TOKEN == "NO TOKEN":
            raise HTTPException(status_code=400, detail="Data not available")

        channel_data = get_channel_info(channel.discord_id)

        if "code" in channel_data:
            if channel_data["code"] == 50001:
                raise HTTPException(
                    status_code=400,
                    detail="Discord bot has no access to channel")

            raise HTTPException(status_code=400,
                                detail="Discord channel not found")
        servers = crud.server.get_multi(
            db, filters={"discord_id": str(channel_data.get("guild_id"))})
        if len(servers) == 0:
            raise HTTPException(status_code=404, detail="Server not found")
        channel = ChannelCreate(
            discordId=channel.discord_id,
            name=channel_data.get("name"),
            serverId=servers[0].id,
        )
    elif channel.server_id != db_channel.server_id:
        server = crud.server.get(db, channel.server_id)

        if not server:
            raise HTTPException(status_code=404, detail="Server not found")

    db_channel = crud.channel.update(db=db, db_obj=db_channel, obj_in=channel)
    return db_channel