Skip to content

Commit

Permalink
Delete games when there's no players pending
Browse files Browse the repository at this point in the history
  • Loading branch information
lexicalunit committed Dec 23, 2024
1 parent 8f5835c commit 92e616a
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 724 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed

- Updated dependencies.
- Delete the pending game when the last player leaves it.
- Cleaned up some code by reducing the number of cogs.

### Removed

- Removes the unfinished ELO and points confirmation code. This can be reintroduced in the future
but it needs to be fully overhauled from its current state.

## [v13.0.1](https://github.com/lexicalunit/spellbot/releases/tag/v13.0.1) - 2024-12-15

Expand Down
22 changes: 11 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ aiohttp-jinja2 = "^1.6"
aiohttp-retry = "^2.8.3"
alembic = "^1.13.1"
asgiref = "^3.8.1"
attrs = "<24,>=23.2.0"
babel = "^2.14.0"
certifi = ">=2024.2.2,<2025.0.0"
click = "^8.1.7"
Expand Down
2 changes: 0 additions & 2 deletions src/spellbot/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .block_action import BlockAction
from .leave_action import LeaveAction
from .lfg_action import LookingForGameAction
from .record_action import RecordAction
from .score_action import ScoreAction
from .tasks_action import TasksAction
from .verify_action import VerifyAction
Expand All @@ -15,7 +14,6 @@
"BlockAction",
"LeaveAction",
"LookingForGameAction",
"RecordAction",
"ScoreAction",
"TasksAction",
"VerifyAction",
Expand Down
57 changes: 45 additions & 12 deletions src/spellbot/actions/leave_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ddtrace import tracer

