Пример #1
0
    async def accept(self, ctx):
        if not self.db.is_challenged(ctx.guild.id, ctx.author.id):
            await discord_.send_message(ctx, "No-one is challenging you")
            return
        embed = Embed(description=f"Preparing to start the match...",
                      color=Color.green())
        embed.set_footer(
            text=f"You can now conduct tournaments using the bot.\n"
            f"Type .tournament faq for more info")
        await ctx.send(embed=embed)

        data = self.db.get_challenge_info(ctx.guild.id, ctx.author.id)
        self.db.remove_challenge(ctx.guild.id, ctx.author.id)

        handle1, handle2 = self.db.get_handle(ctx.guild.id,
                                              data.p1_id), self.db.get_handle(
                                                  ctx.guild.id, data.p2_id)
        problems = await codeforces.find_problems(
            [handle1, handle2], [data.rating + i * 100 for i in range(0, 5)])

        if not problems[0]:
            await discord_.send_message(ctx, problems[1])
            return

        problems = problems[1]
        self.db.add_to_ongoing(data, int(time.time()), problems)

        match_info = self.db.get_match_info(ctx.guild.id, ctx.author.id)
        await ctx.send(embed=discord_.match_problems_embed(match_info))
Пример #2
0
 async def problems(self, ctx, member: discord.Member = None):
     if member is None:
         member = ctx.author
     if not self.db.in_a_match(ctx.guild.id, member.id):
         await discord_.send_message(
             ctx, f"User {member.mention} is not in a match!")
         return
     await ctx.send(embed=discord_.match_problems_embed(
         self.db.get_match_info(ctx.guild.id, member.id)))
Пример #3
0
    async def update(self, ctx):
        await ctx.send(embed=discord.Embed(
            description=f"Updating matches for this server",
            color=discord.Color.green()))

        matches = self.db.get_all_matches(ctx.guild.id)
        for match in matches:
            try:
                # updates, over, match_status
                resp = await updation.update_match(match)
                if not resp[0]:
                    logging_channel = await self.client.fetch_channel(
                        os.environ.get("LOGGING_CHANNEL"))
                    await logging_channel.send(
                        f"Error while updating matches: {resp[1]}")
                    continue
                resp = resp[1]
                channel = self.client.get_channel(match.channel)
                if resp[1] or len(resp[0]) > 0:
                    mem1, mem2 = await discord_.fetch_member(ctx.guild, match.p1_id), \
                                 await discord_.fetch_member(ctx.guild, match.p2_id)
                    await channel.send(
                        f"{mem1.mention} {mem2.mention}, there is an update in standings!"
                    )

                for x in resp[0]:
                    await channel.send(embed=discord.Embed(
                        description=
                        f"{' '.join([(await discord_.fetch_member(ctx.guild, m)).mention for m in x[1]])} has solved problem worth {x[0]*100} points",
                        color=discord.Color.blue()))

                if not resp[1] and len(resp[0]) > 0:
                    await channel.send(embed=discord_.match_problems_embed(
                        self.db.get_match_info(ctx.guild.id, match.p1_id)))

                if resp[1]:
                    a, b = updation.match_score(resp[2])
                    p1_rank, p2_rank = 1 if a >= b else 2, 1 if b >= a else 2
                    ranklist = []
                    ranklist.append([
                        await discord_.fetch_member(ctx.guild, match.p1_id),
                        p1_rank,
                        self.db.get_match_rating(ctx.guild.id, match.p1_id)[-1]
                    ])
                    ranklist.append([
                        await discord_.fetch_member(ctx.guild, match.p2_id),
                        p2_rank,
                        self.db.get_match_rating(ctx.guild.id, match.p2_id)[-1]
                    ])
                    ranklist = sorted(ranklist, key=itemgetter(1))
                    res = elo.calculateChanges(ranklist)

                    self.db.add_rating_update(ctx.guild.id, match.p1_id,
                                              res[match.p1_id][0])
                    self.db.add_rating_update(ctx.guild.id, match.p2_id,
                                              res[match.p2_id][0])
                    self.db.delete_match(match.guild, match.p1_id)
                    self.db.add_to_finished(match, resp[2])

                    embed = discord.Embed(color=discord.Color.dark_magenta())
                    pos, name, ratingChange = '', '', ''
                    for user in ranklist:
                        pos += f"{':first_place:' if user[1] == 1 else ':second_place:'}\n"
                        name += f"{user[0].mention}\n"
                        ratingChange += f"{res[user[0].id][0]} (**{'+' if res[user[0].id][1] >= 0 else ''}{res[user[0].id][1]}**)\n"
                    embed.add_field(name="Position", value=pos)
                    embed.add_field(name="User", value=name)
                    embed.add_field(name="Rating changes", value=ratingChange)
                    embed.set_author(
                        name=f"Match over! Final standings\nScore: {a}-{b}")
                    await channel.send(embed=embed)
            except Exception as e:
                logging_channel = await self.client.fetch_channel(
                    os.environ.get("LOGGING_CHANNEL"))
                await logging_channel.send(
                    f"Error while updating matches: {str(traceback.format_exc())}"
                )