Skip to content

Commit 0e595d6

Browse files
committed
Tournament: optimized choices view
1 parent 05e8c9c commit 0e595d6

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

core/utils/discord.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ async def selection(interaction: Union[discord.Interaction, commands.Context], *
267267
class YNQuestionView(View):
268268
def __init__(self):
269269
super().__init__(timeout=120)
270-
self.result = False
270+
self.result = None
271271

272272
@discord.ui.button(label='Yes', style=discord.ButtonStyle.green, custom_id='yn_yes')
273273
async def on_yes(self, interaction: Interaction, _: Button):
@@ -288,7 +288,7 @@ async def on_error(self, interaction: Interaction, error: Exception, item: Item[
288288

289289

290290
async def yn_question(ctx: Union[commands.Context, discord.Interaction], question: str,
291-
message: Optional[str] = None, ephemeral: Optional[bool] = True) -> bool:
291+
message: Optional[str] = None, ephemeral: Optional[bool] = True) -> Optional[bool]:
292292
"""
293293
:param ctx: The context in which the yn_question method is being called. It can be either a discord.py commands.Context object or a discord.Interaction object.
294294
:param question: The question to be displayed in the embedded message.
@@ -309,8 +309,7 @@ async def yn_question(ctx: Union[commands.Context, discord.Interaction], questio
309309
view = YNQuestionView()
310310
msg = await ctx.send(embed=embed, view=view, ephemeral=ephemeral)
311311
try:
312-
if await view.wait():
313-
return False
312+
await view.wait()
314313
return view.result
315314
finally:
316315
try:

plugins/gamemaster/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ async def on_message(self, message: discord.Message):
520520
# await server.sendChatMessage(Coalition.RED, message.content, message.author.display_name)
521521
pass
522522
if server.channels[Channel.CHAT] and server.channels[Channel.CHAT] == message.channel.id:
523-
if message.content.startswith('/') is False:
523+
if not message.content.startswith('/'):
524524
await server.sendChatMessage(Coalition.ALL, message.content, message.author.display_name)
525525

526526
@commands.Cog.listener()

plugins/tournament/commands.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ async def setup_server_for_match(self, msg: discord.Message, messages: list[str]
11451145
server.settings['require_pure_scripts'] = True
11461146
server.settings['listShuffle'] = False
11471147
server.settings['listLoop'] = False
1148-
if config.get('allow_exports', False) is False:
1148+
if not config.get('allow_exports', False):
11491149
advanced |= {
11501150
"allow_ownship_export": False,
11511151
"allow_object_export": False,
@@ -1327,7 +1327,9 @@ async def start(self, interaction: discord.Interaction, tournament_id: int, matc
13271327
squadron = await self.get_squadron(match_id, squadrons[side]['id'])
13281328
if squadron.points > min_costs:
13291329
channel = self.bot.get_channel(channels[side])
1330-
await channel.send(_("You can now use {} to chose your customizations!").format(
1330+
await channel.send(_("You can now use {} to chose your customizations!\n"
1331+
"If you do not want to change anything, "
1332+
"please run it and say 'No Change'").format(
13311333
(await utils.get_command(self.bot, group=self.match.name,
13321334
name=self.customize.name)).mention))
13331335
else:
@@ -1591,11 +1593,13 @@ async def customize(self, interaction: discord.Interaction):
15911593
if await view.wait():
15921594
return
15931595

1594-
if not view.saved or not await utils.yn_question(
1596+
if view.acknowledged is None or (view.acknowledged is True and not await utils.yn_question(
15951597
interaction, _("Are you sure?\nYour settings will be directly applied to the next round."),
1596-
ephemeral=True):
1597-
await interaction.followup.send(_("Your choices were saved but will not be applied to the next round."),
1598-
ephemeral=ephemeral)
1598+
ephemeral=True)):
1599+
await interaction.followup.send(
1600+
_("Your choices were saved.\n"
1601+
"If you want them to be applied to the next round, press 'Confirm & Buy'."),
1602+
ephemeral=ephemeral)
15991603
return
16001604

16011605
async with self.apool.connection() as conn:
@@ -1615,7 +1619,11 @@ async def customize(self, interaction: discord.Interaction):
16151619
(squadron_blue = %(squadron_id)s OR squadron_red = %(squadron_id)s)
16161620
AND match_id = %(match_id)s
16171621
""", {"match_id": match_id, "squadron_id": squadron_id})
1618-
await interaction.followup.send(_("Thanks, your selection will now be applied."), ephemeral=True)
1622+
if view.acknowledged is True:
1623+
await interaction.followup.send(_("Thanks, your selection will now be applied."), ephemeral=True)
1624+
else:
1625+
await interaction.followup.send(_("You decided to not buy any customizations in this round."),
1626+
ephemeral=True)
16191627
finally:
16201628
try:
16211629
await msg.delete()

plugins/tournament/view.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def __init__(self, plugin: "Tournament", tournament_id: int, match_id: int, squa
164164
self.match_id = match_id
165165
self.squadron_id = squadron_id
166166
self.config = config
167-
self.saved = False
167+
self.acknowledged = None
168168

169169
async def get_tickets(self) -> dict[str, int]:
170170
async with self.plugin.apool.connection() as conn:
@@ -238,12 +238,17 @@ async def render(self) -> discord.Embed:
238238
], min_values=1, max_values=1)
239239
select.callback = self.add_choice
240240
self.add_item(select)
241-
button = Button(label="Confirm & buy", style=discord.ButtonStyle.green)
242-
button.callback = self.save
243-
self.add_item(button)
244-
button = Button(label="Save & Close", style=discord.ButtonStyle.red)
245-
button.callback = self.cancel
246-
self.add_item(button)
241+
if already_selected:
242+
button = Button(label="Confirm & Buy", style=discord.ButtonStyle.green)
243+
button.callback = self.save
244+
self.add_item(button)
245+
button = Button(label="Save & Close", style=discord.ButtonStyle.red)
246+
button.callback = self.cancel
247+
self.add_item(button)
248+
else:
249+
button = Button(label="Skip this round", style=discord.ButtonStyle.primary)
250+
button.callback = self.no_change
251+
self.add_item(button)
247252
return embed
248253

249254
async def add_choice(self, interaction: discord.Interaction):
@@ -254,7 +259,7 @@ async def add_choice(self, interaction: discord.Interaction):
254259
ticket_name = self.config['presets']['choices'][choice].get('ticket')
255260
ticket_count = tickets.get(ticket_name, 99)
256261

257-
max_num = max(self.config['presets']['choices'][choice].get('max'), ticket_count)
262+
max_num = max(self.config['presets']['choices'][choice].get('max', 99), ticket_count)
258263
if not max_num or max_num > 1:
259264
modal = NumbersModal(choice, costs, squadron.points, max_num)
260265
# noinspection PyUnresolvedReferences
@@ -320,14 +325,20 @@ async def remove_choice(self, interaction: discord.Interaction):
320325
async def save(self, interaction: discord.Interaction):
321326
# noinspection PyUnresolvedReferences
322327
await interaction.response.defer()
323-
self.saved = True
328+
self.acknowledged = True
324329
self.stop()
325330

326331
async def cancel(self, interaction: discord.Interaction):
327332
# noinspection PyUnresolvedReferences
328333
await interaction.response.defer()
329334
self.stop()
330335

336+
async def no_change(self, interaction: discord.Interaction):
337+
# noinspection PyUnresolvedReferences
338+
await interaction.response.defer()
339+
self.acknowledged = False
340+
self.stop()
341+
331342

332343
class ApplicationView(View):
333344

0 commit comments

Comments
 (0)