Skip to content

Commit

Permalink
Switched to dictionary for IN_GAME and IN_MATCHMAKING, added a test i…
Browse files Browse the repository at this point in the history
…nvite function, and fixed a bug from the renaming of the tic_tac_toe internal ID
  • Loading branch information
quantumbagel committed Dec 29, 2024
1 parent d90a8f3 commit b75effe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
28 changes: 25 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,34 @@ async def command_invite(ctx: discord.Interaction,
user4: discord.User = None,
user5: discord.User = None) -> None:
invited_users = {user, user2, user3, user4, user5}



if ctx.user.id not in IN_MATCHMAKING:
await ctx.response.send_message("You aren't in matchmaking, so you can't invite people to play.") # TODO: invites start games
return
matchmaker: MatchmakingInterface = IN_MATCHMAKING[ctx.user.id]
game_type = matchmaker.game.name
server_id = matchmaker.message.guild.id

failed_invites = {}
for invited_user in filter(None, invited_users): # Filter out None values
if invited_user not in matchmaker.message.guild.members:
failed_invites[invited_user] = "Member was not in server that the game was"
continue
embed = CustomEmbed(title=f"👋 Do you want to play a game?", description=f"{ctx.user.mention} has invited you to play a game of {game_type} in \"{matchmaker.message.guild.name}.\" If you don't want to play, just ignore this message. You can also change your DM settings with /playcord settings")
view = discord.ui.View()
button = discord.ui.Button(label="Join Game", style=discord.ButtonStyle.green)
view.add_item(button)
await invited_user.send(embed=embed, view=view)
continue
await ctx.response.send_message("Invites sent successfully.", ephemeral=True)
if not len(failed_invites):
await ctx.response.send_message("Invites sent successfully.", ephemeral=True)
else:
pass


@command_root.command(name="tictactoe", description="Tic-Tac Toe is a game about replacing your toes with Tic-Tacs," " obviously.")
@command_root.command(name="tictactoe", description="Tic-Tac Toe is a game about replacing your toes with Tic-Tacs, obviously.")
async def tictactoe(ctx: discord.Interaction, rated: bool = True):


Expand All @@ -305,7 +327,7 @@ async def tictactoe(ctx: discord.Interaction, rated: bool = True):
thing = await ctx.original_response()


g = MatchmakingInterface(ctx.user, "tic_tac_toe", thing, rated=rated)
g = MatchmakingInterface(ctx.user, "tictactoe", thing, rated=rated)

if g.failed is not None:
await thing.edit(embed=g.failed)
Expand Down
4 changes: 2 additions & 2 deletions configuration/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@



IN_GAME = []
IN_MATCHMAKING = []
IN_GAME = {}
IN_MATCHMAKING = {}


AUTOCOMPLETE_CACHE = {}
Expand Down
17 changes: 7 additions & 10 deletions utils/GameInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, game_type: str, status_message: discord.WebhookMessage, creat
self.game = getattr(self.module, GAME_TYPES[game_type][1])(self.players)

for p in players:
IN_GAME.append(p)
IN_GAME.update({p: self})

async def thread_setup(self) -> None:
"""
Expand Down Expand Up @@ -140,14 +140,11 @@ async def move(self, ctx: discord.Interaction, arguments: dict[str, typing.Any])
rating_groups.append({player: environment.create_rating(player.mu, player.sigma)})
current_ranking += 1

print(environment, rating_groups)
adjusted_rating_groups = environment.rate(rating_groups=rating_groups, ranks=rankings)

print(IN_GAME, 'before postgame')
print(IN_GAME, [p.id for p in self.players])

for p in self.players:
IN_GAME.remove(p)
print(IN_GAME, 'after postgame')
IN_GAME.pop(p)

message = await ctx.followup.send(content="Game over!", ephemeral=True)
await message.delete(delay=5)
Expand Down Expand Up @@ -238,7 +235,7 @@ def __init__(self, creator: discord.User, game_type: str, message: discord.Inter
self.failed = fail_embed
return

IN_MATCHMAKING.extend(self.queued_players)
IN_MATCHMAKING.update({p: self for p in self.queued_players})

# Game class
self.game = getattr(self.module, GAME_TYPES[game_type][1])
Expand Down Expand Up @@ -266,7 +263,7 @@ async def callback_ready_game(self, ctx: discord.Interaction) -> None:
await ctx.followup.send("Couldn't connect to DB!", ephemeral=True)
return
self.queued_players.append(new_player) # Add the player to queued_players
IN_MATCHMAKING.append(new_player)
IN_MATCHMAKING.update({new_player: self})
await self.update_embed() # Update embed on discord side


Expand Down Expand Up @@ -339,7 +336,7 @@ async def callback_leave_game(self, ctx: discord.Interaction) -> None:
for player in self.queued_players:
if player.id == ctx.user.id:
self.queued_players.remove(player)
IN_MATCHMAKING.remove(player)
IN_MATCHMAKING.pop(player)
break
# Nobody is left lol
if not len(self.queued_players):
Expand Down Expand Up @@ -378,7 +375,7 @@ async def successful_matchmaking(game_type: str, message, creator: discord.User,

print(IN_MATCHMAKING, 'matchmaking before pregame')
for p in players:
IN_MATCHMAKING.remove(p)
IN_MATCHMAKING.pop(p)

print(IN_MATCHMAKING, 'matchmaking after pregame')

Expand Down
2 changes: 1 addition & 1 deletion utils/InputTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class String(InputType):
def __init__(self, description, argument_name, optional=False, autocomplete=None, force_reload=False,):
super().__init__(description=description,
argument_name=argument_name,
optional=optional, autocomplete=autocomplete, force_reload=force_reload,)
optional=optional, autocomplete=autocomplete, force_reload=force_reload)
self.type = "string"

def arguments(self):
Expand Down

0 comments on commit b75effe

Please sign in to comment.