Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions eggsplode/cards/bombs.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ async def radioeggtive(
if timed_out:
game.deck.insert(random.randint(0, len(game.deck)), "radioeggtive_face_up")
await game.send(TextView("radioeggtive", game.current_player_id), interaction)
else:
view = DefuseView(
game,
lambda: radioeggtive_finish(game),
card="radioeggtive_face_up",
prev_card="radioeggtive",
)
if await view.skip_if_deck_empty():
return
await interaction.respond(view=view, ephemeral=True)
return
view = DefuseView(
game,
lambda: radioeggtive_finish(game),
card="radioeggtive_face_up",
prev_card="radioeggtive",
)
if await view.skip_if_deck_empty():
return
await interaction.respond(view=view, ephemeral=True)


async def radioeggtive_face_up(
Expand Down
3 changes: 2 additions & 1 deletion eggsplode/cards/future.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def create_selections(self):
self.add_item(self.select_container)
self.add_item(self.confirm_row)

async def finish(self, interaction=None):
async def finish(self):
await super().finish()
await self.callback_action()

async def selection_callback(self, interaction: discord.Interaction):
Expand Down
4 changes: 2 additions & 2 deletions eggsplode/cards/skip.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ def __init__(self, game: "Game"):
self.add_item(self.dig_deeper_section)

async def finish(self, interaction: discord.Interaction | None = None):
await super().finish()
if not interaction:
interaction = self.game.last_interaction
if not interaction:
raise ValueError("No last interaction set for the game.")
_, hold = await self.game.draw_from(interaction)
self.stop()
if hold:
await self.game.events.turn_end()

async def dig_deeper(self, interaction: discord.Interaction):
self.stop()
self.ignore_interactions()
self.disable_all_items()
await interaction.edit(view=self)
await self.game.send(
Expand Down
5 changes: 1 addition & 4 deletions eggsplode/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,7 @@ async def send(
self.last_interaction = interaction
if self.last_interaction is None:
raise ValueError("last_interaction is None")
try:
await self.last_interaction.response.send_message(view=view)
except discord.errors.InteractionResponded:
await self.last_interaction.followup.send(view=view)
await self.last_interaction.respond(view=view)
logger.debug("Game %s: Sent message: %s", self.id, view.copy_text())

@property
Expand Down
2 changes: 1 addition & 1 deletion eggsplode/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contains the views for the Eggsplode game UI.
"""

from .base import BaseView, TextView
from .base import BaseView, BaseGameView, TextView
from .nope import NopeView
from .play import PlayView
from .selections import SelectionView, ChoosePlayerView, DefuseView
Expand Down
20 changes: 19 additions & 1 deletion eggsplode/ui/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@


class BaseView(discord.ui.DesignerView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.is_ignoring_interactions = False

async def interaction_check(self, interaction: discord.Interaction) -> bool:
return (
await super().interaction_check(interaction)
and not self.is_ignoring_interactions
)

def ignore_interactions(self):
self.is_ignoring_interactions = True

def allow_interactions(self):
self.is_ignoring_interactions = False


class BaseGameView(BaseView):
def __init__(self, game: "Game", timeout=None):
super().__init__(timeout=timeout, disable_on_timeout=True)
self.game = game
self.game.events.game_end += self.stop
self.game.events.game_end += self.ignore_interactions

async def interaction_check(self, interaction: discord.Interaction) -> bool:
if not await super().interaction_check(interaction):
Expand Down
26 changes: 10 additions & 16 deletions eggsplode/ui/nope.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from typing import Callable, Coroutine, TYPE_CHECKING
import discord
from eggsplode.strings import format_message
from eggsplode.ui.base import BaseView, TextView
from eggsplode.ui.base import BaseGameView, TextView

if TYPE_CHECKING:
from eggsplode.core import Game


class NopeView(BaseView):
class NopeView(BaseGameView):
def __init__(
self,
game: "Game",
Expand All @@ -32,7 +32,6 @@ def __init__(
self.nope_callback_action = nope_callback_action
self.nope_count = 0
self.players_confirmed = set()
self.disabled = False
self.action_text_display = discord.ui.TextDisplay(message)
self.add_item(self.action_text_display)
self.timer_display = discord.ui.TextDisplay(self.timer_text)
Expand Down Expand Up @@ -74,23 +73,19 @@ def noped(self) -> bool:
return self.nope_count % 2 == 1

async def on_timeout(self):
try:
await super().on_timeout()
finally:
if not self.disabled:
self.disabled = True
self.stop()
if not self.noped and self.ok_callback_action:
await self.ok_callback_action(None)
else:
await self.game.events.action_end()
if not self.is_ignoring_interactions:
self.ignore_interactions()
if not self.noped and self.ok_callback_action:
await self.ok_callback_action(None)
else:
await self.game.events.action_end()

async def interaction_check(self, interaction: discord.Interaction) -> bool:
return (
await super().interaction_check(interaction)
and interaction.user is not None
and interaction.user.id in self.game.players
and not self.disabled
and not self.is_ignoring_interactions
)

def toggle_strike_through(self):
Expand Down Expand Up @@ -170,8 +165,7 @@ async def ok_callback(self, interaction: discord.Interaction):

async def finish_confirmation(self, interaction: discord.Interaction):
self.game.last_interaction = interaction
self.disabled = True
self.stop()
self.ignore_interactions()
self.disable_all_items()
self.remove_item(self.action_row)
self.remove_item(self.timer_display)
Expand Down
8 changes: 4 additions & 4 deletions eggsplode/ui/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from typing import TYPE_CHECKING
import discord
from eggsplode.strings import available_cards, MAX_COMPONENTS, format_message, replace_emojis
from eggsplode.ui.base import TextView
from eggsplode.ui.base import BaseView, TextView

if TYPE_CHECKING:
from eggsplode.core import Game


class PlayView(discord.ui.DesignerView):
class PlayView(BaseView):
MAX_SECTIONS = (MAX_COMPONENTS - 5) // 3

def __init__(self, game: "Game", user_id: int):
Expand All @@ -34,7 +34,7 @@ def __init__(self, game: "Game", user_id: int):
self.forward_button: discord.ui.Button | None = None
self.page_number = 0
self.update_sections()
self.game.events.game_end += self.stop
self.game.events.game_end += self.ignore_interactions

@property
def playable(self) -> bool:
Expand Down Expand Up @@ -140,6 +140,6 @@ async def play_card(self, card: str, interaction: discord.Interaction):
return
self.game.action_id += 1
self.action_id = self.game.action_id
self.stop()
self.ignore_interactions()
await interaction.edit(view=self, delete_after=0)
await self.game.play_callback(interaction, card)
47 changes: 19 additions & 28 deletions eggsplode/ui/selections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import discord

from eggsplode.strings import format_message, tooltip
from eggsplode.ui.base import BaseView

if TYPE_CHECKING:
from eggsplode.core import Game


class SelectionView(discord.ui.DesignerView):
class SelectionView(BaseView):
def __init__(self, timeout: int = 20):
super().__init__(timeout=timeout, disable_on_timeout=True)
self.confirm_button = discord.ui.Button(
Expand All @@ -20,22 +21,20 @@ def __init__(self, timeout: int = 20):
self.confirm_button.callback = self.confirm

async def on_timeout(self):
try:
await super().on_timeout()
finally:
if not self.is_ignoring_interactions:
await self.finish()

async def finish(self, interaction: discord.Interaction | None = None):
pass
async def finish(self):
self.ignore_interactions()

async def confirm(self, interaction: discord.Interaction):
self.disable_all_items()
await interaction.edit(view=self)
self.stop()
await self.finish(interaction)
if not self.is_ignoring_interactions:
await self.finish()


class ChoosePlayerView(discord.ui.DesignerView):
class ChoosePlayerView(BaseView):
def __init__(
self,
game: "Game",
Expand All @@ -50,12 +49,11 @@ def __init__(
self.callback_action = callback_action
self.user_select = None
self.action_row = None
self.game.events.game_end += self.stop
self.game.events.game_end += self.ignore_interactions

async def on_timeout(self):
try:
await super().on_timeout()
finally:
if not self.is_ignoring_interactions:
self.ignore_interactions()
await self.callback_action(self.eligible_players[0])

async def skip_if_single_option(self) -> bool:
Expand Down Expand Up @@ -86,7 +84,7 @@ async def create_user_selection(self):
async def selection_callback(self, interaction: discord.Interaction):
if not (interaction and self.user_select):
return
self.stop()
self.ignore_interactions()
self.disable_all_items()
await interaction.edit(view=self, delete_after=0)
if not isinstance(self.user_select.values[0], str):
Expand All @@ -110,21 +108,13 @@ def __init__(
self.card_position = 0
self.move_prompt_display = discord.ui.TextDisplay(self.move_prompt)
self.add_item(self.move_prompt_display)
self.top_button = discord.ui.Button(
label="Top", style=discord.ButtonStyle.blurple, emoji="⏫"
)
self.top_button = discord.ui.Button(label="Top", emoji="⏫")
self.top_button.callback = self.top
self.move_up_button = discord.ui.Button(
label="Move up", style=discord.ButtonStyle.blurple, emoji="⬆️"
)
self.move_up_button = discord.ui.Button(label="Move up", emoji="🔼")
self.move_up_button.callback = self.move_up
self.move_down_button = discord.ui.Button(
label="Move down", style=discord.ButtonStyle.blurple, emoji="⬇️"
)
self.move_down_button = discord.ui.Button(label="Move down", emoji="🔽")
self.move_down_button.callback = self.move_down
self.bottom_button = discord.ui.Button(
label="Bottom", style=discord.ButtonStyle.blurple, emoji="⏬"
)
self.bottom_button = discord.ui.Button(label="Bottom", emoji="⏬")
self.bottom_button.callback = self.bottom
self.move_action_row = discord.ui.ActionRow(
self.top_button,
Expand All @@ -134,15 +124,16 @@ def __init__(
self.confirm_button,
)
self.add_item(self.move_action_row)
self.game.events.game_end += self.stop
self.game.events.game_end += self.ignore_interactions

async def skip_if_deck_empty(self) -> bool:
if len(self.game.deck) == 0:
await self.finish()
return True
return False

async def finish(self, interaction=None):
async def finish(self):
await super().finish()
self.game.deck.insert(self.card_position, self.card)
await self.callback_action()

Expand Down
Loading
Loading