From 65c20596192ddc93e78d577de8cdb503a2de2224 Mon Sep 17 00:00:00 2001 From: NatrixAeria Date: Mon, 19 Oct 2020 19:22:49 +0200 Subject: Try to never have to same persons together --- bot.py | 1 + commands.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++------- config.py | 4 ++-- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/bot.py b/bot.py index 1e48e48..303333e 100644 --- a/bot.py +++ b/bot.py @@ -22,6 +22,7 @@ class Cupido(commands.Bot): self.vote_map = {} self.read_file() self.score_map = dd(lambda: 0) + self.oldgroups = set() def read_file(self): try: diff --git a/commands.py b/commands.py index feb6c74..62046f6 100644 --- a/commands.py +++ b/commands.py @@ -22,7 +22,7 @@ async def answer(ctx: commands.Context, msg: str): @commands.command(help='display this help message', aliases=('hepl', 'h', '?')) async def help(ctx: commands.Context): command_doc = '\n'.join( - f' * {config.COMMAND_PREFIX.strip()} {c.name:15} - {c.help}' + f' * {config.COMMAND_PREFIX}{c.name:15} - {c.help}' for c in ctx.bot.commands) await answer(ctx, f'''``` {config.HELP_TEXT}\nThese are all available commands:\n{command_doc}```''') @@ -48,6 +48,27 @@ async def destroy(ctx: commands.Context): ctx.bot.pair_channels = [] +def fac(n): + i = 1 + for z in range(2, n+1): + i *= z + return i + + +def n_th_permutation(n, lst): + nlst = [None for _ in lst] + indexes = list(range(len(lst))) + for i in lst: + n, index = divmod(n, len(indexes)) + nlst[indexes.pop(index)] = i + return nlst + + +def round_up_div(a, b): + a, b = divmod(a, b) + return a+1 if b else a + + @commands.command(help='start shuffling', aliases=('start', 'run', 'strat', 'rnu')) async def shuffle(ctx: commands.Context, channel_size=2): if channel_size < 1: @@ -65,7 +86,10 @@ async def shuffle(ctx: commands.Context, channel_size=2): await answer(ctx, f'error: you are too few people ({len(members)}). Group size is {channel_size}') return False - channel_count, excess_slots = divmod(len(members), channel_size) + channel_count = len(members) // channel_size + counts = [0] * channel_count + for i, _ in enumerate(members): + counts[i % len(counts)] += 1 normals = [] tutors = [] random.shuffle(members) @@ -74,14 +98,34 @@ async def shuffle(ctx: commands.Context, channel_size=2): tutors.append(member) else: normals.append(member) + + groups = [[] for _ in range(channel_count)] + for count, group in zip(counts, groups): + tutor_count = min(count, round_up_div(len(tutors), len(groups))) + for i in range(tutor_count): + if tutors: + group.append(tutors.pop()) + for _ in range(count - tutor_count): + for i, member in enumerate(normals): + if any(frozenset((member.id, other.id)) in ctx.bot.oldgroups for other in group): + continue + group.append(normals.pop(i)) + break + else: + group.append(normals.pop()) + futures = [] ctx.bot.pair_channels = await ctx.bot.create_pair_channels(ctx, meta_channel, channel_count) random.shuffle(ctx.bot.pair_channels) - groups = [[] for _ in range(channel_count)] - for i, member in enumerate(tutors + normals): - n = i % channel_count - futures.append(member.move_to(ctx.bot.pair_channels[n])) - groups[n].append(member) + for n, group in enumerate(groups): + for member in group: + futures.append(member.move_to(ctx.bot.pair_channels[n])) + for member1 in group: + for member2 in group: + if member1 != member2: + ctx.bot.oldgroups.add(frozenset((member1, member2))) + + random.shuffle(futures) for group in groups: for member in group: futures.append(ctx.bot.send_panel(ctx, member, group)) diff --git a/config.py b/config.py index c1843d8..d6c223d 100644 --- a/config.py +++ b/config.py @@ -13,7 +13,7 @@ This software is open-source ! CATEGORY_CHANNEL_NAME = 'KENNENLERNEN' LOBBY_CHANNEL_NAME = 'Lobby' -LOBBY_CHANNEL_TOPIC = "Wer auch schon immer so richtig durchgeshuffelt werden wollte ist hier genau richtig!" +LOBBY_CHANNEL_TOPIC = 'Wer auch schon immer so richtig durchgeshuffelt werden wollte ist hier genau richtig!' # time that one loop cycle needs (in seconds) DEFAULT_LOOP_TIME = 3 * 60 @@ -21,7 +21,7 @@ DEFAULT_LOOP_TIME = 3 * 60 DEFAULT_LOOP_COUNT = 3 EMOJI_POOL = ['👾', '🤖', '👻', '🦧', '🍴', '🎮', '🎷', '😈', '🦄', '🐮', '🌻', '🐘', '🍕', '🦉'] -PANEL_TITLE = f'Wer ist Tutor?' +PANEL_TITLE = 'Wer ist Tutor?' PANEL_TEXT = ''' Wenn **du** der Meinung bist, dass du gerade mit einem **Tutor** sprichst, dann stimme jetzt mit einem der entsprechenden Emojis unten ab: -- cgit v1.2.3