from spellbot.operations import (
safe_delete_message,
safe_fetch_text_channel,
safe_get_partial_message,
safe_original_response,
Expand Down Expand Up @@ -34,15 +35,23 @@ async def _handle_click(self) -> None:

game_data = await self.services.games.to_dict()
posts = game_data.get("posts")

player_count = len(await self.services.games.player_xids())
do_delete_game = player_count == 0

for post in posts:
guild_xid = post["guild_xid"]
channel_xid = post["channel_xid"]
message_xid = post["message_xid"]

original_response = await safe_original_response(self.interaction)
if original_response and message_xid and original_response.id == message_xid:
embed = await self.services.games.to_embed()
await safe_update_embed_origin(self.interaction, embed=embed)
if do_delete_game:
assert self.interaction.message is not None
await safe_delete_message(self.interaction.message)
else:
embed = await self.services.games.to_embed()
await safe_update_embed_origin(self.interaction, embed=embed)
continue

channel = await safe_fetch_text_channel(self.bot, guild_xid, channel_xid)
Expand All @@ -53,8 +62,14 @@ async def _handle_click(self) -> None:
if message is None:
continue

embed = await self.services.games.to_embed()
await safe_update_embed(message, embed=embed)
if do_delete_game:
await safe_delete_message(message)
else:
embed = await self.services.games.to_embed()
await safe_update_embed(message, embed=embed)

if do_delete_game:
await self.services.games.delete_games([game_data["id"]])

@tracer.wrap()
async def _handle_command(self) -> None:
Expand All @@ -74,6 +89,8 @@ async def _handle_command(self) -> None:
await self.services.users.leave_game(channel_xid)

game_data = await self.services.games.to_dict()
player_count = len(await self.services.games.player_xids())
do_delete_game = player_count == 0
for post in game_data.get("posts", []):
chan_xid = post["channel_xid"]
guild_xid = post["guild_xid"]
Expand All @@ -84,8 +101,14 @@ async def _handle_command(self) -> None:
if not (message := safe_get_partial_message(channel, guild_xid, message_xid)):
continue

embed = await self.services.games.to_embed()
await safe_update_embed(message, embed=embed)
if do_delete_game:
await safe_delete_message(message)
else:
embed = await self.services.games.to_embed()
await safe_update_embed(message, embed=embed)

if do_delete_game:
await self.services.games.delete_games([game_data["id"]])

await safe_send_channel(
self.interaction,
Expand All @@ -105,9 +128,12 @@ async def execute_all(self) -> None:
"""Leave ALL games in ALL channels for this user."""
game_ids = await self.services.games.dequeue_players([self.interaction.user.id])
message_xids = await self.services.games.message_xids(game_ids)

for message_xid in message_xids:
data = await self.services.games.select_by_message_xid(message_xid)
assert data is not None # This should never happen
player_count = len(await self.services.games.player_xids())
do_delete_game = player_count == 0

for post in data.get("posts", []):
guild_xid = post["guild_xid"]
Expand All @@ -116,12 +142,19 @@ async def execute_all(self) -> None:
if channel:
message = safe_get_partial_message(channel, guild_xid, message_xid)
if message:
embed = await self.services.games.to_embed()
await safe_update_embed(
message,
embed=embed,
view=PendingGameView(bot=self.bot),
)
if do_delete_game:
await safe_delete_message(message)
else:
embed = await self.services.games.to_embed()
await safe_update_embed(
message,
embed=embed,
view=PendingGameView(bot=self.bot),
)

if do_delete_game:
await self.services.games.delete_games([data["id"]])

await safe_send_channel(
self.interaction,
"You were removed from all pending games.",
Expand Down
87 changes: 6 additions & 81 deletions src/spellbot/actions/lfg_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
safe_update_embed_origin,
)
from spellbot.settings import settings
from spellbot.views import BaseView, PendingGameView, StartedGameView, StartedGameViewWithConfirm
from spellbot.views import BaseView, PendingGameView, StartedGameView

from .base_action import BaseAction

Expand Down Expand Up @@ -141,11 +141,8 @@ async def upsert_game(
):
embed = await self.services.games.to_embed()
view: BaseView | None = None
if self.channel_data.get("show_points", False) and not found["confirmed"]:
if self.channel_data.get("require_confirmation", False):
view = StartedGameViewWithConfirm(bot=self.bot)
else:
view = StartedGameView(bot=self.bot)
if self.channel_data.get("show_points", False):
view = StartedGameView(bot=self.bot)
await safe_update_embed(message, embed=embed, view=view)

return None
Expand All @@ -154,7 +151,7 @@ async def upsert_game(
return False

@tracer.wrap()
async def execute( # noqa: C901
async def execute(
self,
friends: str | None = None,
seats: int | None = None,
Expand Down Expand Up @@ -186,14 +183,6 @@ async def execute( # noqa: C901
await safe_followup_channel(self.interaction, msg)
return None

req_confirm = self.channel_data["require_confirmation"]
if req_confirm and not await self.services.users.is_confirmed(self.channel.id):
msg = "You need to confirm your points before joining another game."
if origin:
return await safe_send_user(self.interaction.user, msg)
await safe_followup_channel(self.interaction, msg)
return None

if await self.services.users.pending_games() + 1 > settings.MAX_PENDING_GAMES:
msg = "You're in too many pending games to join another one at this time."
if origin:
Expand Down Expand Up @@ -287,71 +276,10 @@ async def add_points(self, message: Message, points: int) -> None:
)
return

plays = await self.services.games.get_plays()
if plays.get(self.interaction.user.id, {}).get("confirmed_at", None):
await safe_send_user(
self.interaction.user,
f"You've already confirmed your points for game SB{found.get('id')}.",
)
return

# if at least one player has confirmed their points, then changing points not allowed
if any(play.get("confirmed_at") is not None for play in plays.values()):
await safe_send_user(
self.interaction.user,
(
f"Points for game SB{found.get('id')} are locked in,"
" please confirm them or contact a mod."
),
)
return

await self.services.games.add_points(self.interaction.user.id, points)
embed = await self.services.games.to_embed()
await safe_update_embed(message, embed=embed)

@tracer.wrap()
async def confirm_points(self, message: Message) -> None:
found = await self.services.games.select_by_message_xid(message.id)
if not found:
return

if not await self.services.games.players_included(self.interaction.user.id):
await safe_send_user(
self.interaction.user,
f"You are not one of the players in game SB{found.get('id')}.",
)
return

plays = await self.services.games.get_plays()
if plays.get(self.interaction.user.id, {}).get("confirmed_at", None):
await safe_send_user(
self.interaction.user,
f"You've already confirmed your points for game SB{found.get('id')}",
)
return

if any(play.get("points") is None for play in plays.values()):
await safe_send_user(
self.interaction.user,
(
"Please wait until all players have reported"
f" before confirming points for game SB{found.get('id')}"
),
)
return

confirmed_at = await self.services.games.confirm_points(player_xid=self.interaction.user.id)
embed = await self.services.games.to_embed()
data = await self.services.games.to_dict()
if data["confirmed"]:
await safe_update_embed(message, embed=embed, view=None)
else:
await safe_update_embed(message, embed=embed)
plays[self.interaction.user.id]["confirmed_at"] = confirmed_at
if all(play["confirmed_at"] is not None for play in plays.values()):
await self.services.games.update_records(plays)

@tracer.wrap()
async def create_game(
self,
Expand Down Expand Up @@ -493,11 +421,8 @@ async def _handle_embed_creation( # noqa: C901,PLR0912

view: BaseView | None = None
if fully_seated:
if self.channel_data.get("show_points", False) and not game_data["confirmed"]:
if self.channel_data.get("require_confirmation", False):
view = StartedGameViewWithConfirm(bot=self.bot)
else:
view = StartedGameView(bot=self.bot)
if self.channel_data.get("show_points", False):
view = StartedGameView(bot=self.bot)
else:
view = PendingGameView(bot=self.bot)

Expand Down
Loading

0 comments on commit 92e616a

Please sign in to comment.