Added PlayInitException Added complete_and_post_play for log commands Added many more log plays Add undo-play Added query logging
167 lines
6.8 KiB
Python
167 lines
6.8 KiB
Python
import discord
|
|
import logging
|
|
|
|
from sqlmodel import Session
|
|
|
|
from in_game.gameplay_models import Lineup, Play
|
|
from in_game.gameplay_queries import get_sorted_lineups
|
|
|
|
class DropdownOptions(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):
|
|
"""
|
|
https://discordpy.readthedocs.io/en/latest/interactions/api.html#select
|
|
"""
|
|
def __init__(self, dropdown_objects: list[discord.ui.Select], timeout: float = 300.0):
|
|
super().__init__(timeout=timeout)
|
|
|
|
# self.add_item(Dropdown())
|
|
for x in dropdown_objects:
|
|
self.add_item(x)
|
|
|
|
|
|
class SelectViewDefense(discord.ui.Select):
|
|
def __init__(self, options: list, this_play: Play, base_embed: discord.Embed, session: Session, sorted_lineups: list[Lineup]):
|
|
self.embed = base_embed
|
|
self.session = session
|
|
self.play = this_play
|
|
self.sorted_lineups = sorted_lineups
|
|
super().__init__(options=options)
|
|
|
|
async def callback(self, interaction: discord.Interaction):
|
|
logging.info(f'SelectViewDefense - selection: {self.values[0]}')
|
|
|
|
this_lineup = self.session.get(Lineup, self.values[0])
|
|
self.embed.set_image(url=this_lineup.player.image)
|
|
|
|
select_player_options = [
|
|
discord.SelectOption(label=f'{x.position} - {x.player.name}', value=f'{x.id}', default=this_lineup.position == x.position) for x in self.sorted_lineups
|
|
]
|
|
player_dropdown = SelectViewDefense(
|
|
options=select_player_options,
|
|
this_play=self.play,
|
|
base_embed=self.embed,
|
|
session=self.session,
|
|
sorted_lineups=self.sorted_lineups
|
|
)
|
|
new_view = DropdownView(
|
|
dropdown_objects=[player_dropdown],
|
|
timeout=60
|
|
)
|
|
|
|
await interaction.response.edit_message(content=None, embed=self.embed, view=new_view)
|
|
|
|
|
|
|
|
class SelectOpenPack(discord.ui.Select):
|
|
def __init__(self, options: list, team: dict):
|
|
self.owner_team = team
|
|
super().__init__(placeholder='Select a Pack Type', options=options)
|
|
|
|
async def callback(self, interaction: discord.Interaction):
|
|
logging.info(f'SelectPackChoice - selection: {self.values[0]}')
|
|
pack_vals = self.values[0].split('-')
|
|
logging.info(f'pack_vals: {pack_vals}')
|
|
|
|
# Get the selected packs
|
|
params = [('team_id', self.owner_team['id']), ('opened', False), ('limit', 5), ('exact_match', True)]
|
|
|
|
open_type = 'standard'
|
|
if 'Standard' in pack_vals:
|
|
open_type = 'standard'
|
|
params.append(('pack_type_id', 1))
|
|
elif 'Premium' in pack_vals:
|
|
open_type = 'standard'
|
|
params.append(('pack_type_id', 3))
|
|
elif 'Daily' in pack_vals:
|
|
params.append(('pack_type_id', 4))
|
|
elif 'Promo Choice' in pack_vals:
|
|
open_type = 'choice'
|
|
params.append(('pack_type_id', 9))
|
|
elif 'MVP' in pack_vals:
|
|
open_type = 'choice'
|
|
params.append(('pack_type_id', 5))
|
|
elif 'All Star' in pack_vals:
|
|
open_type = 'choice'
|
|
params.append(('pack_type_id', 6))
|
|
elif 'Mario' in pack_vals:
|
|
open_type = 'choice'
|
|
params.append(('pack_type_id', 7))
|
|
elif 'Team Choice' in pack_vals:
|
|
open_type = 'choice'
|
|
params.append(('pack_type_id', 8))
|
|
else:
|
|
raise KeyError(f'Cannot identify pack details: {pack_vals}')
|
|
|
|
# If team isn't already set on team choice pack, make team pack selection now
|
|
await interaction.response.edit_message(view=None)
|
|
|
|
cardset_id = None
|
|
if 'Team Choice' in pack_vals and 'Cardset' in pack_vals:
|
|
# cardset_id = pack_vals[2]
|
|
cardset_index = pack_vals.index('Cardset')
|
|
cardset_id = pack_vals[cardset_index + 1]
|
|
params.append(('pack_cardset_id', cardset_id))
|
|
if 'Team' not in pack_vals:
|
|
view = SelectView(
|
|
[SelectChoicePackTeam('AL', self.owner_team, cardset_id),
|
|
SelectChoicePackTeam('NL', self.owner_team, cardset_id)],
|
|
timeout=30
|
|
)
|
|
await interaction.channel.send(
|
|
content=None,
|
|
view=view
|
|
)
|
|
return
|
|
|
|
params.append(('pack_team_id', pack_vals[pack_vals.index('Team') + 1]))
|
|
else:
|
|
if 'Team' in pack_vals:
|
|
params.append(('pack_team_id', pack_vals[pack_vals.index('Team') + 1]))
|
|
if 'Cardset' in pack_vals:
|
|
cardset_id = pack_vals[pack_vals.index('Cardset') + 1]
|
|
params.append(('pack_cardset_id', cardset_id))
|
|
|
|
p_query = await db_get('packs', params=params)
|
|
if p_query['count'] == 0:
|
|
logging.error(f'open-packs - no packs found with params: {params}')
|
|
raise ValueError(f'Unable to open packs')
|
|
|
|
# Open the packs
|
|
if open_type == 'standard':
|
|
await open_st_pr_packs(p_query['packs'], self.owner_team, interaction)
|
|
elif open_type == 'choice':
|
|
await open_choice_pack(p_query['packs'][0], self.owner_team, interaction, cardset_id)
|
|
|