Example #1
0
    async def get(self, sid: str = None, pipeline: str = None, keep_raw: bool = False, ptr_cache: PtrCache = None):
        '''Awaitable. Get this object from the pipeline.\n
        `sid` id identifying the session on the pipeline to reuse.\n
        `pipeline` key identifying the pipeline to execute against.\n
        `keep_raw` flag for storing raw data of the request as a dictionary.\n
        `ptr_cache` intercepts a PtrCache, usage details please refer to documentations.\n
        '''
        self.set_pipeline(pipeline)
        token = await self.create_token()

        if ptr_cache:
            if not isinstance(ptr_cache, PtrCache):
                raise TypeError(f"'ptr_cache' receives object of type 'PtrCache', got '{ptr_cache.__class__.__name__}'")
            item = ptr_cache.get(token.stringify + self._meta.filter_key)
            if item:
                return item

        data = await self._meta.pipeline.get(token, sid)
        data = self._filter(data)
        if keep_raw:
            self._meta.raw_data = fast_copy(data)
        self._meta.data = self._transform(data)
        self._fill()

        if ptr_cache:
            ptr_cache.set(token.stringify + self._meta.filter_key, self)

        return self
Example #2
0
    async def clash(self, ctx: Context, *name: str):
        name = ''.join(name)

        summoner = await get_summoner(ctx, name)

        if not summoner:
            return

        buf = await summoner.clash_players.get()
        incomplete_clash_players = buf.players

        if not incomplete_clash_players:
            return await ctx.send(
                f'{summoner.name} doesnt belong to any clash team')

        team = await incomplete_clash_players[0].team.get()

        async with Gatherer() as gatherer:
            players_cache = PtrCache()
            match_history_cache = PtrCache()
            champions_cache = PtrCache()

            gatherer.statements = [player.summoner for player in team.players]
            players = await gatherer.gather()  # type: list[lol.Summoner]

            for player in players:
                players_cache.set(player.id, player)

            statements = [
                player.match_history.query(queue_ids=[700])
                for player in players
            ]
            gatherer.statements = statements
            match_histories = await gatherer.gather(
            )  # type: list[lol.MatchHistory]

            for history in match_histories:
                match_history_cache.set(history.account_id, history)

            gatherer.statements = [player.profile_icon for player in players]
            await gatherer.gather()

            champion_set = set()
            most_played_champions = {}
            for match_history in match_histories:  # type: lol.MatchHistory
                top_champs = match_history_most_played_champs(
                    match_history)[:5]
                most_played_champions[match_history.account_id] = top_champs
                champion_set.update([champ for champ, times in top_champs])

            statements = [lol.Champion(id=idx) for idx in champion_set]
            gatherer.statements = statements
            champions = await gatherer.gather()  # type: list[lol.Champion]

            for champion in champions:
                champions_cache.set(champion.id, champion)

            for acc_id, champs in most_played_champions.items():
                most_played_champions[acc_id] = [
                    (champions_cache.get(champ_id).name, n)
                    for champ_id, n in champs
                ]

        team_embed = Embed()

        team_name = team.name if team.name else 'Team Name'
        team_tag = team.abbreviation if team.abbreviation else 'TAG'
        clash_icon = f'https://raw.communitydragon.org/latest/game/assets/clash/roster-logos/{team.icon_id}/1_64.png'

        team_embed.set_author(name=f'[{team_tag}] {team_name}',
                              icon_url=clash_icon)
        team_embed.set_thumbnail(url=clash_icon)

        captain = players_cache.get(
            team.captain_summoner_id)  # type: lol.Summoner
        team_data = [f'Captain: {captain.name}', f'Tier: {team.tier}']

        team_embed.add_field(name='General info',
                             value='\n'.join(team_data),
                             inline=False)

        player_names: list[str] = []
        player_lanes: list[tuple[str, str]] = []
        player_embeds: list[Embed] = []
        for clash_player_data in sorted(
                team.players,
                key=lambda x: lane_data[x.position]['sort_value']):
            player = players_cache.get(
                clash_player_data.summoner_id)  # type: lol.Summoner

            position = clash_player_data.position
            match_history = match_history_cache.get(
                player.account_id)  # type: lol.MatchHistory

            champs_played = most_played_champions[player.account_id]

            embed = await gather_summoner_data(player, position, match_history,
                                               champs_played)

            player_embeds.append(embed)
            player_names.append(player.name)
            player_lanes.append((player.name, clash_player_data.position))

        player_roles = [
            f'{name}: {lane_data[lane]["display_name"]}'
            for name, lane in player_lanes
        ]
        team_embed.add_field(name='Members', value='\n'.join(player_roles))

        url_encoded_names = ",".join(
            [parse.quote(name) for name in player_names])

        urls = {
            'op.gg':
            f'https://eune.op.gg/multi/query={url_encoded_names}',
            'u.gg':
            f'https://u.gg/multisearch?summoners={url_encoded_names}&region=eun1',
            'porofessor.gg':
            f'https://porofessor.gg/pregame/eune/{url_encoded_names}'
        }

        aliased_links = [f'[{site}]({url})' for site, url in urls.items()]
        team_embed.add_field(name='Third party sites',
                             value='\n'.join(aliased_links),
                             inline=False)

        await ctx.send(embed=team_embed)
        for embed in player_embeds:
            await ctx.send(embed=embed)

        refresh_op_gg_profiles(player_names)