def test_matchmaking_logic(): game_queue.reset_queue() # We queue for everything except the red support for player_id in range(0, 9): game_queue.add_player(player_id, roles_list[player_id % 5], 0, 0) assert not find_best_game(GameQueue(0)) # We add the last player game_queue.add_player(9, "SUP", 0, 0) game = find_best_game(GameQueue(0)) assert game # We commit the game to the database with session_scope() as session: session.add(game) # We say player 0 won his last game on server 0 score_game_from_winning_player(0, 0) # We check that everything got changed with session_scope() as session: # We recreate the game object so it’s associated with this new session game = session.query(Game).order_by(Game.start.desc()).first() for side, role in game.participants: participant = game.participants[side, role] assert participant.player.ratings[role].trueskill_mu != 25
async def queue( self, ctx: commands.Context, role: RoleConverter(), ): """ Adds you to the current channel’s queue for the given role Roles are TOP, JGL, MID, BOT/ADC, and SUP Example: !queue SUP !queue support !queue bot !queue adc """ # Queuing the player game_queue.add_player( player_id=ctx.author.id, name=ctx.author.display_name, role=role, channel_id=ctx.channel.id, server_id=ctx.guild.id, ) await self.run_matchmaking_logic(ctx=ctx) await queue_channel_handler.update_queue_channels( bot=self.bot, server_id=ctx.guild.id)
async def games(self, ctx: commands.Context): """ Creates 100 games in the database with random results """ game_queue.reset_queue() for game_count in range(100): # We add the context creator as well game_queue.add_player( ctx.author.id, roles_list[4], ctx.channel.id, ctx.guild.id, name=ctx.author.display_name ) # We put 15 people in the queue, but only the first ones should get picked for i in range(0, 15): game_queue.add_player(i, roles_list[i % 5], ctx.channel.id, ctx.guild.id, name=str(i)) game = matchmaking_logic.find_best_game(GameQueue(ctx.channel.id)) with session_scope() as session: session.add(game) winner = game.player_ids_list[int(random.random() * 10)] game_queue.start_ready_check([i for i in range(0, 9)] + [ctx.author.id], ctx.channel.id, 0) game_queue.validate_ready_check(0) matchmaking_logic.score_game_from_winning_player(player_id=winner, server_id=ctx.guild.id) await ranking_channel_handler.update_ranking_channels(self.bot, ctx.guild.id) await ctx.send("100 games have been created in the database") await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id)
def test_multiple_queues(): game_queue.reset_queue() game_queue.add_player(0, roles_list[0], 0, 0, name="0") # This will take a few seconds for i in range(1000): GameQueue(0)
def test_duo_queue(): game_queue.reset_queue() # Adding it all except last support for player_id in range(0, 9): game_queue.add_player(player_id, roles_list[player_id % 5], 0, 0, name=str(player_id)) # Marking players 0 and 9 as duo game_queue.add_duo(0, "TOP", 9, "SUP", 0, 0, first_player_name="0", second_player_name="9") print(GameQueue(0)) print(GameQueue(0).duos) assert len(GameQueue(0)) == 10 assert len(GameQueue(0).duos) == 1 # Removing their duo status with 0 calling !solo game_queue.remove_duo(0, 0) assert len(GameQueue(0)) == 10 assert len(GameQueue(0).duos) == 0
def test_multiple_queues(): game_queue.reset_queue() game_queue.add_player(0, roles_list[0], 0, 0) # This will take at least 30s to crash because of the queue timeout for i in range(1000): GameQueue(0)
def test_unmark_queue(): game_queue.add_player(0, roles_list[0], 2, 0, name="0") assert len(GameQueue(2)) == 1 queue_channel_handler.unmark_queue_channel(2) assert len(GameQueue(2)) == 0
def test_queue_full(): game_queue.reset_queue() for player_id in range(0, 10): game_queue.add_player(player_id, roles_list[player_id % 5], 0, 0, name=str(player_id)) assert len(GameQueue(0)) == 10 # We queue our player 0 in channel 1, which he should be allowed to do game_queue.add_player(0, roles_list[0], 1, 0, name="0") assert len(GameQueue(1)) == 1 # We queue our player 0 for every other role in channel 0 for role in roles_list: game_queue.add_player(0, role, 0, 0, name="0") assert len(GameQueue( 0)) == 14 # Every role should count as a different QueuePlayer # Assuming our matchmaking logic found a good game (id 0) game_queue.start_ready_check(list(range(0, 10)), 0, 0) assert len( GameQueue(0) ) == 0 # Player 0 should not be counted in queue for any role anymore # Our player 0 in channel 1 should not be counted in queue either assert len(GameQueue(1)) == 0 # We check that our player 0 is not allowed to queue in other channels with pytest.raises(game_queue.PlayerInReadyCheck): game_queue.add_player(0, roles_list[0], 2, 0, name="0") # We cancel the ready check and drop player 0 from all queues on the server game_queue.cancel_ready_check(ready_check_id=0, ids_to_drop=[0], server_id=0) assert len(GameQueue(0)) == 9 # We check player 0 got dropped from queue 1 too assert len(GameQueue(1)) == 0 # We queue again, with player 10 game_queue.add_player(10, roles_list[0], 0, 0, name="10") # We start and validate the ready check (message id 1) game_queue.start_ready_check(list(range(1, 11)), 0, 1) game_queue.validate_ready_check(1) # We verify that both queues are empty assert len(GameQueue(0)) == 0 assert len(GameQueue(1)) == 0
async def queue(self, ctx: commands.Context): """ Testing the queue pop message """ # We put 10 people in the queue for i in range(0, 10): game_queue.add_player(i, roles_list[i % 5], ctx.channel.id, ctx.guild.id, name=str(i)) await ctx.send("The queue has been filled") await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id)
def test_queue_remove(): game_queue.reset_queue() game_queue.add_player(0, roles_list[0], 0, 0, name="0") assert len(GameQueue(0)) == 1 # We queue our player 0 in channel 1, which he should be allowed to do game_queue.remove_player(0, 0) assert len(GameQueue(0)) == 0
def test_duo_matchmaking(): game_queue.reset_queue() # Playing 100 games with random outcomes and making sure 0 and 9 are always on the same team for game_count in range(100): # We queue for everything except the red support for player_id in range(0, 9): game_queue.add_player(player_id, roles_list[player_id % 5], 0, 0, name=str(player_id)) # We add the last player as duo with player 0 game_queue.add_duo( 0, "TOP", 9, "SUP", 0, 0, second_player_name="9", first_player_name="0", ) game = find_best_game(GameQueue(0)) player_0 = next(p for p in game.participants.values() if p.player_id == 0) player_9 = next(p for p in game.participants.values() if p.player_id == 9) assert player_0.side == player_9.side with session_scope() as session: session.add(game) winner = game.player_ids_list[int(random.random() * 10)] game_queue.start_ready_check([i for i in range(0, 10)], 0, 0) game_queue.validate_ready_check(0) score_game_from_winning_player(player_id=winner, server_id=0)
def test_matchmaking_logic_priority(): """ Making sure players who spent more time in queue will be considered first """ game_queue.reset_queue() # TODO LOW PRIO Rewrite the test to make it properly test age-based matchmaking, even with an empty DB # I think even without age-based matchmaking it could pass at the moment, since on an empty DB the first # tested game has a perfect 50% evaluation. Won’t happen after other tests though so ok atm # We queue for everything, with 0, 1, 2, 3 being top, 4, 5, 6, 7 being jgl, ... for player_id in range(0, 20): game_queue.add_player(player_id, roles_list[int(player_id / 4 % 5)], 0, 0) game = find_best_game(GameQueue(0)) print(game.blue_expected_winrate) # Assert we chose 0, 1, 4, 5, 8, 9, 13, 13, 16, 17 players for participant in game.participants.values(): assert participant.player_id % 4 < 2
async def duo(self, ctx: commands.Context): """ Testing the duo queue feature """ # We put 10 people in the queue for i in range(0, 10): game_queue.add_player(i, roles_list[i % 5], ctx.channel.id, ctx.guild.id, name=str(i)) game_queue.add_duo( 6, "JGL", ctx.author.id, "MID", ctx.channel.id, ctx.guild.id, first_player_name="6", second_player_name=ctx.author.display_name, ) await ctx.send("The queue has been filled and you have been put in mid/jgl duo with player 6") await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id)
async def game(self, ctx: commands.Context): """ Creating a fake game in the database with players 0 to 8 and the ctx author """ # We reset the queue # We put 9 people in the queue for i in range(0, 9): game_queue.add_player(i, roles_list[i % 5], ctx.channel.id, ctx.guild.id, name=str(i)) game_queue.add_player( ctx.author.id, roles_list[4], ctx.channel.id, ctx.guild.id, name=ctx.author.display_name ) game = matchmaking_logic.find_best_game(GameQueue(ctx.channel.id)) with session_scope() as session: session.add(game) msg = await ctx.send("The queue has been reset, filled again, and a game created (with no winner)") game_queue.start_ready_check([i for i in range(0, 9)] + [ctx.author.id], ctx.channel.id, msg.id) game_queue.validate_ready_check(msg.id) await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id)
class QueueCog(commands.Cog, name="Queue"): """ Manage your queue status and score games """ def __init__(self, bot: InhouseBot): self.bot = bot # Makes them jump ahead on the next queue # player_id -> timestamp self.players_whose_last_game_got_cancelled = {} self.games_getting_scored_ids = set() async def run_matchmaking_logic( self, ctx: commands.Context, ): """ Runs the matchmaking logic in the channel defined by the context Should only be called inside guilds """ queue = game_queue.GameQueue(ctx.channel.id) game = matchmaking_logic.find_best_game(queue) if not game: return elif game and game.matchmaking_score < 0.2: embed = game.get_embed(embed_type="GAME_FOUND", validated_players=[], bot=self.bot) # We notify the players and send the message ready_check_message = await ctx.send(content=game.players_ping, embed=embed, delete_after=60 * 15) # We mark the ready check as ongoing (which will be used to the queue) game_queue.start_ready_check( player_ids=game.player_ids_list, channel_id=ctx.channel.id, ready_check_message_id=ready_check_message.id, ) # We update the queue in all channels await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id) # And then we wait for the validation try: category = await ctx.guild.create_category('⚔ Game-1') text_channel = await ctx.guild.create_text_channel('chat', category=category) blue_side = await ctx.guild.create_voice_channel('🔵 Blue Side', category=category) red_side = await ctx.guild.create_voice_channel('🔴 Red Side', category=category) ready, players_to_drop = await checkmark_validation( bot=self.bot, message=ready_check_message, validating_players_ids=game.player_ids_list, validation_threshold=10, game=game, ) # We catch every error here to make sure it does not become blocking except Exception as e: self.bot.logger.error(e) game_queue.cancel_ready_check( ready_check_id=ready_check_message.id, ids_to_drop=game.player_ids_list, server_id=ctx.guild.id, ) await ctx.send( "There was a bug with the ready-check message, all players have been dropped from queue\n" "Please queue again to restart the process" ) await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id) return if ready is True: # We drop all 10 players from the queue game_queue.validate_ready_check(ready_check_message.id) # We commit the game to the database (without a winner) with session_scope() as session: session.expire_on_commit = False game = session.merge(game) # This gets us the game ID category = await ctx.guild.create_category('⚔ Game-%s' % str(game.id)) text_channel = await ctx.guild.create_text_channel('chat', category=category) blue_side = await ctx.guild.create_voice_channel('🔵 Blue Side', category=category) red_side = await ctx.guild.create_voice_channel('🔴 Red Side', category=category) queue_channel_handler.mark_queue_related_message( await ctx.send(embed=game.get_embed("GAME_ACCEPTED"),) ) elif ready is False: # We remove the player who cancelled game_queue.cancel_ready_check( ready_check_id=ready_check_message.id, ids_to_drop=players_to_drop, channel_id=ctx.channel.id, ) await ctx.send( f"A player cancelled the game and was removed from the queue\n" f"All other players have been put back in the queue", ) # We restart the matchmaking logic await self.run_matchmaking_logic(ctx) elif ready is None: # We remove the timed out players from *all* channels (hence giving server id) game_queue.cancel_ready_check( ready_check_id=ready_check_message.id, ids_to_drop=players_to_drop, server_id=ctx.guild.id, ) await ctx.send( "The check timed out and players who did not answer have been dropped from all queues", ) # We restart the matchmaking logic await self.run_matchmaking_logic(ctx) elif game and game.matchmaking_score >= 0.2: # One side has over 70% predicted winrate, we do not start anything await ctx.send( f"The best match found had a side with a {(.5 + game.matchmaking_score)*100:.1f}%" f" predicted winrate and was not started" ) @commands.command(aliases=["view_queue", "refresh"]) @queue_channel_only() async def view( self, ctx: commands.Context, ): """ Refreshes the queue in the current channel Almost never needs to get used directly """ await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id) @commands.command() @queue_channel_only() @doc(f""" Adds you to the current channel’s queue for the given role To duo queue, add @player role at the end (cf examples) Roles are TOP, JGL, MID, BOT/ADC, and SUP Example: {PREFIX}queue SUP {PREFIX}queue bot {PREFIX}queue adc {PREFIX}queue adc @CoreJJ support """) async def queue( self, ctx: commands.Context, role: RoleConverter(), duo: discord.Member = None, duo_role: RoleConverter() = None, ): # Checking if the last game of this player got cancelled # If so, we put them in the queue in front of other players jump_ahead = False # pop with two arguments returns the second one if the key was not found if cancel_timestamp := self.players_whose_last_game_got_cancelled.pop(ctx.author.id, None): if datetime.now() - cancel_timestamp < timedelta(hours=1): jump_ahead = True if not duo: # Simply queuing the player game_queue.add_player( player_id=ctx.author.id, name=ctx.author.display_name, role=role, channel_id=ctx.channel.id, server_id=ctx.guild.id, jump_ahead=jump_ahead, ) # If there is a duo, we go for a different flow (which should likely be another function) else: if not duo_role: await ctx.send("You need to input a role for your duo partner") return duo_validation_message = await ctx.send( f"<@{ctx.author.id}> {get_role_emoji(role)} wants to duo with <@{duo.id}> {get_role_emoji(duo_role)}\n" f"Press ✅ to accept the duo queue" ) validated, players_who_refused = await checkmark_validation( bot=self.bot, message=duo_validation_message, validating_players_ids=[duo.id], validation_threshold=1, ) if not validated: await ctx.send(f"<@{ctx.author.id}>: Duo queue was refused") return # Here, we have a working duo queue game_queue.add_duo( first_player_id=ctx.author.id, first_player_role=role, first_player_name=ctx.author.display_name, second_player_id=duo.id, second_player_role=duo_role, second_player_name=duo.display_name, channel_id=ctx.channel.id, server_id=ctx.guild.id, jump_ahead=jump_ahead, ) await self.run_matchmaking_logic(ctx=ctx) await queue_channel_handler.update_queue_channels(bot=self.bot, server_id=ctx.guild.id)