Add Dropdown class

This commit is contained in:
Cal Corum 2024-07-11 15:08:14 -05:00
parent 2046ebcdde
commit 89daa5fa15
2 changed files with 134 additions and 2 deletions

View File

@ -224,6 +224,21 @@ RARITY = {
'Reserve': 1,
'Replacement': 0
}
SELECT_CARDSET_OPTIONS = [
discord.SelectOption(label='2024 Live', value='17'),
discord.SelectOption(label='2024 Promos', value='18'),
discord.SelectOption(label='2023 Season', value='9'),
discord.SelectOption(label='2023 Promos', value='10'),
discord.SelectOption(label='2022 Season', value='3'),
discord.SelectOption(label='2022 Promos', value='4'),
discord.SelectOption(label='2021 Season', value='1'),
discord.SelectOption(label='2019 Season', value='5'),
discord.SelectOption(label='2018 Season', value='13'),
discord.SelectOption(label='2018 Promos', value='14'),
discord.SelectOption(label='2016 Season', value='11'),
discord.SelectOption(label='2013 Season', value='6'),
discord.SelectOption(label='2012 Season', value='7')
]
class Question:
@ -1142,6 +1157,49 @@ class SelectView(discord.ui.View):
self.add_item(x)
class Dropdown(discord.ui.Select):
def __init__(self, option_list: list, placeholder: str = 'Make your selection', min_values: int = 1,
max_values: int = 1, callback=None):
# Set the options that will be presented inside the dropdown
# options = [
# discord.SelectOption(label='Red', description='Your favourite colour is red', emoji='🟥'),
# discord.SelectOption(label='Green', description='Your favourite colour is green', emoji='🟩'),
# discord.SelectOption(label='Blue', description='Your favourite colour is blue', emoji='🟦'),
# ]
# The placeholder is what will be shown when no option is chosen
# The min and max values indicate we can only pick one of the three options
# The options parameter defines the dropdown options. We defined this above
# If a default option is set on any SelectOption, the View will not process if only the default is
# selected by the user
self.custom_callback = callback
super().__init__(
placeholder=placeholder,
min_values=min_values,
max_values=max_values,
options=option_list
)
async def callback(self, interaction: discord.Interaction):
# Use the interaction object to send a response message containing
# the user's favourite colour or choice. The self object refers to the
# Select object, and the values attribute gets a list of the user's
# selected options. We only want the first one.
# await interaction.response.send_message(f'Your favourite colour is {self.values[0]}')
logging.info(f'Dropdown callback: {self.custom_callback}')
await self.custom_callback(interaction, self.values)
class DropdownView(discord.ui.View):
def __init__(self, dropdown_objects: [Dropdown], timeout: float = 300.0):
super().__init__(timeout=timeout)
# self.add_item(Dropdown())
for x in dropdown_objects:
self.add_item(x)
def random_conf_gif():
conf_gifs = [
'https://tenor.com/view/boom-annakendrick-pitchperfect-pitchperfect2-micdrop-gif-5143507',

View File

@ -4,8 +4,10 @@ import logging
from typing import Optional, Literal
from db_calls import db_get
from helpers import PD_SEASON
PLAYER_CACHE = {}
TEAM_CACHE = {}
BATTINGCARD_CACHE = {} # { <player_id: int>: { <variant: int>: BattingWrapper } }
PITCHINGCARD_CACHE = {} # { <player_id: int>: { <variant: int>: PitchingWrapper } }
@ -71,6 +73,51 @@ class Player:
}
@dataclass
class Team:
id: int
abbrev: str
sname: str
lname: str
gmid: int
gmname: str
gsheet: str
wallet: int
team_value: int
collection_value: int
logo: str
color: str
season: int
event: bool
career: int
ranking: int
has_guide: bool
is_ai: bool
created: datetime.datetime = datetime.datetime.now()
def to_dict(self):
return {
'id': self.id,
'abbrev': self.abbrev,
'sname': self.sname,
'lname': self.lname,
'gmid': self.gmid,
'gmname': self.gmname,
'gsheet': self.gsheet,
'wallet': self.wallet,
'team_value': self.team_value,
'collection_value': self.collection_value,
'logo': self.logo,
'color': self.color,
'season': self.season,
'event': self.event,
'career': self.career,
'ranking': self.ranking,
'has_guide': self.has_guide,
'is_ai': self.is_ai
}
@dataclass
class BattingCard:
player_id: int
@ -214,8 +261,8 @@ class PitchingWrapper:
created: datetime.datetime = datetime.datetime.now()
async def get_pd_player(player_id, as_dict: Optional[bool] = True):
if player_id in PLAYER_CACHE:
async def get_pd_player(player_id, as_dict: Optional[bool] = True, skip_cache: bool = False):
if player_id in PLAYER_CACHE and not skip_cache:
tdelta = datetime.datetime.now() - PLAYER_CACHE[player_id].created
if tdelta.total_seconds() < 1209600:
logging.debug(f'this_player: {PLAYER_CACHE[player_id]}')
@ -238,6 +285,33 @@ async def get_pd_player(player_id, as_dict: Optional[bool] = True):
return PLAYER_CACHE[player_id]
async def get_pd_team(team_or_gm_id, as_dict: bool = True, skip_cache: bool = False):
if team_or_gm_id in TEAM_CACHE and not skip_cache:
tdelta = datetime.datetime.now() - TEAM_CACHE[team_or_gm_id].created
if tdelta.total_seconds() < 1209600:
logging.info(f'this_team: {TEAM_CACHE[team_or_gm_id]}')
if as_dict:
return TEAM_CACHE[team_or_gm_id].to_dict()
else:
return TEAM_CACHE[team_or_gm_id]
else:
logging.error(f'Refreshing team {team_or_gm_id} in cache...')
this_team = await db_get('teams', object_id=team_or_gm_id, params=[('inc_packs', False)])
if this_team is None:
t_query = await db_get('teams', params=[('season', PD_SEASON), ('gm_id', team_or_gm_id)])
if t_query is None:
raise KeyError(f'No team found for ID {team_or_gm_id}')
this_team = t_query['teams'][0]
logging.info(f'this_team: {this_team}')
TEAM_CACHE[team_or_gm_id] = Team(**this_team)
if as_dict:
return this_team
return TEAM_CACHE[team_or_gm_id]
async def get_pd_battingcard(player_id: int, variant: Optional[int] = 0):
if player_id in BATTINGCARD_CACHE and variant in BATTINGCARD_CACHE[player_id]:
tdelta = datetime.datetime.now() - BATTINGCARD_CACHE[player_id][variant].created