Update helpers.py

Add support for multiple Select views in a SelectView; add SelectPaperdexTeam
This commit is contained in:
Cal Corum 2023-02-26 15:38:32 -06:00
parent 43b86a32b2
commit 065382b57b

View File

@ -448,6 +448,7 @@ class SelectPaperdexCardset(discord.ui.Select):
super().__init__(placeholder='Select a Cardset', options=options)
async def callback(self, interaction: discord.Interaction):
logging.info(f'SelectPaperdexCardset - selection: {self.values[0]}')
cardset_id = None
if self.values[0] == '2022 Season':
cardset_id = 3
@ -467,10 +468,125 @@ class SelectPaperdexCardset(discord.ui.Select):
await embed_pagination(cardset_embeds, interaction.channel, interaction.user)
class SelectPaperdexTeam(discord.ui.Select):
def __init__(self, which: Literal['AL', 'NL']):
self.which = which
if which == 'AL':
options = [
discord.SelectOption(label='Baltimore Orioles'),
discord.SelectOption(label='Boston Red Sox'),
discord.SelectOption(label='Chicago White Sox'),
discord.SelectOption(label='Cleveland Guardians'),
discord.SelectOption(label='Detroit Tigers'),
discord.SelectOption(label='Houston Astros'),
discord.SelectOption(label='Kansas City Royals'),
discord.SelectOption(label='Los Angeles Angels'),
discord.SelectOption(label='Minnesota Twins'),
discord.SelectOption(label='New York Yankees'),
discord.SelectOption(label='Oakland Athletics'),
discord.SelectOption(label='Seattle Mariners'),
discord.SelectOption(label='Tampa Bay Rays'),
discord.SelectOption(label='Texas Rangers'),
discord.SelectOption(label='Toronto Blue Jays')
]
else:
options = [
discord.SelectOption(label='Arizona Diamondbacks'),
discord.SelectOption(label='Atlanta Braves'),
discord.SelectOption(label='Chicago Cubs'),
discord.SelectOption(label='Cincinnati Reds'),
discord.SelectOption(label='Colorado Rockies'),
discord.SelectOption(label='Los Angeles Dodgers'),
discord.SelectOption(label='Miami Marlins'),
discord.SelectOption(label='Milwaukee Brewers'),
discord.SelectOption(label='New York Mets'),
discord.SelectOption(label='Philadelphia Phillies'),
discord.SelectOption(label='Pittsburgh Pirates'),
discord.SelectOption(label='San Diego Padres'),
discord.SelectOption(label='San Francisco Giants'),
discord.SelectOption(label='St. Louis Cardinals'),
discord.SelectOption(label='Washington Nationals')
]
super().__init__(placeholder=f'Select an {which} team', options=options)
async def callback(self, interaction: discord.Interaction):
team_id = None
if self.which == 'AL':
if self.values[0] == 'Baltimore Orioles':
team_id = 3
elif self.values[0] == 'Boston Red Sox':
team_id = 4
elif self.values[0] == 'Chicago White Sox':
team_id = 6
elif self.values[0] == 'Cleveland Guardians':
team_id = 8
elif self.values[0] == 'Detroit Tigers':
team_id = 10
elif self.values[0] == 'Houston Astros':
team_id = 11
elif self.values[0] == 'Kansas City Royals':
team_id = 12
elif self.values[0] == 'Los Angeles Angels':
team_id = 13
elif self.values[0] == 'Minnesota Twins':
team_id = 17
elif self.values[0] == 'New York Yankees':
team_id = 19
elif self.values[0] == 'Oakland Athletics':
team_id = 20
elif self.values[0] == 'Seattle Mariners':
team_id = 24
elif self.values[0] == 'Tampa Bay Rays':
team_id = 27
elif self.values[0] == 'Texas Rangers':
team_id = 28
elif self.values[0] == 'Toronto Blue Jays':
team_id = 29
else:
if self.values[0] == 'Arizona Diamondbacks':
team_id = 1
elif self.values[0] == 'Atlanta Braves':
team_id = 2
elif self.values[0] == 'Chicago Cubs':
team_id = 5
elif self.values[0] == 'Cincinnati Reds':
team_id = 7
elif self.values[0] == 'Colorado Rockies':
team_id = 9
elif self.values[0] == 'Los Angeles Dodgers':
team_id = 14
elif self.values[0] == 'Miami Marlins':
team_id = 15
elif self.values[0] == 'Milwaukee Brewers':
team_id = 16
elif self.values[0] == 'New York Mets':
team_id = 18
elif self.values[0] == 'Philadelphia Phillies':
team_id = 21
elif self.values[0] == 'Pittsburgh Pirates':
team_id = 22
elif self.values[0] == 'San Diego Padres':
team_id = 23
elif self.values[0] == 'San Francisco Giants':
team_id = 25
elif self.values[0] == 'St. Louis Cardinals':
team_id = 26
elif self.values[0] == 'Washington Nationals':
team_id = 30
t_query = db_get('teams', object_id=team_id, none_okay=False)
await interaction.response.edit_message(content=f'Okay, sifting through your cards...', view=None)
team_embeds = paperdex_team_embed(team=get_team_by_owner(interaction.user.id), mlb_team=t_query)
await embed_pagination(team_embeds, interaction.channel, interaction.user)
class SelectView(discord.ui.View):
def __init__(self, select_object: discord.ui.Select, timeout: float = 300.0):
def __init__(self, select_objects: [discord.ui.Select], timeout: float = 300.0):
super().__init__(timeout=timeout)
self.add_item(select_object)
for x in select_objects:
self.add_item(x)
# class Email(discord.ui.Modal, title='Team Sheet Share'):
# def __init__(self, title: str, timeout: float = 300.0, custom_id: int = None):
@ -2046,3 +2162,81 @@ def paperdex_cardset_embed(team: dict, this_cardset: dict) -> [discord.Embed]:
return display_embeds
def paperdex_team_embed(team: dict, mlb_team: dict) -> [discord.Embed]:
all_dex = db_get(
'paperdex',
params=[('team_id', team['id']), ('franchise', mlb_team['lname']), ('flat', True)]
)
dex_player_list = [x['player'] for x in all_dex['paperdex']]
c_query = db_get('cardsets')
coll_data = {'total_owned': 0}
total_players = 0
for x in c_query['cardsets']:
set_players = db_get(
'players',
params=[('cardset_id', x['id']), ('franchise', mlb_team['lname']), ('flat', True), ('inc_dex', False)]
)
if set_players is not None:
coll_data[x['id']] = {
'name': x['name'],
'owned': 0,
'players': [],
'embeds': [get_team_embed(f'{team["lname"]} Collection', team=team)]
}
total_players += set_players['count']
for player in set_players['players']:
if player['player_id'] in dex_player_list:
coll_data[x['id']]['owned'] += 1
coll_data['total_owned'] += 1
player['owned'] = True
else:
player['owned'] = False
logging.debug(f'player: {player} / type: {type(player)}')
coll_data[x['id']]['players'].append(player)
cover_embed = get_team_embed(f'{team["lname"]} Collection', team=team)
cover_embed.description = mlb_team['lname']
cover_embed.add_field(name='# Total Cards', value=f'{total_players}')
cover_embed.add_field(name='# Collected', value=f'{coll_data["total_owned"]}')
display_embeds = [cover_embed]
for cardset_id in coll_data:
if cardset_id != 'total_owned':
if coll_data[cardset_id]['players']:
coll_data[cardset_id]['embeds'][0].description = f'Team: {coll_data[cardset_id]["name"]} ' \
f'{mlb_team["lname"]}'
coll_data[cardset_id]['embeds'][0].add_field(
name='# Collected / # Total Cards',
value=f'{coll_data[cardset_id]["owned"]} / {len(coll_data[cardset_id]["players"])}',
inline=False
)
chunk_string = ''
for index, this_player in enumerate(coll_data[cardset_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[cardset_id]["players"]):
coll_data[cardset_id]['embeds'][0].add_field(
name=f'Group {math.ceil((index + 1) / 20)} / '
f'{math.ceil(len(coll_data[cardset_id]["players"]) / 20)}',
value=chunk_string
)
elif (index + 1) % 20 == 0:
coll_data[cardset_id]['embeds'][0].add_field(
name=f'Group {math.floor((index + 1) / 20)} / '
f'{math.ceil(len(coll_data[cardset_id]["players"]) / 20)}',
value=chunk_string
)
chunk_string = ''
display_embeds.append(coll_data[cardset_id]['embeds'][0])
return display_embeds