コード例 #1
0
ファイル: gitgud.py プロジェクト: JoshuaTianYangLiu/JOMD
async def gotgud(ctx):
    query = Query()
    gitgud_util = Gitgud_utils()
    username = query.get_handle(ctx.author.id, ctx.get_guild().id)

    if username is None:
        return await ctx.respond("You are not linked with a DMOJ Account")

    user = await query.get_user(username)
    current = gitgud_util.get_current(username, ctx.get_guild().id)
    closest = -1000
    for key in RATING_TO_POINT:
        if abs(key - user.rating) <= abs(closest - user.rating):
            closest = key

    # convert rating to point and get difference
    rating_point = RATING_TO_POINT[closest]
    if current is None or current.problem_id is None:
        return await ctx.respond("No pending challenges")

    # check if user is scamming the bot :monkey:
    if gitgud_util.has_solved(username, current.problem_id):
        # get closest rating
        closest = -1000
        for key in RATING_TO_POINT:
            if abs(key - user.rating) <= abs(closest - user.rating):
                closest = key
        # convert rating to point and get difference
        rating_point = RATING_TO_POINT[closest]
        point_diff = POINT_VALUES.index(current.point) - POINT_VALUES.index(rating_point)

        point = 10 + 2 * (point_diff)
        point = max(point, 0)

        gitgud_util.insert(username, ctx.get_guild().id, point, current.problem_id, datetime.now())
        gitgud_util.clear(username, ctx.get_guild().id)

        completion_time = datetime.now() - current.time
        # convert from timedelta to readable string
        ret = ""
        cnt = 0
        if completion_time.days // 365 != 0:
            ret += f" {completion_time.days // 365} years"
            cnt += 1
        if completion_time.days % 365 != 0:
            ret += f" {completion_time.days % 365} days"
            cnt += 1
        if completion_time.seconds // 3600 != 0:
            ret += f" {completion_time.seconds // 3600} hours"
            cnt += 1
        if cnt < 3 and completion_time.seconds % 3600 // 60 != 0:
            ret += f" {completion_time.seconds % 3600 // 60} minutes"
            cnt += 1
        if cnt < 3 and completion_time.seconds % 60 != 0:
            ret += f" {completion_time.seconds % 60} seconds"

        return await ctx.respond(f"Challenge took{ret}. " f"{current.handle} gained {point} points")

    else:
        return await ctx.respond("You have not completed the challenge")
コード例 #2
0
ファイル: gitgud.py プロジェクト: JoshuaTianYangLiu/JOMD
async def gitgud(ctx: lightbulb.Context) -> None:
    # TODO Fix converters for slash commands
    points = ctx.options.points
    filters = ctx.options.filters
    query = Query()
    gitgud_util = Gitgud_utils()
    # get the user's dmoj handle
    username = query.get_handle(ctx.author.id, ctx.get_guild().id)
    # user = await query.get_user(username)

    if username is None:
        return await ctx.respond("You are not linked to a DMOJ Account. " "Please link your account before continuing")

    user = await query.get_user(username)

    if points is None:
        points = [0, 0]
        closest = -1000
        for key in RATING_TO_POINT:
            if abs(key - user.rating) <= abs(closest - user.rating):
                closest = key
        points[0] = RATING_TO_POINT[closest]
        points[1] = points[0]
    # return if the user haven't finished the previous problem
    current = gitgud_util.get_current(username, ctx.get_guild().id)

    if current is not None and current.problem_id is not None:
        if not gitgud_util.has_solved(username, current.problem_id):
            # User has a current problem unsolved
            problem = await query.get_problem(current.problem_id)
            embed = hikari.Embed(
                description=f"You currently have an uncompleted "
                f"challenge, [{problem.name}]"
                f"(https://dmoj.ca/problem/{problem.code})",
                color=0xFCDB05,
            )
            return await ctx.respond(embed=embed)

    filter_list = []
    for filter in filters:
        if filter in SHORTHANDS:
            filter_list.append(SHORTHANDS[filter])

    filters = filter_list

    embed, problem = await gimme_common(username, points, filters)

    if embed is None:
        return await ctx.respond("No problems that satisfies the filter")

    gitgud_util.bind(username, ctx.get_guild().id, problem.code, problem.points, datetime.now())

    embed.description = "Points: %s\nProblem Types ||%s||" % (problem.points, ", ".join(problem.types))

    return await ctx.respond(embed=embed)
コード例 #3
0
ファイル: gitgud.py プロジェクト: jacklee1792/JOMD
    async def gitgud(self, ctx, points: typing.Optional[point_range],
                     *filters):
        """
          Recommend a problem and gain point upon completion

        SHORTHANDS:
        - adhoc
        - math
        - bf
        - ctf
        - ds
        - d&c
        - dp
        - geo
        - gt
        - greedy
        - regex
        - string
        """

        filters = list(filters)
        query = Query()
        gitgud_util = Gitgud_utils()
        # get the user's dmoj handle
        username = query.get_handle(ctx.author.id, ctx.guild.id)
        # user = await query.get_user(username)

        if username is None:
            return await ctx.send("You are not linked to a DMOJ Account. "
                                  "Please link your account before continuing")

        user = await query.get_user(username)

        if points is None:
            points = [0, 0]
            closest = -1000
            for key in RATING_TO_POINT:
                if abs(key - user.rating) <= abs(closest - user.rating):
                    closest = key
            points[0] = RATING_TO_POINT[closest]
            points[1] = points[0]
        # return if the user haven't finished the previous problem
        current = gitgud_util.get_current(username, ctx.guild.id)

        if current is not None and current.problem_id is not None:
            if not gitgud_util.has_solved(username, current.problem_id):
                # User has a current problem unsolved
                problem = await query.get_problem(current.problem_id)
                embed = discord.Embed(
                    description=f"You currently have an uncompleted "
                    f"challenge, [{problem.name}]"
                    f"(https://dmoj.ca/problem/{problem.code})",
                    color=0xfcdb05,
                )
                return await ctx.send(embed=embed)

        filter_list = []
        for filter in filters:
            if filter in SHORTHANDS:
                filter_list.append(SHORTHANDS[filter])

        filters = filter_list

        embed, problem = await gimme_common(username, points, filters)

        if embed is None:
            return await ctx.send("No problems that satisfies the filter")

        gitgud_util.bind(username, ctx.guild.id, problem.code, problem.points,
                         datetime.now())

        embed.description = "Points: %s\nProblem Types ||%s||" % \
                            (problem.points, ', '.join(problem.types))

        return await ctx.send(embed=embed)