diff --git a/cogs/players.py b/cogs/players.py index 0af7587..93dee3c 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -18,7 +18,7 @@ from db_calls import db_get, db_post, db_patch from helpers import PD_PLAYERS_ROLE_NAME, IMAGES, PD_SEASON, random_conf_gif, fuzzy_player_search, ALL_MLB_TEAMS, \ fuzzy_search, get_channel, display_cards, get_card_embeds, get_team_embed, cardset_search, get_blank_team_card, \ get_team_by_owner, get_rosters, get_roster_sheet, legal_channel, random_conf_word, embed_pagination, get_cal_user, \ - team_summary_embed + team_summary_embed, SelectView, SelectPaperdexCardset from typing import Optional, Literal # date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}' @@ -540,164 +540,72 @@ class Players(commands.Cog): ) await ctx.send(content=None, embeds=this_embed) - @commands.hybrid_group(name='paperdex', help='Check your collection counts') + group_paperdex = app_commands.Group(name='paperdex', description='Check your collection counts') + + @group_paperdex.command(name='cardset', description='Check your collection of a specific cardset') @commands.has_any_role(PD_PLAYERS_ROLE_NAME) @commands.check(legal_channel) - async def paperdex_command(self, ctx: commands.Context): - if ctx.invoked_subcommand is None: - await ctx.send(f'The available dex commands are: `cardset`') - - @paperdex_command.command(name='cardset', help='Check your collection of a specific cardset') - @commands.has_any_role(PD_PLAYERS_ROLE_NAME) - @commands.check(legal_channel) - async def paperdex_cardset( - self, ctx: commands.Context, - cardset_name: Literal['2013 Season', '2019 Season', '2021 Season', '2022 Season', '2022 Promos', - 'Sams Choice']): - team = get_team_by_owner(ctx.author.id) - - c_query = db_get('cardsets', params=[('name', cardset_name)]) - if c_query['count'] == 0: - await ctx.send(f'Ope, I couldn\'t find that cardset. {get_cal_user(ctx).mention} halp.') - return - this_cardset = c_query['cardsets'][0] - - all_dex = db_get( - 'paperdex', - params=[('team_id', team['id']), ('cardset_id', this_cardset['id']), ('flat', True)] - ) - dex_player_list = [x['player'] for x in all_dex['paperdex']] - - hof_embed = get_team_embed(f'{team["lname"]} Collection', team=team) - mvp_embed = get_team_embed(f'{team["lname"]} Collection', team=team) - as_embed = get_team_embed(f'{team["lname"]} Collection', team=team) - sta_embed = get_team_embed(f'{team["lname"]} Collection', team=team) - res_embed = get_team_embed(f'{team["lname"]} Collection', team=team) - rep_embed = get_team_embed(f'{team["lname"]} Collection', team=team) - - coll_data = { - 99: { - 'name': 'Hall of Fame', - 'owned': 0, - 'players': [], - 'embeds': [hof_embed] - }, - 1: { - 'name': 'MVP', - 'owned': 0, - 'players': [], - 'embeds': [mvp_embed] - }, - 2: { - 'name': 'All-Star', - 'owned': 0, - 'players': [], - 'embeds': [as_embed] - }, - 3: { - 'name': 'Starter', - 'owned': 0, - 'players': [], - 'embeds': [sta_embed] - }, - 4: { - 'name': 'Reserve', - 'owned': 0, - 'players': [], - 'embeds': [res_embed] - }, - 5: { - 'name': 'Replacement', - 'owned': 0, - 'players': [], - 'embeds': [rep_embed] - }, - 'total_owned': 0 - } - - response = await ctx.send(f'Okay, sifting through your cards...') - - set_players = db_get( - 'players', - params=[('cardset_id', this_cardset['id']), ('flat', True), ('inc_dex', False)], - timeout=5 - ) - if not set_players: - await ctx.send('Yikes, I don\'t see any players for that set.') + async def paperdex_cardset_slash(self, interaction: discord.Interaction): + team = get_team_by_owner(interaction.user.id) + if not team: + await interaction.response.send_message(f'Do you even have a team? I don\'t know you.', ephemeral=True) return - for player in set_players['players']: - if player['player_id'] in dex_player_list: - coll_data[player['rarity']]['owned'] += 1 - coll_data['total_owned'] += 1 - player['owned'] = True - else: - player['owned'] = False + await interaction.response.send_message( + content=None, + view=SelectView(SelectPaperdexCardset(), timeout=15), + ephemeral=True + ) - logging.debug(f'player: {player} / type: {type(player)}') - coll_data[player['rarity']]['players'].append(player) - await response.delete() - - cover_embed = get_team_embed(f'{team["lname"]} Collection', team=team) - cover_embed.description = this_cardset['name'] - cover_embed.add_field(name='# Total Cards', value=f'{set_players["count"]}') - cover_embed.add_field(name='# Collected', value=f'{coll_data["total_owned"]}') - display_embeds = [cover_embed] - - for rarity_id in coll_data: - if rarity_id != 'total_owned': - if coll_data[rarity_id]['players']: - coll_data[rarity_id]['embeds'][0].description = f'Rarity: {coll_data[rarity_id]["name"]}' - coll_data[rarity_id]['embeds'][0].add_field( - name='# Collected / # Total Cards', - value=f'{coll_data[rarity_id]["owned"]} / {len(coll_data[rarity_id]["players"])}', - inline=False - ) - - chunk_string = '' - for index, this_player in enumerate(coll_data[rarity_id]['players']): - logging.debug(f'this_player: {this_player}') - chunk_string += '☑ ' if this_player['owned'] else '⬜ ' - chunk_string += f'{this_player["p_name"]}\n' - - if (index + 1) == len(coll_data[rarity_id]["players"]): - coll_data[rarity_id]['embeds'][0].add_field( - name=f'Group {math.ceil((index + 1) / 20)} / ' - f'{math.ceil(len(coll_data[rarity_id]["players"]) / 20)}', - value=chunk_string - ) - - elif (index + 1) % 20 == 0: - coll_data[rarity_id]['embeds'][0].add_field( - name=f'Group {math.floor((index + 1) / 20)} / ' - f'{math.ceil(len(coll_data[rarity_id]["players"]) / 20)}', - value=chunk_string - ) - chunk_string = '' - - display_embeds.append(coll_data[rarity_id]['embeds'][0]) - - await embed_pagination(display_embeds, ctx.channel, ctx.author, timeout=30) - - @paperdex_command.command(name='team', help='Check your collection of a specific MLB team') - @commands.has_any_role(PD_PLAYERS_ROLE_NAME) - @commands.check(legal_channel) - async def paperdex_team(self, ctx: commands.Context, team_name: str): - team = get_team_by_owner(ctx.author.id) - - team_choice = None - if team_name.title() in ALL_MLB_TEAMS.keys(): - team_choice = team_name.title() - else: - for x in ALL_MLB_TEAMS: - if team_name.upper() in ALL_MLB_TEAMS[x] or team_name.title() in ALL_MLB_TEAMS[x]: - team_choice = x - break - - p_query = db_get('players', params=[('franchise', team_choice)]) - if p_query['count'] == 0: - await ctx.send(f'Hmm...I don\'t see any players from the {team_choice}. What\'s up with that ' - f'{get_cal_user(ctx).mention}?') + # @commands.hybrid_group(name='paperdex', help='Check your collection counts') + # @commands.has_any_role(PD_PLAYERS_ROLE_NAME) + # @commands.check(legal_channel) + # async def paperdex_command(self, ctx: commands.Context): + # if ctx.invoked_subcommand is None: + # await ctx.send(f'The available dex commands are: `cardset`') + # + # @paperdex_command.command(name='cardset', help='Check your collection of a specific cardset') + # @commands.has_any_role(PD_PLAYERS_ROLE_NAME) + # @commands.check(legal_channel) + # async def paperdex_cardset( + # self, ctx: commands.Context, + # cardset_name: Literal['2013 Season', '2019 Season', '2021 Season', '2022 Season', '2022 Promos', + # 'Sams Choice']): + # team = get_team_by_owner(ctx.author.id) + # + # c_query = db_get('cardsets', params=[('name', cardset_name)]) + # if c_query['count'] == 0: + # await ctx.send(f'Ope, I couldn\'t find that cardset. {get_cal_user(ctx).mention} halp.') + # return + # this_cardset = c_query['cardsets'][0] + # + # + # + # await embed_pagination(display_embeds, ctx.channel, ctx.author, timeout=30) + # + # @paperdex_command.command(name='team', help='Check your collection of a specific MLB team') + # @commands.has_any_role(PD_PLAYERS_ROLE_NAME) + # @commands.check(legal_channel) + # async def paperdex_team(self, ctx: commands.Context, team_name: str): + # await interaction.response.defer() + # team = get_team_by_owner(ctx.author.id) + # + # team_choice = None + # if team_name.title() in ALL_MLB_TEAMS.keys(): + # team_choice = team_name.title() + # else: + # for x in ALL_MLB_TEAMS: + # if team_name.upper() in ALL_MLB_TEAMS[x] or team_name.title() in ALL_MLB_TEAMS[x]: + # team_choice = x + # break + # + # if not team_choice: + # + # + # p_query = db_get('players', params=[('franchise', team_choice)]) + # if p_query['count'] == 0: + # await ctx.send(f'Hmm...I don\'t see any players from the {team_choice}. What\'s up with that ' + # f'{get_cal_user(ctx).mention}?') @commands.hybrid_command(name='ai-teams', help='Get list of AI teams and abbreviations') @commands.has_any_role(PD_PLAYERS_ROLE_NAME) diff --git a/helpers.py b/helpers.py index 6814307..07fc7ed 100644 --- a/helpers.py +++ b/helpers.py @@ -1,6 +1,7 @@ import asyncio import datetime import logging +import math import os import random import traceback @@ -435,6 +436,42 @@ class Pagination(discord.ui.View): self.stop() +class SelectPaperdexCardset(discord.ui.Select): + def __init__(self): + options = [ + discord.SelectOption(label='2022 Season'), + discord.SelectOption(label='2022 Promos'), + discord.SelectOption(label='2021 Season'), + discord.SelectOption(label='2019 Season'), + discord.SelectOption(label='2013 Season') + ] + super().__init__(placeholder='Select a Cardset', options=options) + + async def callback(self, interaction: discord.Interaction): + cardset_id = None + if self.values[0] == '2022 Season': + cardset_id = 3 + elif self.values[0] == '2022 Promos': + cardset_id = 4 + elif self.values[0] == '2021 Season': + cardset_id = 1 + elif self.values[0] == '2019 Season': + cardset_id = 5 + elif self.values[0] == '2013 Season': + cardset_id = 6 + + c_query = db_get('cardsets', object_id=cardset_id, none_okay=False) + await interaction.response.edit_message(content=f'Okay, sifting through your cards...', view=None) + + cardset_embeds = paperdex_cardset_embed(team=get_team_by_owner(interaction.user.id), this_cardset=c_query) + await embed_pagination(cardset_embeds, interaction.channel, interaction.user) + + +class SelectView(discord.ui.View): + def __init__(self, select_object: discord.ui.Select, timeout: float = 300.0): + super().__init__(timeout=timeout) + self.add_item(select_object) + # class Email(discord.ui.Modal, title='Team Sheet Share'): # def __init__(self, title: str, timeout: float = 300.0, custom_id: int = None): # super().__init__(timeout=timeout) @@ -1138,7 +1175,7 @@ async def display_cards( async def embed_pagination( - all_embeds: list, channel: commands.Context.channel, user: discord.Member, custom_message: str = None, + all_embeds: list, channel, user: discord.Member, custom_message: str = None, timeout: int = 10, start_page: int = 0): if start_page > len(all_embeds) - 1 or start_page < 0: page_num = 0 @@ -1146,8 +1183,8 @@ async def embed_pagination( page_num = start_page view = Pagination([user], timeout=timeout) - l_emoji = await get_emoji(channel.guild, 'arrow_left') - r_emoji = await get_emoji(channel.guild, 'arrow_right') + l_emoji = '' + r_emoji = '' view.right_button.label = f'Next: {page_num + 2}/{len(all_embeds)}{r_emoji}' view.cancel_button.label = f'Cancel' view.left_button.label = f'{l_emoji}Prev: {page_num}/{len(all_embeds)}' @@ -1896,3 +1933,116 @@ def get_ratings_guide(sheets): 'pitcher_ratings': pitchers } + +def paperdex_cardset_embed(team: dict, this_cardset: dict) -> [discord.Embed]: + all_dex = db_get( + 'paperdex', + params=[('team_id', team['id']), ('cardset_id', this_cardset['id']), ('flat', True)] + ) + dex_player_list = [x['player'] for x in all_dex['paperdex']] + + hof_embed = get_team_embed(f'{team["lname"]} Collection', team=team) + mvp_embed = get_team_embed(f'{team["lname"]} Collection', team=team) + as_embed = get_team_embed(f'{team["lname"]} Collection', team=team) + sta_embed = get_team_embed(f'{team["lname"]} Collection', team=team) + res_embed = get_team_embed(f'{team["lname"]} Collection', team=team) + rep_embed = get_team_embed(f'{team["lname"]} Collection', team=team) + + coll_data = { + 99: { + 'name': 'Hall of Fame', + 'owned': 0, + 'players': [], + 'embeds': [hof_embed] + }, + 1: { + 'name': 'MVP', + 'owned': 0, + 'players': [], + 'embeds': [mvp_embed] + }, + 2: { + 'name': 'All-Star', + 'owned': 0, + 'players': [], + 'embeds': [as_embed] + }, + 3: { + 'name': 'Starter', + 'owned': 0, + 'players': [], + 'embeds': [sta_embed] + }, + 4: { + 'name': 'Reserve', + 'owned': 0, + 'players': [], + 'embeds': [res_embed] + }, + 5: { + 'name': 'Replacement', + 'owned': 0, + 'players': [], + 'embeds': [rep_embed] + }, + 'total_owned': 0 + } + + set_players = db_get( + 'players', + params=[('cardset_id', this_cardset['id']), ('flat', True), ('inc_dex', False)], + timeout=5 + ) + + for player in set_players['players']: + if player['player_id'] in dex_player_list: + coll_data[player['rarity']]['owned'] += 1 + coll_data['total_owned'] += 1 + player['owned'] = True + else: + player['owned'] = False + + logging.debug(f'player: {player} / type: {type(player)}') + coll_data[player['rarity']]['players'].append(player) + + cover_embed = get_team_embed(f'{team["lname"]} Collection', team=team) + cover_embed.description = this_cardset['name'] + cover_embed.add_field(name='# Total Cards', value=f'{set_players["count"]}') + cover_embed.add_field(name='# Collected', value=f'{coll_data["total_owned"]}') + display_embeds = [cover_embed] + + for rarity_id in coll_data: + if rarity_id != 'total_owned': + if coll_data[rarity_id]['players']: + coll_data[rarity_id]['embeds'][0].description = f'Rarity: {coll_data[rarity_id]["name"]}' + coll_data[rarity_id]['embeds'][0].add_field( + name='# Collected / # Total Cards', + value=f'{coll_data[rarity_id]["owned"]} / {len(coll_data[rarity_id]["players"])}', + inline=False + ) + + chunk_string = '' + for index, this_player in enumerate(coll_data[rarity_id]['players']): + logging.debug(f'this_player: {this_player}') + chunk_string += '☑ ' if this_player['owned'] else '⬜ ' + chunk_string += f'{this_player["p_name"]}\n' + + if (index + 1) == len(coll_data[rarity_id]["players"]): + coll_data[rarity_id]['embeds'][0].add_field( + name=f'Group {math.ceil((index + 1) / 20)} / ' + f'{math.ceil(len(coll_data[rarity_id]["players"]) / 20)}', + value=chunk_string + ) + + elif (index + 1) % 20 == 0: + coll_data[rarity_id]['embeds'][0].add_field( + name=f'Group {math.floor((index + 1) / 20)} / ' + f'{math.ceil(len(coll_data[rarity_id]["players"]) / 20)}', + value=chunk_string + ) + chunk_string = '' + + display_embeds.append(coll_data[rarity_id]['embeds'][0]) + + return display_embeds +