From aa929d2a43a45e6d775af09550c0d9ac1417510b Mon Sep 17 00:00:00 2001 From: NatrixAeria Date: Sun, 18 Oct 2020 19:43:51 +0200 Subject: Fix channel shuffling --- commands.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'commands.py') diff --git a/commands.py b/commands.py index b974435..8e673c2 100644 --- a/commands.py +++ b/commands.py @@ -4,7 +4,6 @@ import random import config -import discord from discord.ext import commands @@ -65,16 +64,16 @@ async def shuffle(ctx: commands.Context, channel_size=2): if len(members) < channel_size: await answer(ctx, f'error: you are too few people ({len(members)}). Group size is {channel_size}') return False - channel_count = len(members) // channel_size - excess_slots = list(range(channel_count)) - random.shuffle(excess_slots) - excess_slots = excess_slots[:len(members) - channel_count * channel_size] + + channel_count, excess_slots = divmod(len(members), channel_size) + counts = [0] * channel_count + for i, _ in enumerate(members): + counts[i % len(counts)] += 1 + random.shuffle(counts) + slots = [j for i, c in enumerate(counts) for j in [i] * c] + slots = random.sample(slots, k=len(members)) + ctx.bot.pair_channels = await ctx.bot.create_pair_channels(ctx, meta_channel, channel_count) - slots = [] - for i, _ in enumerate(ctx.bot.pair_channels): - slots.extend([i] * channel_size) - slots.extend(excess_slots) - random.shuffle(slots) futures = [] groups = [[] for _ in range(channel_count)] member_slot_map = [] @@ -83,9 +82,9 @@ async def shuffle(ctx: commands.Context, channel_size=2): futures.append(member.move_to(channel)) groups[slot].append(member) member_slot_map.append(slot) - for i, (member, slot) in enumerate(zip(members, member_slot_map)): + for member, slot in zip(members, member_slot_map): group = groups[slot] - await ctx.bot.send_panel(ctx, member, group) + futures.append(ctx.bot.send_panel(ctx, member, group)) await await_n(futures) return True -- cgit v1.2.3-70-g09d2