summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNatrixAeria <upezu@student.kit.edu>2020-10-18 19:43:51 +0200
committerNatrixAeria <upezu@student.kit.edu>2020-10-18 19:43:51 +0200
commitaa929d2a43a45e6d775af09550c0d9ac1417510b (patch)
tree79a4c9b5a592bc626a6a74d5905599fd910f4593
parent20736fc1d026fdc02009f5a3083f93315973142a (diff)
Fix channel shuffling
-rw-r--r--commands.py23
1 files changed, 11 insertions, 12 deletions
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